Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Verify first boot with a real vphoned ping

Lakra85bea27781d
diff --git a/sources/VPhoneCore/VPhoneBootPatterns.swift b/sources/VPhoneCore/VPhoneBootPatterns.swift
index b41f2258..e735aa09 100644
--- a/sources/VPhoneCore/VPhoneBootPatterns.swift
+++ b/sources/VPhoneCore/VPhoneBootPatterns.swift
@@ -7,10 +7,6 @@ public enum VPhoneBootPatterns {
/// Kernel panic marker in the guest serial log.
public static let panicRegex = #"(^|[^p])(panic|kernel panic|panic\.apple\.com|stackshot succeeded)"#
- /// A connected guest daemon is the first-boot success marker. The old
- /// iosbinpack shell prompt no longer exists in the JB-only guest.
- public static let panicOrVphonedRegex = "(?i:\(panicRegex))|\\[control\\] connected to vphoned v[0-9]+"
-
/// Accept 1-16 ASCII hex digits, with an optional 0x prefix.
public static func normalizeECID(_ raw: String) -> String? {
var value = raw
diff --git a/sources/vphone-cli/VPhoneCreateOrchestrator.swift b/sources/vphone-cli/VPhoneCreateOrchestrator.swift
index 5dae1f7a..259d5764 100644
--- a/sources/vphone-cli/VPhoneCreateOrchestrator.swift
+++ b/sources/vphone-cli/VPhoneCreateOrchestrator.swift
@@ -423,28 +423,37 @@ public struct VPhoneCreateOrchestrator {
private func runBootAnalysis(bundleURL: URL, verbosity v: VPhoneVerbosity) throws {
let configURL = bundleURL.appendingPathComponent("config.plist")
- let (vmExe, vmArgs) = launcher.plan(["--config", configURL.path, "--headless"])
+ // A newly restored guest may need its first graphical session to
+ // finish setup before vphoned accepts a connection. A headless first
+ // boot timed out on 26.6.2, while the same disk reached vphoned in
+ // GUI mode; later headless boots then connected normally.
+ let (vmExe, vmArgs) = launcher.plan(["--config", configURL.path])
trace("spawn \(vmExe.path) \(vmArgs.joined(separator: " ")) (guest serial: off)", v)
let vm = VPhoneManagedProcess(vmExe, vmArgs, cwd: bundleURL, echo: false)
try vm.start()
defer { vm.terminate() }
- let outcome = vm.waitForOutput(matching: VPhoneBootPatterns.panicOrVphonedRegex, timeout: 300)
- trace("first-boot managed-process outcome: \(outcome)", v)
- switch outcome {
- case .matched:
- if case .matched = vm.waitForOutput(matching: "(?i:\(VPhoneBootPatterns.panicRegex))", timeout: 0) {
+ let socketPath = bundleURL.appendingPathComponent("vphone.sock").path
+ let deadline = Date().addingTimeInterval(300)
+ while Date() < deadline {
+ switch vm.waitForOutput(matching: "(?i:\(VPhoneBootPatterns.panicRegex))", timeout: 0) {
+ case .matched:
print("[-] Boot analysis: panic detected, stopping VM.")
throw VPhoneCreateError.bootAnalysisPanic
+ case let .exited(code):
+ print("[-] Boot analysis: VM process exited before success marker.")
+ throw VPhoneCreateError.bootAnalysisExited(code)
+ case .timedOut:
+ break
}
- print("[+] First boot: vphoned connected.")
- case let .exited(code):
- print("[-] Boot analysis: VM process exited before success marker.")
- throw VPhoneCreateError.bootAnalysisExited(code)
- case .timedOut:
- print("[-] Boot analysis timeout (300s); stopping VM.")
- throw VPhoneCreateError.bootAnalysisTimeout
+ if VPhoneHostControlProbe.ping(socketPath: socketPath) {
+ print("[+] First boot: vphoned ping succeeded.")
+ return
+ }
+ Thread.sleep(forTimeInterval: 1)
}
+ print("[-] Boot analysis timeout (300s); stopping VM.")
+ throw VPhoneCreateError.bootAnalysisTimeout
}
}
diff --git a/sources/vphone-cli/VPhoneHostControlProbe.swift b/sources/vphone-cli/VPhoneHostControlProbe.swift
new file mode 100644
index 00000000..9c3b91b2
--- /dev/null
+++ b/sources/vphone-cli/VPhoneHostControlProbe.swift
@@ -0,0 +1,58 @@
+import Darwin
+import Foundation
+
+/// Ask the GUI VM's local control socket to perform a real vphoned ping.
+/// `vm create` uses this instead of parsing the VM process's buffered stdout.
+enum VPhoneHostControlProbe {
+ static func ping(socketPath: String) -> Bool {
+ let path = socketPath.utf8CString
+ var address = sockaddr_un()
+ guard path.count <= MemoryLayout.size(ofValue: address.sun_path) else { return false }
+ address.sun_family = sa_family_t(AF_UNIX)
+ withUnsafeMutablePointer(to: &address.sun_path) { pointer in
+ pointer.withMemoryRebound(to: CChar.self, capacity: path.count) { destination in
+ for (index, byte) in path.enumerated() { destination[index] = byte }
+ }
+ }
+
+ let fd = socket(AF_UNIX, SOCK_STREAM, 0)
+ guard fd >= 0 else { return false }
+ defer { close(fd) }
+
+ var timeout = timeval(tv_sec: 2, tv_usec: 0)
+ _ = withUnsafePointer(to: &timeout) {
+ setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, $0, socklen_t(MemoryLayout<timeval>.size))
+ }
+ var noSigPipe: Int32 = 1
+ _ = withUnsafePointer(to: &noSigPipe) {
+ setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, $0, socklen_t(MemoryLayout<Int32>.size))
+ }
+ let connected = withUnsafePointer(to: &address) { pointer in
+ pointer.withMemoryRebound(to: sockaddr.self, capacity: 1) {
+ connect(fd, $0, socklen_t(MemoryLayout<sockaddr_un>.size))
+ }
+ }
+ guard connected == 0 else { return false }
+
+ let request = Data("{\"t\":\"ping\",\"screen\":false}\n".utf8)
+ let written = request.withUnsafeBytes { bytes in
+ Darwin.write(fd, bytes.baseAddress, bytes.count)
+ }
+ guard written == request.count else { return false }
+
+ var reply = Data()
+ var buffer = [UInt8](repeating: 0, count: 512)
+ while reply.count < 4096 {
+ let count = buffer.withUnsafeMutableBytes { bytes in
+ Darwin.read(fd, bytes.baseAddress, bytes.count)
+ }
+ guard count > 0 else { return false }
+ reply.append(contentsOf: buffer.prefix(count))
+ if let newline = reply.firstIndex(of: 0x0A),
+ let json = try? JSONSerialization.jsonObject(with: Data(reply[..<newline])) as? [String: Any] {
+ return json["ok"] as? Bool == true
+ }
+ }
+ return false
+ }
+}
diff --git a/tests/VPhoneCoreTests/CreateOrchestratorTests.swift b/tests/VPhoneCoreTests/CreateOrchestratorTests.swift
index c72d6ecf..b8088e38 100644
--- a/tests/VPhoneCoreTests/CreateOrchestratorTests.swift
+++ b/tests/VPhoneCoreTests/CreateOrchestratorTests.swift
@@ -4,10 +4,9 @@ import Testing
// Tests for `VPhoneBootPatterns` — the pure, device-independent pieces of the
// native `vm create` pipeline (`VPhoneCreateOrchestrator`, executable target).
-// The orchestrator's live stages (DFU/restore/CFW/boot check)
-// need a real device and are NOT exercised here; see the Task 2 report.
+// The orchestrator's live stages (DFU/restore/CFW/boot check) require a VM.
struct CreateOrchestratorTests {
- // MARK: - first-boot log markers
+ // MARK: - first-boot panic markers
private func matches(_ pattern: String, _ line: String) throws -> Bool {
let re = try NSRegularExpression(pattern: pattern)
@@ -26,16 +25,6 @@ struct CreateOrchestratorTests {
#expect(try !matches(VPhoneBootPatterns.panicRegex, "vphoned: connected, awaiting handshake"))
}
- @Test func bootCheckMatchesPanicCaseInsensitively() throws {
- #expect(try matches(VPhoneBootPatterns.panicOrVphonedRegex, "PANIC(cpu 0 caller 0x0): uppercase panic"))
- }
-
- @Test func bootCheckRequiresVphonedConnectionInsteadOfShellPrompt() throws {
- #expect(try matches(VPhoneBootPatterns.panicOrVphonedRegex,
- "[control] connected to vphoned v1 (192.168.64.2) iOS 26.6.2"))
- #expect(try !matches(VPhoneBootPatterns.panicOrVphonedRegex, "bash-3.2#"))
- }
-
// MARK: - normalizeECID (port of setup_machine.sh's normalize_ecid)
@Test func normalizeECIDStripsPrefixAndPads() {