1 ; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
2 ; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
3 ; RUN: opt -passes=safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
4 ; RUN: opt -passes=safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
9 ; CHECK: @__safestack_unsafe_stack_ptr = external thread_local(initialexec) global ptr
11 define void @foo(ptr %a) nounwind uwtable safestack {
13 ; CHECK: %[[USP:.*]] = load ptr, ptr @__safestack_unsafe_stack_ptr
15 ; CHECK: %[[USST:.*]] = getelementptr i8, ptr %[[USP]], i32 -16
17 ; CHECK: store ptr %[[USST]], ptr @__safestack_unsafe_stack_ptr
19 %a.addr = alloca ptr, align 8
20 %buf = alloca [4 x i8], align 1
22 ; CHECK: %[[AADDR:.*]] = alloca ptr, align 8
23 ; CHECK: store ptr {{.*}}, ptr %[[AADDR]], align 8
24 store ptr %a, ptr %a.addr, align 8
26 ; CHECK: %[[A2:.*]] = load ptr, ptr %[[AADDR]], align 8
27 ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, ptr %[[USP]], i32 -4
28 %a2 = load ptr, ptr %a.addr, align 8
30 ; CHECK: call ptr @strcpy(ptr %[[BUFPTR]], ptr %[[A2]])
31 %call = call ptr @strcpy(ptr %buf, ptr %a2)
33 ; CHECK: store ptr %[[USP]], ptr @__safestack_unsafe_stack_ptr
37 ; Load from an array at a fixed offset, no overflow.
38 define i8 @StaticArrayFixedSafe() nounwind uwtable safestack {
40 ; CHECK-LABEL: define i8 @StaticArrayFixedSafe(
41 ; CHECK-NOT: __safestack_unsafe_stack_ptr
43 %buf = alloca i8, i32 4, align 1
44 %gep = getelementptr inbounds i8, ptr %buf, i32 2
45 %x = load i8, ptr %gep, align 1
49 ; Load from an array at a fixed offset with overflow.
50 define i8 @StaticArrayFixedUnsafe() nounwind uwtable safestack {
52 ; CHECK-LABEL: define i8 @StaticArrayFixedUnsafe(
53 ; CHECK: __safestack_unsafe_stack_ptr
55 %buf = alloca i8, i32 4, align 1
56 %gep = getelementptr inbounds i8, ptr %buf, i32 5
57 %x = load i8, ptr %gep, align 1
61 ; Load from an array at an unknown offset.
62 define i8 @StaticArrayVariableUnsafe(i32 %ofs) nounwind uwtable safestack {
64 ; CHECK-LABEL: define i8 @StaticArrayVariableUnsafe(
65 ; CHECK: __safestack_unsafe_stack_ptr
67 %buf = alloca i8, i32 4, align 1
68 %gep = getelementptr inbounds i8, ptr %buf, i32 %ofs
69 %x = load i8, ptr %gep, align 1
73 ; Load from an array of an unknown size.
74 define i8 @DynamicArrayUnsafe(i32 %sz) nounwind uwtable safestack {
76 ; CHECK-LABEL: define i8 @DynamicArrayUnsafe(
77 ; CHECK: __safestack_unsafe_stack_ptr
79 %buf = alloca i8, i32 %sz, align 1
80 %gep = getelementptr inbounds i8, ptr %buf, i32 2
81 %x = load i8, ptr %gep, align 1
85 declare ptr @strcpy(ptr, ptr)