Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Fix AMFI escalator on arm64e.x1 hosts (#518)

Ziva7a9b4f7ce110
diff --git a/Research/Host/macos27_m6_amfi.md b/Research/Host/macos27_m6_amfi.md
new file mode 100644
index 00000000..92eaa8b2
--- /dev/null
+++ b/Research/Host/macos27_m6_amfi.md
@@ -0,0 +1,23 @@
+# macOS 27 / Apple M6 AMFI host notes
+
+On Apple M6 with macOS 27.0 (26A428), `amfid` runs as `ARM64E.X1` rather than the plain `arm64e` slice. The AMFI framework's shared-cache addresses differ between those architecture variants.
+
+`VPhoneEscalator` derives the `AMFIRequirementsManager` singleton slot from its own copy of `+[AMFIRequirementsManager sharedManager]`, then reads the same slot in `amfid`. A plain-arm64e escalator therefore derives the wrong shared-cache slot when `amfid` is X1. The failure presents as an invalid remote pointer such as `0x73db0000ffffffff` and `mach_vm_read_overwrite` returns `KERN_INVALID_ADDRESS`.
+
+The supported build fix is to ship `vphone-escalator` as a fat Mach-O containing both `arm64e` and `arm64e.x1`. dyld then selects the architecture variant matching the host, so the slot derivation uses the same shared-cache layout as `amfid`.
+
+The escalator also strips pointer-authentication/tag bits from the remote singleton pointer using `machdep.virtual_address_size`, matching the upstream `Lakr233/amfi-allow` fix merged in PR #1 (`strip-pac-vm-reads`).
+
+## Validation on M6
+
+1. Confirm `/usr/libexec/amfid` is reported as `ARM64E.X1` by `vmmap`/process metadata.
+2. Build `VPhoneEscalator` with `ARCHS="arm64e arm64e.x1"` and `ONLY_ACTIVE_ARCH=NO`.
+3. Confirm `file`/`lipo -info` report both `arm64e` and `arm64e.x1` slices.
+4. Run `vphone-escalator status` as root. The singleton slot must resolve to a mapped `AMFIRequirementsManager`, its isa class bits must match, and both boolean ivars must read as 0 or 1.
+5. Do not attempt the `allow` write path until the read-only status check succeeds.
+
+This fix does not disable SIP, patch executable code in `amfid`, or change `vm.cs_system_enforcement`.
+
+## Bundle staging
+
+`StageBundle.sh` must not pin the Escalator build to `-destination 'platform=macOS,arch=arm64e'`: Xcode treats that destination as an active-architecture constraint and emits a thin `arm64e` executable even when the project `ARCHS` contains both variants. The staging build therefore passes `ARCHS="arm64e arm64e.x1"` and `ONLY_ACTIVE_ARCH=NO` explicitly and does not set an arm64e-only destination.
diff --git a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/VPhoneEscalator.c b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/VPhoneEscalator.c
index ea76e464..58c6ad23 100644
--- a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/VPhoneEscalator.c
+++ b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/VPhoneEscalator.c
@@ -120,6 +120,21 @@
static mach_port_t g_task = MACH_PORT_NULL;
static pid_t g_amfid = -1;
+static void *strip_pac(void *addr) {
+#if defined(__arm64__)
+ static uint32_t bits = 0;
+ static int have_bits = 0;
+ if (!have_bits) {
+ size_t len = sizeof(bits);
+ if (sysctlbyname("machdep.virtual_address_size", &bits, &len, NULL, 0) != 0) bits = -1;
+ have_bits = 1;
+ }
+ return (void *)((uintptr_t)addr & ((1UL << bits) - 1));
+#else
+ return addr;
+#endif
+}
+
// --------------------------------------------------------------------------
// this process
// --------------------------------------------------------------------------
@@ -328,6 +343,8 @@ static mach_vm_address_t amfid_manager(const char *self_path) {
return 0;
}
+ remote = (uintptr_t)strip_pac((void *)remote);
+
// An ObjC object here, not a stale word: the class bits of its isa have to
// match the class bits of ours.
const uintptr_t kISAClassBits = 0x0000000FFFFFFFF8ULL;
diff --git a/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh b/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh
index 2761d62c..08636631 100755
--- a/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh
+++ b/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh
@@ -38,8 +38,8 @@ build_project -project "$root/VPhoneDaemon/VPhoneDaemon.xcodeproj" \
build_project -project "$root/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator.xcodeproj" \
-scheme VPhoneEscalator -configuration "$configuration" \
- -destination 'platform=macOS,arch=arm64e' \
- -derivedDataPath "$root/.build/XcodeAMFIAllow" CODE_SIGNING_ALLOWED=NO build
+ -derivedDataPath "$root/.build/XcodeAMFIAllow" \
+ ARCHS="arm64e arm64e.x1" ONLY_ACTIVE_ARCH=NO CODE_SIGNING_ALLOWED=NO build
/usr/bin/make -C "$root/VPhoneGuestComponents" OUT="$root/.build/guest-components" all