Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Refuse a dyld shared cache that overruns the guest kernel's shared region

Jacky31b138545da0
diff --git a/Documents/Guides/compatibility.md b/Documents/Guides/compatibility.md
index 2c67eb32..d6ea007b 100644
--- a/Documents/Guides/compatibility.md
+++ b/Documents/Guides/compatibility.md
@@ -37,8 +37,8 @@ selector-24 pre-check rejects every binary at init and the boot panics with
`unexpected SIGKILL of init`. The `txm-boot-precheck_admission` patch
(`Research/Firmware/txm_selector24_cms_gate.md`) fixes it; verified the same
day: `iPhone17,3 27.0.1 + 26.4-23E5207q` with the patch boots to the guest
-and answers a vphoned `device.info`. Until a bundle release carrying the
-patch ships, treat 27.0.x past 24A435 as needing a current local build.
+and answers a vphoned `device.info`. Bundle 2.6.0 is the first release that
+carries it.
## Other iPhone models
@@ -47,6 +47,18 @@ See [Other iPhone models](iphone-models.md).
| Host | iPhone restore IPSW | PCC/cloudOS IPSW | Observed result |
| --- | --- | --- | --- |
| Mac17,9 (M5 Pro) 27.0.1 | `18,1_26.6.2_23G90` (iPhone 17 Pro) | `26.4-23E5207q` | Launchpad `vm create` (2.5.1 local bundle): prepare, patch, restore, CFW, boot to the home screen at 402x874 pt @3x, `model` iPhone18,1, vphoned ping |
+| Mac17,9 (M5 Pro) 27.0.1 | `18,2_27.0.1_24A446` (iPhone 17 Pro Max) | `26.4-23E5207q` | does not boot: `cfw install` refuses it, see below |
+
+**iPhone18,1 and iPhone18,2 on iOS 27 do not boot**
+([issue #596](https://github.com/Lakr233/vphone-cli/issues/596), 27.0 and
+27.0.1). Their dyld shared cache records a `sharedRegionSize` of
+`0x185804000` on 27.0.1, larger than the `0x180000000` shared region of the
+cloudOS 26.4 guest kernel, so it cannot map even with `dyld-boot-maxslide`
+taking its slide away: launchd panics on first boot with `Library not loaded:
+/usr/lib/libSystem.B.dylib`. iPhone17,3's 27.0.1 cache (`0x17D508000`) still
+fits. Since this check, `cfw install` refuses such a cache with that
+explanation instead of leaving a guest that panics. Use iPhone17,3 for iOS 27,
+or iOS 26 on these devices.
## iPadOS
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DyldSharedCache/Patchers/DyldSharedCacheMaxSlidePatcher.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DyldSharedCache/Patchers/DyldSharedCacheMaxSlidePatcher.swift
index c359b4b0..a5c34d19 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DyldSharedCache/Patchers/DyldSharedCacheMaxSlidePatcher.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DyldSharedCache/Patchers/DyldSharedCacheMaxSlidePatcher.swift
@@ -171,6 +171,52 @@ public enum DyldSharedCacheMaxSlidePatcher {
}
}
+ // MARK: - Region Check
+
+ /// A cache that does not fit the kernel's shared region even at slide 0.
+ ///
+ /// Zeroing `maxSlide` only gives the slide back. When `sharedRegionSize`
+ /// alone is larger than the region, `_shared_region_map_and_slide` still
+ /// returns ENOMEM and launchd panics on first boot with "Library not
+ /// loaded: /usr/lib/libSystem.B.dylib". The iOS 27 caches for iPhone18,1
+ /// and iPhone18,2 are such caches: 0x185804000 on 27.0.1 (24A446), where
+ /// iPhone17,3's 0x17D508000 still fits.
+ public struct RegionOverflow: Error, CustomStringConvertible, Sendable, Equatable {
+ /// `dyld_cache_header.sharedRegionSize`, as found.
+ public let sharedRegionSize: UInt64
+ /// The guest kernel's shared region size it was checked against.
+ public let region: UInt64
+
+ public var description: String {
+ "The dyld shared cache spans \(DyldSharedCacheMaxSlidePatcher.hex(sharedRegionSize)), "
+ + "more than the guest kernel's \(DyldSharedCacheMaxSlidePatcher.hex(region)) shared region "
+ + "even with no slide, so this guest would panic on first boot (launchd cannot load "
+ + "libSystem). Choose a device and iOS version whose cache fits, such as iPhone17,3, "
+ + "or iOS 26 for this device."
+ }
+ }
+
+ /// Throws ``RegionOverflow`` when the cache under `chunksDirectory` cannot
+ /// map at any slide, and returns its `sharedRegionSize` otherwise. Reads
+ /// only; nothing is written.
+ @discardableResult
+ public static func requireFitsAtSlideZero(
+ chunksDirectory: URL,
+ architecture: String = "arm64e",
+ kernelRegionSize: UInt64 = kernelSharedRegionSize,
+ ) throws -> UInt64 {
+ let chunks = try DyldSharedCacheChunkSet(directory: chunksDirectory, architecture: architecture)
+ let header = try readHeader(
+ from: chunks,
+ at: headerVMA(of: chunks),
+ chunkName: "dyld_shared_cache_\(architecture)",
+ )
+ guard header.sharedRegionSize <= kernelRegionSize else {
+ throw RegionOverflow(sharedRegionSize: header.sharedRegionSize, region: kernelRegionSize)
+ }
+ return header.sharedRegionSize
+ }
+
// MARK: - Patching
/// Clamp `maxSlide` in the cache under `chunksDirectory`.
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/DyldSharedCache/DyldSharedCacheMaxSlidePatcherTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/DyldSharedCache/DyldSharedCacheMaxSlidePatcherTests.swift
index 7326ddff..4a296528 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/DyldSharedCache/DyldSharedCacheMaxSlidePatcherTests.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/DyldSharedCache/DyldSharedCacheMaxSlidePatcherTests.swift
@@ -859,3 +859,68 @@ struct DyldSharedCacheMaxSlideGateTests {
#expect(untouchedSlide == 0x2000_0000, "the refusal still wrote")
}
}
+
+// MARK: - The region check, on synthetic caches
+
+/// `requireFitsAtSlideZero`, which `cfw install` runs before any dyld patch.
+/// Synthetic caches only, so this runs without the real-cache fixture: the
+/// sizes are the iOS 27.0.1 (24A446) headers from issue #596.
+@Suite
+struct DyldSharedCacheRegionCheckTests {
+ private func withCache(
+ regionSize: UInt64,
+ maxSlide: UInt64 = 0x2000_0000,
+ _ body: (URL) throws -> Void,
+ ) throws {
+ let directory = FileManager.default.temporaryDirectory
+ .appendingPathComponent("vphone-dsc-region-\(UUID().uuidString)")
+ defer { try? FileManager.default.removeItem(at: directory) }
+ try DyldSharedCacheMaxSlideGateTests.writeCache(
+ into: directory, regionSize: regionSize, maxSlide: maxSlide,
+ )
+ try body(directory)
+ }
+
+ @Test
+ func `An iPhone18,2 27.0.1 cache overruns the region even at slide 0`() throws {
+ try withCache(regionSize: 0x1_8580_4000) { directory in
+ let main = directory.appendingPathComponent(MaxSlideFixture.mainChunkName)
+ let before = try Data(contentsOf: main)
+ #expect(throws: DyldSharedCacheMaxSlidePatcher.RegionOverflow(
+ sharedRegionSize: 0x1_8580_4000,
+ region: DyldSharedCacheMaxSlidePatcher.kernelSharedRegionSize,
+ )) {
+ try DyldSharedCacheMaxSlidePatcher.requireFitsAtSlideZero(chunksDirectory: directory)
+ }
+ #expect(try Data(contentsOf: main) == before, "the check wrote")
+ }
+ }
+
+ @Test
+ func `An iPhone17,3 27.0.1 cache fits once its slide is dropped`() throws {
+ try withCache(regionSize: 0x1_7D50_8000) { directory in
+ let size = try DyldSharedCacheMaxSlidePatcher.requireFitsAtSlideZero(chunksDirectory: directory)
+ #expect(size == 0x1_7D50_8000)
+ }
+ }
+
+ @Test
+ func `A cache exactly the size of the region fits`() throws {
+ let region = DyldSharedCacheMaxSlidePatcher.kernelSharedRegionSize
+ try withCache(regionSize: region, maxSlide: 0) { directory in
+ let size = try DyldSharedCacheMaxSlidePatcher.requireFitsAtSlideZero(chunksDirectory: directory)
+ #expect(size == region)
+ }
+ }
+
+ @Test
+ func `The refusal names both sizes and a way out`() {
+ let message = DyldSharedCacheMaxSlidePatcher.RegionOverflow(
+ sharedRegionSize: 0x1_8580_4000,
+ region: 0x1_8000_0000,
+ ).description
+ #expect(message.contains("0x185804000"))
+ #expect(message.contains("0x180000000"))
+ #expect(message.contains("iPhone17,3"))
+ }
+}
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift
index ac46545a..265c2a81 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift
@@ -1059,6 +1059,19 @@ struct VPhoneCustomFirmwareInstaller {
}
let version = try productVersion(system: system)
+ // 0. A cache whose span alone overruns the guest kernel's shared region
+ // maps at no slide, and the guest panics on first boot. Refuse before
+ // the redeploy and the cache patches. A header this check cannot read
+ // is left to the dyld patches below, as it was before the check.
+ let dsc = try verifiedDyldCacheDirectory(system: system)
+ do {
+ try DyldSharedCacheMaxSlidePatcher.requireFitsAtSlideZero(chunksDirectory: URL(fileURLWithPath: dsc))
+ } catch let overflow as DyldSharedCacheMaxSlidePatcher.RegionOverflow {
+ throw overflow
+ } catch {
+ print(" [!] shared region check skipped: \(error)")
+ }
+
// 1. The redeploy first, so no later patch failure can block it. vphoned
// and the environment are boot-essential (always on). The environment
// keeps its old narrow rule under `environmentOnly`.
@@ -1078,7 +1091,6 @@ struct VPhoneCustomFirmwareInstaller {
// 2. The dyld shared cache. The version branches and the declarations'
// applicability say the same thing; this is what a VM with no plan
// still follows.
- let dsc = try verifiedDyldCacheDirectory(system: system)
var dyld: [(id: String, verb: String, args: [String])] = []
if version.hasPrefix("27.") {
dyld += [