|
| 1 | +commit 7e5350751e17515253c096350dddd3aeeda0eee2 |
| 2 | +Author: Evan Wilde <etceterawilde@gmail.com> |
| 3 | +Date: Tue Jul 9 22:45:20 2024 -0700 |
| 4 | + |
| 5 | + Workaround broken glibc modulemap |
| 6 | + |
| 7 | + We have been running into modularization issues on newer versions of |
| 8 | + various Linux distros, resulting in the compiler saying that the Glibc |
| 9 | + module doesn't have a SOCK_STREAM or SOCK_DGRAM. From some poking |
| 10 | + around, the definition is now coming from the CoreFoundation module as |
| 11 | + far as Swift is concerned. This is ultimately because our modulemap for |
| 12 | + Glibc is bad, but also means that I can't bring up Swift 6 on all of the |
| 13 | + Linux distros that 5.10 has support for. |
| 14 | + |
| 15 | + This workaround removes the explicit module name from `SOCK_STREAM` and |
| 16 | + renames it to `FOUNDATION_SOCK_STREAM` to avoid an ambiguous name, and |
| 17 | + completely removes `SOCK_DGRAM`. |
| 18 | + |
| 19 | + Both SOCK_STREAM and SOCK_DGRAM are fileprivates, so changing them will |
| 20 | + have no visible external effect. It is true that if another header |
| 21 | + somewhere defines `SOCK_STREAM`, we may pick it up instead of the |
| 22 | + definition from the glibc module, but that will also cause some nasty |
| 23 | + surprises to anyone using that header in C, so it is unlikely. |
| 24 | + |
| 25 | + Fixes: rdar://128079849 |
| 26 | + |
| 27 | +diff --git a/swift-corelibs-foundation/Sources/Foundation/Port.swift b/swift-corelibs-foundation/Sources/Foundation/Port.swift |
| 28 | +index c53263f0..f06f95a9 100644 |
| 29 | +--- a/swift-corelibs-foundation/Sources/Foundation/Port.swift |
| 30 | ++++ b/swift-corelibs-foundation/Sources/Foundation/Port.swift |
| 31 | +@@ -90,18 +90,22 @@ open class SocketPort: Port {} |
| 32 | + |
| 33 | + #else |
| 34 | + |
| 35 | ++#if canImport(Darwin) |
| 36 | ++import Darwin |
| 37 | ++fileprivate let FOUNDATION_SOCK_STREAM = SOCK_STREAM |
| 38 | ++fileprivate let FOUNDATION_IPPROTO_TCP = IPPROTO_TCP |
| 39 | ++#endif |
| 40 | ++ |
| 41 | + #if canImport(Glibc) && !os(Android) && !os(OpenBSD) |
| 42 | + import Glibc |
| 43 | +-fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM.rawValue) |
| 44 | +-fileprivate let SOCK_DGRAM = Int32(Glibc.SOCK_DGRAM.rawValue) |
| 45 | +-fileprivate let IPPROTO_TCP = Int32(Glibc.IPPROTO_TCP) |
| 46 | ++fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM.rawValue) |
| 47 | ++fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP) |
| 48 | + #endif |
| 49 | + |
| 50 | + #if canImport(Glibc) && os(Android) || os(OpenBSD) |
| 51 | + import Glibc |
| 52 | +-fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM) |
| 53 | +-fileprivate let SOCK_DGRAM = Int32(Glibc.SOCK_DGRAM) |
| 54 | +-fileprivate let IPPROTO_TCP = Int32(Glibc.IPPROTO_TCP) |
| 55 | ++fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM) |
| 56 | ++fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP) |
| 57 | + fileprivate let INADDR_ANY: in_addr_t = 0 |
| 58 | + #if os(OpenBSD) |
| 59 | + fileprivate let INADDR_LOOPBACK = 0x7f000001 |
| 60 | +@@ -123,7 +127,8 @@ import WinSDK |
| 61 | + fileprivate typealias sa_family_t = ADDRESS_FAMILY |
| 62 | + fileprivate typealias in_port_t = USHORT |
| 63 | + fileprivate typealias in_addr_t = UInt32 |
| 64 | +-fileprivate let IPPROTO_TCP = Int32(WinSDK.IPPROTO_TCP.rawValue) |
| 65 | ++fileprivate let FOUNDATION_SOCK_STREAM = SOCK_STREAM |
| 66 | ++fileprivate let FOUNDATION_IPPROTO_TCP = Int32(WinSDK.IPPROTO_TCP.rawValue) |
| 67 | + #endif |
| 68 | + |
| 69 | + // MARK: Darwin representation of socket addresses |
| 70 | +@@ -484,7 +489,7 @@ open class SocketPort : Port { |
| 71 | + |
| 72 | + let data = withUnsafeBytes(of: address) { Data($0) } |
| 73 | + |
| 74 | +- self.init(protocolFamily: PF_INET, socketType: SOCK_STREAM, protocol: IPPROTO_TCP, address: data) |
| 75 | ++ self.init(protocolFamily: PF_INET, socketType: FOUNDATION_SOCK_STREAM, protocol: FOUNDATION_IPPROTO_TCP, address: data) |
| 76 | + } |
| 77 | + |
| 78 | + private final func createNonuniquedCore(from socket: CFSocket, protocolFamily family: Int32, socketType type: Int32, protocol: Int32) { |
| 79 | +@@ -500,7 +505,7 @@ open class SocketPort : Port { |
| 80 | + var context = CFSocketContext() |
| 81 | + context.info = Unmanaged.passUnretained(self).toOpaque() |
| 82 | + var s: CFSocket |
| 83 | +- if type == SOCK_STREAM { |
| 84 | ++ if type == FOUNDATION_SOCK_STREAM { |
| 85 | + s = CFSocketCreate(nil, family, type, `protocol`, CFOptionFlags(kCFSocketAcceptCallBack), __NSFireSocketAccept, &context) |
| 86 | + } else { |
| 87 | + s = CFSocketCreate(nil, family, type, `protocol`, CFOptionFlags(kCFSocketDataCallBack), __NSFireSocketDatagram, &context) |
| 88 | +@@ -519,7 +524,7 @@ open class SocketPort : Port { |
| 89 | + var context = CFSocketContext() |
| 90 | + context.info = Unmanaged.passUnretained(self).toOpaque() |
| 91 | + var s: CFSocket |
| 92 | +- if type == SOCK_STREAM { |
| 93 | ++ if type == FOUNDATION_SOCK_STREAM { |
| 94 | + s = CFSocketCreateWithNative(nil, CFSocketNativeHandle(sock), CFOptionFlags(kCFSocketAcceptCallBack), __NSFireSocketAccept, &context) |
| 95 | + } else { |
| 96 | + s = CFSocketCreateWithNative(nil, CFSocketNativeHandle(sock), CFOptionFlags(kCFSocketDataCallBack), __NSFireSocketDatagram, &context) |
| 97 | +@@ -543,7 +548,7 @@ open class SocketPort : Port { |
| 98 | + sinAddr.sin_addr = inAddr |
| 99 | + |
| 100 | + let data = withUnsafeBytes(of: sinAddr) { Data($0) } |
| 101 | +- self.init(remoteWithProtocolFamily: PF_INET, socketType: SOCK_STREAM, protocol: IPPROTO_TCP, address: data) |
| 102 | ++ self.init(remoteWithProtocolFamily: PF_INET, socketType: FOUNDATION_SOCK_STREAM, protocol: FOUNDATION_IPPROTO_TCP, address: data) |
| 103 | + return |
| 104 | + } |
| 105 | + } |
| 106 | +@@ -556,7 +561,7 @@ open class SocketPort : Port { |
| 107 | + sinAddr.sin6_addr = in6Addr |
| 108 | + |
| 109 | + let data = withUnsafeBytes(of: sinAddr) { Data($0) } |
| 110 | +- self.init(remoteWithProtocolFamily: PF_INET, socketType: SOCK_STREAM, protocol: IPPROTO_TCP, address: data) |
| 111 | ++ self.init(remoteWithProtocolFamily: PF_INET, socketType: FOUNDATION_SOCK_STREAM, protocol: FOUNDATION_IPPROTO_TCP, address: data) |
| 112 | + return |
| 113 | + } |
| 114 | + } |
| 115 | +@@ -573,7 +578,7 @@ open class SocketPort : Port { |
| 116 | + withUnsafeBytes(of: in_addr_t(INADDR_LOOPBACK).bigEndian) { buffer.copyMemory(from: $0) } |
| 117 | + } |
| 118 | + let data = withUnsafeBytes(of: sinAddr) { Data($0) } |
| 119 | +- self.init(remoteWithProtocolFamily: PF_INET, socketType: SOCK_STREAM, protocol: IPPROTO_TCP, address: data) |
| 120 | ++ self.init(remoteWithProtocolFamily: PF_INET, socketType: FOUNDATION_SOCK_STREAM, protocol: FOUNDATION_IPPROTO_TCP, address: data) |
| 121 | + } |
| 122 | + |
| 123 | + private static let remoteSocketCoresLock = NSLock() |
| 124 | +@@ -1049,7 +1054,7 @@ open class SocketPort : Port { |
| 125 | + if let connector = core.connectors[signature], CFSocketIsValid(connector) { |
| 126 | + return connector |
| 127 | + } else { |
| 128 | +- if signature.socketType == SOCK_STREAM { |
| 129 | ++ if signature.socketType == FOUNDATION_SOCK_STREAM { |
| 130 | + if let connector = CFSocketCreate(nil, socketKind.protocolFamily, socketKind.socketType, socketKind.protocol, CFOptionFlags(kCFSocketDataCallBack), __NSFireSocketData, &context), CFSocketIsValid(connector) { |
| 131 | + var timeout = time - Date.timeIntervalSinceReferenceDate |
| 132 | + if timeout < 0 || timeout >= SocketPort.maximumTimeout { |
0 commit comments