Skip to content

Commit d89a517

Browse files
committed
stdlib: add underscored "withUnprotected..Pointer" functions
Those functions work the same way as their "unprotected" counterparts, except that they don't trigger stack protection for the pointers. * `_withUnprotectedUnsafeMutablePointer` * `_withUnprotectedUnsafePointer` * `_withUnprotectedUnsafeBytes` (two times)
1 parent 2d80d9f commit d89a517

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

stdlib/public/core/LifetimeManager.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ public func withUnsafeMutablePointer<T, Result>(
8282
return try body(UnsafeMutablePointer<T>(Builtin.addressof(&value)))
8383
}
8484

85+
/// Calls the given closure with a mutable pointer to the given argument.
86+
///
87+
/// This function is similar to `withUnsafeMutablePointer`, except that it
88+
/// doesn't trigger stack protection for the pointer.
89+
@_alwaysEmitIntoClient
90+
public func _withUnprotectedUnsafeMutablePointer<T, Result>(
91+
to value: inout T,
92+
_ body: (UnsafeMutablePointer<T>) throws -> Result
93+
) rethrows -> Result
94+
{
95+
return try body(UnsafeMutablePointer<T>(Builtin.unprotectedAddressOf(&value)))
96+
}
97+
8598
/// Invokes the given closure with a pointer to the given argument.
8699
///
87100
/// The `withUnsafePointer(to:_:)` function is useful for calling Objective-C
@@ -144,6 +157,19 @@ public func withUnsafePointer<T, Result>(
144157
return try body(UnsafePointer<T>(Builtin.addressof(&value)))
145158
}
146159

160+
/// Invokes the given closure with a pointer to the given argument.
161+
///
162+
/// This function is similar to `withUnsafePointer`, except that it
163+
/// doesn't trigger stack protection for the pointer.
164+
@_alwaysEmitIntoClient
165+
public func _withUnprotectedUnsafePointer<T, Result>(
166+
to value: inout T,
167+
_ body: (UnsafePointer<T>) throws -> Result
168+
) rethrows -> Result
169+
{
170+
return try body(UnsafePointer<T>(Builtin.unprotectedAddressOf(&value)))
171+
}
172+
147173
extension String {
148174
/// Calls the given closure with a pointer to the contents of the string,
149175
/// represented as a null-terminated sequence of UTF-8 code units.

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,22 @@ public func withUnsafeBytes<T, Result>(
10371037
}
10381038
}
10391039

1040+
/// Invokes the given closure with a buffer pointer covering the raw bytes of
1041+
/// the given argument.
1042+
///
1043+
/// This function is similar to `withUnsafeBytes`, except that it
1044+
/// doesn't trigger stack protection for the pointer.
1045+
@_alwaysEmitIntoClient
1046+
public func _withUnprotectedUnsafeBytes<T, Result>(
1047+
of value: inout T,
1048+
_ body: (UnsafeRawBufferPointer) throws -> Result
1049+
) rethrows -> Result
1050+
{
1051+
return try _withUnprotectedUnsafePointer(to: &value) {
1052+
try body(UnsafeRawBufferPointer(start: $0, count: MemoryLayout<T>.size))
1053+
}
1054+
}
1055+
10401056
/// Invokes the given closure with a buffer pointer covering the raw bytes of
10411057
/// the given argument.
10421058
///
@@ -1066,6 +1082,21 @@ public func withUnsafeBytes<T, Result>(
10661082
return try body(buffer)
10671083
}
10681084

1085+
/// Invokes the given closure with a buffer pointer covering the raw bytes of
1086+
/// the given argument.
1087+
///
1088+
/// This function is similar to `withUnsafeBytes`, except that it
1089+
/// doesn't trigger stack protection for the pointer.
1090+
@_alwaysEmitIntoClient
1091+
public func _withUnprotectedUnsafeBytes<T, Result>(
1092+
of value: T,
1093+
_ body: (UnsafeRawBufferPointer) throws -> Result
1094+
) rethrows -> Result {
1095+
let addr = UnsafeRawPointer(Builtin.unprotectedAddressOfBorrow(value))
1096+
let buffer = UnsafeRawBufferPointer(start: addr, count: MemoryLayout<T>.size)
1097+
return try body(buffer)
1098+
}
1099+
10691100
// ${'Local Variables'}:
10701101
// eval: (read-only-mode 1)
10711102
// End:

0 commit comments

Comments
 (0)