blob: b6993523223380a105d1626ef0a5ec25e6cbc6de [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
David L Kreitzerd5c67552016-10-14 17:56:00 +00002; RUN: opt -safe-stack -S -mtriple=i386-pc-contiki-unknown < %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
4
5; array [4 x i8]
6; Requires protector.
7
Evgeniy Stepanov8827f2d2015-12-22 00:13:11 +00008; CHECK: @__safestack_unsafe_stack_ptr = external thread_local(initialexec) global i8*
9; SINGLE-THREAD: @__safestack_unsafe_stack_ptr = external global i8*
10
Peter Collingbourne82437bf2015-06-15 21:07:11 +000011define void @foo(i8* %a) nounwind uwtable safestack {
12entry:
13 ; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
14
15 ; CHECK: %[[USST:.*]] = getelementptr i8, i8* %[[USP]], i32 -16
16
17 ; CHECK: store i8* %[[USST]], i8** @__safestack_unsafe_stack_ptr
18
Peter Collingbourne82437bf2015-06-15 21:07:11 +000019 %a.addr = alloca i8*, align 8
Peter Collingbourne82437bf2015-06-15 21:07:11 +000020 %buf = alloca [4 x i8], align 1
21
Evgeniy Stepanov45fa0fd2016-06-16 22:34:04 +000022 ; CHECK: %[[AADDR:.*]] = alloca i8*, align 8
Peter Collingbourne82437bf2015-06-15 21:07:11 +000023 ; CHECK: store i8* {{.*}}, i8** %[[AADDR]], align 8
24 store i8* %a, i8** %a.addr, align 8
25
Evgeniy Stepanov45fa0fd2016-06-16 22:34:04 +000026 ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, i8* %[[USP]], i32 -4
27 ; CHECK: %[[BUFPTR2:.*]] = bitcast i8* %[[BUFPTR]] to [4 x i8]*
Peter Collingbourne82437bf2015-06-15 21:07:11 +000028 ; 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
30
31 ; CHECK: %[[A2:.*]] = load i8*, i8** %[[AADDR]], align 8
32 %a2 = load i8*, i8** %a.addr, align 8
33
34 ; CHECK: call i8* @strcpy(i8* %[[GEP]], i8* %[[A2]])
35 %call = call i8* @strcpy(i8* %gep, i8* %a2)
36
37 ; CHECK: store i8* %[[USP]], i8** @__safestack_unsafe_stack_ptr
38 ret void
39}
40
Evgeniy Stepanova4ac3f42015-12-01 00:06:13 +000041; Load from an array at a fixed offset, no overflow.
42define i8 @StaticArrayFixedSafe() nounwind uwtable safestack {
43entry:
44 ; CHECK-LABEL: define i8 @StaticArrayFixedSafe(
45 ; CHECK-NOT: __safestack_unsafe_stack_ptr
46 ; CHECK: ret i8
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
50 ret i8 %x
51}
52
53; Load from an array at a fixed offset with overflow.
54define i8 @StaticArrayFixedUnsafe() nounwind uwtable safestack {
55entry:
56 ; CHECK-LABEL: define i8 @StaticArrayFixedUnsafe(
57 ; CHECK: __safestack_unsafe_stack_ptr
58 ; CHECK: ret i8
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
62 ret i8 %x
63}
64
65; Load from an array at an unknown offset.
66define i8 @StaticArrayVariableUnsafe(i32 %ofs) nounwind uwtable safestack {
67entry:
68 ; CHECK-LABEL: define i8 @StaticArrayVariableUnsafe(
69 ; CHECK: __safestack_unsafe_stack_ptr
70 ; CHECK: ret i8
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
74 ret i8 %x
75}
76
77; Load from an array of an unknown size.
78define i8 @DynamicArrayUnsafe(i32 %sz) nounwind uwtable safestack {
79entry:
80 ; CHECK-LABEL: define i8 @DynamicArrayUnsafe(
81 ; CHECK: __safestack_unsafe_stack_ptr
82 ; CHECK: ret i8
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
86 ret i8 %x
87}
88
Peter Collingbourne82437bf2015-06-15 21:07:11 +000089declare i8* @strcpy(i8*, i8*)