Skip to content

Commit 14a0142

Browse files
arsenmPhilippRados
authored andcommitted
SafeStack: Respect alloca addrspace (llvm#112536)
Just insert addrspacecast in cases where the alloca uses a different address space, since I don't know what else you could possibly do.
1 parent 86a5cfb commit 14a0142

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

llvm/lib/CodeGen/SafeStack.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class SafeStack {
192192
SafeStack(Function &F, const TargetLoweringBase &TL, const DataLayout &DL,
193193
DomTreeUpdater *DTU, ScalarEvolution &SE)
194194
: F(F), TL(TL), DL(DL), DTU(DTU), SE(SE),
195-
StackPtrTy(PointerType::getUnqual(F.getContext())),
195+
StackPtrTy(DL.getAllocaPtrType(F.getContext())),
196196
IntPtrTy(DL.getIntPtrType(F.getContext())),
197197
Int32Ty(Type::getInt32Ty(F.getContext())) {}
198198

@@ -616,7 +616,8 @@ Value *SafeStack::moveStaticAllocasToUnsafeStack(
616616
IRBuilder<> IRBUser(InsertBefore);
617617
Value *Off =
618618
IRBUser.CreatePtrAdd(BasePointer, ConstantInt::get(Int32Ty, -Offset));
619-
Value *Replacement = IRBUser.CreateBitCast(Off, AI->getType(), Name);
619+
Value *Replacement =
620+
IRBUser.CreateAddrSpaceCast(Off, AI->getType(), Name);
620621

621622
if (auto *PHI = dyn_cast<PHINode>(User))
622623
// PHI nodes may have multiple incoming edges from the same BB (why??),

llvm/lib/CodeGen/TargetLoweringBase.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,8 @@ TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilderBase &IRB,
18491849
auto UnsafeStackPtr =
18501850
dyn_cast_or_null<GlobalVariable>(M->getNamedValue(UnsafeStackPtrVar));
18511851

1852-
Type *StackPtrTy = PointerType::getUnqual(M->getContext());
1852+
const DataLayout &DL = M->getDataLayout();
1853+
PointerType *StackPtrTy = DL.getAllocaPtrType(M->getContext());
18531854

18541855
if (!UnsafeStackPtr) {
18551856
auto TLSModel = UseTLS ?
@@ -1863,6 +1864,8 @@ TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilderBase &IRB,
18631864
UnsafeStackPtrVar, nullptr, TLSModel);
18641865
} else {
18651866
// The variable exists, check its type and attributes.
1867+
//
1868+
// FIXME: Move to IR verifier.
18661869
if (UnsafeStackPtr->getValueType() != StackPtrTy)
18671870
report_fatal_error(Twine(UnsafeStackPtrVar) + " must have void* type");
18681871
if (UseTLS != UnsafeStackPtr->isThreadLocal())
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: not --crash opt -passes=safe-stack -mtriple=x86_64-pc-linux-gnu -disable-output %s 2>&1 | FileCheck %s
3+
4+
target datalayout = "A5"
5+
6+
; Declaration of __safestack_unsafe_stack_ptr already exists with wrong address space
7+
@__safestack_unsafe_stack_ptr = external thread_local(initialexec) global ptr
8+
9+
; CHECK: LLVM ERROR: __safestack_unsafe_stack_ptr must have void* type
10+
11+
define void @alloca_addrspace() nounwind uwtable safestack {
12+
%a = alloca i8, align 8, addrspace(5)
13+
call void @Capture_as5(ptr addrspace(5) %a)
14+
ret void
15+
}
16+
17+
declare void @Capture_as5(ptr addrspace(5))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=safe-stack -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s --check-prefix=TLS
3+
4+
target datalayout = "A5"
5+
6+
; Use has wrong address space for the alloca, addrspacecast is inserted.
7+
define void @correct_alloca_addrspace() nounwind uwtable safestack {
8+
; TLS-LABEL: define void @correct_alloca_addrspace(
9+
; TLS-SAME: ) #[[ATTR0:[0-9]+]] !annotation [[META0:![0-9]+]] {
10+
; TLS-NEXT: [[ENTRY:.*:]]
11+
; TLS-NEXT: [[UNSAFE_STACK_PTR:%.*]] = load ptr addrspace(5), ptr @__safestack_unsafe_stack_ptr, align 8
12+
; TLS-NEXT: [[UNSAFE_STACK_STATIC_TOP:%.*]] = getelementptr i8, ptr addrspace(5) [[UNSAFE_STACK_PTR]], i32 -16
13+
; TLS-NEXT: store ptr addrspace(5) [[UNSAFE_STACK_STATIC_TOP]], ptr @__safestack_unsafe_stack_ptr, align 8
14+
; TLS-NEXT: [[TMP0:%.*]] = getelementptr i8, ptr addrspace(5) [[UNSAFE_STACK_PTR]], i32 -8
15+
; TLS-NEXT: [[A_UNSAFE:%.*]] = addrspacecast ptr addrspace(5) [[TMP0]] to ptr
16+
; TLS-NEXT: call void @Capture(ptr [[A_UNSAFE]])
17+
; TLS-NEXT: store ptr addrspace(5) [[UNSAFE_STACK_PTR]], ptr @__safestack_unsafe_stack_ptr, align 8
18+
; TLS-NEXT: ret void
19+
;
20+
entry:
21+
%a = alloca i8, align 8
22+
call void @Capture(ptr %a)
23+
ret void
24+
}
25+
26+
declare void @Capture(ptr)
27+
;.
28+
; TLS: [[META0]] = !{!"unsafe-stack-size", i32 16}
29+
;.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=safe-stack -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s --check-prefix=TLS
3+
4+
target datalayout = "A5"
5+
6+
define void @correct_alloca_addrspace() nounwind uwtable safestack {
7+
; TLS-LABEL: define void @correct_alloca_addrspace(
8+
; TLS-SAME: ) #[[ATTR0:[0-9]+]] !annotation [[META0:![0-9]+]] {
9+
; TLS-NEXT: [[ENTRY:.*:]]
10+
; TLS-NEXT: [[UNSAFE_STACK_PTR:%.*]] = load ptr addrspace(5), ptr @__safestack_unsafe_stack_ptr, align 8
11+
; TLS-NEXT: [[UNSAFE_STACK_STATIC_TOP:%.*]] = getelementptr i8, ptr addrspace(5) [[UNSAFE_STACK_PTR]], i32 -16
12+
; TLS-NEXT: store ptr addrspace(5) [[UNSAFE_STACK_STATIC_TOP]], ptr @__safestack_unsafe_stack_ptr, align 8
13+
; TLS-NEXT: [[TMP0:%.*]] = getelementptr i8, ptr addrspace(5) [[UNSAFE_STACK_PTR]], i32 -8
14+
; TLS-NEXT: call void @Capture_as5(ptr addrspace(5) [[TMP0]])
15+
; TLS-NEXT: store ptr addrspace(5) [[UNSAFE_STACK_PTR]], ptr @__safestack_unsafe_stack_ptr, align 8
16+
; TLS-NEXT: ret void
17+
;
18+
entry:
19+
%a = alloca i8, align 8, addrspace(5)
20+
call void @Capture_as5(ptr addrspace(5) %a)
21+
ret void
22+
}
23+
24+
declare void @Capture_as5(ptr addrspace(5))
25+
;.
26+
; TLS: [[META0]] = !{!"unsafe-stack-size", i32 16}
27+
;.

0 commit comments

Comments
 (0)