Run a virtual iPhone on an Apple Silicon Mac.
Swift
Fix the tunnel mode build and stop SIGPIPE from killing the VM
Lakr17503c898c23
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/main.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/main.swiftindex 60f76346..fd6e0993 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/main.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/main.swift@@ -15,6 +15,10 @@ import ArgumentParserimport Foundationimport VPhoneCoreKit+// Restore, guest RPC and child-process plumbing all write to descriptors whose+// far end may already be gone. Make that an EPIPE error, not a silent exit.+signal(SIGPIPE, SIG_IGN)+do {let command = try VPhoneCommand.parseAsRoot()diff --git a/VPhoneExecutable/VPhoneVirtualization/VirtualMachineProcess/main.swift b/VPhoneExecutable/VPhoneVirtualization/VirtualMachineProcess/main.swiftindex 5706d625..2dab8d80 100644--- a/VPhoneExecutable/VPhoneVirtualization/VirtualMachineProcess/main.swift+++ b/VPhoneExecutable/VPhoneVirtualization/VirtualMachineProcess/main.swift@@ -10,7 +10,15 @@// binary has exactly one job, so there is nothing to select between.import ArgumentParser+import Foundationimport VPhoneCoreKitimport VPhoneVirtualMachineKit+// This process writes to sockets and pipes whose far end can vanish at any time:+// the guest network's host connections, vphone.sock clients, the camera and+// control channels. A write to a closed one raises SIGPIPE, whose default action+// kills the process and the guest with it. Ignore it once for the whole process+// so every such write fails with EPIPE, which each caller already handles.+signal(SIGPIPE, SIG_IGN)+VPhoneGuestApp.run(VPhoneBootCommand.parseOrExit())diff --git a/VPhoneKit/VPhoneCoreKit/Network/VPhoneTCPForwarder.swift b/VPhoneKit/VPhoneCoreKit/Network/VPhoneTCPForwarder.swiftindex eb1ef3db..aabfc929 100644--- a/VPhoneKit/VPhoneCoreKit/Network/VPhoneTCPForwarder.swift+++ b/VPhoneKit/VPhoneCoreKit/Network/VPhoneTCPForwarder.swift@@ -343,6 +343,9 @@ final class VPhoneTCPForwarder: @unchecked Sendable {address.sin_addr = in_addr(s_addr: hostOrder.bigEndian)_ = fcntl(descriptor, F_SETFL, fcntl(descriptor, F_GETFL, 0) | O_NONBLOCK)+ // A guest writing to a connection the server already reset makes our+ // `send` raise SIGPIPE, which would take the whole VM process down.+ _ = fcntl(descriptor, F_SETNOSIGPIPE, 1)var one: Int32 = 1_ = setsockopt(descriptor, IPPROTO_TCP, TCP_NODELAY, &one, socklen_t(MemoryLayout<Int32>.size))diff --git a/VPhoneKit/VPhoneCoreKit/Network/VPhoneUDPForwarder.swift b/VPhoneKit/VPhoneCoreKit/Network/VPhoneUDPForwarder.swiftindex 875b7e75..f94571b1 100644--- a/VPhoneKit/VPhoneCoreKit/Network/VPhoneUDPForwarder.swift+++ b/VPhoneKit/VPhoneCoreKit/Network/VPhoneUDPForwarder.swift@@ -177,6 +177,7 @@ final class VPhoneUDPForwarder: @unchecked Sendable {let destination = resolveDestination(for: flow)let descriptor = socket(AF_INET, SOCK_DGRAM, 0)guard descriptor >= 0 else { return nil }+ _ = fcntl(descriptor, F_SETNOSIGPIPE, 1)var address = sockaddr_in()address.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)diff --git a/VPhoneKit/VPhoneCoreKit/Network/VPhoneUserspaceNetwork.swift b/VPhoneKit/VPhoneCoreKit/Network/VPhoneUserspaceNetwork.swiftindex d2f2b529..579b5427 100644--- a/VPhoneKit/VPhoneCoreKit/Network/VPhoneUserspaceNetwork.swift+++ b/VPhoneKit/VPhoneCoreKit/Network/VPhoneUserspaceNetwork.swift@@ -77,7 +77,7 @@ public final class VPhoneUserspaceNetwork: @unchecked Sendable {/// jumbo-sized IP packet; the guest is expected to stay within `mtu`.private static let frameCapacity = 9216- public init(configuration: VPhoneUserspaceNetworkConfiguration = .default) throws {+ public convenience init(configuration: VPhoneUserspaceNetworkConfiguration = .default) throws {var descriptors: [Int32] = [-1, -1]guard socketpair(AF_UNIX, SOCK_DGRAM, 0, &descriptors) == 0 else {throw VPhoneUserspaceNetworkError.socketPairFailed(errno: errno)@@ -115,6 +115,9 @@ public final class VPhoneUserspaceNetwork: @unchecked Sendable {// is waiting for a reply, so the reply is never read. The symptom is// a guest whose DNS queries leave but whose answers never arrive._ = fcntl(descriptor, F_SETFL, fcntl(descriptor, F_GETFL, 0) | O_NONBLOCK)+ // VZ closes its end when the device goes away; a frame sent after that+ // must fail with EPIPE, not kill the VM process with SIGPIPE.+ _ = fcntl(descriptor, F_SETNOSIGPIPE, 1)}socket = hostDescriptordiff --git a/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneInternetProtocolTests.swift b/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneInternetProtocolTests.swiftindex 0bfe067c..b7179672 100644--- a/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneInternetProtocolTests.swift+++ b/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneInternetProtocolTests.swift@@ -110,7 +110,7 @@ struct VPhoneInternetProtocolTests {acknowledgmentNumber: 0,flags: VPhoneTCPFlags.syn,windowSize: 65_535,- maximumSegmentSize: 1460,+ advertisedMSS: 1460,advertisedWindowScale: 7,)let bytes = segment.bytes(source: .any, destination: .any)diff --git a/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneTCPForwarderTests.swift b/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneTCPForwarderTests.swiftindex 7a458f5a..c2d07644 100644--- a/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneTCPForwarderTests.swift+++ b/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneTCPForwarderTests.swift@@ -140,11 +140,11 @@ struct VPhoneTCPForwarderTests {/// - Parameter session: runs with the accepted socket on a background/// queue and owns closing it.init(session: @escaping @Sendable (Int32, LoopbackServer) -> Void) throws {- listener = socket(AF_INET, SOCK_STREAM, 0)- guard listener >= 0 else { throw Error.socketFailed }+ let descriptor = socket(AF_INET, SOCK_STREAM, 0)+ guard descriptor >= 0 else { throw Error.socketFailed }var reuse: Int32 = 1- setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &reuse, socklen_t(MemoryLayout<Int32>.size))+ setsockopt(descriptor, SOL_SOCKET, SO_REUSEADDR, &reuse, socklen_t(MemoryLayout<Int32>.size))var address = sockaddr_in()address.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)@@ -153,22 +153,22 @@ struct VPhoneTCPForwarderTests {address.sin_addr = in_addr(s_addr: UInt32(0x7F00_0001).bigEndian)let bound = withUnsafePointer(to: &address) { pointer inpointer.withMemoryRebound(to: sockaddr.self, capacity: 1) {- bind(listener, $0, socklen_t(MemoryLayout<sockaddr_in>.size))+ bind(descriptor, $0, socklen_t(MemoryLayout<sockaddr_in>.size))}}guard bound == 0 else { throw Error.bindFailed }- guard listen(listener, 4) == 0 else { throw Error.listenFailed }+ guard listen(descriptor, 4) == 0 else { throw Error.listenFailed }var assigned = sockaddr_in()var length = socklen_t(MemoryLayout<sockaddr_in>.size)_ = withUnsafeMutablePointer(to: &assigned) { pointer inpointer.withMemoryRebound(to: sockaddr.self, capacity: 1) {- getsockname(listener, $0, &length)+ getsockname(descriptor, $0, &length)}}port = UInt16(bigEndian: assigned.sin_port)+ listener = descriptor- let descriptor = listenerlet server = selfDispatchQueue.global().async {let client = accept(descriptor, nil, nil)@@ -299,6 +299,9 @@ struct VPhoneTCPForwarderTests {// Advance the window; the transfer must resume.var acknowledged = isn &+ 1 &+ UInt32(afterFirstWindow)for _ in 2 ... 8 {+ // `feed` runs synchronously, so whatever the ACK releases is already+ // sent when it returns. Read the count before it, not after.+ let before = harness.sentDataBytesharness.feed(VPhoneTCPSegment(sourcePort: harness.guestPort,destinationPort: server.port,@@ -307,7 +310,6 @@ struct VPhoneTCPForwarderTests {flags: VPhoneTCPFlags.ack,windowSize: window,))- let before = harness.sentDataBytesharness.waitUntil("more data", timeout: 3) { harness.sentDataBytes > before }acknowledged = isn &+ 1 &+ UInt32(harness.sentDataBytes)if harness.sentDataBytes >= total { break }@@ -389,7 +391,7 @@ struct VPhoneTCPForwarderTests {sequenceNumber: 0, acknowledgmentNumber: 0,flags: VPhoneTCPFlags.syn,windowSize: 65_535,- maximumSegmentSize: 1460,+ advertisedMSS: 1460,advertisedWindowScale: 7,)let encoded = offered.bytes(@@ -435,8 +437,8 @@ struct VPhoneTCPForwarderTests {acknowledgmentNumber: isn &+ 1,flags: VPhoneTCPFlags.ack | VPhoneTCPFlags.psh,windowSize: 512,- windowScale: 7,payload: [0x41],+ windowScale: 7,))try #require(harness.waitUntil("a scaled window's worth") { harness.sentDataBytes > 512 * 8 })diff --git a/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneUserspaceNetworkTests.swift b/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneUserspaceNetworkTests.swiftindex 33b02dc0..5323c07b 100644--- a/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneUserspaceNetworkTests.swift+++ b/VPhoneKit/VPhoneCoreKitTests/Network/VPhoneUserspaceNetworkTests.swift@@ -306,22 +306,6 @@ struct VPhoneUserspaceNetworkTests {#expect(payload == [0xAB, 0xCD, 0x01, 0x00])}- /// Without a learned MAC there is nowhere to send the answer, so no forward- /// may be produced.- @Test func `UDP is dropped before the guest MAC is known`() {- let query = VPhoneUDPDatagram(sourcePort: 51000, destinationPort: 53, payload: [0x00])- let frame = ipv4Frame(- source: configuration.guestAddress,- destination: configuration.hostAddress,- proto: .udp,- payload: query.bytes(source: configuration.guestAddress, destination: configuration.hostAddress),- destinationMAC: .gateway,- )- if case .forward = responder().handle(frame) {- Issue.record("a forward was produced without a guest MAC")- }- }-/// DHCP stays local: it is the one UDP exchange this side finishes itself.@Test func `DHCP is answered locally, not forwarded`() {if case .forward = responder().handle(dhcpFrame(type: .discover)) {diff --git a/VPhoneKit/VPhoneCoreKitTests/Support/NetworkingTests.swift b/VPhoneKit/VPhoneCoreKitTests/Support/NetworkingTests.swiftindex f74333b4..bad1a16e 100644--- a/VPhoneKit/VPhoneCoreKitTests/Support/NetworkingTests.swift+++ b/VPhoneKit/VPhoneCoreKitTests/Support/NetworkingTests.swift@@ -60,13 +60,13 @@ struct NetworkingTests {@Test func `make network device tunnel carries an in-process backend`() throws {let (dev, backend) = try VPhoneNetworking.makeNetworkDevice(NetworkConfig(mode: .tunnel, macAddress: ""))#expect(dev?.attachment is VZFileHandleNetworkDeviceAttachment)- let backend = try #require(backend)- #expect(backend.configuration == .default)+ let network = try #require(backend)+ #expect(network.configuration == .default)// Starting and stopping must not trap; the rest of the behaviour is// covered by the frame-level tests.- backend.start()- backend.start()- backend.stop()- backend.stop()+ network.start()+ network.start()+ network.stop()+ network.stop()}}