Compare commits

...
2 Commits
Author SHA1 Message Date
George Barnett 84b1d49411 Add a few more inlinability annotation (#383)
Motivation:

Version 3.13.0 hits a compiler assertion when compiling in release mode
on Linux with Swift 6.1. This appears to stem from missing inlinable
annotations.

Modifications:

- Add a few more `@inlinable` and `@usableFromInline` annotations

Result:

Builds on 6.1+ in release mode
2025-07-22 14:05:35 +00:00
Cory Benfield 88b9c9b3b7 Work around Swift compiler issue with consuming and TSAN (#384)
This patch works around the absence of a fix for
https://github.com/swiftlang/swift/pull/76186 on older Swift compilers.
2025-07-22 14:31:55 +01:00
3 changed files with 120 additions and 36 deletions

No files matched your search

+2 -36
View File
@@ -21,15 +21,12 @@ private let emptyStorage:SecureBytes.Backing = SecureBytes.Backing.createEmpty()
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
struct SecureBytes {
@usableFromInline
var backing: Backing
@inlinable
init() {
self = .init(count: 0)
}
@usableFromInline
init(count: Int) {
if count == 0 {
self.backing = emptyStorage
@@ -43,7 +40,6 @@ struct SecureBytes {
}
/// Allows initializing a SecureBytes object with a closure that will initialize the memory.
@usableFromInline
init(unsafeUninitializedCapacity: Int, initializingWith callback: (inout UnsafeMutableRawBufferPointer, inout Int) throws -> Void) rethrows {
self.backing = Backing.create(capacity: unsafeUninitializedCapacity)
try self.backing._withVeryUnsafeMutableBytes { veryUnsafePointer in
@@ -59,7 +55,6 @@ struct SecureBytes {
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SecureBytes {
@inlinable
mutating func append<C: Collection>(_ data: C) where C.Element == UInt8 {
let requiredCapacity = self.count + data.count
let backingCapacity = self.backing.allocatedCapacity
@@ -71,7 +66,6 @@ extension SecureBytes {
self.backing._appendBytes(data)
}
@usableFromInline
mutating func reserveCapacity(_ n: Int) {
let backingCapacity = self.backing.allocatedCapacity
if backingCapacity >= n {
@@ -95,32 +89,27 @@ extension SecureBytes: Equatable {
// MARK: - Collection conformance
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SecureBytes: Collection {
@usableFromInline
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
struct Index {
/* fileprivate but usableFromInline */ @usableFromInline var offset: Int
/* fileprivate but usableFromInline */ var offset: Int
/*@inlinable*/ @usableFromInline internal init(offset: Int) {
/*@inlinable*/ internal init(offset: Int) {
self.offset = offset
}
}
@inlinable
var startIndex: Index {
return Index(offset: 0)
}
@inlinable
var endIndex: Index {
return Index(offset: self.count)
}
@inlinable
var count: Int {
return self.backing.count
}
@inlinable
subscript(_ index: Index) -> UInt8 {
get {
return self.backing[offset: index.offset]
@@ -130,7 +119,6 @@ extension SecureBytes: Collection {
}
}
@inlinable
func index(after index: Index) -> Index {
return index.advanced(by: 1)
}
@@ -139,7 +127,6 @@ extension SecureBytes: Collection {
// MARK: - BidirectionalCollection conformance
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SecureBytes: BidirectionalCollection {
@inlinable
func index(before index: Index) -> Index {
return index.advanced(by: -1)
}
@@ -156,7 +143,6 @@ extension SecureBytes: MutableCollection { }
// MARK: - RangeReplaceableCollection conformance
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SecureBytes: RangeReplaceableCollection {
@inlinable
mutating func replaceSubrange<C: Collection>(_ subrange: Range<Index>, with newElements: C) where C.Element == UInt8 {
let requiredCapacity = self.backing.count - subrange.count + newElements.count
let backingCapacity = self.backing.allocatedCapacity
@@ -182,7 +168,6 @@ extension SecureBytes: RangeReplaceableCollection {
}
// The default implementation of this from RangeReplaceableCollection can't take advantage of `ContiguousBytes`, so we override it here
@inlinable
public mutating func append<Elements: Sequence>(contentsOf newElements: Elements) where Elements.Element == UInt8 {
let done:Void? = newElements.withContiguousStorageIfAvailable {
replaceSubrange(endIndex..<endIndex, with: $0)
@@ -199,12 +184,10 @@ extension SecureBytes: RangeReplaceableCollection {
// MARK: - ContiguousBytes conformance
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SecureBytes: ContiguousBytes {
@inlinable
func withUnsafeBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T {
return try self.backing.withUnsafeBytes(body)
}
@inlinable
mutating func withUnsafeMutableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
if !isKnownUniquelyReferenced(&self.backing) {
self.backing = Backing.create(copying: self.backing)
@@ -213,7 +196,6 @@ extension SecureBytes: ContiguousBytes {
return try self.backing.withUnsafeMutableBytes(body)
}
@inlinable
func withContiguousStorageIfAvailable<R>(_ body: (UnsafeBufferPointer<UInt8>) throws -> R) rethrows -> R? {
return try self.backing.withContiguousStorageIfAvailable(body)
}
@@ -222,7 +204,6 @@ extension SecureBytes: ContiguousBytes {
// MARK: - DataProtocol conformance
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SecureBytes: DataProtocol {
@inlinable
var regions: CollectionOfOne<SecureBytes> {
return CollectionOfOne(self)
}
@@ -257,37 +238,29 @@ extension SecureBytes.Index: Strideable {
// MARK: - Heap allocated backing storage.
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SecureBytes {
@usableFromInline
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
internal struct BackingHeader {
@usableFromInline
internal var count: Int
@usableFromInline
internal var capacity: Int
}
@usableFromInline
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
internal class Backing: ManagedBuffer<BackingHeader, UInt8> {
@usableFromInline
class func createEmpty() -> Backing {
return Backing.create(minimumCapacity: 0, makingHeaderWith: { _ in BackingHeader(count: 0, capacity: 0) }) as! Backing
}
@usableFromInline
class func create(capacity: Int) -> Backing {
let capacity = Int(UInt32(capacity).nextPowerOf2ClampedToMax())
return Backing.create(minimumCapacity: capacity, makingHeaderWith: { _ in BackingHeader(count: 0, capacity: capacity) }) as! Backing
}
@usableFromInline
class func create(copying original: Backing) -> Backing {
return Backing.create(bytes: original)
}
@inlinable
class func create<D: ContiguousBytes>(bytes: D) -> Backing {
return bytes.withUnsafeBytes { bytesPtr in
let backing = Backing.create(capacity: bytesPtr.count)
@@ -300,7 +273,6 @@ extension SecureBytes {
}
}
@usableFromInline
class func create(randomBytes: Int) -> Backing {
let backing = Backing.create(capacity: randomBytes)
backing._withVeryUnsafeMutableBytes { targetPtr in
@@ -320,7 +292,6 @@ extension SecureBytes {
}
}
@usableFromInline
var count: Int {
get {
return self.header.count
@@ -330,7 +301,6 @@ extension SecureBytes {
}
}
@usableFromInline
subscript(offset offset: Int) -> UInt8 {
get {
// precondition(offset >= 0 && offset < self.count)
@@ -346,7 +316,6 @@ extension SecureBytes {
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SecureBytes.Backing {
@usableFromInline
var allocatedCapacity: Int {
#if os(OpenBSD)
return self.header.capacity
@@ -419,7 +388,6 @@ extension SecureBytes.Backing {
/// Moves the range of bytes identified by the slice by the delta, crashing if the move would
/// place the bytes out of the storage. Note that this does not update the count: external code
/// must ensure that that happens.
@usableFromInline
/* private but usableFromInline */ func _moveBytes(range: Range<Int>, by delta: Int) {
// We have to check that the range is within the delta, as is the new location.
precondition(range.lowerBound >= 0)
@@ -437,7 +405,6 @@ extension SecureBytes.Backing {
}
// Copies some bytes into the buffer at the appropriate place. Does not update count: external code must do so.
@inlinable
/* private but inlinable */ func _copyBytes<C: Collection>(_ bytes: C, at offset: Int) where C.Element == UInt8 {
precondition(offset >= 0)
precondition(offset + bytes.count <= self.allocatedCapacity)
@@ -470,7 +437,6 @@ extension SecureBytes.Backing: ContiguousBytes {
}
/// Very unsafe in the sense that this points to uninitialized memory. Used only for implementations within this file.
@inlinable
/* private but inlinable */ func _withVeryUnsafeMutableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
let capacity = self.allocatedCapacity
@@ -69,6 +69,10 @@ package struct EllipticCurvePoint: @unchecked Sendable {
try self.multiply(by: rhs, on: group, context: context)
}
// This enhancement can only be present on 6.1 or later because of the
// absence of https://github.com/swiftlang/swift/pull/76186 in older
// compilers.
#if compiler(>=6.1)
@usableFromInline
package consuming func multiplying(
by rhs: ArbitraryPrecisionInteger,
@@ -78,6 +82,18 @@ package struct EllipticCurvePoint: @unchecked Sendable {
try self.multiply(by: rhs, on: group, context: context)
return self
}
#else
@usableFromInline
package func multiplying(
by rhs: ArbitraryPrecisionInteger,
on group: BoringSSLEllipticCurveGroup,
context: FiniteFieldArithmeticContext? = nil
) throws -> EllipticCurvePoint {
var `self` = self
try self.multiply(by: rhs, on: group, context: context)
return self
}
#endif
@usableFromInline
package static func multiplying(
@@ -110,6 +126,10 @@ package struct EllipticCurvePoint: @unchecked Sendable {
try self.add(rhs, on: group, context: context)
}
// This enhancement can only be present on 6.1 or later because of the
// absence of https://github.com/swiftlang/swift/pull/76186 in older
// compilers.
#if compiler(>=6.1)
@usableFromInline
package consuming func adding(
_ rhs: consuming EllipticCurvePoint,
@@ -119,7 +139,23 @@ package struct EllipticCurvePoint: @unchecked Sendable {
try self.add(rhs, on: group, context: context)
return self
}
#else
@usableFromInline
package func adding(
_ rhs: consuming EllipticCurvePoint,
on group: BoringSSLEllipticCurveGroup,
context: FiniteFieldArithmeticContext? = nil
) throws -> EllipticCurvePoint {
var `self` = self
try self.add(rhs, on: group, context: context)
return self
}
#endif
// This enhancement can only be present on 6.1 or later because of the
// absence of https://github.com/swiftlang/swift/pull/76186 in older
// compilers.
#if compiler(>=6.1)
@usableFromInline
package static func adding(
_ lhs: consuming EllipticCurvePoint,
@@ -130,6 +166,19 @@ package struct EllipticCurvePoint: @unchecked Sendable {
try lhs.add(rhs, on: group, context: context)
return lhs
}
#else
@usableFromInline
package static func adding(
_ lhs: EllipticCurvePoint,
_ rhs: EllipticCurvePoint,
on group: BoringSSLEllipticCurveGroup,
context: FiniteFieldArithmeticContext? = nil
) throws -> EllipticCurvePoint {
var lhs = lhs
try lhs.add(rhs, on: group, context: context)
return lhs
}
#endif
@usableFromInline
package mutating func invert(
@@ -150,6 +199,10 @@ package struct EllipticCurvePoint: @unchecked Sendable {
try self.invert(on: group, context: context)
}
// This enhancement can only be present on 6.1 or later because of the
// absence of https://github.com/swiftlang/swift/pull/76186 in older
// compilers.
#if compiler(>=6.1)
@usableFromInline
package consuming func inverting(
on group: BoringSSLEllipticCurveGroup,
@@ -158,7 +211,22 @@ package struct EllipticCurvePoint: @unchecked Sendable {
try self.invert(on: group, context: context)
return self
}
#else
@usableFromInline
package func inverting(
on group: BoringSSLEllipticCurveGroup,
context: FiniteFieldArithmeticContext? = nil
) throws -> EllipticCurvePoint {
var `self` = self
try self.invert(on: group, context: context)
return self
}
#endif
// This enhancement can only be present on 6.1 or later because of the
// absence of https://github.com/swiftlang/swift/pull/76186 in older
// compilers.
#if compiler(>=6.1)
@usableFromInline
package static func inverting(
_ point: consuming EllipticCurvePoint,
@@ -168,6 +236,18 @@ package struct EllipticCurvePoint: @unchecked Sendable {
try point.invert(on: group, context: context)
return point
}
#else
@usableFromInline
package static func inverting(
_ point: EllipticCurvePoint,
on group: BoringSSLEllipticCurveGroup,
context: FiniteFieldArithmeticContext? = nil
) throws -> EllipticCurvePoint {
var point = point
try point.invert(on: group, context: context)
return point
}
#endif
@usableFromInline
package mutating func subtract(
@@ -190,6 +270,10 @@ package struct EllipticCurvePoint: @unchecked Sendable {
try self.subtract(rhs, on: group, context: context)
}
// This enhancement can only be present on 6.1 or later because of the
// absence of https://github.com/swiftlang/swift/pull/76186 in older
// compilers.
#if compiler(>=6.1)
@usableFromInline
package consuming func subtracting(
_ rhs: consuming EllipticCurvePoint,
@@ -199,7 +283,23 @@ package struct EllipticCurvePoint: @unchecked Sendable {
try self.subtract(rhs, on: group, context: context)
return self
}
#else
@usableFromInline
package func subtracting(
_ rhs: EllipticCurvePoint,
on group: BoringSSLEllipticCurveGroup,
context: FiniteFieldArithmeticContext? = nil
) throws -> EllipticCurvePoint {
var `self` = self
try self.subtract(rhs, on: group, context: context)
return self
}
#endif
// This enhancement can only be present on 6.1 or later because of the
// absence of https://github.com/swiftlang/swift/pull/76186 in older
// compilers.
#if compiler(>=6.1)
@usableFromInline
package static func subtracting(
_ rhs: consuming EllipticCurvePoint,
@@ -210,6 +310,19 @@ package struct EllipticCurvePoint: @unchecked Sendable {
try lhs.subtract(rhs, on: group, context: context)
return lhs
}
#else
@usableFromInline
package static func subtracting(
_ rhs: EllipticCurvePoint,
from lhs: EllipticCurvePoint,
on group: BoringSSLEllipticCurveGroup,
context: FiniteFieldArithmeticContext? = nil
) throws -> EllipticCurvePoint {
var lhs = lhs
try lhs.subtract(rhs, on: group, context: context)
return lhs
}
#endif
@usableFromInline
package init<MessageBytes: ContiguousBytes, DSTBytes: ContiguousBytes>(
@@ -15,9 +15,14 @@
import Foundation
extension Data {
// This enhancement can only be present on 6.1 or later because of the
// absence of https://github.com/swiftlang/swift/pull/76186 in older
// compilers.
#if compiler(>=6.1)
// This overload reduces allocations when used in a chain of infix operations.
static func + (lhs: consuming Data, rhs: consuming Data) -> Data {
lhs.append(contentsOf: rhs)
return lhs
}
#endif
}