1 ; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
2 ; RUN: opt -safe-stack -S -mtriple=i386-pc-contiki-unknown < %s -o - | FileCheck -check-prefix=SINGLE-THREAD %s
3 ; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
8 ; CHECK: @__safestack_unsafe_stack_ptr = external thread_local(initialexec) global i8*
9 ; SINGLE-THREAD: @__safestack_unsafe_stack_ptr = external global i8*
11 define void @foo(i8* %a) nounwind uwtable safestack {
13 ; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
15 ; CHECK: %[[USST:.*]] = getelementptr i8, i8* %[[USP]], i32 -16
17 ; CHECK: store i8* %[[USST]], i8** @__safestack_unsafe_stack_ptr
19 %a.addr = alloca i8*, align 8
20 %buf = alloca [4 x i8], align 1
22 ; CHECK: %[[AADDR:.*]] = alloca i8*, align 8
23 ; CHECK: store i8* {{.*}}, i8** %[[AADDR]], align 8
24 store i8* %a, i8** %a.addr, align 8
26 ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, i8* %[[USP]], i32 -4
27 ; CHECK: %[[BUFPTR2:.*]] = bitcast i8* %[[BUFPTR]] to [4 x i8]*
28 ; CHECK: %[[GEP:.*]] = getelementptr inbounds [4 x i8], [4 x i8]* %[[BUFPTR2]], i32 0, i32 0
29 %gep = getelementptr inbounds [4 x i8], [4 x i8]* %buf, i32 0, i32 0
31 ; CHECK: %[[A2:.*]] = load i8*, i8** %[[AADDR]], align 8
32 %a2 = load i8*, i8** %a.addr, align 8
34 ; CHECK: call i8* @strcpy(i8* %[[GEP]], i8* %[[A2]])
35 %call = call i8* @strcpy(i8* %gep, i8* %a2)
37 ; CHECK: store i8* %[[USP]], i8** @__safestack_unsafe_stack_ptr
41 ; Load from an array at a fixed offset, no overflow.
42 define i8 @StaticArrayFixedSafe() nounwind uwtable safestack {
44 ; CHECK-LABEL: define i8 @StaticArrayFixedSafe(
45 ; CHECK-NOT: __safestack_unsafe_stack_ptr
47 %buf = alloca i8, i32 4, align 1
48 %gep = getelementptr inbounds i8, i8* %buf, i32 2
49 %x = load i8, i8* %gep, align 1
53 ; Load from an array at a fixed offset with overflow.
54 define i8 @StaticArrayFixedUnsafe() nounwind uwtable safestack {
56 ; CHECK-LABEL: define i8 @StaticArrayFixedUnsafe(
57 ; CHECK: __safestack_unsafe_stack_ptr
59 %buf = alloca i8, i32 4, align 1
60 %gep = getelementptr inbounds i8, i8* %buf, i32 5
61 %x = load i8, i8* %gep, align 1
65 ; Load from an array at an unknown offset.
66 define i8 @StaticArrayVariableUnsafe(i32 %ofs) nounwind uwtable safestack {
68 ; CHECK-LABEL: define i8 @StaticArrayVariableUnsafe(
69 ; CHECK: __safestack_unsafe_stack_ptr
71 %buf = alloca i8, i32 4, align 1
72 %gep = getelementptr inbounds i8, i8* %buf, i32 %ofs
73 %x = load i8, i8* %gep, align 1
77 ; Load from an array of an unknown size.
78 define i8 @DynamicArrayUnsafe(i32 %sz) nounwind uwtable safestack {
80 ; CHECK-LABEL: define i8 @DynamicArrayUnsafe(
81 ; CHECK: __safestack_unsafe_stack_ptr
83 %buf = alloca i8, i32 %sz, align 1
84 %gep = getelementptr inbounds i8, i8* %buf, i32 2
85 %x = load i8, i8* %gep, align 1
89 declare i8* @strcpy(i8*, i8*)