Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Finish firmware cache and image cleanup review

Lakrf09ed9c14e50
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcher.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcher.swift
index dc9b68f8..6f92f71f 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcher.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcher.swift
@@ -77,7 +77,11 @@ public final class CryptexFilesystemPatcher: Patcher {
public func apply() throws -> Int {
print("Merging filesystems…")
let (unencryptedImage, aeaImage) = try mergeFilesystems()
- defer { try? FileManager.default.removeItem(at: unencryptedImage) }
+ defer {
+ if attachedDevices.isEmpty {
+ try? FileManager.default.removeItem(at: unencryptedImage)
+ }
+ }
print("Creating trustcache…")
let trustcachePath = try createTrustcache(filesystem: unencryptedImage)
@@ -104,6 +108,10 @@ public final class CryptexFilesystemPatcher: Patcher {
)
rebuiltData = try serializePayload(updatedManifest)
+ guard attachedDevices.isEmpty else {
+ throw FirmwarePatcher.PatcherError.patchVerificationFailed("Filesystem image is still attached")
+ }
+
return 1
}
@@ -148,6 +156,10 @@ public final class CryptexFilesystemPatcher: Patcher {
try patchLaunchdCacheLoader(targetMount: targetMount)
}
+ guard attachedDevices.isEmpty else {
+ throw FirmwarePatcher.PatcherError.patchVerificationFailed("Filesystem image is still attached")
+ }
+
print("- Finalizing merged image…")
try shrinkImage(dmg: targetImagePath)
try convertToUDRWImage(input: targetImagePath, output: newDmgPath)
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherDiskImage.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherDiskImage.swift
index 2e6831d3..5dd5b4ab 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherDiskImage.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherDiskImage.swift
@@ -164,6 +164,7 @@ extension CryptexFilesystemPatcher {
let data = pipe.fileHandleForReading.readDataToEndOfFile()
guard process.terminationStatus == 0 else {
let output = String(data: data, encoding: .utf8) ?? ""
+ detachReportedImage(in: output)
throw ProcessError.failed(process.terminationStatus, output)
}
@@ -171,19 +172,11 @@ extension CryptexFilesystemPatcher {
do {
root = try parsePlist(data: data)
} catch {
- if let output = String(data: data, encoding: .utf8),
- let range = output.range(of: #"/dev/disk[0-9]+"#, options: .regularExpression)
- {
- _ = try? runProcess("/usr/bin/hdiutil", ["detach", "-force", String(output[range])])
- }
+ detachReportedImage(in: String(data: data, encoding: .utf8) ?? "")
throw error
}
guard let entries = root["system-entities"] as? [Any] else {
- if let output = String(data: data, encoding: .utf8),
- let range = output.range(of: #"/dev/disk[0-9]+"#, options: .regularExpression)
- {
- _ = try? runProcess("/usr/bin/hdiutil", ["detach", "-force", String(output[range])])
- }
+ detachReportedImage(in: String(data: data, encoding: .utf8) ?? "")
throw FirmwareManifest.ManifestError.missingKey("system-entities")
}
for entry in entries {
@@ -208,14 +201,19 @@ extension CryptexFilesystemPatcher {
}
return (device, mountPoint)
}
- if let device = (entries.compactMap { ($0 as? PlistDict)?["dev-entry"] as? String })
- .first(where: { $0.hasPrefix("/dev/disk") })
- {
- _ = try? runProcess("/usr/bin/hdiutil", ["detach", "-force", device])
- }
+ detachReportedImage(in: String(data: data, encoding: .utf8) ?? "")
throw FirmwareManifest.ManifestError.missingKey("dev-entry or mount-point")
}
+ private func detachReportedImage(in output: String) {
+ guard let range = output.range(of: #"/dev/disk[0-9]+"#, options: .regularExpression) else {
+ return
+ }
+ let device = String(output[range])
+ attachedDevices.insert(device)
+ try? detachImage(deviceNode: device)
+ }
+
func detachImage(deviceNode: String) throws {
do {
_ = try runProcess("/usr/bin/hdiutil", ["detach", deviceNode])
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift
index fbca9c40..bba898aa 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift
@@ -123,10 +123,10 @@ struct VPhoneCustomFirmwareInstaller {
}
try fm.createDirectory(at: system, withIntermediateDirectories: false)
try fm.createDirectory(at: data, withIntermediateDirectories: false)
- try tool("/sbin/mount_apfs", ["-o", "rw", "/dev/\(container)s1", system.path])
systemMounted = true
- try tool("/sbin/mount_apfs", ["-o", "rw", "/dev/\(container)s3", data.path])
+ try tool("/sbin/mount_apfs", ["-o", "rw", "/dev/\(container)s1", system.path])
dataMounted = true
+ try tool("/sbin/mount_apfs", ["-o", "rw", "/dev/\(container)s3", data.path])
print("[*] JB system install: \(bundle.lastPathComponent)")
try installMounted(system: system, data: data, work: work)
_ = try tool("/sbin/umount", [data.path])
@@ -255,13 +255,16 @@ struct VPhoneCustomFirmwareInstaller {
let appMount = work.appendingPathComponent("mnt-app")
try fm.createDirectory(at: osMount, withIntermediateDirectories: true)
try fm.createDirectory(at: appMount, withIntermediateDirectories: true)
+ var osNeedsDetach = true
+ defer { if osNeedsDetach { try? detachImage(at: osMount) } }
try tool(
"/usr/bin/hdiutil",
["attach", "-mountpoint", osMount.path,
plain.path, "-nobrowse", "-owners", "off"],
quiet: true,
)
- defer { _ = try? tool("/usr/bin/hdiutil", ["detach", "-force", osMount.path], quiet: true) }
+ var appNeedsDetach = true
+ defer { if appNeedsDetach { try? detachImage(at: appMount) } }
try tool(
"/usr/bin/hdiutil",
["attach", "-mountpoint", appMount.path,
@@ -269,7 +272,6 @@ struct VPhoneCustomFirmwareInstaller {
"-nobrowse", "-owners", "off"],
quiet: true,
)
- defer { _ = try? tool("/usr/bin/hdiutil", ["detach", "-force", appMount.path], quiet: true) }
for (source, destination) in [(osMount, os), (appMount, app)] {
// The restored rootfs has dangling Cryptex symlinks. fileExists
// follows those links and reports false until Preboot is populated.
@@ -283,6 +285,10 @@ struct VPhoneCustomFirmwareInstaller {
try fm.copyItem(at: entry, to: destination.appendingPathComponent(entry.lastPathComponent))
}
}
+ try detachImage(at: appMount)
+ appNeedsDetach = false
+ try detachImage(at: osMount)
+ osNeedsDetach = false
}
try symlink("../../../System/Cryptexes/OS/System/Library/Caches/com.apple.dyld",
at: system.appendingPathComponent("System/Library/Caches/com.apple.dyld"))
@@ -459,6 +465,14 @@ struct VPhoneCustomFirmwareInstaller {
try fm.removeItem(at: work)
}
+ private func detachImage(at mount: URL) throws {
+ do {
+ _ = try tool("/usr/bin/hdiutil", ["detach", mount.path], quiet: true)
+ } catch {
+ _ = try tool("/usr/bin/hdiutil", ["detach", "-force", mount.path], quiet: true)
+ }
+ }
+
@discardableResult
private func patch(_ verb: String, _ arguments: [String]) throws -> String {
try tool(executable.path, ["cfw", verb] + arguments)
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareDownloadCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareDownloadCommand.swift
index 50d188cd..1fde18f4 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareDownloadCommand.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareDownloadCommand.swift
@@ -81,9 +81,16 @@ struct VPhoneFirmwareSealToolCommand: ParsableCommand {
try FileManager.default.createDirectory(at: output, withIntermediateDirectories: true)
let ramdisk = try vphoneRunBlocking { try await Self.fetchRamdisk(version: version, in: output) }
- defer { try? FileManager.default.removeItem(at: ramdisk.deletingLastPathComponent()) }
+ var attached = false
+ defer {
+ if attached {
+ fputs("warning: restore ramdisk is still mounted; left it at \(ramdisk.path)\n", stderr)
+ } else {
+ try? FileManager.default.removeItem(at: ramdisk.deletingLastPathComponent())
+ }
+ }
- try Self.copyOut(of: ramdisk, to: destination)
+ try Self.copyOut(of: ramdisk, to: destination, attached: &attached)
// Lifted out of someone else's signed image; re-seal it so the kernel
// will exec it here.
try Self.run("/usr/bin/codesign", ["--force", "--sign", "-", destination.path])
@@ -150,23 +157,39 @@ struct VPhoneFirmwareSealToolCommand: ParsableCommand {
}
/// Mount the ramdisk read-only and take the one file out of it.
- private static func copyOut(of dmg: URL, to destination: URL) throws {
- let attached = try run("/usr/bin/hdiutil",
- ["attach", "-readonly", "-nobrowse", "-plist", dmg.path])
+ private static func copyOut(of dmg: URL, to destination: URL, attached: inout Bool) throws {
+ let output = try run("/usr/bin/hdiutil",
+ ["attach", "-readonly", "-nobrowse", "-plist", dmg.path])
+ attached = true
guard let plist = try PropertyListSerialization.propertyList(
- from: Data(attached.utf8),
+ from: Data(output.utf8),
format: nil,
) as? [String: Any],
- let entities = plist["system-entities"] as? [[String: Any]],
- let mount = entities.compactMap({ $0["mount-point"] as? String }).first
- else { throw VPhoneRemoteZip.Error.malformed("hdiutil attached nothing with a mount point") }
- defer { _ = try? run("/usr/bin/hdiutil", ["detach", mount]) }
-
- let source = URL(fileURLWithPath: mount).appending(
- path: "System/Library/Filesystems/apfs.fs/Contents/Resources/apfs_sealvolume",
- )
- try FileManager.default.copyItem(at: source, to: destination)
- try FileManager.default.setAttributes([.posixPermissions: 0o755],
- ofItemAtPath: destination.path)
+ let entities = plist["system-entities"] as? [[String: Any]]
+ else { throw VPhoneRemoteZip.Error.malformed("hdiutil returned no disk information") }
+ let mount = entities.compactMap { $0["mount-point"] as? String }.first
+ let device = entities.compactMap { $0["dev-entry"] as? String }.first
+ guard let target = device ?? mount else {
+ throw VPhoneRemoteZip.Error.malformed("hdiutil attached nothing that can be detached")
+ }
+
+ let copied: Result<Void, Swift.Error> = Result {
+ guard let mount else {
+ throw VPhoneRemoteZip.Error.malformed("hdiutil attached nothing with a mount point")
+ }
+ let source = URL(fileURLWithPath: mount).appending(
+ path: "System/Library/Filesystems/apfs.fs/Contents/Resources/apfs_sealvolume",
+ )
+ try FileManager.default.copyItem(at: source, to: destination)
+ try FileManager.default.setAttributes([.posixPermissions: 0o755],
+ ofItemAtPath: destination.path)
+ }
+ do {
+ try run("/usr/bin/hdiutil", ["detach", target])
+ } catch {
+ try run("/usr/bin/hdiutil", ["detach", "-force", target])
+ }
+ attached = false
+ try copied.get()
}
}
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Host/VPhonePCCGPURecovery.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Host/VPhonePCCGPURecovery.swift
index 14a0f475..dff9f0b5 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Host/VPhonePCCGPURecovery.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Host/VPhonePCCGPURecovery.swift
@@ -123,8 +123,9 @@ enum VPhonePCCGPURecovery {
throw Error.toolFailed("hdiutil", "attached no disk device")
}
var mountToClean: URL?
+ var diskAttached = true
defer {
- if (try? run("/usr/bin/hdiutil", ["detach", baseDisk])) == nil {
+ if diskAttached, (try? run("/usr/bin/hdiutil", ["detach", baseDisk])) == nil {
_ = try? run("/usr/bin/hdiutil", ["detach", "-force", baseDisk])
}
if let mountToClean {
@@ -164,8 +165,8 @@ enum VPhonePCCGPURecovery {
_ = try? run("/sbin/umount", ["-f", mount.path])
}
}
- try run("/sbin/mount_apfs", ["-o", "rdonly", "/dev/\(container)s1", mount.path])
mounted = true
+ try run("/sbin/mount_apfs", ["-o", "rdonly", "/dev/\(container)s1", mount.path])
let source = mount.appending(
path: "System/Library/Extensions/\(VPhonePCCGPUDriver.name)",
@@ -175,6 +176,18 @@ enum VPhonePCCGPURecovery {
into: restoreDirectory,
expectedPlatformVersion: expectedPlatformVersion,
)
+ do {
+ try run("/sbin/umount", [mount.path])
+ } catch {
+ try run("/sbin/umount", ["-f", mount.path])
+ }
+ mounted = false
+ do {
+ try run("/usr/bin/hdiutil", ["detach", baseDisk])
+ } catch {
+ try run("/usr/bin/hdiutil", ["detach", "-force", baseDisk])
+ }
+ diskAttached = false
print("[+] GPU driver staged from cloudOS restored by vphone-cli")
}
diff --git a/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneIPSWCache.swift b/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneIPSWCache.swift
index 436df3db..5673652c 100644
--- a/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneIPSWCache.swift
+++ b/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneIPSWCache.swift
@@ -34,7 +34,7 @@ public enum VPhoneIPSWCache {
public static func resolve(
_ source: String,
in cacheDirectory: URL,
- session: URLSession = .shared,
+ session: URLSession = URLSession(configuration: .ephemeral),
) async throws -> Archive {
guard let url = URL(string: source), let scheme = url.scheme?.lowercased() else {
return try inspect(URL(fileURLWithPath: source))
diff --git a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneAEA.swift b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneAEA.swift
index 588da48a..40b934ec 100644
--- a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneAEA.swift
+++ b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneAEA.swift
@@ -118,7 +118,9 @@ public enum VPhoneAEA {
let wrapped = response["wrapped-key"].flatMap({ Data(base64Encoded: $0) })
else { throw Error.malformedFCSResponse }
- let (pem, urlResponse) = try await URLSession.shared.data(from: keyURL)
+ let session = URLSession(configuration: .ephemeral)
+ defer { session.invalidateAndCancel() }
+ let (pem, urlResponse) = try await session.data(from: keyURL)
if let http = urlResponse as? HTTPURLResponse, http.statusCode != 200 {
throw Error.keyFetchFailed(keyURL, http.statusCode)
}
diff --git a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareIndex.swift b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareIndex.swift
index 9cbd23cc..5fcf55ec 100644
--- a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareIndex.swift
+++ b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareIndex.swift
@@ -56,19 +56,6 @@ public enum VPhoneFirmwareIndex {
static let catalogueURL = URL(string: "https://api.appledb.dev/main.json.xz")!
- /// How long a downloaded catalogue is reused before it is fetched again.
- ///
- /// Apple ships a firmware every few weeks; a day-old catalogue is wrong
- /// only in the window between a release and the next refresh, and a user
- /// who hits that can delete the file. Re-downloading 8 MB for every
- /// `fw prepare` would be the worse trade.
- static let maxAge: TimeInterval = 24 * 60 * 60
-
- static var cacheFile: URL {
- VPhoneResources.userDataRoot()
- .appendingPathComponent("cache/appledb-main.json")
- }
-
// MARK: - Queries
/// Every released iOS restore URL for one device identifier, newest first.
@@ -154,34 +141,13 @@ public enum VPhoneFirmwareIndex {
}
private static func catalogueData() async throws -> Data {
- let cache = cacheFile
- if let attributes = try? FileManager.default.attributesOfItem(atPath: cache.path),
- let modified = attributes[.modificationDate] as? Date,
- Date().timeIntervalSince(modified) < maxAge,
- let cached = try? Data(contentsOf: cache, options: .mappedIfSafe)
- {
- return cached
- }
-
- let (compressed, response) = try await URLSession.shared.data(from: catalogueURL)
+ let session = URLSession(configuration: .ephemeral)
+ defer { session.invalidateAndCancel() }
+ let (compressed, response) = try await session.data(from: catalogueURL)
if let http = response as? HTTPURLResponse, http.statusCode != 200 {
- // A stale catalogue beats no catalogue: the network is the thing
- // most likely to be missing, and last week's list still has every
- // firmware from last week.
- if let cached = try? Data(contentsOf: cache, options: .mappedIfSafe) {
- return cached
- }
throw Error.fetchFailed(catalogueURL, http.statusCode)
}
-
- let json = try decompressXZ(compressed)
- try? FileManager.default.createDirectory(
- at: cache.deletingLastPathComponent(),
- withIntermediateDirectories: true,
- )
- try? json.write(to: cache)
- try? VPhoneHostFilePermissions.makeAccessible(at: cache)
- return json
+ return try decompressXZ(compressed)
}
/// `.xz`, through libcompression.
diff --git a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneRemoteZip.swift b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneRemoteZip.swift
index 1a9de186..61675147 100644
--- a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneRemoteZip.swift
+++ b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneRemoteZip.swift
@@ -20,6 +20,8 @@ import Compression
import Foundation
public struct VPhoneRemoteZip: Sendable {
+ private static let session = URLSession(configuration: .ephemeral)
+
public struct Entry: Sendable {
public let name: String
public let compressedSize: UInt64
@@ -169,7 +171,7 @@ public struct VPhoneRemoteZip: Sendable {
private static func contentLength(of url: URL) async throws -> UInt64 {
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
- let (_, response) = try await URLSession.shared.data(for: request)
+ let (_, response) = try await session.data(for: request)
guard let http = response as? HTTPURLResponse else { throw Error.notSeekable(url) }
guard http.statusCode == 200 else { throw Error.http(http.statusCode, url) }
guard http.expectedContentLength > 0,
@@ -182,7 +184,7 @@ public struct VPhoneRemoteZip: Sendable {
guard count > 0 else { return Data() }
var request = URLRequest(url: url)
request.setValue("bytes=\(offset)-\(offset + count - 1)", forHTTPHeaderField: "Range")
- let (data, response) = try await URLSession.shared.data(for: request)
+ let (data, response) = try await session.data(for: request)
guard let http = response as? HTTPURLResponse else { throw Error.notSeekable(url) }
// 206 is the only acceptable answer. A 200 means the server ignored the
// header and is sending the whole 18 GB, which is exactly the thing this
diff --git a/VPhoneKit/VPhoneCoreKitTests/Firmware/ResourcesTests.swift b/VPhoneKit/VPhoneCoreKitTests/Firmware/ResourcesTests.swift
index 24934f06..7b7e634d 100644
--- a/VPhoneKit/VPhoneCoreKitTests/Firmware/ResourcesTests.swift
+++ b/VPhoneKit/VPhoneCoreKitTests/Firmware/ResourcesTests.swift
@@ -37,8 +37,8 @@ struct ResourcesTests {
#expect(r.base.path == root.resolvingSymlinksInPath().path)
}
- @Test func `VM root is home relative`() {
- // The VPHONE_ROOT override would relocate the library; assert the default
+ @Test func `user data root is home relative`() {
+ // The VPHONE_ROOT override would relocate the data root; assert the default
// with the variable held clear, rather than bailing out when some other
// suite happens to have set it — that skip was the old way of living
// with the race `ProcessEnvironment` now closes.
@@ -58,17 +58,15 @@ struct ResourcesTests {
/// interpreter. That claim is what the deleted venv tests used to guard
/// from the other side, so assert it directly: every URL this type hands
/// out is rooted in `base`.
- @Test func `every resource is rooted in the base or the data root`() {
- ProcessEnvironment.withOverrides(["VPHONE_ROOT": "/tmp/vphone-test-root"]) {
- let base = URL(fileURLWithPath: "/x")
- let r = VPhoneResources(base: base)
- let rooted = [
- r.scriptsDir,
- r.vphoned,
- ]
- for url in rooted {
- #expect(url.path.hasPrefix("/x/"), "\(url.path) escapes the resource base")
- }
+ @Test func `every resource is rooted in the base`() {
+ let base = URL(fileURLWithPath: "/x")
+ let r = VPhoneResources(base: base)
+ let rooted = [
+ r.scriptsDir,
+ r.vphoned,
+ ]
+ for url in rooted {
+ #expect(url.path.hasPrefix("/x/"), "\(url.path) escapes the resource base")
}
}