Skip to content

Commit 89d0277

Browse files
committed
Enable strict memory safety in the Cxx/CxxStdlib modules
1 parent 1efb994 commit 89d0277

File tree

5 files changed

+83
-80
lines changed

5 files changed

+83
-80
lines changed

stdlib/public/Cxx/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_swift_target_library(swiftCxx STATIC NO_LINK_NAME IS_STDLIB IS_SWIFT_ONLY
2323
-cxx-interoperability-mode=default
2424
-enable-experimental-feature Span
2525
-enable-experimental-feature BuiltinModule
26+
-strict-memory-safety
2627
# This module should not pull in the C++ standard library, so we disable it explicitly.
2728
# For functionality that depends on the C++ stdlib, use C++ stdlib overlay (`swiftstd` module).
2829
-Xcc -nostdinc++

stdlib/public/Cxx/CxxSpan.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,26 @@ extension CxxSpan {
6363
/// Creates a C++ span from a Swift UnsafeBufferPointer
6464
@inlinable
6565
public init(_ unsafeBufferPointer: UnsafeBufferPointer<Element>) {
66-
precondition(unsafeBufferPointer.baseAddress != nil,
66+
unsafe precondition(unsafeBufferPointer.baseAddress != nil,
6767
"UnsafeBufferPointer should not point to nil")
68-
self.init(unsafeBufferPointer.baseAddress!, Size(unsafeBufferPointer.count))
68+
unsafe self.init(unsafeBufferPointer.baseAddress!, Size(unsafeBufferPointer.count))
6969
}
7070

7171
@inlinable
7272
public init(_ unsafeMutableBufferPointer: UnsafeMutableBufferPointer<Element>) {
73-
precondition(unsafeMutableBufferPointer.baseAddress != nil,
73+
unsafe precondition(unsafeMutableBufferPointer.baseAddress != nil,
7474
"UnsafeMutableBufferPointer should not point to nil")
75-
self.init(unsafeMutableBufferPointer.baseAddress!, Size(unsafeMutableBufferPointer.count))
75+
unsafe self.init(unsafeMutableBufferPointer.baseAddress!, Size(unsafeMutableBufferPointer.count))
7676
}
7777

7878
@available(SwiftStdlib 6.1, *)
7979
@inlinable
8080
@unsafe
8181
public init(_ span: Span<Element>) {
82-
let (p, c) = unsafeBitCast(span, to: (UnsafeRawPointer?, Int).self)
83-
precondition(p != nil, "Span should not point to nil")
84-
let binding = p!.bindMemory(to: Element.self, capacity: c)
85-
self.init(binding, Size(c))
82+
let (p, c) = unsafe unsafeBitCast(span, to: (UnsafeRawPointer?, Int).self)
83+
unsafe precondition(p != nil, "Span should not point to nil")
84+
let binding = unsafe p!.bindMemory(to: Element.self, capacity: c)
85+
unsafe self.init(binding, Size(c))
8686
}
8787
}
8888

@@ -94,10 +94,10 @@ extension Span {
9494
public init<T: CxxSpan<Element>>(
9595
_unsafeCxxSpan span: borrowing T,
9696
) {
97-
let buffer = UnsafeBufferPointer(start: span.__dataUnsafe(), count: Int(span.size()))
97+
let buffer = unsafe UnsafeBufferPointer(start: span.__dataUnsafe(), count: Int(span.size()))
9898
let newSpan = Span(_unsafeElements: buffer)
9999
// 'self' is limited to the caller's scope of the variable passed to the 'span' argument.
100-
self = _overrideLifetime(newSpan, borrowing: span)
100+
self = unsafe _overrideLifetime(newSpan, borrowing: span)
101101
}
102102
}
103103

@@ -113,8 +113,8 @@ extension CxxMutableSpan {
113113
/// Creates a C++ span from a Swift UnsafeMutableBufferPointer
114114
@inlinable
115115
public init(_ unsafeMutableBufferPointer: UnsafeMutableBufferPointer<Element>) {
116-
precondition(unsafeMutableBufferPointer.baseAddress != nil,
116+
unsafe precondition(unsafeMutableBufferPointer.baseAddress != nil,
117117
"UnsafeMutableBufferPointer should not point to nil")
118-
self.init(unsafeMutableBufferPointer.baseAddress!, Size(unsafeMutableBufferPointer.count))
118+
unsafe self.init(unsafeMutableBufferPointer.baseAddress!, Size(unsafeMutableBufferPointer.count))
119119
}
120120
}

stdlib/public/Cxx/UnsafeCxxIterators.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public protocol UnsafeCxxInputIterator: Equatable {
3434
func successor() -> Self
3535
}
3636

37-
extension UnsafePointer: UnsafeCxxInputIterator {}
37+
extension UnsafePointer: @unsafe UnsafeCxxInputIterator {}
3838

39-
extension UnsafeMutablePointer: UnsafeCxxInputIterator {}
39+
extension UnsafeMutablePointer: @unsafe UnsafeCxxInputIterator {}
4040

4141
extension Optional: UnsafeCxxInputIterator where Wrapped: UnsafeCxxInputIterator {
4242
public typealias Pointee = Wrapped.Pointee
@@ -79,9 +79,9 @@ public protocol UnsafeCxxRandomAccessIterator: UnsafeCxxInputIterator {
7979
static func +=(lhs: inout Self, rhs: Distance)
8080
}
8181

82-
extension UnsafePointer: UnsafeCxxRandomAccessIterator {}
82+
extension UnsafePointer: @unsafe UnsafeCxxRandomAccessIterator {}
8383

84-
extension UnsafeMutablePointer: UnsafeCxxRandomAccessIterator {}
84+
extension UnsafeMutablePointer: @unsafe UnsafeCxxRandomAccessIterator {}
8585

8686
public protocol UnsafeCxxMutableRandomAccessIterator:
8787
UnsafeCxxRandomAccessIterator, UnsafeCxxMutableInputIterator {}

stdlib/public/Cxx/std/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ add_swift_target_library(swiftCxxStdlib STATIC NO_LINK_NAME IS_STDLIB IS_SWIFT_O
6060
# using C++ symbols in resilient overlays (see f4204568).
6161
-enable-experimental-feature AssumeResilientCxxTypes
6262

63+
-strict-memory-safety
64+
6365
# The varying modularization of the C++ standard library on different
6466
# platforms makes it difficult to enable MemberImportVisibility for this
6567
# module

0 commit comments

Comments
 (0)