Run a virtual iPhone on an Apple Silicon Mac.
Swift
perf: map files instead of reading them, and stop the test run eating the machine
Lakra908f819440c
diff --git a/scripts/check_aux.sh b/scripts/check_aux.shindex 3d0d7dbd..6b312983 100755--- a/scripts/check_aux.sh+++ b/scripts/check_aux.sh@@ -359,6 +359,48 @@ check_sources() {donegreen " ok gate 2 [dist]: ${#dist_clean} dist scripts reach for nothing outside macOS"+ # --- Swift: read a file by mapping it, never by slurping it. ---+ #+ # `Data(contentsOf:)` with no options reads the whole file into resident+ # memory. That is fine for a plist and ruinous for what this project actually+ # opens: `ManifestHashPatcher` hashes the `OS` component, which is a ten+ # gigabyte filesystem image, and `DSCLocalSymbolTable` parses+ # dyld_shared_cache_arm64e.symbols, which is 1.17 GB on iOS 27 — it read both+ # tables whole, so every symbol resolver cost over a gigabyte and a test run+ # that built several took the machine down.+ #+ # `.mappedIfSafe` costs address space instead: pages fault in where they are+ # touched and the kernel evicts them again.+ #+ # There IS one case where mapping is wrong, and it is not a matter of taste:+ # a buffer that will be mutated and written back over its own file. The write+ # replaces the file the buffer is mapped from, and the next page fault+ # through that mapping is a SIGBUS — a killed process with no failed+ # assertion, which is how it presented. Those reads say so in the spelling:+ # `Data(contentsOfFileToRewrite:)`, defined once, in+ # sources/FirmwarePatcher/Binary/InPlaceRewrite.swift, with the reason.+ #+ # So the rule for sources/ is: no bare `Data(contentsOf:)`. Either it is+ # mapped, or it names itself as the rewrite case.+ #+ # tests/ is deliberately NOT covered, and that is not laziness. What a test+ # opens is a committed fixture of a few megabytes, so mapping buys nothing —+ # and the fixtures are exactly what the patch tests mutate and write back+ # over, which is the one shape where mapping is wrong. Holding the tests to+ # the production rule would trade a memory problem they do not have for a+ # SIGBUS they would.+ hits=$(grep -rn 'Data(contentsOf:' --include='*.swift' sources/ 2>/dev/null \+ | grep -v 'mappedIfSafe' \+ | grep -vE ':[0-9]+: *//')+ if [[ -n "$hits" ]]; then+ while IFS= read -r line; do+ [[ -z "$line" ]] && continue+ fail "gate 2: unmapped file read — ${line%%:*}:${${line#*:}%%:*} (add options: .mappedIfSafe)"+ done <<< "$hits"+ else+ green " ok gate 2: every file read in sources/ is mapped or a declared rewrite"+ fi+# --- No interpreter, in any tier. ---# This is D1's completion gate, and it deliberately does not look for a# pattern: it fails on the word `python` wherever this project's own shell can@@ -449,6 +491,26 @@ check_smoke() {fifi+ # The IM4P half of what `ipsw` used to do, round-tripped. Deliberately not+ # `fw aea-key`, `fw urls` or `fw seal-tool`: those three are the other half+ # and every one of them makes a network request, which would turn this gate+ # amber on a train. They are checked by hand — `fw urls` against `ipsw+ # download ipsw --urls`, `fw aea-key` against `ipsw fw aea --key`.+ if [[ -x "$cli" ]]; then+ local im4p="$tmp/im4p"+ mkdir -p "$im4p"+ print "payload" > "$im4p/in.bin"+ if env -i PATH=/usr/bin:/bin HOME="$tmp" "$cli" fw im4p-create \+ --type isys --version 0 -o "$im4p/c.im4p" "$im4p/in.bin" >/dev/null 2>&1 \+ && env -i PATH=/usr/bin:/bin HOME="$tmp" "$cli" fw im4p-extract \+ --output "$im4p/out.bin" "$im4p/c.im4p" >/dev/null 2>&1 \+ && cmp -s "$im4p/in.bin" "$im4p/out.bin"; then+ green " ok gate 3: vphone-cli fw im4p-create/extract with no ipsw on PATH"+ else+ fail "gate 3: vphone-cli could not round-trip an IM4P"+ fi+ fi+# vphone-vm is deliberately NOT smoke-tested here: amfid refuses it unless the# host's own AMFI bypass allows this copy — and the copy under test is a# relocated one, with a path no allowlist was told about. Its exit code woulddiff --git a/scripts/guest_binaries.mk b/scripts/guest_binaries.mkindex c14d6768..f327da7e 100644--- a/scripts/guest_binaries.mk+++ b/scripts/guest_binaries.mk@@ -64,13 +64,32 @@ sdk_check:echo " sudo xcode-select -s /Applications/Xcode.app" >&2; \exit 1)+# The argument order below is the installers' own, framework list included. It+# looks arbitrary and it is load-bearing: `-framework` order decides+# LC_LOAD_DYLIB order, which decides the indirect symbol table's numbering, so+# reordering the list changes the output bytes for no reason. Keeping it means+# these come out byte-identical to what `cfw install` used to compile on the+# user's machine. Verified: vpregister and libvcamcaptured.dylib are identical;+# TweakLoader.dylib differs in exactly two fields per slice —+#+# LC_UUID clang derives it from the content, so any two links differ+# LC_ID_DYLIB it takes no -install_name, so this defaults to the output+# path. It used to be the installer's own+# <vm>/.cfw_temp/TweakLoader.dylib and is now this stable one.+# Nothing reads it: the guest loads the file by absolute path+# from /var/jb/usr/lib, not through its install name.+# TweakLoader — the substrate-style loader injected into guest processes. Fat:# arm64 for the older bases, arm64e for iOS 27. No -install_name; the installer# places it at a path the injected LC_LOAD_DYLIB already names.$(GUEST_DIR)/TweakLoader.dylib: scripts/tweakloader/TweakLoader.m@echo "=== Building TweakLoader.dylib (arm64 + arm64e, iphoneos) ==="- @$(IOS_CC) $(IOS_CFLAGS) -arch arm64 -arch arm64e -O3 \- -dynamiclib -framework Foundation \+ @$(IOS_CC) -isysroot $(IOS_SDK) \+ -arch arm64 -arch arm64e \+ $(IOS_MIN) \+ -dynamiclib \+ -fobjc-arc -O3 \+ -framework Foundation \-o $@ $<# vpregister — registers JB app bundles through the containerized LaunchServices@@ -79,30 +98,51 @@ $(GUEST_DIR)/TweakLoader.dylib: scripts/tweakloader/TweakLoader.m# LaunchServices symbols it calls are resolved by the guest's dyld.$(GUEST_DIR)/vpregister: scripts/vpregister/vpregister.m@echo "=== Building vpregister (arm64e, iphoneos) ==="- @$(IOS_CC) $(IOS_CFLAGS) -arch arm64e -Os \- -framework Foundation -Wl,-undefined,dynamic_lookup \+ @$(IOS_CC) -isysroot $(IOS_SDK) \+ -arch arm64e \+ $(IOS_MIN) \+ -fobjc-arc -Os \+ -framework Foundation \+ -Wl,-undefined,dynamic_lookup \-o $@ $<# libvcamcaptured — loaded into /usr/libexec/cameracaptured via an injected# LC_LOAD_DYLIB, so the -install_name has to be the guest path exactly.$(GUEST_DIR)/libvcamcaptured.dylib: scripts/vcamcaptured/libvcamcaptured.m@echo "=== Building libvcamcaptured.dylib (arm64e, iphoneos) ==="- @$(IOS_CC) $(IOS_CFLAGS) -arch arm64e -Os \- -dynamiclib -install_name /var/jb/usr/lib/libvcamcaptured.dylib \- -framework Foundation -framework CoreMedia -framework CoreVideo \+ @$(IOS_CC) -isysroot $(IOS_SDK) \+ -arch arm64e \+ $(IOS_MIN) \+ -dynamiclib \+ -fobjc-arc -Os \+ -install_name /var/jb/usr/lib/libvcamcaptured.dylib \+ -framework CoreMedia \+ -framework CoreVideo \+ -framework Foundation \-o $@ $<# libcamfix — the substrate plugin TweakLoader loads into every AVFoundation# client. Same rule about -install_name.$(GUEST_DIR)/libcamfix.dylib: scripts/camfix/libcamfix.m@echo "=== Building libcamfix.dylib (arm64e, iphoneos) ==="- @$(IOS_CC) $(IOS_CFLAGS) -arch arm64e -Os \+ @$(IOS_CC) -isysroot $(IOS_SDK) \+ -arch arm64e \+ $(IOS_MIN) \-dynamiclib \+ -fobjc-arc -Os \-install_name /var/jb/Library/MobileSubstrate/DynamicLibraries/libcamfix.dylib \- -framework AVFoundation -framework CoreImage -framework CoreGraphics \- -framework CoreMedia -framework CoreVideo -framework Foundation \- -framework ImageIO -framework IOSurface -framework MobileCoreServices \- -framework Photos -framework QuartzCore -framework UIKit \+ -framework AVFoundation \+ -framework CoreImage \+ -framework CoreGraphics \+ -framework CoreMedia \+ -framework CoreVideo \+ -framework Foundation \+ -framework ImageIO \+ -framework IOSurface \+ -framework MobileCoreServices \+ -framework Photos \+ -framework QuartzCore \+ -framework UIKit \-o $@ $<# vphoned keeps its own Makefile — it is a multi-file target with a vendoreddiff --git a/sources/FirmwarePatcher/Binary/BinaryBuffer.swift b/sources/FirmwarePatcher/Binary/BinaryBuffer.swiftindex 17382641..bb79236d 100644--- a/sources/FirmwarePatcher/Binary/BinaryBuffer.swift+++ b/sources/FirmwarePatcher/Binary/BinaryBuffer.swift@@ -35,7 +35,11 @@ public final class BinaryBuffer: @unchecked Sendable {}public convenience init(contentsOf url: URL) throws {- try self.init(Data(contentsOf: url))+ // Read, not mapped. A `BinaryBuffer` exists to be mutated, and what it+ // is built from — a kernelcache, an iBoot — is written back over the+ // path it came from. See InPlaceRewrite.swift for why a mapping cannot+ // survive that.+ try self.init(Data(contentsOfFileToRewrite: url))}// MARK: - Read Helpersdiff --git a/sources/FirmwarePatcher/Binary/IM4PHandler.swift b/sources/FirmwarePatcher/Binary/IM4PHandler.swiftindex 5ce5cd71..ec1f5d29 100644--- a/sources/FirmwarePatcher/Binary/IM4PHandler.swift+++ b/sources/FirmwarePatcher/Binary/IM4PHandler.swift@@ -12,7 +12,13 @@ public enum IM4PHandler {/// - Parameter url: Path to the firmware file./// - Returns: Tuple of (extracted payload data, original IM4P if applicable).public static func load(contentsOf url: URL) throws -> (payload: Data, im4p: IM4P?) {- let fileData = try Data(contentsOf: url)+ // READ, not mapped, and the asymmetry is deliberate. `save` writes the+ // patched container back over the same path this loaded from — see+ // `FirmwarePipeline.ContainerFirmwareLoader`, whose `save` re-loads the+ // original to keep its metadata and then writes to that very url. A+ // mapping of a file being replaced is a SIGBUS on the next fault; see+ // InPlaceRewrite.swift.+ let fileData = try Data(contentsOfFileToRewrite: url)// Try to parse as IM4P firstif let im4p = try? IM4P(fileData) {diff --git a/sources/FirmwarePatcher/Binary/InPlaceRewrite.swift b/sources/FirmwarePatcher/Binary/InPlaceRewrite.swiftnew file mode 100644index 00000000..ffc93e7c--- /dev/null+++ b/sources/FirmwarePatcher/Binary/InPlaceRewrite.swift@@ -0,0 +1,38 @@+// InPlaceRewrite.swift — the one file read in this module that is NOT mapped.+//+// Everything else opens a file with `Data(contentsOf:options:.mappedIfSafe)`,+// because the files this project opens run to gigabytes and reading them whole+// is how a test run used to take the machine down.+//+// The patchers are the exception, and it is not a matter of taste:+//+// var data = try Data(contentsOf: url, options: .mappedIfSafe)+// patch(&data)+// try data.write(to: url) // <- the file `data` is mapped from+//+// `Data.write(to:)` replaces the destination, and the destination is the file+// the source buffer is mapped from. The mapping is invalidated underneath the+// write that is reading through it, and the next page fault is a SIGBUS — a+// killed process with no failed assertion and nothing in the log, which is+// exactly how this presented.+//+// So a buffer that will be mutated and written back over its own file is READ.+// Nothing that comes through here is large: these are single Mach-Os out of a+// guest filesystem — launchd is 639 KB, the biggest is mobileactivationd at+// 4.6 MB — so the copy costs nothing worth having, and `make check-aux` knows+// this spelling is the deliberate one.++import Foundation++extension Data {+ /// Read a file that is about to be mutated and written back over itself.+ ///+ /// Not `Data(contentsOf:)` underneath, deliberately: the admission gate+ /// bans that spelling outright so nobody reintroduces an unbounded read by+ /// habit, and this is the one place allowed to say "a copy is what I want".+ init(contentsOfFileToRewrite url: URL) throws {+ let handle = try FileHandle(forReadingFrom: url)+ defer { try? handle.close() }+ self = try handle.readToEnd() ?? Data()+ }+}diff --git a/sources/FirmwarePatcher/CFW/CFWBuildVersion.swift b/sources/FirmwarePatcher/CFW/CFWBuildVersion.swiftindex 66de6807..3e8ccc52 100644--- a/sources/FirmwarePatcher/CFW/CFWBuildVersion.swift+++ b/sources/FirmwarePatcher/CFW/CFWBuildVersion.swift@@ -124,7 +124,7 @@ public enum CFWBuildVersion {private static func readFile(at url: URL) throws -> Data {do {- return try Data(contentsOf: url)+ return try Data(contentsOfFileToRewrite: url)} catch {throw PatcherError.fileNotFound(url.path)}diff --git a/sources/FirmwarePatcher/CFW/CFWDaemons.swift b/sources/FirmwarePatcher/CFW/CFWDaemons.swiftindex 0aba0cf3..c630fc77 100644--- a/sources/FirmwarePatcher/CFW/CFWDaemons.swift+++ b/sources/FirmwarePatcher/CFW/CFWDaemons.swift@@ -82,7 +82,7 @@ public enum CFWDaemons {guard FileManager.default.fileExists(atPath: url.path) else {throw DaemonError.fileNotFound(url.path)}- let data = try Data(contentsOf: url)+ let data = try Data(contentsOfFileToRewrite: url)guard let dict = try PropertyListSerialization.propertyList(from: data,options: [],diff --git a/sources/FirmwarePatcher/CFW/CFWInjectDylib.swift b/sources/FirmwarePatcher/CFW/CFWInjectDylib.swiftindex 9ba80c1d..bc5e5ab5 100644--- a/sources/FirmwarePatcher/CFW/CFWInjectDylib.swift+++ b/sources/FirmwarePatcher/CFW/CFWInjectDylib.swift@@ -139,7 +139,7 @@ public enum CFWInjectDylib {guard FileManager.default.fileExists(atPath: url.path) else {throw PatcherError.fileNotFound(url.path)}- var data = try Data(contentsOf: url)+ var data = try Data(contentsOfFileToRewrite: url)let injections = try inject(dylibPath: dylibPath,into: &data,diff --git a/sources/FirmwarePatcher/CFW/CFWMachLookupExceptions.swift b/sources/FirmwarePatcher/CFW/CFWMachLookupExceptions.swiftindex 6332bf0b..872acda9 100644--- a/sources/FirmwarePatcher/CFW/CFWMachLookupExceptions.swift+++ b/sources/FirmwarePatcher/CFW/CFWMachLookupExceptions.swift@@ -65,7 +65,7 @@ public enum CFWMachLookupExceptions {public static func merge(at url: URL, verbose: Bool = true) throws -> Outcome {let data: Datado {- data = try Data(contentsOf: url)+ data = try Data(contentsOfFileToRewrite: url)} catch {throw PatcherError.fileNotFound(url.path)}diff --git a/sources/FirmwarePatcher/CFW/CFWMachOCodeSignature.swift b/sources/FirmwarePatcher/CFW/CFWMachOCodeSignature.swiftindex 2f57b632..87de928b 100644--- a/sources/FirmwarePatcher/CFW/CFWMachOCodeSignature.swift+++ b/sources/FirmwarePatcher/CFW/CFWMachOCodeSignature.swift@@ -343,7 +343,7 @@ public enum CFWMachOCodeSignature {guard FileManager.default.fileExists(atPath: url.path) else {throw PatcherError.fileNotFound(url.path)}- var data = try Data(contentsOf: url)+ var data = try Data(contentsOfFileToRewrite: url)let records = try reattest(&data, modifiedOffsets: modifiedOffsets, dryRun: dryRun)if !dryRun, !records.isEmpty {try data.write(to: url)diff --git a/sources/FirmwarePatcher/CFW/CFWPostRestoreDeviceTree.swift b/sources/FirmwarePatcher/CFW/CFWPostRestoreDeviceTree.swiftindex 3f764ff7..6ac94f55 100644--- a/sources/FirmwarePatcher/CFW/CFWPostRestoreDeviceTree.swift+++ b/sources/FirmwarePatcher/CFW/CFWPostRestoreDeviceTree.swift@@ -74,7 +74,7 @@ public enum CFWPostRestoreDeviceTree {) throws -> Outcome {let data: Datado {- data = try Data(contentsOf: url)+ data = try Data(contentsOfFileToRewrite: url)} catch {throw PatcherError.fileNotFound(url.path)}diff --git a/sources/FirmwarePatcher/CFW/Patches/CFWCacheloader.swift b/sources/FirmwarePatcher/CFW/Patches/CFWCacheloader.swiftindex 44814fa2..e98db25f 100644--- a/sources/FirmwarePatcher/CFW/Patches/CFWCacheloader.swift+++ b/sources/FirmwarePatcher/CFW/Patches/CFWCacheloader.swift@@ -190,7 +190,7 @@ public enum CFWCacheLoaderPatcher {guard FileManager.default.fileExists(atPath: url.path) else {throw PatcherError.fileNotFound(url.path)}- var data = try Data(contentsOf: url)+ var data = try Data(contentsOfFileToRewrite: url)let report = try patch(&data,dryRun: dryRun,diff --git a/sources/FirmwarePatcher/CFW/Patches/CFWDiskimagesiod.swift b/sources/FirmwarePatcher/CFW/Patches/CFWDiskimagesiod.swiftindex 56068922..78c40605 100644--- a/sources/FirmwarePatcher/CFW/Patches/CFWDiskimagesiod.swift+++ b/sources/FirmwarePatcher/CFW/Patches/CFWDiskimagesiod.swift@@ -259,7 +259,7 @@ public enum CFWDiskimagesiod {guard FileManager.default.fileExists(atPath: url.path) else {throw PatcherError.fileNotFound(url.path)}- var data = try Data(contentsOf: url)+ var data = try Data(contentsOfFileToRewrite: url)let report = try patch(&data, reattest: reattest, dryRun: dryRun, log: log)if !dryRun, report.record != nil || !report.rehashes.isEmpty {try data.write(to: url)diff --git a/sources/FirmwarePatcher/CFW/Patches/CFWJetsam.swift b/sources/FirmwarePatcher/CFW/Patches/CFWJetsam.swiftindex eac50ba0..f8894cdc 100644--- a/sources/FirmwarePatcher/CFW/Patches/CFWJetsam.swift+++ b/sources/FirmwarePatcher/CFW/Patches/CFWJetsam.swift@@ -193,7 +193,7 @@ public enum CFWJetsamPatcher {guard FileManager.default.fileExists(atPath: url.path) else {throw PatcherError.fileNotFound(url.path)}- var data = try Data(contentsOf: url)+ var data = try Data(contentsOfFileToRewrite: url)let outcome = try patch(&data, dryRun: dryRun, reattest: reattest, log: log)if !dryRun, outcome.verdict == .patched {try data.write(to: url)diff --git a/sources/FirmwarePatcher/CFW/Patches/CFWMobileactivationd.swift b/sources/FirmwarePatcher/CFW/Patches/CFWMobileactivationd.swiftindex 9b6ede8d..7ed67b26 100644--- a/sources/FirmwarePatcher/CFW/Patches/CFWMobileactivationd.swift+++ b/sources/FirmwarePatcher/CFW/Patches/CFWMobileactivationd.swift@@ -151,7 +151,7 @@ public enum CFWMobileactivationd {guard FileManager.default.fileExists(atPath: url.path) else {throw PatcherError.fileNotFound(url.path)}- var data = try Data(contentsOf: url)+ var data = try Data(contentsOfFileToRewrite: url)let before = datalet report = try patch(&data, resign: resign, dryRun: dryRun, log: log)diff --git a/sources/FirmwarePatcher/CFW/Patches/CFWSeputil.swift b/sources/FirmwarePatcher/CFW/Patches/CFWSeputil.swiftindex 9a143b52..34a1d4f7 100644--- a/sources/FirmwarePatcher/CFW/Patches/CFWSeputil.swift+++ b/sources/FirmwarePatcher/CFW/Patches/CFWSeputil.swift@@ -181,7 +181,7 @@ public enum CFWSeputil {throw PatcherError.fileNotFound(url.path)}log?(" [.] \(url.path)")- var data = try Data(contentsOf: url)+ var data = try Data(contentsOfFileToRewrite: url)let outcome = try patch(&data, dryRun: dryRun, reattest: reattest, log: log)if outcome.verdict == .patched {try data.write(to: url)diff --git a/sources/FirmwarePatcher/CFW/Patches/CFWWatchdogd.swift b/sources/FirmwarePatcher/CFW/Patches/CFWWatchdogd.swiftindex 8831fcb7..aa091723 100644--- a/sources/FirmwarePatcher/CFW/Patches/CFWWatchdogd.swift+++ b/sources/FirmwarePatcher/CFW/Patches/CFWWatchdogd.swift@@ -228,7 +228,7 @@ public enum CFWWatchdogd {guard FileManager.default.fileExists(atPath: url.path) else {throw PatcherError.fileNotFound(url.path)}- var data = try Data(contentsOf: url)+ var data = try Data(contentsOfFileToRewrite: url)let report = try patch(&data, dryRun: dryRun, log: log)if !dryRun, report.outcome == .patched {try data.write(to: url)diff --git a/sources/FirmwarePatcher/DSC/DSCChunkSet.swift b/sources/FirmwarePatcher/DSC/DSCChunkSet.swiftindex c80c4ffa..944c017d 100644--- a/sources/FirmwarePatcher/DSC/DSCChunkSet.swift+++ b/sources/FirmwarePatcher/DSC/DSCChunkSet.swift@@ -363,10 +363,8 @@ public final class DSCChunkSet {guard !needle.isEmpty else { return [] }var results: [UInt64] = []for mapping in mappings where mapping.isExecutable {- let buffer = try Self.read(- url: mapping.chunkURL,- offset: mapping.fileOffset,- length: Int(mapping.size)+ let buffer = try Self.window(+ over: mapping, at: mapping.fileOffset, length: Int(mapping.size))var searchFrom = buffer.startIndexwhile searchFrom < buffer.endIndex,@@ -398,9 +396,9 @@ public final class DSCChunkSet {let scanLength = min(localOffset, maxWalk)let scanStart = localOffset - scanLength// +4 so a header sitting right at the scan's end is still matched.- let buffer = try Self.read(- url: mapping.chunkURL,- offset: mapping.fileOffset &+ UInt64(scanStart),+ let buffer = try Self.window(+ over: mapping,+ at: mapping.fileOffset &+ UInt64(scanStart),length: scanLength + 4)var searchEnd = buffer.endIndex@@ -513,6 +511,10 @@ public final class DSCChunkSet {}}+ /// A bounded read: a seek and a copy of `length` bytes.+ ///+ /// Right for the small reads — a load command, a page, a run of a few+ /// kilobytes. Wrong for a whole mapping; use `window(over:)` for those.static func read(url: URL, offset: UInt64, length: Int) throws -> Data {guard length > 0 else { return Data() }let handle = try FileHandle(forReadingFrom: url)@@ -520,4 +522,29 @@ public final class DSCChunkSet {try handle.seek(toOffset: offset)return try handle.read(upToCount: length) ?? Data()}++ /// A whole mapping, as a slice of the MAPPED chunk file.+ ///+ /// The scans — `findStringVMAs` over every executable mapping,+ /// `findMachOHeaderBefore` walking back up to 64 MB — used `read` for this,+ /// which copied the mapping into the heap: an executable mapping is around+ /// 130 MB and there are two dozen of them, so one string search cost 3.3 GB+ /// of resident memory and a test run that did a few in a row took the+ /// machine down.+ ///+ /// A slice of a mapping is not a copy. The bytes fault in as the scan walks+ /// them and the kernel evicts them again behind it, so the same search+ /// costs a working set rather than the whole cache.+ ///+ /// The returned slice keeps the mapping alive for as long as it is held,+ /// and its `startIndex` is NOT zero — every caller here already works in+ /// terms of `buffer.startIndex`, which is what makes that safe.+ static func window(over mapping: Mapping, at offset: UInt64, length: Int) throws -> Data {+ guard length > 0 else { return Data() }+ let file = try Data(contentsOf: mapping.chunkURL, options: .mappedIfSafe)+ let lower = file.startIndex + Int(offset)+ guard lower <= file.endIndex else { return Data() }+ let upper = min(file.endIndex, lower + length)+ return file[lower ..< upper]+ }}diff --git a/sources/FirmwarePatcher/DSC/DSCLocalSymbolTable.swift b/sources/FirmwarePatcher/DSC/DSCLocalSymbolTable.swiftindex 651add25..24141057 100644--- a/sources/FirmwarePatcher/DSC/DSCLocalSymbolTable.swift+++ b/sources/FirmwarePatcher/DSC/DSCLocalSymbolTable.swift@@ -40,10 +40,33 @@ public struct DSCLocalSymbolTable: Sendable {}public let url: URL- /// The whole nlist array, `nlistCount` × 16 bytes.- let nlistData: Data- /// The string table the nlist entries index into.- let stringData: Data++ /// The whole `.symbols` file, MAPPED — never read.+ ///+ /// On an iOS 27 arm64e cache this file is 1.17 GB, and the string table and+ /// nlist array inside it are most of that. Reading them with+ /// `FileHandle.read(upToCount:)` — which is what this did — made every+ /// `DSCSymbolResolver` cost well over a gigabyte of resident memory, and a+ /// test run that builds several in parallel took the machine down with it.+ ///+ /// `.mappedIfSafe` costs address space instead. Pages fault in only where a+ /// lookup actually touches them, the kernel evicts them again under+ /// pressure, and nothing is ever copied — a symbol lookup reads a few+ /// hundred kilobytes of a 1.17 GB file and that is what it pays for.+ ///+ /// The two tables are held as OFFSET RANGES into this one mapping rather+ /// than as sliced `Data`s. That is not a style choice: `loadLE(_:at:)`+ /// takes an offset it passes straight to `Data.copyBytes(to:from:)`, whose+ /// range is in the collection's own index space, so it is only correct on a+ /// `Data` whose `startIndex` is zero. A slice of a mapping has neither a+ /// zero `startIndex` nor, if it were re-based with `Data(…)`, the mapping.+ private let file: Data+ /// Absolute offsets into `file` for the nlist array (`nlistCount` × 16 B).+ private let nlistBase: Int+ /// Absolute offset and size of the string table the nlist entries index.+ private let stringBase: Int+ private let stringSize: Int+public let nlistCount: Intpublic let entries: [ImageEntry]@@ -51,46 +74,43 @@ public struct DSCLocalSymbolTable: Sendable {public init(url: URL) throws {self.url = url- let handle = try FileHandle(forReadingFrom: url)- defer { try? handle.close() }+ let file = try Data(contentsOf: url, options: .mappedIfSafe)+ self.file = file- guard let head = try handle.read(upToCount: 0x100), head.count >= 0x50,- head.prefix(7) == Data("dyld_v1".utf8)+ guard file.count >= 0x50,+ file.prefix(7) == Data("dyld_v1".utf8)else {throw DSCError.notADyldCache(url.path)}- let infoOffset = head.loadLE(UInt64.self, at: 0x48)- guard infoOffset > 0 else {+ // Every read below goes through `at:` on the mapped file, so each one+ // is a page fault at most rather than a copy.+ let infoOffset = Int(file.at(UInt64.self, 0x48))+ guard infoOffset > 0, infoOffset + 24 <= file.count else {throw DSCError.symbolNotFound(symbol: "<local symbols table>", image: nil)}- try handle.seek(toOffset: infoOffset)- guard let info = try handle.read(upToCount: 24), info.count == 24 else {- throw DSCError.symbolNotFound(symbol: "<local symbols table>", image: nil)- }- let nlistOffset = UInt64(info.loadLE(UInt32.self, at: 0))- let nlistCount = Int(info.loadLE(UInt32.self, at: 4))- let stringsOffset = UInt64(info.loadLE(UInt32.self, at: 8))- let stringsSize = Int(info.loadLE(UInt32.self, at: 12))- let entriesOffset = UInt64(info.loadLE(UInt32.self, at: 16))- let entriesCount = Int(info.loadLE(UInt32.self, at: 20))-- try handle.seek(toOffset: infoOffset + stringsOffset)- stringData = try handle.read(upToCount: stringsSize) ?? Data()-- try handle.seek(toOffset: infoOffset + nlistOffset)- nlistData = try handle.read(upToCount: nlistCount * 16) ?? Data()- self.nlistCount = min(nlistCount, nlistData.count / 16)-- try handle.seek(toOffset: infoOffset + entriesOffset)- let entryData = try handle.read(upToCount: entriesCount * 16) ?? Data()- entries = (0 ..< min(entriesCount, entryData.count / 16)).map { index in- let base = index * 16+ let nlistOffset = Int(file.at(UInt32.self, infoOffset))+ let declaredNlistCount = Int(file.at(UInt32.self, infoOffset + 4))+ let stringsOffset = Int(file.at(UInt32.self, infoOffset + 8))+ let stringsSize = Int(file.at(UInt32.self, infoOffset + 12))+ let entriesOffset = Int(file.at(UInt32.self, infoOffset + 16))+ let entriesCount = Int(file.at(UInt32.self, infoOffset + 20))++ stringBase = infoOffset + stringsOffset+ stringSize = max(0, min(stringsSize, file.count - stringBase))++ nlistBase = infoOffset + nlistOffset+ nlistCount = max(0, min(declaredNlistCount, (file.count - nlistBase) / 16))++ let entryBase = infoOffset + entriesOffset+ let usableEntries = max(0, min(entriesCount, (file.count - entryBase) / 16))+ entries = (0 ..< usableEntries).map { index in+ let base = entryBase + index * 16return ImageEntry(- dylibOffset: entryData.loadLE(UInt64.self, at: base),- nlistStartIndex: Int(entryData.loadLE(UInt32.self, at: base + 8)),- nlistCount: Int(entryData.loadLE(UInt32.self, at: base + 12))+ dylibOffset: file.at(UInt64.self, base),+ nlistStartIndex: Int(file.at(UInt32.self, base + 8)),+ nlistCount: Int(file.at(UInt32.self, base + 12)))}}@@ -99,12 +119,13 @@ public struct DSCLocalSymbolTable: Sendable {/// The name at `stringIndex`, or `nil` when the index is out of range.func name(atStringIndex stringIndex: Int) -> String? {- guard stringIndex >= 0, stringIndex < stringData.count else { return nil }- var end = stringIndex- while end < stringData.count, stringData[stringData.startIndex + end] != 0 { end += 1 }- guard end > stringIndex else { return "" }- let bytes = stringData[(stringData.startIndex + stringIndex) ..< (stringData.startIndex + end)]- return String(decoding: bytes, as: UTF8.self)+ guard stringIndex >= 0, stringIndex < stringSize else { return nil }+ let start = file.startIndex + stringBase + stringIndex+ let limit = file.startIndex + stringBase + stringSize+ var end = start+ while end < limit, file[end] != 0 { end += 1 }+ guard end > start else { return "" }+ return String(decoding: file[start ..< end], as: UTF8.self)}/// `n_value` of the first entry named `name`, scanning the whole table.@@ -112,22 +133,24 @@ public struct DSCLocalSymbolTable: Sendable {/// First match wins, which is what the Python reference does. Names repeat/// across images — every ObjC class that answers `+alloc` contributes one —/// so prefer `symbols(in:)` when the image is known.+ ///+ /// The scan walks the mapping directly and builds no `String`: a table with+ /// millions of entries would otherwise allocate millions of them to throw+ /// all but one away.public func firstAddress(of name: String) -> UInt64? {let wanted = Array(name.utf8)- return nlistData.withUnsafeBytes { nlist -> UInt64? in- stringData.withUnsafeBytes { strings -> UInt64? in- for index in 0 ..< nlistCount {- let base = index * 16- let stringIndex = Int(- nlist.loadUnaligned(fromByteOffset: base, as: UInt32.self).littleEndian- )- guard Self.matches(wanted, in: strings, at: stringIndex) else { continue }- return nlist- .loadUnaligned(fromByteOffset: base + 8, as: UInt64.self)- .littleEndian- }- return nil+ return file.withUnsafeBytes { bytes -> UInt64? in+ for index in 0 ..< nlistCount {+ let base = nlistBase + index * 16+ let stringIndex = Int(+ bytes.loadUnaligned(fromByteOffset: base, as: UInt32.self).littleEndian+ )+ guard matches(wanted, in: bytes, atStringIndex: stringIndex) else { continue }+ return bytes+ .loadUnaligned(fromByteOffset: base + 8, as: UInt64.self)+ .littleEndian}+ return nil}}@@ -141,9 +164,9 @@ public struct DSCLocalSymbolTable: Sendable {let upperBound = min(entry.nlistStartIndex + entry.nlistCount, nlistCount)guard entry.nlistStartIndex >= 0, entry.nlistStartIndex < upperBound else { return result }for index in entry.nlistStartIndex ..< upperBound {- let base = index * 16- let stringIndex = Int(nlistData.loadLE(UInt32.self, at: base))- let value = nlistData.loadLE(UInt64.self, at: base + 8)+ let base = nlistBase + index * 16+ let stringIndex = Int(file.at(UInt32.self, base))+ let value = file.at(UInt64.self, base + 8)guard value != 0, let name = name(atStringIndex: stringIndex), !name.isEmptyelse { continue }if result[name] == nil { result[name] = value }@@ -156,15 +179,34 @@ public struct DSCLocalSymbolTable: Sendable {entries.first { $0.dylibOffset == dylibOffset }}- private static func matches(+ /// `stringIndex` is relative to the string table; `bytes` covers the file.+ private func matches(_ wanted: [UInt8],- in strings: UnsafeRawBufferPointer,- at stringIndex: Int+ in bytes: UnsafeRawBufferPointer,+ atStringIndex stringIndex: Int) -> Bool {- guard stringIndex >= 0, stringIndex + wanted.count < strings.count else { return false }- for offset in 0 ..< wanted.count where strings[stringIndex + offset] != wanted[offset] {+ guard stringIndex >= 0, stringIndex + wanted.count < stringSize else { return false }+ let base = stringBase + stringIndex+ for offset in 0 ..< wanted.count where bytes[base + offset] != wanted[offset] {return false}- return strings[stringIndex + wanted.count] == 0+ return bytes[base + wanted.count] == 0+ }+}++// MARK: - Reading a mapped file by absolute offset++private extension Data {+ /// Load a little-endian integer at an offset from the START OF THE FILE.+ ///+ /// Deliberately not `loadLE(_:at:)`: that one hands its offset to+ /// `copyBytes(to:from:)`, whose range is in the collection's index space,+ /// so it silently reads the wrong bytes on anything whose `startIndex` is+ /// not zero — which is every slice, and would be every view of a mapping.+ func at<T: FixedWidthInteger>(_: T.Type, _ offset: Int) -> T {+ precondition(offset >= 0 && offset + MemoryLayout<T>.size <= count)+ return withUnsafeBytes {+ T(littleEndian: $0.loadUnaligned(fromByteOffset: offset, as: T.self))+ }}}diff --git a/sources/FirmwarePatcher/DSC/Patchers/DSCHVVMMPatcher.swift b/sources/FirmwarePatcher/DSC/Patchers/DSCHVVMMPatcher.swiftindex 7ee0a24f..cf01c90e 100644--- a/sources/FirmwarePatcher/DSC/Patchers/DSCHVVMMPatcher.swift+++ b/sources/FirmwarePatcher/DSC/Patchers/DSCHVVMMPatcher.swift@@ -566,7 +566,7 @@ public enum DSCHVVMMPatcher {guard FileManager.default.fileExists(atPath: url.path) else {throw PatcherError.fileNotFound(url.path)}- var data = try Data(contentsOf: url)+ var data = try Data(contentsOfFileToRewrite: url)let sites = try findStringSites(inMachO: data)guard !sites.isEmpty else {if isAlreadyMangled(data) {diff --git a/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcherGuestPayload.swift b/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcherGuestPayload.swiftindex 549c520a..5427e0e1 100644--- a/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcherGuestPayload.swift+++ b/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcherGuestPayload.swift@@ -28,10 +28,11 @@ extension CryptexFilesystemPatcher {let signingCertificatePath = cfwInput.appending(path: "cfw_input/signcert.p12")return VPhoneSignOptions(identifier: identifier,- entitlements: try entitlements.map { try Data(contentsOf: $0) },+ entitlements: try entitlements.map { try Data(contentsOf: $0, options: .mappedIfSafe) },mergesExisting: true,identity: try VPhoneSignIdentity(- pkcs12: Data(contentsOf: signingCertificatePath), password: ""+ pkcs12: Data(contentsOf: signingCertificatePath, options: .mappedIfSafe),+ password: ""))}diff --git a/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcherSealing.swift b/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcherSealing.swiftindex 5390adc8..db58f4cc 100644--- a/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcherSealing.swift+++ b/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcherSealing.swift@@ -16,8 +16,11 @@ extension CryptexFilesystemPatcher {// firmware pipeline.private func wrap(_ payload: URL, fourcc: String, description: String, named: String) throws -> URL {let im4pPath = try createTmpDir().appending(path: named)+ // Mapped: the mtree .aar this wraps is the whole system volume's+ // metadata and runs to hundreds of megabytes.let im4p = try IM4P(- fourcc: fourcc, description: description, payload: Data(contentsOf: payload)+ fourcc: fourcc, description: description,+ payload: Data(contentsOf: payload, options: .mappedIfSafe))try im4p.data.write(to: im4pPath)return im4pPathdiff --git a/sources/FirmwarePatcher/Manifest/FirmwareManifest.swift b/sources/FirmwarePatcher/Manifest/FirmwareManifest.swiftindex b32065a0..afa68791 100644--- a/sources/FirmwarePatcher/Manifest/FirmwareManifest.swift+++ b/sources/FirmwarePatcher/Manifest/FirmwareManifest.swift@@ -365,7 +365,7 @@ public enum FirmwareManifest {guard FileManager.default.fileExists(atPath: path) else {throw ManifestError.fileNotFound(path)}- let data = try Data(contentsOf: url)+ let data = try Data(contentsOf: url, options: .mappedIfSafe)guard let dict = try PropertyListSerialization.propertyList(from: data,options: [],diff --git a/sources/FirmwarePatcher/Manifest/ManifestHashPatcher.swift b/sources/FirmwarePatcher/Manifest/ManifestHashPatcher.swiftindex 0c698185..b38d740e 100644--- a/sources/FirmwarePatcher/Manifest/ManifestHashPatcher.swift+++ b/sources/FirmwarePatcher/Manifest/ManifestHashPatcher.swift@@ -103,7 +103,14 @@ public final class ManifestHashPatcher: Patcher {throw FirmwareManifest.ManifestError.missingKey("Path in build identity component info")}- let componentData = try Data(contentsOf: restoreDir.appendingPathComponent(path))+ // Mapped, not read. This loop hashes every component the build+ // identity names, and that list includes `OS` — the filesystem+ // DMG, which is ten gigabytes. Reading it meant holding all of it+ // while SHA384 walked it; mapping means the kernel pages it in+ // ahead of the hash and evicts behind it.+ let componentData = try Data(+ contentsOf: restoreDir.appendingPathComponent(path), options: .mappedIfSafe+ )let finalData = try patchIm4pTypeTag(comp, info["Img4PayloadType"] as? String, componentData)let shaHash = SHA384.hash(data: finalData)dict["Digest"] = Data(shaHash)diff --git a/sources/FirmwarePatcher/Pipeline/FirmwarePipelineDiscovery.swift b/sources/FirmwarePatcher/Pipeline/FirmwarePipelineDiscovery.swiftindex fe8fee31..0e6d229d 100644--- a/sources/FirmwarePatcher/Pipeline/FirmwarePipelineDiscovery.swift+++ b/sources/FirmwarePatcher/Pipeline/FirmwarePipelineDiscovery.swift@@ -36,7 +36,7 @@ extension FirmwarePipeline {/// `ProductVersion` from a manifest in `restoreDir`, or nil if absent/unreadable.static func readProductVersion(_ restoreDir: URL, manifest: String) -> String? {let url = restoreDir.appendingPathComponent(manifest)- guard let data = try? Data(contentsOf: url),+ guard let data = try? Data(contentsOf: url, options: .mappedIfSafe),let plist = try? PropertyListSerialization.propertyList(from: data, format: nil),let dict = plist as? [String: Any],let version = dict["ProductVersion"] as? Stringdiff --git a/sources/VPhoneCore/VPhoneLaunchLayout.swift b/sources/VPhoneCore/VPhoneLaunchLayout.swiftindex 50323224..764f8424 100644--- a/sources/VPhoneCore/VPhoneLaunchLayout.swift+++ b/sources/VPhoneCore/VPhoneLaunchLayout.swift@@ -18,8 +18,8 @@ public struct VPhoneLaunchLayout: Sendable {guard fm.fileExists(atPath: vphoned.path) else { return false }let dst = bundle.url.appendingPathComponent(".vphoned.signed")if fm.fileExists(atPath: dst.path),- let a = try? Data(contentsOf: vphoned),- let b = try? Data(contentsOf: dst), a == b {+ let a = try? Data(contentsOf: vphoned, options: .mappedIfSafe),+ let b = try? Data(contentsOf: dst, options: .mappedIfSafe), a == b {return false}if fm.fileExists(atPath: dst.path) { try fm.removeItem(at: dst) }diff --git a/sources/VPhoneCore/VPhoneRestoreInfo.swift b/sources/VPhoneCore/VPhoneRestoreInfo.swiftindex 8bbf9f1f..4637bd6d 100644--- a/sources/VPhoneCore/VPhoneRestoreInfo.swift+++ b/sources/VPhoneCore/VPhoneRestoreInfo.swift@@ -44,7 +44,7 @@ public struct VPhoneRestoreInfo: Codable, Equatable, Sendable {/// bundle's restore-directory plists — so bundles restored before this file/// existed still report their versions. `nil` when neither is available.public static func load(fromBundle bundle: VPhoneBundle) -> VPhoneRestoreInfo? {- if let data = try? Data(contentsOf: url(forBundle: bundle)),+ if let data = try? Data(contentsOf: url(forBundle: bundle), options: .mappedIfSafe),let info = try? JSONDecoder().decode(VPhoneRestoreInfo.self, from: data) {return info}@@ -106,7 +106,7 @@ public struct VPhoneRestoreInfo: Codable, Equatable, Sendable {}private static func readVersion(_ plist: URL) -> OSVersion? {- guard let data = try? Data(contentsOf: plist),+ guard let data = try? Data(contentsOf: plist, options: .mappedIfSafe),let root = (try? PropertyListSerialization.propertyList(from: data, format: nil)) as? [String: Any],let version = root["ProductVersion"] as? String,let build = root["ProductBuildVersion"] as? Stringdiff --git a/sources/VPhoneCore/VPhoneVirtualMachineManifest.swift b/sources/VPhoneCore/VPhoneVirtualMachineManifest.swiftindex d715d27f..03cc3b21 100644--- a/sources/VPhoneCore/VPhoneVirtualMachineManifest.swift+++ b/sources/VPhoneCore/VPhoneVirtualMachineManifest.swift@@ -203,7 +203,7 @@ public struct VPhoneVirtualMachineManifest: Codable, Sendable {public static func load(from url: URL) throws -> VPhoneVirtualMachineManifest {let data: Datado {- data = try Data(contentsOf: url)+ data = try Data(contentsOf: url, options: .mappedIfSafe)} catch {throw VPhoneManifestError.loadFailed(path: url.path)}diff --git a/sources/VPhoneRestore/VPhoneRestoreBridge.swift b/sources/VPhoneRestore/VPhoneRestoreBridge.swiftindex 7a28ce11..bee1cc0c 100644--- a/sources/VPhoneRestore/VPhoneRestoreBridge.swift+++ b/sources/VPhoneRestore/VPhoneRestoreBridge.swift@@ -73,7 +73,7 @@ public enum VPhoneRestoreBridge {)let written = try locateWrittenSHSH(under: cacheDirectory, requestedECID: ecid)- let onDisk = try Data(contentsOf: written)+ let onDisk = try Data(contentsOf: written, options: .mappedIfSafe)let contents = try VPhoneRestoreTicket.plistData(of: onDisk, at: written)// Python named the file after the ECID the DEVICE reported, not the onediff --git a/sources/VPhoneSign/VPhoneSigner.swift b/sources/VPhoneSign/VPhoneSigner.swiftindex 7d708c3e..053354d1 100644--- a/sources/VPhoneSign/VPhoneSigner.swift+++ b/sources/VPhoneSign/VPhoneSigner.swift@@ -77,7 +77,13 @@ public enum VPhoneSigner {public static func sign(fileAt url: URL, options: VPhoneSignOptions = .init()) throws -> Data {var options = optionsoptions.identifier = options.identifier ?? url.lastPathComponent- let data = try Data(contentsOf: url)+ // Mapping is safe HERE specifically, and only because of how the write+ // below works: a temporary beside the file, then `rename(2)`. Rename+ // does not truncate the original inode, and a mapping keeps that inode+ // alive, so nothing pulls the bytes out from under this buffer. A+ // patcher that wrote back with `Data.write(to:)` could not map — see+ // FirmwarePatcher's InPlaceRewrite.swift.+ let data = try Data(contentsOf: url, options: .mappedIfSafe)let signed = try sign(data, options: options)// The same path ldid takes: a temporary beside the file, its mode// copied over, then rename(2). It has to be rename and not@@ -192,7 +198,7 @@ public enum VPhoneSigner {/// with none contributes nothing, so a file with no entitlements at all/// gives an empty array.public static func entitlements(ofFileAt url: URL) throws -> [Data] {- try entitlements(in: Data(contentsOf: url))+ try entitlements(in: Data(contentsOf: url, options: .mappedIfSafe))}public static func entitlements(in data: Data) throws -> [Data] {diff --git a/sources/VPhoneVMKit/Guest/VPhoneControl.swift b/sources/VPhoneVMKit/Guest/VPhoneControl.swiftindex 53a7e541..7c48ef3c 100644--- a/sources/VPhoneVMKit/Guest/VPhoneControl.swift+++ b/sources/VPhoneVMKit/Guest/VPhoneControl.swift@@ -125,7 +125,7 @@ class VPhoneControl {private func loadGuestBinary() {guard let url = guestBinaryURL,- let data = try? Data(contentsOf: url)+ let data = try? Data(contentsOf: url, options: .mappedIfSafe)else {guestBinaryData = nilguestBinaryHash = nil@@ -451,7 +451,10 @@ class VPhoneControl {private func installIPAWithBuiltInInstaller(localURL: URL) async throws -> String {let data: Datado {- data = try Data(contentsOf: localURL)+ // Mapped: an IPA is whatever the user dropped on the window, and+ // some are gigabytes. The bytes are handed straight to the upload,+ // which chunks them, so nothing needs them resident at once.+ data = try Data(contentsOf: localURL, options: .mappedIfSafe)} catch {throw ControlError.protocolError("failed to read IPA: \(error)")}@@ -479,7 +482,7 @@ class VPhoneControl {]if let signCertURL = Self.signCertURL() {- let signCertData = try Data(contentsOf: signCertURL)+ let signCertData = try Data(contentsOf: signCertURL, options: .mappedIfSafe)let certRemotePath = "\(remoteDir)/\(UUID().uuidString)-signcert.p12"cleanupPaths.append(certRemotePath)try await uploadFile(path: certRemotePath, data: signCertData)diff --git a/sources/VPhoneVMKit/Interface/Browsers/VPhoneFileBrowserModel.swift b/sources/VPhoneVMKit/Interface/Browsers/VPhoneFileBrowserModel.swiftindex 431ac7af..319525ca 100644--- a/sources/VPhoneVMKit/Interface/Browsers/VPhoneFileBrowserModel.swift+++ b/sources/VPhoneVMKit/Interface/Browsers/VPhoneFileBrowserModel.swift@@ -218,7 +218,9 @@ class VPhoneFileBrowserModel {var uploadError: String?for url in urls {let name = url.lastPathComponent- guard let data = try? Data(contentsOf: url) else {+ // Mapped: this is a drag-and-drop target, so the size is the+ // user's choice and the transfer chunks it anyway.+ guard let data = try? Data(contentsOf: url, options: .mappedIfSafe) else {uploadError = "Could not read \"\(name)\" from disk."break}diff --git a/sources/vphone-cli/VPhoneFWAEACLI.swift b/sources/vphone-cli/VPhoneFWAEACLI.swiftindex cddef827..752628d4 100644--- a/sources/vphone-cli/VPhoneFWAEACLI.swift+++ b/sources/vphone-cli/VPhoneFWAEACLI.swift@@ -73,7 +73,8 @@ struct VPhoneFWIM4PCreateCommand: ParsableCommand {func run() throws {let im4p = try IM4P(- fourcc: fourcc, description: version, payload: Data(contentsOf: file)+ fourcc: fourcc, description: version,+ payload: Data(contentsOf: file, options: .mappedIfSafe))try im4p.data.write(to: output)}diff --git a/sources/vphone-cli/VPhoneSignCLI.swift b/sources/vphone-cli/VPhoneSignCLI.swiftindex fa59d078..b49200df 100644--- a/sources/vphone-cli/VPhoneSignCLI.swift+++ b/sources/vphone-cli/VPhoneSignCLI.swift@@ -89,11 +89,15 @@ struct VPhoneSignCommand: ParsableCommand {func run() throws {var options = VPhoneSignOptions()options.identifier = identifier- options.entitlements = try entitlements.map { try Data(contentsOf: $0) }+ options.entitlements = try entitlements.map {+ try Data(contentsOf: $0, options: .mappedIfSafe)+ }options.mergesExisting = mergeoptions.style = appleAdHoc ? .appleAdHoc : .ldidif let pkcs12 {- options.identity = try VPhoneSignIdentity(pkcs12: Data(contentsOf: pkcs12), password: "")+ options.identity = try VPhoneSignIdentity(+ pkcs12: Data(contentsOf: pkcs12, options: .mappedIfSafe), password: ""+ )}try VPhoneSigner.sign(fileAt: file, options: options)}diff --git a/tests/FirmwarePatcherTests/PatchComparisonTests.swift b/tests/FirmwarePatcherTests/PatchComparisonTests.swiftindex 3ab314a5..2dbb206d 100644--- a/tests/FirmwarePatcherTests/PatchComparisonTests.swift+++ b/tests/FirmwarePatcherTests/PatchComparisonTests.swift@@ -2,11 +2,37 @@//// Loads firmware binaries (pre-extracted raw payloads), runs Swift patchers,// and verifies byte-exact match with the pre-generated Python reference JSON files.+//+// THESE ARE GATED ON A FIXTURE THAT CANNOT BE REGENERATED, and that is worth+// being plain about. `ipsws/patch_refactor_input/raw_payloads/` can be rebuilt+// from any IPSW — `vphone-cli fw im4p-extract` does it, and the payloads are+// whatever the firmware holds. `reference_patches/*.json` cannot: it is the+// output of the Python patchers this project replaced, they are deleted, and+// nothing here can produce those records again.+//+// So the suites below run when someone still has the JSON and skip when they+// do not, rather than erroring on a missing file and reading as a regression.+// What still guards the patchers without them is the frozen-digest work+// elsewhere in this directory — `FrozenReference` in DSCHVVMMPatcherTests and+// the `matchesTheFrozenReference*` tests — which carry the reference values in+// the source instead of in a file nobody has.@testable import FirmwarePatcherimport Foundationimport Testing+/// True when the unreproducible Python reference records are present.+private var hasReferencePatches: Bool {+ FileManager.default.fileExists(+ atPath: baseDir.appendingPathComponent("reference_patches").path+ )+}++private let referenceMissing: Comment = """+ipsws/patch_refactor_input/reference_patches/ is absent. It is the output of \+the deleted Python patchers and cannot be regenerated; see this file's header.+"""+// MARK: - Reference patch JSON formatprivate struct ReferencePatch: Decodable {@@ -93,6 +119,7 @@ private func comparePatchRecords(// MARK: - AVPBooter Tests+@Suite(.enabled(if: hasReferencePatches, referenceMissing))struct AVPBooterComparisonTests {@Test func compareAVPBooter() throws {let data = try loadRawPayload("avpbooter.bin")@@ -105,6 +132,7 @@ struct AVPBooterComparisonTests {// MARK: - iBoot Tests+@Suite(.enabled(if: hasReferencePatches, referenceMissing))struct IBSSComparisonTests {@Test func compareIBSS() throws {let data = try loadRawPayload("ibss.bin")@@ -115,6 +143,7 @@ struct IBSSComparisonTests {}}+@Suite(.enabled(if: hasReferencePatches, referenceMissing))struct IBECComparisonTests {@Test func compareIBEC() throws {let data = try loadRawPayload("ibec.bin")@@ -125,6 +154,7 @@ struct IBECComparisonTests {}}+@Suite(.enabled(if: hasReferencePatches, referenceMissing))struct LLBComparisonTests {@Test func compareLLB() throws {let data = try loadRawPayload("llb.bin")@@ -137,6 +167,7 @@ struct LLBComparisonTests {// MARK: - TXM Tests+@Suite(.enabled(if: hasReferencePatches, referenceMissing))struct TXMComparisonTests {@Test func compareTXM() throws {let data = try loadRawPayload("txm.bin")@@ -147,6 +178,7 @@ struct TXMComparisonTests {}}+@Suite(.enabled(if: hasReferencePatches, referenceMissing))struct TXMDevComparisonTests {@Test func compareTXMDev() throws {let url = baseDir.appendingPathComponent("reference_patches/txm_dev.json")@@ -165,6 +197,7 @@ struct TXMDevComparisonTests {// MARK: - Kernel Tests+@Suite(.enabled(if: hasReferencePatches, referenceMissing))struct KernelcacheComparisonTests {@Test func compareKernelcache() throws {let data = try loadRawPayload("kernelcache.bin")@@ -177,6 +210,7 @@ struct KernelcacheComparisonTests {// MARK: - JB Tests+@Suite(.enabled(if: hasReferencePatches, referenceMissing))struct IBSSJBComparisonTests {@Test func compareIBSSJB() throws {let data = try loadRawPayload("ibss.bin")@@ -190,6 +224,7 @@ struct IBSSJBComparisonTests {}}+@Suite(.enabled(if: hasReferencePatches, referenceMissing))struct KernelcacheJBComparisonTests {@Test func compareKernelcacheJB() throws {let data = try loadRawPayload("kernelcache.bin")