Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

test: cover iOS 16 transport layouts and ephemeral lab guards

黑曜猫3e131cb3f371
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/ARM64EncoderTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/ARM64EncoderTests.swift
index 5f66962f..d271860d 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/ARM64EncoderTests.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Core/ARM64EncoderTests.swift
@@ -30,6 +30,26 @@ import Foundation
import Testing
import VPhonePatchKit
+// LDR literal and BR cases below were checked using the same native Keystone
+// 0.9.2 C driver, outside the repository, for the iOS 16 IOSurface transport stub.
+
+@Suite("Literal transport stub encoders")
+struct ARM64LiteralStubTests {
+ @Test func `Keystone checked literal load and branch operands`() {
+ #expect(ARM64Encoder.encodeLdrXLiteral(rt: 16, from: 0, to: 8) == ARM64.encodeU32(0x5800_0050))
+ #expect(ARM64Encoder.encodeLdrXLiteral(rt: 0, from: 0, to: -1048576) == ARM64.encodeU32(0x5880_0000))
+ #expect(ARM64Encoder.encodeLdrXLiteral(rt: 30, from: 0, to: 1048572) == ARM64.encodeU32(0x587F_FFFE))
+ #expect(ARM64Encoder.encodeBR(rn: 16) == ARM64.encodeU32(0xD61F_0200))
+ #expect(ARM64Encoder.encodeBR(rn: 0) == ARM64.encodeU32(0xD61F_0000))
+ #expect(ARM64Encoder.encodeBR(rn: 30) == ARM64.encodeU32(0xD61F_03C0))
+ #expect(ARM64Encoder.encodeBR(rn: 31) == nil)
+ #expect(ARM64Encoder.encodeLdrXLiteral(rt: 32, from: 0, to: 8) == nil)
+ #expect(ARM64Encoder.encodeLdrXLiteral(rt: 16, from: 0, to: 7) == nil)
+ #expect(ARM64Encoder.encodeLdrXLiteral(rt: 16, from: 0, to: 1048576) == nil)
+ #expect(ARM64Encoder.encodeLdrXLiteral(rt: 16, from: 0, to: -1048580) == nil)
+ }
+}
+
// MARK: - Case Model
/// One encoder output paired with the keystone encoding of the same instruction.
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmwareIOS16EphemeralStorageTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmwareIOS16EphemeralStorageTests.swift
new file mode 100644
index 00000000..9a1a3d14
--- /dev/null
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/CustomFirmwareIOS16EphemeralStorageTests.swift
@@ -0,0 +1,45 @@
+import FirmwarePatcher
+import Foundation
+import Testing
+
+@Suite("iOS 16 lab storage declaration")
+struct CustomFirmwareIOS16EphemeralStorageTests {
+ private func word(_ value: UInt32) -> Data { withUnsafeBytes(of: value.littleEndian) { Data($0) } }
+ private func node(_ properties: [(String, Data)], children: [Data] = []) -> Data {
+ var data = word(UInt32(properties.count)) + word(UInt32(children.count))
+ for (name, value) in properties {
+ var key = Data(name.utf8); key.append(Data(repeating: 0, count: 32 - key.count))
+ data.append(key); data.append(word(UInt32(value.count))); data.append(value)
+ data.append(Data(repeating: 0, count: (4 - value.count % 4) % 4))
+ }
+ for child in children { data.append(child) }
+ return data
+ }
+ private func fixture(chosen: UInt32 = 0, product: UInt32 = 0, productWidth: Int = 8) -> Data {
+ node([("name", Data("device-tree\0".utf8)), ("unrelated", Data([9, 8, 7, 6]))], children: [
+ node([("name", Data("chosen\0".utf8)), ("ephemeral-storage", word(chosen))]),
+ node([("name", Data("product\0".utf8)), ("ephemeral-data-mode", word(product) + Data(repeating: 0, count: productWidth - 4))]),
+ ])
+ }
+
+ @Test func `Only the two boolean bytes change and a second pass is identical`() throws {
+ for width in [4, 8] {
+ let original = fixture(productWidth: width)
+ let (patched, changes) = try CustomFirmwarePostRestoreDeviceTree.withIOS16EphemeralStorage(original)
+ #expect(patched.count == original.count)
+ #expect(zip(original, patched).filter { $0.0 != $0.1 }.count == 2)
+ #expect(Set(changes.map(\.property)) == ["chosen/ephemeral-storage", "product/ephemeral-data-mode"])
+ let (again, repeated) = try CustomFirmwarePostRestoreDeviceTree.withIOS16EphemeralStorage(patched)
+ #expect(again == patched && repeated.isEmpty)
+ }
+ }
+
+ @Test func `Unknown boolean values are refused before writing`() {
+ #expect(throws: (any Error).self) {
+ try CustomFirmwarePostRestoreDeviceTree.withIOS16EphemeralStorage(fixture(chosen: 2))
+ }
+ #expect(throws: (any Error).self) {
+ try CustomFirmwarePostRestoreDeviceTree.withIOS16EphemeralStorage(fixture(product: 256))
+ }
+ }
+}
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/PatchSets/FirmwarePatchSetCatalogTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/PatchSets/FirmwarePatchSetCatalogTests.swift
index 086f1768..808ed4ff 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/PatchSets/FirmwarePatchSetCatalogTests.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/PatchSets/FirmwarePatchSetCatalogTests.swift
@@ -18,7 +18,7 @@ import VPhonePatchKit
@Suite("Bundled patch set catalogue")
struct FirmwarePatchSetCatalogTests {
@Test func `Legacy startup compatibility is opt in and scoped to iOS 16`() throws {
- for id in [FirmwareGuestSystemPatchSet.ios16MachPorts, FirmwareGuestSystemPatchSet.ios16PortsLookup, FirmwareGuestSystemPatchSet.ios16Desktop, FirmwareGuestSystemPatchSet.ios16Keybag, FirmwareGuestSystemPatchSet.ios16Migration] {
+ for id in [FirmwareGuestSystemPatchSet.ios16KeybaglessUI, FirmwareGuestSystemPatchSet.ios16Touchscreen, FirmwareGuestSystemPatchSet.ios16EphemeralStorage, FirmwareGuestSystemPatchSet.ios16Framebuffer, FirmwareGuestSystemPatchSet.ios16Surface, FirmwareGuestSystemPatchSet.ios16MachPorts, FirmwareGuestSystemPatchSet.ios16PortsLookup, FirmwareGuestSystemPatchSet.ios16Desktop, FirmwareGuestSystemPatchSet.ios16Keybag, FirmwareGuestSystemPatchSet.ios16Migration] {
for version in ["16.6.1", "17.7", "18.6.2", "26.1", "27.1"] {
for selected in [false, true] {
var preset = FirmwarePatchSetCatalog.standardPreset
diff --git a/VPhoneGuestComponents/Tests/IOS16LabPolicyTest.c b/VPhoneGuestComponents/Tests/IOS16LabPolicyTest.c
new file mode 100644
index 00000000..40ba4ad7
--- /dev/null
+++ b/VPhoneGuestComponents/Tests/IOS16LabPolicyTest.c
@@ -0,0 +1,28 @@
+#include "VPhoneIOS16LabPolicy.h"
+#include <assert.h>
+#include <stdio.h>
+int main(void) {
+ struct stat valid = {0};
+ valid.st_mode = S_IFREG | 0644;
+ valid.st_uid = 0;
+ valid.st_nlink = 1;
+ valid.st_size = sizeof(VP_IOS16_KEYBAGLESS_MAGIC) - 1;
+ valid.st_mtime = 100;
+ assert(vp_ios16_lab_lease_valid(&valid, 100));
+ assert(vp_ios16_lab_lease_valid(&valid, 103));
+ assert(!vp_ios16_lab_lease_valid(&valid, 104));
+ assert(!vp_ios16_lab_lease_valid(&valid, 99));
+ struct stat bad = valid; bad.st_uid = 501;
+ assert(!vp_ios16_lab_lease_valid(&bad, 101));
+ bad = valid; bad.st_nlink = 2;
+ assert(!vp_ios16_lab_lease_valid(&bad, 101));
+ bad = valid; bad.st_mode = S_IFLNK | 0644;
+ assert(!vp_ios16_lab_lease_valid(&bad, 101));
+ bad = valid; bad.st_mode |= 0020;
+ assert(!vp_ios16_lab_lease_valid(&bad, 101));
+ bad = valid; bad.st_mode |= 0002;
+ assert(!vp_ios16_lab_lease_valid(&bad, 101));
+ bad = valid; bad.st_size--;
+ assert(!vp_ios16_lab_lease_valid(&bad, 101));
+ puts("iOS 16 lab lease policy: passed");
+}
diff --git a/VPhoneGuestComponents/Tests/IOS16SurfaceReplyTest.c b/VPhoneGuestComponents/Tests/IOS16SurfaceReplyTest.c
new file mode 100644
index 00000000..11b04383
--- /dev/null
+++ b/VPhoneGuestComponents/Tests/IOS16SurfaceReplyTest.c
@@ -0,0 +1,61 @@
+#include "VPhoneIOS16SurfaceReply.h"
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+static void put64(uint8_t *buffer, size_t offset, uint64_t value) { memcpy(buffer + offset, &value, 8); }
+static uint32_t get32(const uint8_t *buffer, size_t offset) { uint32_t value; memcpy(&value, buffer + offset, 4); return value; }
+
+int main(void) {
+ uint8_t modern[VP_MODERN_SURFACE_REPLY] = {0}, old[VP_IOS16_SURFACE_REPLY];
+ put64(modern, 0, 0x12345678000); // mapped address must stay 64-bit
+ put64(modern, 0x20, 12105216); put64(modern, 0x28, 1179);
+ put64(modern, 0x30, 2556); put64(modern, 0x38, 4736);
+ put64(modern, 0x88, 1179); put64(modern, 0x90, 2556);
+ memset(modern + 0x88 + 0x98, 0xFF, sizeof(modern) - 0x88 - 0x98);
+ assert(vpIOS16SurfaceReply(old, modern)); // unused planes must not overflow
+ uint64_t address; memcpy(&address, old, 8); assert(address == 0x12345678000);
+ assert(get32(old, 0x1C) == 12105216 && get32(old, 0x20) == 1179);
+ assert(get32(old, 0x24) == 2556 && get32(old, 0x28) == 4736);
+ assert(get32(old, 0x68) == 1179 && get32(old, 0x6C) == 2556);
+
+ memset(modern, 0, sizeof(modern));
+ uint32_t planes = 20; memcpy(modern + 0x58, &planes, 4);
+ for (unsigned index = 0; index < planes; index++) {
+ put64(modern, 0x88 + index * 0x98, 640 >> (index % 3));
+ modern[0x88 + index * 0x98 + 0x97] = (uint8_t)(index + 1);
+ }
+ assert(vpIOS16SurfaceReply(old, modern));
+ for (unsigned index = 0; index < planes; index++) {
+ assert(get32(old, 0x68 + index * 0x80) == (640U >> (index % 3)));
+ assert(old[0x68 + index * 0x80 + 0x7F] == index + 1);
+ }
+ put64(modern, 0x88 + 19 * 0x98 + 5 * 8, UINT64_C(1) << 32);
+ assert(!vpIOS16SurfaceReply(old, modern));
+ planes = 21; memcpy(modern + 0x58, &planes, 4); assert(!vpIOS16SurfaceReply(old, modern));
+ memset(modern, 0, sizeof(modern)); put64(modern, 0x20, UINT64_C(1) << 32);
+ assert(!vpIOS16SurfaceReply(old, modern));
+
+ uint8_t info[0x28] = {0}, infoOld[0x18];
+ put64(info, 16, 0x4000); put64(info, 24, 0x4000); info[32] = 1; info[34] = 1;
+ assert(vpIOS16SurfaceInfo(infoOld, info));
+ assert(get32(infoOld, 12) == 0x4000 && get32(infoOld, 16) == 0x4000);
+ assert(infoOld[20] == 1 && infoOld[22] == 1);
+ put64(info, 24, UINT64_C(1) << 32); assert(!vpIOS16SurfaceInfo(infoOld, info));
+ uint8_t swapOld[0x46C] = {0}, swapNew[0x560];
+ put64(swapOld, 0x38, 0x123456789); // unshifted parameter pointer
+ uint32_t token = 3469, surface = 10, width = 1179, height = 2556;
+ memcpy(swapOld + 0x50, &token, 4); memcpy(swapOld + 0x54, &surface, 4);
+ memcpy(swapOld + 0x6C, &width, 4); memcpy(swapOld + 0x70, &height, 4);
+ swapOld[0x2CB] = 1; swapOld[0x2E6] = 1; swapOld[0x30C] = 1; swapOld[0x467] = 1;
+ vpIOS16FramebufferSwap(swapNew, swapOld);
+ memcpy(&address, swapNew + 0x38, 8); assert(address == 0x123456789);
+ assert(get32(swapNew, 0x98) == token && get32(swapNew, 0x9C) == surface);
+ assert(get32(swapNew, 0xB4) == width && get32(swapNew, 0xB8) == height);
+ assert(swapNew[0x337] == 1 && swapNew[0x353] == 1 && swapNew[0x384] == 1 && swapNew[0x503] == 1);
+ for (unsigned index = 0x50; index < 0x98; index++) assert(swapNew[index] == 0);
+ for (unsigned index = 0x2A8; index < 0x2CC; index++) assert(swapNew[index] == 0);
+ for (unsigned index = 0x508; index < 0x560; index++) assert(swapNew[index] == 0);
+ puts("IOSurface reply conversion: packed plane, 20 planes, metadata, overflow, capabilities passed");
+ return 0;
+}