Skip to content

Commit 177d16c

Browse files
committed
Enable strict memory safety in the Concurrency module
1 parent 0c130a9 commit 177d16c

28 files changed

+780
-773
lines changed

stdlib/public/Concurrency/AsyncStreamBuffer.swift

Lines changed: 133 additions & 131 deletions
Large diffs are not rendered by default.

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ list(APPEND SWIFT_RUNTIME_CONCURRENCY_SWIFT_FLAGS
6565
"IsolatedAny"
6666
)
6767

68+
list(APPEND SWIFT_RUNTIME_CONCURRENCY_SWIFT_FLAGS "-strict-memory-safety")
69+
6870
list(APPEND SWIFT_RUNTIME_CONCURRENCY_C_FLAGS
6971
"-D__STDC_WANT_LIB_EXT1__=1")
7072

stdlib/public/Concurrency/CheckedContinuation.swift

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,59 +27,59 @@ internal final class CheckedContinuationCanary: @unchecked Sendable {
2727

2828
private static func _create(continuation: UnsafeRawPointer, function: String)
2929
-> CheckedContinuationCanary {
30-
let instance = Builtin.allocWithTailElems_1(CheckedContinuationCanary.self,
30+
let instance = unsafe Builtin.allocWithTailElems_1(CheckedContinuationCanary.self,
3131
1._builtinWordValue,
3232
(UnsafeRawPointer?, String).self)
3333

34-
instance._continuationPtr.initialize(to: continuation)
35-
instance._functionPtr.initialize(to: function)
34+
unsafe instance._continuationPtr.initialize(to: continuation)
35+
unsafe instance._functionPtr.initialize(to: function)
3636
return instance
3737
}
3838

3939
private var _continuationPtr: UnsafeMutablePointer<UnsafeRawPointer?> {
40-
return UnsafeMutablePointer<UnsafeRawPointer?>(
40+
return unsafe UnsafeMutablePointer<UnsafeRawPointer?>(
4141
Builtin.projectTailElems(self, (UnsafeRawPointer?, String).self))
4242
}
4343
private var _functionPtr: UnsafeMutablePointer<String> {
44-
let tailPtr = UnsafeMutableRawPointer(
44+
let tailPtr = unsafe UnsafeMutableRawPointer(
4545
Builtin.projectTailElems(self, (UnsafeRawPointer?, String).self))
4646

47-
let functionPtr = tailPtr
47+
let functionPtr = unsafe tailPtr
4848
+ MemoryLayout<(UnsafeRawPointer?, String)>.offset(of: \(UnsafeRawPointer?, String).1)!
4949

50-
return functionPtr.assumingMemoryBound(to: String.self)
50+
return unsafe functionPtr.assumingMemoryBound(to: String.self)
5151
}
5252

5353
internal static func create<T, E>(continuation: UnsafeContinuation<T, E>,
5454
function: String) -> CheckedContinuationCanary {
55-
return _create(
55+
return unsafe _create(
5656
continuation: unsafeBitCast(continuation, to: UnsafeRawPointer.self),
5757
function: function)
5858
}
5959

6060
internal var function: String {
61-
return _functionPtr.pointee
61+
return unsafe _functionPtr.pointee
6262
}
6363

6464
// Take the continuation away from the container, or return nil if it's
6565
// already been taken.
6666
internal func takeContinuation<T, E>() -> UnsafeContinuation<T, E>? {
6767
// Atomically exchange the current continuation value with a null pointer.
68-
let rawContinuationPtr = unsafeBitCast(_continuationPtr,
68+
let rawContinuationPtr = unsafe unsafeBitCast(_continuationPtr,
6969
to: Builtin.RawPointer.self)
7070
let rawOld = Builtin.atomicrmw_xchg_seqcst_Word(rawContinuationPtr,
7171
0._builtinWordValue)
7272

73-
return unsafeBitCast(rawOld, to: UnsafeContinuation<T, E>?.self)
73+
return unsafe unsafeBitCast(rawOld, to: UnsafeContinuation<T, E>?.self)
7474
}
7575

7676
deinit {
77-
_functionPtr.deinitialize(count: 1)
77+
unsafe _functionPtr.deinitialize(count: 1)
7878
// Log if the continuation was never consumed before the instance was
7979
// destructed.
80-
if _continuationPtr.pointee != nil {
80+
if unsafe _continuationPtr.pointee != nil {
8181
#if !$Embedded
82-
logFailedCheck("SWIFT TASK CONTINUATION MISUSE: \(function) leaked its continuation without resuming it. This may cause tasks waiting on it to remain suspended forever.\n")
82+
unsafe logFailedCheck("SWIFT TASK CONTINUATION MISUSE: \(function) leaked its continuation without resuming it. This may cause tasks waiting on it to remain suspended forever.\n")
8383
#else
8484
fatalError("SWIFT TASK CONTINUATION MISUSE")
8585
#endif
@@ -144,7 +144,7 @@ public struct CheckedContinuation<T, E: Error>: Sendable {
144144
/// source for the continuation, used to identify the continuation in
145145
/// runtime diagnostics related to misuse of this continuation.
146146
public init(continuation: UnsafeContinuation<T, E>, function: String = #function) {
147-
canary = CheckedContinuationCanary.create(
147+
canary = unsafe CheckedContinuationCanary.create(
148148
continuation: continuation,
149149
function: function)
150150
}
@@ -162,8 +162,8 @@ public struct CheckedContinuation<T, E: Error>: Sendable {
162162
/// the caller. The task continues executing when its executor is
163163
/// able to reschedule it.
164164
public func resume(returning value: sending T) {
165-
if let c: UnsafeContinuation<T, E> = canary.takeContinuation() {
166-
c.resume(returning: value)
165+
if let c: UnsafeContinuation<T, E> = unsafe canary.takeContinuation() {
166+
unsafe c.resume(returning: value)
167167
} else {
168168
#if !$Embedded
169169
fatalError("SWIFT TASK CONTINUATION MISUSE: \(canary.function) tried to resume its continuation more than once, returning \(value)!\n")
@@ -186,8 +186,8 @@ public struct CheckedContinuation<T, E: Error>: Sendable {
186186
/// the caller. The task continues executing when its executor is
187187
/// able to reschedule it.
188188
public func resume(throwing error: __owned E) {
189-
if let c: UnsafeContinuation<T, E> = canary.takeContinuation() {
190-
c.resume(throwing: error)
189+
if let c: UnsafeContinuation<T, E> = unsafe canary.takeContinuation() {
190+
unsafe c.resume(throwing: error)
191191
} else {
192192
#if !$Embedded
193193
fatalError("SWIFT TASK CONTINUATION MISUSE: \(canary.function) tried to resume its continuation more than once, throwing \(error)!\n")
@@ -301,9 +301,9 @@ public func withCheckedContinuation<T>(
301301
_ body: (CheckedContinuation<T, Never>) -> Void
302302
) async -> sending T {
303303
return await Builtin.withUnsafeContinuation {
304-
let unsafeContinuation = UnsafeContinuation<T, Never>($0)
305-
return body(CheckedContinuation(continuation: unsafeContinuation,
306-
function: function))
304+
let unsafeContinuation = unsafe UnsafeContinuation<T, Never>($0)
305+
return body(unsafe CheckedContinuation(continuation: unsafeContinuation,
306+
function: function))
307307
}
308308
}
309309

@@ -320,8 +320,8 @@ public func _unsafeInheritExecutor_withCheckedContinuation<T>(
320320
function: String = #function,
321321
_ body: (CheckedContinuation<T, Never>) -> Void
322322
) async -> T {
323-
return await withUnsafeContinuation {
324-
body(CheckedContinuation(continuation: $0, function: function))
323+
return await unsafe withUnsafeContinuation {
324+
body(unsafe CheckedContinuation(continuation: $0, function: function))
325325
}
326326
}
327327

@@ -365,9 +365,9 @@ public func withCheckedThrowingContinuation<T>(
365365
_ body: (CheckedContinuation<T, Error>) -> Void
366366
) async throws -> sending T {
367367
return try await Builtin.withUnsafeThrowingContinuation {
368-
let unsafeContinuation = UnsafeContinuation<T, Error>($0)
369-
return body(CheckedContinuation(continuation: unsafeContinuation,
370-
function: function))
368+
let unsafeContinuation = unsafe UnsafeContinuation<T, Error>($0)
369+
return body(unsafe CheckedContinuation(continuation: unsafeContinuation,
370+
function: function))
371371
}
372372
}
373373

@@ -384,8 +384,8 @@ public func _unsafeInheritExecutor_withCheckedThrowingContinuation<T>(
384384
function: String = #function,
385385
_ body: (CheckedContinuation<T, Error>) -> Void
386386
) async throws -> T {
387-
return try await withUnsafeThrowingContinuation {
388-
body(CheckedContinuation(continuation: $0, function: function))
387+
return try await unsafe withUnsafeThrowingContinuation {
388+
body(unsafe CheckedContinuation(continuation: $0, function: function))
389389
}
390390
}
391391

@@ -397,15 +397,15 @@ public func _unsafeInheritExecutor_withCheckedThrowingContinuation<T>(
397397
internal func _createCheckedContinuation<T>(
398398
_ continuation: __owned UnsafeContinuation<T, Never>
399399
) -> CheckedContinuation<T, Never> {
400-
return CheckedContinuation(continuation: continuation)
400+
return unsafe CheckedContinuation(continuation: continuation)
401401
}
402402

403403
@available(SwiftStdlib 5.1, *)
404404
@_alwaysEmitIntoClient
405405
internal func _createCheckedThrowingContinuation<T>(
406406
_ continuation: __owned UnsafeContinuation<T, Error>
407407
) -> CheckedContinuation<T, Error> {
408-
return CheckedContinuation(continuation: continuation)
408+
return unsafe CheckedContinuation(continuation: continuation)
409409
}
410410

411411
@available(SwiftStdlib 5.1, *)

stdlib/public/Concurrency/ContinuousClock.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ extension ContinuousClock: Clock {
7575
public var minimumResolution: Swift.Duration {
7676
var seconds = Int64(0)
7777
var nanoseconds = Int64(0)
78-
_getClockRes(
78+
unsafe _getClockRes(
7979
seconds: &seconds,
8080
nanoseconds: &nanoseconds,
8181
clock: _ClockID.continuous.rawValue)
@@ -86,7 +86,7 @@ extension ContinuousClock: Clock {
8686
public static var now: ContinuousClock.Instant {
8787
var seconds = Int64(0)
8888
var nanoseconds = Int64(0)
89-
_getTime(
89+
unsafe _getTime(
9090
seconds: &seconds,
9191
nanoseconds: &nanoseconds,
9292
clock: _ClockID.continuous.rawValue)

0 commit comments

Comments
 (0)