From 34cb4444363862f9b7cdba79146916b2acf90ebd Mon Sep 17 00:00:00 2001 From: Jeremy Schonfeld Date: Mon, 21 Oct 2024 14:57:01 -0700 Subject: [PATCH 1/2] Ensure NSLock/NSThread don't have stored properties with CoreFoundation types --- Sources/Foundation/NSLock.swift | 4 ++++ Sources/Foundation/Thread.swift | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/Sources/Foundation/NSLock.swift b/Sources/Foundation/NSLock.swift index fe1d08b775..207ccd966e 100644 --- a/Sources/Foundation/NSLock.swift +++ b/Sources/Foundation/NSLock.swift @@ -11,6 +11,10 @@ #if canImport(Glibc) import Glibc + +// Favor the Glibc declarations over the CoreFoundation declarations to ensure that stored properties use types from publicly imported modules +typealias pthread_mutex_t = Glibc.pthread_mutex_t +typealias pthread_cond_t = Glibc.pthread_cond_t #endif #if os(Windows) diff --git a/Sources/Foundation/Thread.swift b/Sources/Foundation/Thread.swift index 5e79579c62..5ffd592e71 100644 --- a/Sources/Foundation/Thread.swift +++ b/Sources/Foundation/Thread.swift @@ -15,8 +15,14 @@ import WinSDK #if canImport(Glibc) import Glibc + +// Favor the Glibc declarations over the CoreFoundation declarations to ensure that stored properties use types from publicly imported modules +typealias pthread_t = Glibc.pthread_t #elseif canImport(Musl) import Musl + +// Favor the Musl declarations over the CoreFoundation declarations to ensure that stored properties use types from publicly imported modules +typealias pthread_t = Musl.pthread_t #endif // WORKAROUND_SR9811 From 70d2bd82e51f4c9f24725be19fc5c70224841055 Mon Sep 17 00:00:00 2001 From: Jeremy Schonfeld Date: Tue, 22 Oct 2024 10:50:54 -0700 Subject: [PATCH 2/2] Store UnsafeMutableRawPointer/Any instead of concrete c types --- Sources/Foundation/NSLock.swift | 97 +++++++++++++++++++++++---------- Sources/Foundation/Thread.swift | 53 +++++++++++++----- 2 files changed, 108 insertions(+), 42 deletions(-) diff --git a/Sources/Foundation/NSLock.swift b/Sources/Foundation/NSLock.swift index 207ccd966e..6e953c0ad8 100644 --- a/Sources/Foundation/NSLock.swift +++ b/Sources/Foundation/NSLock.swift @@ -11,10 +11,6 @@ #if canImport(Glibc) import Glibc - -// Favor the Glibc declarations over the CoreFoundation declarations to ensure that stored properties use types from publicly imported modules -typealias pthread_mutex_t = Glibc.pthread_mutex_t -typealias pthread_cond_t = Glibc.pthread_cond_t #endif #if os(Windows) @@ -54,24 +50,37 @@ private typealias _ConditionVariablePointer = UnsafeMutablePointer Void = {} - private var _thread: _swift_CFThreadRef? = nil + private var __thread: Any? = nil + + private var _thread: _swift_CFThreadRef? { + get { + __thread as! _swift_CFThreadRef? + } + set { + __thread = newValue + } + } #if os(Windows) && !CYGWIN private class NonexportedAttrStorage { - var value = _CFThreadAttributes(dwSizeOfAttributes: DWORD(MemoryLayout<_CFThreadAttributes>.size), + var value: Any = _CFThreadAttributes(dwSizeOfAttributes: DWORD(MemoryLayout<_CFThreadAttributes>.size), dwThreadStackReservation: 0) } private let _attrStorage = NonexportedAttrStorage() internal final var _attr: _CFThreadAttributes { - get { _attrStorage.value } + get { _attrStorage.value as! _CFThreadAttributes } set { _attrStorage.value = newValue } } #elseif CYGWIN || os(OpenBSD) - internal var _attr : pthread_attr_t? = nil + internal var __attr : Any? = nil + + internal var _attr: pthread_attr_t? { + get { + __attr as! pthread_attr_t? + } + set { + __attr = newValue + } + } #else - internal var _attr = pthread_attr_t() + internal var __attr : Any = pthread_attr_t() + + internal var _attr: pthread_attr_t { + get { + __attr as! pthread_attr_t + } + set { + __attr = newValue + } + } #endif internal var _status = _NSThreadStatus.initialized internal var _cancelled = false @@ -245,16 +266,22 @@ open class Thread : NSObject { internal init(thread: _swift_CFThreadRef) { // Note: even on Darwin this is a non-optional _CFThreadRef; this is only used for valid threads, which are never null pointers. - _thread = thread + __thread = thread } public override init() { #if !os(Windows) - let _ = withUnsafeMutablePointer(to: &_attr) { attr in + #if os(OpenBSD) + var attr: pthread_attr_t? = nil + #else + var attr = pthread_attr_t() + #endif + let _ = withUnsafeMutablePointer(to: &attr) { attr in pthread_attr_init(attr) pthread_attr_setscope(attr, Int32(PTHREAD_SCOPE_SYSTEM)) pthread_attr_setdetachstate(attr, Int32(PTHREAD_CREATE_DETACHED)) } + __attr = attr #endif }