|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// Copyright © 2025-2026 Apple Inc. and the Containerization project authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#if os(macOS) |
| 18 | + |
| 19 | +import Darwin |
| 20 | +import Foundation |
| 21 | +import Testing |
| 22 | + |
| 23 | +@testable import Containerization |
| 24 | + |
| 25 | +/// Tests for the VsockTransport fd lifecycle fix. |
| 26 | +/// |
| 27 | +/// The Virtualization framework tears down the vsock endpoint when a |
| 28 | +/// VZVirtioSocketConnection is closed, invalidating dup'd descriptors. |
| 29 | +/// The fix keeps the connection alive via VsockTransport until the gRPC |
| 30 | +/// channel is shut down. |
| 31 | +/// |
| 32 | +/// These tests use Unix socket pairs to verify: |
| 33 | +/// 1. A dup'd fd is fully functional when the original is kept alive. |
| 34 | +/// 2. The specific fcntl call that triggers the NIO crash (F_SETNOSIGPIPE) |
| 35 | +/// works on the dup'd fd. |
| 36 | +/// 3. The correct teardown order (close dup'd fd first, then original) |
| 37 | +/// preserves the connection for the peer until the original is closed. |
| 38 | +@Suite("VsockTransport tests") |
| 39 | +struct VsockTransportTests { |
| 40 | + |
| 41 | + /// Creates a connected Unix socket pair. Returns (fd0, fd1). |
| 42 | + private func makeSocketPair() throws -> (Int32, Int32) { |
| 43 | + var fds: [Int32] = [0, 0] |
| 44 | + let result = socketpair(AF_UNIX, SOCK_STREAM, 0, &fds) |
| 45 | + try #require(result == 0, "socketpair should succeed") |
| 46 | + return (fds[0], fds[1]) |
| 47 | + } |
| 48 | + |
| 49 | + // MARK: - fd lifecycle tests |
| 50 | + |
| 51 | + /// Verifies that F_SETNOSIGPIPE (the exact fcntl call where NIO crashes) |
| 52 | + /// succeeds on a dup'd fd when the original is kept alive. |
| 53 | + @Test func dupdDescriptorSupportsFcntlWhenOriginalAlive() throws { |
| 54 | + let (fd0, fd1) = try makeSocketPair() |
| 55 | + defer { |
| 56 | + close(fd0) |
| 57 | + close(fd1) |
| 58 | + } |
| 59 | + |
| 60 | + let dupdFd = dup(fd0) |
| 61 | + try #require(dupdFd != -1) |
| 62 | + defer { close(dupdFd) } |
| 63 | + |
| 64 | + // This is the exact operation that triggers the NIO EBADF crash |
| 65 | + // when the underlying vsock endpoint has been torn down. |
| 66 | + let result = fcntl(dupdFd, F_SETNOSIGPIPE, 1) |
| 67 | + #expect(result == 0, "F_SETNOSIGPIPE should succeed on dup'd fd when original is alive") |
| 68 | + } |
| 69 | + |
| 70 | + /// Verifies that a dup'd fd can read data written by the peer when the |
| 71 | + /// original fd is kept alive. |
| 72 | + @Test func dupdDescriptorCanReadWhenOriginalAlive() throws { |
| 73 | + let (fd0, fd1) = try makeSocketPair() |
| 74 | + defer { |
| 75 | + close(fd0) |
| 76 | + close(fd1) |
| 77 | + } |
| 78 | + |
| 79 | + let dupdFd = dup(fd0) |
| 80 | + try #require(dupdFd != -1) |
| 81 | + defer { close(dupdFd) } |
| 82 | + |
| 83 | + // Peer writes data. |
| 84 | + let message: [UInt8] = [1, 2, 3] |
| 85 | + let writeResult = message.withUnsafeBufferPointer { buf in |
| 86 | + write(fd1, buf.baseAddress, buf.count) |
| 87 | + } |
| 88 | + try #require(writeResult == 3) |
| 89 | + |
| 90 | + // Dup'd fd can read because the original keeps the connection alive. |
| 91 | + var readBuf = [UInt8](repeating: 0, count: 3) |
| 92 | + let readResult = readBuf.withUnsafeMutableBufferPointer { buf in |
| 93 | + read(dupdFd, buf.baseAddress, buf.count) |
| 94 | + } |
| 95 | + #expect(readResult == 3) |
| 96 | + #expect(readBuf == [1, 2, 3]) |
| 97 | + } |
| 98 | + |
| 99 | + /// Verifies the correct teardown order: closing the dup'd fd first (gRPC |
| 100 | + /// channel shutdown) does not break the connection for the peer, because |
| 101 | + /// the original fd (transport) is still alive. |
| 102 | + @Test func peerCanWriteAfterDupdFdClosedWhileOriginalAlive() throws { |
| 103 | + let (fd0, fd1) = try makeSocketPair() |
| 104 | + defer { |
| 105 | + close(fd0) |
| 106 | + close(fd1) |
| 107 | + } |
| 108 | + |
| 109 | + let dupdFd = dup(fd0) |
| 110 | + try #require(dupdFd != -1) |
| 111 | + |
| 112 | + // Close the dup'd fd (simulates gRPC channel shutdown). |
| 113 | + close(dupdFd) |
| 114 | + |
| 115 | + // The peer can still write because the original fd keeps the |
| 116 | + // connection alive. This matters for orderly shutdown: the guest |
| 117 | + // doesn't see an unexpected EOF while the host is still tearing |
| 118 | + // down the gRPC channel. |
| 119 | + let message: [UInt8] = [42] |
| 120 | + let writeResult = message.withUnsafeBufferPointer { buf in |
| 121 | + write(fd1, buf.baseAddress, buf.count) |
| 122 | + } |
| 123 | + #expect(writeResult == 1, "Peer can still write after dup'd fd is closed") |
| 124 | + |
| 125 | + // Read from the original to confirm data arrived. |
| 126 | + var readBuf = [UInt8](repeating: 0, count: 1) |
| 127 | + let readResult = readBuf.withUnsafeMutableBufferPointer { buf in |
| 128 | + read(fd0, buf.baseAddress, buf.count) |
| 129 | + } |
| 130 | + #expect(readResult == 1) |
| 131 | + #expect(readBuf == [42]) |
| 132 | + } |
| 133 | + |
| 134 | + /// Verifies that after both the dup'd fd and the original are closed, |
| 135 | + /// the peer sees EOF (read returns 0). |
| 136 | + @Test func peerSeesEOFAfterBothDescriptorsClosed() throws { |
| 137 | + let (fd0, fd1) = try makeSocketPair() |
| 138 | + defer { close(fd1) } |
| 139 | + |
| 140 | + let dupdFd = dup(fd0) |
| 141 | + try #require(dupdFd != -1) |
| 142 | + |
| 143 | + // Close dup'd fd first (gRPC shutdown), then original (transport.close()). |
| 144 | + close(dupdFd) |
| 145 | + close(fd0) |
| 146 | + |
| 147 | + // Peer should see EOF. |
| 148 | + var readBuf = [UInt8](repeating: 0, count: 1) |
| 149 | + let readResult = readBuf.withUnsafeMutableBufferPointer { buf in |
| 150 | + read(fd1, buf.baseAddress, buf.count) |
| 151 | + } |
| 152 | + #expect(readResult == 0, "Peer should see EOF after both descriptors are closed") |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +#endif |
0 commit comments