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
4target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
5target triple = "x86_64-unknown-linux-gnu"
6
7@.str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
8
9; no arrays / no nested arrays
10; Requires no protector.
11
12define void @foo(i8* %a) nounwind uwtable safestack {
13entry:
14  ; CHECK-LABEL: define void @foo(
15  ; CHECK-NOT: __safestack_unsafe_stack_ptr
16  ; CHECK: ret void
17  %a.addr = alloca i8*, align 8
18  store i8* %a, i8** %a.addr, align 8
19  %0 = load i8*, i8** %a.addr, align 8
20  %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* %0)
21  ret void
22}
23
24declare i32 @printf(i8*, ...)
25
26define void @call_memset(i64 %len) safestack {
27entry:
28  ; CHECK-LABEL: define void @call_memset
29  ; CHECK: @__safestack_unsafe_stack_ptr
30  ; CHECK: ret void
31  %q = alloca [10 x i8], align 1
32  %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
33  call void @llvm.memset.p0i8.i64(i8* %arraydecay, i8 1, i64 %len, i1 false)
34  ret void
35}
36
37define void @call_constant_memset() safestack {
38entry:
39  ; CHECK-LABEL: define void @call_constant_memset
40  ; CHECK-NOT: @__safestack_unsafe_stack_ptr
41  ; CHECK: ret void
42  %q = alloca [10 x i8], align 1
43  %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 2
44  call void @llvm.memset.p0i8.i64(i8* %arraydecay, i8 1, i64 7, i1 false)
45  ret void
46}
47
48define void @call_constant_overflow_memset() safestack {
49entry:
50  ; CHECK-LABEL: define void @call_constant_overflow_memset
51  ; CHECK: @__safestack_unsafe_stack_ptr
52  ; CHECK: ret void
53  %q = alloca [10 x i8], align 1
54  %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 7
55  call void @llvm.memset.p0i8.i64(i8* %arraydecay, i8 1, i64 5, i1 false)
56  ret void
57}
58
59define void @call_constant_underflow_memset() safestack {
60entry:
61  ; CHECK-LABEL: define void @call_constant_underflow_memset
62  ; CHECK: @__safestack_unsafe_stack_ptr
63  ; CHECK: ret void
64  %q = alloca [10 x i8], align 1
65  %arraydecay = getelementptr [10 x i8], [10 x i8]* %q, i32 0, i32 -1
66  call void @llvm.memset.p0i8.i64(i8* %arraydecay, i8 1, i64 3, i1 false)
67  ret void
68}
69
70; Readnone nocapture -> safe
71define void @call_readnone(i64 %len) safestack {
72entry:
73  ; CHECK-LABEL: define void @call_readnone
74  ; CHECK-NOT: @__safestack_unsafe_stack_ptr
75  ; CHECK: ret void
76  %q = alloca [10 x i8], align 1
77  %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
78  call void @readnone(i8* %arraydecay)
79  ret void
80}
81
82; Arg0 is readnone, arg1 is not. Pass alloca ptr as arg0 -> safe
83define void @call_readnone0_0(i64 %len) safestack {
84entry:
85  ; CHECK-LABEL: define void @call_readnone0_0
86  ; CHECK-NOT: @__safestack_unsafe_stack_ptr
87  ; CHECK: ret void
88  %q = alloca [10 x i8], align 1
89  %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
90  call void @readnone0(i8* %arraydecay, i8* zeroinitializer)
91  ret void
92}
93
94; Arg0 is readnone, arg1 is not. Pass alloca ptr as arg1 -> unsafe
95define void @call_readnone0_1(i64 %len) safestack {
96entry:
97  ; CHECK-LABEL: define void @call_readnone0_1
98  ; CHECK: @__safestack_unsafe_stack_ptr
99  ; CHECK: ret void
100  %q = alloca [10 x i8], align 1
101  %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
102  call void @readnone0(i8 *zeroinitializer, i8* %arraydecay)
103  ret void
104}
105
106; Readonly nocapture -> unsafe
107define void @call_readonly(i64 %len) safestack {
108entry:
109  ; CHECK-LABEL: define void @call_readonly
110  ; CHECK: @__safestack_unsafe_stack_ptr
111  ; CHECK: ret void
112  %q = alloca [10 x i8], align 1
113  %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
114  call void @readonly(i8* %arraydecay)
115  ret void
116}
117
118; Readonly nocapture -> unsafe
119define void @call_arg_readonly(i64 %len) safestack {
120entry:
121  ; CHECK-LABEL: define void @call_arg_readonly
122  ; CHECK: @__safestack_unsafe_stack_ptr
123  ; CHECK: ret void
124  %q = alloca [10 x i8], align 1
125  %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
126  call void @arg_readonly(i8* %arraydecay)
127  ret void
128}
129
130; Readwrite nocapture -> unsafe
131define void @call_readwrite(i64 %len) safestack {
132entry:
133  ; CHECK-LABEL: define void @call_readwrite
134  ; CHECK: @__safestack_unsafe_stack_ptr
135  ; CHECK: ret void
136  %q = alloca [10 x i8], align 1
137  %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
138  call void @readwrite(i8* %arraydecay)
139  ret void
140}
141
142; Captures the argument -> unsafe
143define void @call_capture(i64 %len) safestack {
144entry:
145  ; CHECK-LABEL: define void @call_capture
146  ; CHECK: @__safestack_unsafe_stack_ptr
147  ; CHECK: ret void
148  %q = alloca [10 x i8], align 1
149  %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
150  call void @capture(i8* %arraydecay)
151  ret void
152}
153
154; Lifetime intrinsics are always safe.
155define void @call_lifetime(i32* %p) {
156  ; CHECK-LABEL: define void @call_lifetime
157  ; CHECK-NOT: @__safestack_unsafe_stack_ptr
158  ; CHECK: ret void
159entry:
160  %q = alloca [100 x i8], align 16
161  %0 = bitcast [100 x i8]* %q to i8*
162  call void @llvm.lifetime.start.p0i8(i64 100, i8* %0)
163  call void @llvm.lifetime.end.p0i8(i64 100, i8* %0)
164  ret void
165}
166
167declare void @readonly(i8* nocapture) readonly
168declare void @arg_readonly(i8* readonly nocapture)
169declare void @readwrite(i8* nocapture)
170declare void @capture(i8* readnone) readnone
171
172declare void @readnone(i8* nocapture) readnone
173declare void @readnone0(i8* nocapture readnone, i8* nocapture)
174
175declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i1) nounwind argmemonly
176
177declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) nounwind argmemonly
178declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) nounwind argmemonly
179