Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Skip holes when scanning Disk.img for the root snapshot

Lakr9a1018bdd3bc
diff --git a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneAPFSSnapshot.swift b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneAPFSSnapshot.swift
index ac3e1b9f..9ba8bbaa 100644
--- a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneAPFSSnapshot.swift
+++ b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneAPFSSnapshot.swift
@@ -229,10 +229,26 @@ public enum VPhoneAPFSSnapshot {
// once can fill host RAM, even though the file is sparse. Each window
// starts on a page and APFS block boundary, so scan() still sees each
// complete metadata block and returns the same records.
+ //
+ // Only windows holding data are mapped. A 128 or 256 GB image has
+ // under 10 GB written; scanning its holes faults in the rest as zero
+ // pages for nothing, which is where large disks spent their time.
let windowSize = 64 * 1024 * 1024
var blocks: [(blockOffset: Int, offsetsInBlock: [Int])] = []
var snapshotName: String?
- for offset in stride(from: 0, to: length, by: windowSize) {
+ var offset = 0
+ while offset < length {
+ let data = lseek(fd, off_t(offset), SEEK_DATA)
+ // ENXIO: nothing but hole from here to the end. Any other failure
+ // means the volume cannot report holes (ExFAT, SMB), so the
+ // window is scanned as before.
+ if data < 0, errno == ENXIO {
+ break
+ }
+ if data >= 0, Int(data) >= offset + windowSize {
+ offset = Int(data) / windowSize * windowSize
+ continue
+ }
let count = min(windowSize, length - offset)
guard let base = mmap(nil, count, PROT_READ, MAP_PRIVATE, fd, off_t(offset)),
base != MAP_FAILED
@@ -247,6 +263,7 @@ public enum VPhoneAPFSSnapshot {
(blockOffset: offset + $0.blockOffset, offsetsInBlock: $0.offsetsInBlock)
})
_ = munmap(base, count)
+ offset += windowSize
}
let report = Report(snapshotName: snapshotName, blocks: blocks)
diff --git a/VPhoneKit/VPhoneCoreKitTests/Firmware/APFSSnapshotTests.swift b/VPhoneKit/VPhoneCoreKitTests/Firmware/APFSSnapshotTests.swift
index 7036462d..79934dac 100644
--- a/VPhoneKit/VPhoneCoreKitTests/Firmware/APFSSnapshotTests.swift
+++ b/VPhoneKit/VPhoneCoreKitTests/Firmware/APFSSnapshotTests.swift
@@ -164,6 +164,24 @@ struct APFSSnapshotTests {
#expect(stored == computed)
}
+ @Test
+ func `finds a record past a hole in a 128 GB sparse image`() throws {
+ // Mid-window, so skipping the holes must still land on the window
+ // boundary before it. Scanning every hole would take minutes.
+ let recordOffset = 100 * 1024 * 1024 * 1024 + 3 * 64 * 1024 * 1024 + 8 * VPhoneAPFSSnapshot.blockSize
+ let block = Self.makeValidBlock(payload: Self.snapshotName(hash: Self.validHash), at: 100)
+ let url = try Self.write([])
+ defer { try? FileManager.default.removeItem(at: url) }
+ let handle = try FileHandle(forWritingTo: url)
+ try handle.seek(toOffset: UInt64(recordOffset))
+ try handle.write(contentsOf: Data(block))
+ try handle.truncate(atOffset: 128 * 1_000_000_000)
+ try handle.close()
+
+ let report = try VPhoneAPFSSnapshot.rename(imageAt: url, dryRun: true, log: { _ in })
+ #expect(report.blocks.map(\.blockOffset) == [recordOffset])
+ }
+
@Test
func `renames snapshot records on both sides of a 64 MiB scan window`() throws {
let name = Self.snapshotName(hash: Self.validHash)