File: ServiceHostConnection.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (242 lines) | stat: -rw-r--r-- 10,613 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SWBLibc
import SWBUtil
import Foundation

#if canImport(System)
import System
#else
import SystemPackage
#endif

/// Convert a count to a UInt8 buffer.
private func int32ToArray(_ value: UnsafePointer<Int32>) -> [UInt8] {
    return value.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout<Int32>.size) { Array(UnsafeBufferPointer(start: $0, count: MemoryLayout<Int32>.size)) }
}
private func int64ToArray(_ value: UnsafePointer<UInt64>) -> [UInt8] {
    return value.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout<UInt64>.size) { Array(UnsafeBufferPointer(start: $0, count: MemoryLayout<Int64>.size)) }
}

public enum ServiceHostConnectionMode: Sendable {
    case inProcess
    case outOfProcess
}

/// This class manages the connection to the build service host.
///
/// This class is designed to be thread safe: clients can send messages from any thread and they will be sent in FIFO order. Received messages are, however, only processed one at a time in FIFO order.
final class ServiceHostConnection: @unchecked Sendable {
    /// The shutdown handler.
    var shutdownHandler: @Sendable (_ error: (any Error)?) -> Void = { (_) in }

    /// The client handler.
    var handler: (UInt64, [UInt8]) async -> Void = { (_, _) in }

    /// Whether the queue is suspended.
    private let isSuspended = LockedValue(true)

    /// The queue used to send outgoing messages.
    private let sendQueue: SWBQueue

    /// The file descriptor to read on.
    private let inputFD: FileDescriptor

    /// The file descriptor to write on.
    private let outputFD: FileDescriptor

    /// Create a new connection to the host process.
    ///
    /// The connection input and output pipes are assumed to be provided on stdin and stdout, with a normal output stream on stderr. As part of initialization, the connection will clone the IO pipes to stable file descriptors and replace stdout with the stderr stream, so that the program can use regular print statements and have them go to a visible output stream.
    ///
    /// - Parameters:
    ///   - inputFD: The input file descriptor for incoming messages.
    ///   - outputFD: The output file descriptor for outgoing messages.
    init(inputFD: FileDescriptor, outputFD: FileDescriptor) {
        self.inputFD = inputFD
        self.outputFD = outputFD
        // The queues for the service host connection are given .userInitiated QOS (not .utility, which most queues in Swift Build have) because we don't know whether we're servicing a user interaction request.  Most requests should be shunted to a background thread unless there's a reason to send a quick response at high priority.
        self.sendQueue = SWBQueue(label: "SWBBuildService.ServiceHostConnection.sendQueue", qos: .userInitiated, autoreleaseFrequency: .workItem)
    }

    /// Extract and handle all messages in the given buffer, returning the number of remaining bytes.
    ///
    /// - start: A pointer to the start of the buffer.
    /// - count: The number of bytes in the buffer.
    /// - returns: The number of unhandled bytes at the end of the buffer.
    private func extractAndHandleMessages(_ start: UnsafePointer<UInt8>, _ count: Int) async -> Int {
        var start = start
        var count = count
        while count > MemoryLayout<UInt64>.size + MemoryLayout<Int32>.size {
            // The message header consist of a 64-bit channel number followed by a 32-bit payload size.
            let headerSize = MemoryLayout<UInt64>.size + MemoryLayout<Int32>.size
            var channelValue: UInt64 = 0
            withUnsafeMutableBytes(of: &channelValue) { valuePtr in
                valuePtr.copyBytes(from: UnsafeRawBufferPointer(start: start, count: MemoryLayout<UInt64>.size))
            }
            let channelID = UInt64(littleEndian: channelValue)

            var sizeValue: UInt32 = 0
            withUnsafeMutableBytes(of: &sizeValue) { valuePtr in
                valuePtr.copyBytes(from: UnsafeRawBufferPointer(start: start.advanced(by: MemoryLayout<UInt64>.size), count: MemoryLayout<UInt32>.size))
            }
            let payloadSize = Int(UInt32(littleEndian: sizeValue))
            let totalSize = headerSize + payloadSize

            #if DEBUG
            // A well behaved client would not send a negative payloadSize, but this can happen when we hit <rdar://problem/62081788>.
            // In that case, consider all remaining bytes to be bogus and drop them.
            if payloadSize < 0 {
                count = 0
                break
            }
            #endif

            // If we do not have a complete message, we are done.
            if count < totalSize {
                break
            }

            // Otherwise, handle the packet and continue.
            await self.handler(channelID, Array<UInt8>(UnsafeBufferPointer(start: start.advanced(by: headerSize), count: payloadSize)))

            // Advance to the start of the next packet.
            start = start.advanced(by: totalSize)
            count -= totalSize
        }
        return count
    }

    func suspend() {
        isSuspended.withLock { $0 = true }
    }

    /// Resume the connection.
    ///
    /// NOTE: The service will automatically stop the entire process when the connection is closed.
    func resume() {
        // If the connection not suspended, do nothing.
        if !isSuspended.withLock({ $0 }) {
            return
        }

        // Otherwise, launch the receive pump.
        isSuspended.withLock { $0 = false }
        Task<Void, Never>(priority: .userInitiated) {
            // Read data forever.
            var data: [UInt8] = []
            let tmpBufferSize = 4096
            let tmp = UnsafeMutableBufferPointer(start: UnsafeMutablePointer<UInt8>.allocate(capacity: tmpBufferSize), count: tmpBufferSize)

            var error: (any Error)?
            while !self.isSuspended.withLock({ $0 }) {
                #if DEBUG
                // Work around <rdar://problem/62081788> read syscall can sometimes return a value greater than the count parameter
                // We set the whole buffer to 0xFF so that parts that are not written will be interpreted as a payload with a negative length.
                // This is only a mitigation that may not catch all cases.
                tmp.update(repeating: 0xFF)
                #endif

                // Read data.
                let result: Int
                do {
                    let buf = try await DispatchFD(fileDescriptor: self.inputFD).readChunk(upToLength: tmpBufferSize)
                    result = buf.count
                    buf.copyBytes(to: tmp)
                } catch let readError {
                    error = ServiceHostIOError(message: "read from client failed", cause: readError)
                    break
                }
                if result == 0 {
                    if !data.isEmpty {
                        log("warning: connection closed with data remaining")
                    }
                    break
                }
                #if DEBUG
                if result > tmpBufferSize {
                    log("warning: read returned more bytes than requested: \(result) > \(tmpBufferSize)")
                }
                #endif

                // Extract all the messages, combining into a contiguous buffer first if necessary.
                if !data.isEmpty {
                    data.append(contentsOf: UnsafeBufferPointer(start: tmp.baseAddress, count: Int(result)))
                    let dataCopy = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: data.count)
                    defer {
                        dataCopy.deallocate()
                    }
                    data.copyBytes(to: dataCopy)
                    let remaining = await self.extractAndHandleMessages(dataCopy.baseAddress!, dataCopy.count)

                    // Update data to contain only the remaining data.
                    assert(remaining <= data.count)
                    if remaining != data.count {
                        data = Array<UInt8>(data[data.count - remaining..<data.count])
                    }
                } else {
                    let remaining = await self.extractAndHandleMessages(tmp.baseAddress!, Int(result))

                    // Set data to the remaining data, if present.
                    if remaining != 0 {
                        data = Array<UInt8>(UnsafeBufferPointer(start: tmp.baseAddress!.advanced(by: Int(result) - remaining), count: remaining))
                    }
                }
            }

            tmp.deallocate()

            // If the receive pump stops, tell the service to shut down.
            //
            // FIXME: We may at some point need to coordinate with outstanding work.
            self.shutdownHandler(error)
        }
    }

    /// Send a message.
    func send(_ channel: UInt64, _ message: [UInt8]) {
        sendQueue.async {
            var channel = channel
            // FIXME: We should switch to using Dispatch.
            guard var length = Int32(exactly: message.count) else {
                return self.shutdownHandler(ServiceHostIOError(message: "Message too large", cause: nil))
            }
            let header = int64ToArray(&channel) + int32ToArray(&length)

            do {
                try self.outputFD.writeAll(header + message)
            } catch {
                self.shutdownHandler(ServiceHostIOError(message: "write of \(length) bytes on channel \(channel) failed - the client may have exited", cause: error))
            }
        }
    }

    /// Send a message.
    func send(_ channel: UInt64, _ message: ByteString) {
        send(channel, message.bytes)
    }
}

private struct ServiceHostIOError: Error, CustomStringConvertible {
    public let message: String
    public let cause: (any Error)?

    public init(message: String, cause: (any Error)?) {
        self.message = message
        self.cause = cause
    }

    public var description: String {
        return "\(message) (\(cause?.localizedDescription ?? "unknown cause"))"
    }
}