blob: 7dcf7fa50d94957e080473a5174050b0099d4932 [file] [log] [blame]
Peter Collingbourne82437bf2015-06-15 21:07:11 +00001; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
Evgeniy Stepanov8827f2d2015-12-22 00:13:11 +00002; RUN: opt -safe-stack -safe-stack-usp-storage=single-thread -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck -check-prefix=SINGLE-THREAD %s
Peter Collingbourne82437bf2015-06-15 21:07:11 +00003; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
Evgeniy Stepanov8827f2d2015-12-22 00:13:11 +00004; 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
Peter Collingbourne82437bf2015-06-15 21:07:11 +00005
6; array [4 x i8]
7; Requires protector.
8
Evgeniy Stepanov8827f2d2015-12-22 00:13:11 +00009; CHECK: @__safestack_unsafe_stack_ptr = external thread_local(initialexec) global i8*
10; SINGLE-THREAD: @__safestack_unsafe_stack_ptr = external global i8*
11
Peter Collingbourne82437bf2015-06-15 21:07:11 +000012define void @foo(i8* %a) nounwind uwtable safestack {
13entry:
14 ; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
15
16 ; CHECK: %[[USST:.*]] = getelementptr i8, i8* %[[USP]], i32 -16
17
18 ; CHECK: store i8* %[[USST]], i8** @__safestack_unsafe_stack_ptr
19
Peter Collingbourne82437bf2015-06-15 21:07:11 +000020 %a.addr = alloca i8*, align 8
Peter Collingbourne82437bf2015-06-15 21:07:11 +000021 %buf = alloca [4 x i8], align 1
22
Evgeniy Stepanov45fa0fd2016-06-16 22:34:04 +000023 ; CHECK: %[[AADDR:.*]] = alloca i8*, align 8
Peter Collingbourne82437bf2015-06-15 21:07:11 +000024 ; CHECK: store i8* {{.*}}, i8** %[[AADDR]], align 8
25 store i8* %a, i8** %a.addr, align 8
26
Evgeniy Stepanov45fa0fd2016-06-16 22:34:04 +000027 ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, i8* %[[USP]], i32 -4
28 ; CHECK: %[[BUFPTR2:.*]] = bitcast i8* %[[BUFPTR]] to [4 x i8]*
Peter Collingbourne82437bf2015-06-15 21:07:11 +000029 ; CHECK: %[[GEP:.*]] = getelementptr inbounds [4 x i8], [4 x i8]* %[[BUFPTR2]], i32 0, i32 0
30 %gep = getelementptr inbounds [4 x i8], [4 x i8]* %buf, i32 0, i32 0
31
32 ; CHECK: %[[A2:.*]] = load i8*, i8** %[[AADDR]], align 8
33 %a2 = load i8*, i8** %a.addr, align 8
34
35 ; CHECK: call i8* @strcpy(i8* %[[GEP]], i8* %[[A2]])
36 %call = call i8* @strcpy(i8* %gep, i8* %a2)
37
38 ; CHECK: store i8* %[[USP]], i8** @__safestack_unsafe_stack_ptr
39 ret void
40}
41
Evgeniy Stepanova4ac3f42015-12-01 00:06:13 +000042; Load from an array at a fixed offset, no overflow.
43define i8 @StaticArrayFixedSafe() nounwind uwtable safestack {
44entry:
45 ; CHECK-LABEL: define i8 @StaticArrayFixedSafe(
46 ; CHECK-NOT: __safestack_unsafe_stack_ptr
47 ; CHECK: ret i8
48 %buf = alloca i8, i32 4, align 1
49 %gep = getelementptr inbounds i8, i8* %buf, i32 2
50 %x = load i8, i8* %gep, align 1
51 ret i8 %x
52}
53
54; Load from an array at a fixed offset with overflow.
55define i8 @StaticArrayFixedUnsafe() nounwind uwtable safestack {
56entry:
57 ; CHECK-LABEL: define i8 @StaticArrayFixedUnsafe(
58 ; CHECK: __safestack_unsafe_stack_ptr
59 ; CHECK: ret i8
60 %buf = alloca i8, i32 4, align 1
61 %gep = getelementptr inbounds i8, i8* %buf, i32 5
62 %x = load i8, i8* %gep, align 1
63 ret i8 %x
64}
65
66; Load from an array at an unknown offset.
67define i8 @StaticArrayVariableUnsafe(i32 %ofs) nounwind uwtable safestack {
68entry:
69 ; CHECK-LABEL: define i8 @StaticArrayVariableUnsafe(
70 ; CHECK: __safestack_unsafe_stack_ptr
71 ; CHECK: ret i8
72 %buf = alloca i8, i32 4, align 1
73 %gep = getelementptr inbounds i8, i8* %buf, i32 %ofs
74 %x = load i8, i8* %gep, align 1
75 ret i8 %x
76}
77
78; Load from an array of an unknown size.
79define i8 @DynamicArrayUnsafe(i32 %sz) nounwind uwtable safestack {
80entry:
81 ; CHECK-LABEL: define i8 @DynamicArrayUnsafe(
82 ; CHECK: __safestack_unsafe_stack_ptr
83 ; CHECK: ret i8
84 %buf = alloca i8, i32 %sz, align 1
85 %gep = getelementptr inbounds i8, i8* %buf, i32 2
86 %x = load i8, i8* %gep, align 1
87 ret i8 %x
88}
89
Peter Collingbourne82437bf2015-06-15 21:07:11 +000090declare i8* @strcpy(i8*, i8*)