1 ; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
2 ; RUN: opt -safe-stack -safe-stack-usp-storage=single-thread -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck -check-prefix=SINGLE-THREAD %s
3 ; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
4 ; RUN: opt -safe-stack -safe-stack-usp-storage=single-thread -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck -check-prefix=SINGLE-THREAD %s
9 ; CHECK: @__safestack_unsafe_stack_ptr = external thread_local(initialexec) global i8*
10 ; SINGLE-THREAD: @__safestack_unsafe_stack_ptr = external global i8*
12 define void @foo(i8* %a) nounwind uwtable safestack {
14 ; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
16 ; CHECK: %[[USST:.*]] = getelementptr i8, i8* %[[USP]], i32 -16
18 ; CHECK: store i8* %[[USST]], i8** @__safestack_unsafe_stack_ptr
20 ; CHECK: %[[AADDR:.*]] = alloca i8*, align 8
21 %a.addr = alloca i8*, align 8
23 ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, i8* %[[USP]], i32 -4
24 ; CHECK: %[[BUFPTR2:.*]] = bitcast i8* %[[BUFPTR]] to [4 x i8]*
25 %buf = alloca [4 x i8], align 1
27 ; CHECK: store i8* {{.*}}, i8** %[[AADDR]], align 8
28 store i8* %a, i8** %a.addr, align 8
30 ; CHECK: %[[GEP:.*]] = getelementptr inbounds [4 x i8], [4 x i8]* %[[BUFPTR2]], i32 0, i32 0
31 %gep = getelementptr inbounds [4 x i8], [4 x i8]* %buf, i32 0, i32 0
33 ; CHECK: %[[A2:.*]] = load i8*, i8** %[[AADDR]], align 8
34 %a2 = load i8*, i8** %a.addr, align 8
36 ; CHECK: call i8* @strcpy(i8* %[[GEP]], i8* %[[A2]])
37 %call = call i8* @strcpy(i8* %gep, i8* %a2)
39 ; CHECK: store i8* %[[USP]], i8** @__safestack_unsafe_stack_ptr
43 ; Load from an array at a fixed offset, no overflow.
44 define i8 @StaticArrayFixedSafe() nounwind uwtable safestack {
46 ; CHECK-LABEL: define i8 @StaticArrayFixedSafe(
47 ; CHECK-NOT: __safestack_unsafe_stack_ptr
49 %buf = alloca i8, i32 4, align 1
50 %gep = getelementptr inbounds i8, i8* %buf, i32 2
51 %x = load i8, i8* %gep, align 1
55 ; Load from an array at a fixed offset with overflow.
56 define i8 @StaticArrayFixedUnsafe() nounwind uwtable safestack {
58 ; CHECK-LABEL: define i8 @StaticArrayFixedUnsafe(
59 ; CHECK: __safestack_unsafe_stack_ptr
61 %buf = alloca i8, i32 4, align 1
62 %gep = getelementptr inbounds i8, i8* %buf, i32 5
63 %x = load i8, i8* %gep, align 1
67 ; Load from an array at an unknown offset.
68 define i8 @StaticArrayVariableUnsafe(i32 %ofs) nounwind uwtable safestack {
70 ; CHECK-LABEL: define i8 @StaticArrayVariableUnsafe(
71 ; CHECK: __safestack_unsafe_stack_ptr
73 %buf = alloca i8, i32 4, align 1
74 %gep = getelementptr inbounds i8, i8* %buf, i32 %ofs
75 %x = load i8, i8* %gep, align 1
79 ; Load from an array of an unknown size.
80 define i8 @DynamicArrayUnsafe(i32 %sz) nounwind uwtable safestack {
82 ; CHECK-LABEL: define i8 @DynamicArrayUnsafe(
83 ; CHECK: __safestack_unsafe_stack_ptr
85 %buf = alloca i8, i32 %sz, align 1
86 %gep = getelementptr inbounds i8, i8* %buf, i32 2
87 %x = load i8, i8* %gep, align 1
91 declare i8* @strcpy(i8*, i8*)