Skip to content

Commit 83164fc

Browse files
committed
stdlib: loadUnaligned doesn't need stack protection for it's temporary
rdar://105231457
1 parent a0e1524 commit 83164fc

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

stdlib/public/core/UnsafeRawPointer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ public struct UnsafeRawPointer: _Pointer {
463463
as type: T.Type
464464
) -> T {
465465
_debugPrecondition(_isPOD(T.self))
466-
return withUnsafeTemporaryAllocation(of: T.self, capacity: 1) {
466+
return _withUnprotectedUnsafeTemporaryAllocation(of: T.self, capacity: 1) {
467467
let temporary = $0.baseAddress._unsafelyUnwrappedUnchecked
468468
Builtin.int_memcpy_RawPointer_RawPointer_Int64(
469469
temporary._rawValue,
@@ -1245,7 +1245,7 @@ public struct UnsafeMutableRawPointer: _Pointer {
12451245
as type: T.Type
12461246
) -> T {
12471247
_debugPrecondition(_isPOD(T.self))
1248-
return withUnsafeTemporaryAllocation(of: T.self, capacity: 1) {
1248+
return _withUnprotectedUnsafeTemporaryAllocation(of: T.self, capacity: 1) {
12491249
let temporary = $0.baseAddress._unsafelyUnwrappedUnchecked
12501250
Builtin.int_memcpy_RawPointer_RawPointer_Int64(
12511251
temporary._rawValue,

test/SILOptimizer/stack_protection.sil

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,31 @@ bb0:
136136
return %4 : $Int64
137137
}
138138

139-
// CHECK-LABEL: sil [stack_protection] @stack_alloc_builtin_with_3_args
139+
// CHECK-LABEL: sil [stack_protection] @stack_alloc_builtin
140140
// CHECK-NOT: copy_addr
141-
// CHECK: } // end sil function 'stack_alloc_builtin_with_3_args'
142-
sil @stack_alloc_builtin_with_3_args : $@convention(thin) () -> () {
141+
// CHECK: } // end sil function 'stack_alloc_builtin'
142+
sil @stack_alloc_builtin: $@convention(thin) () -> () {
143143
bb0:
144144
%0 = integer_literal $Builtin.Word, 8
145145
%1 = builtin "stackAlloc"(%0 : $Builtin.Word, %0 : $Builtin.Word, %0 : $Builtin.Word) : $Builtin.RawPointer
146146
%2 = builtin "stackDealloc"(%1 : $Builtin.RawPointer) : $()
147147
%r = tuple ()
148148
return %r : $()
149149
}
150+
151+
// CHECK-LABEL: sil @unprotected_stack_alloc_builtin
152+
// CHECK-NOT: copy_addr
153+
// CHECK: } // end sil function 'unprotected_stack_alloc_builtin'
154+
sil @unprotected_stack_alloc_builtin: $@convention(thin) () -> () {
155+
bb0:
156+
%0 = integer_literal $Builtin.Word, 8
157+
%1 = integer_literal $Builtin.Int1, 0
158+
%2 = builtin "unprotectedStackAlloc"(%0 : $Builtin.Word, %0 : $Builtin.Word, %0 : $Builtin.Word, %1 : $Builtin.Int1) : $Builtin.RawPointer
159+
%3 = builtin "stackDealloc"(%2 : $Builtin.RawPointer) : $()
160+
%r = tuple ()
161+
return %r : $()
162+
}
163+
150164
// CHECK-LABEL: sil [stack_protection] @function_local_stack_obj
151165
// CHECK-NOT: copy_addr
152166
// CHECK: } // end sil function 'function_local_stack_obj'

test/SILOptimizer/stack_protection.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,10 @@ public func testWithUnsafeTemporaryAllocation() {
111111
}
112112
}
113113

114+
// CHECK-LABEL: sil @$s4test13loadUnalignedySiSVF
115+
// CHECK-NOT: copy_addr
116+
// CHECK: } // end sil function '$s4test13loadUnalignedySiSVF'
117+
public func loadUnaligned(_ urp: UnsafeRawPointer) -> Int {
118+
return urp.loadUnaligned(as: Int.self)
119+
}
120+

0 commit comments

Comments
 (0)