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
4 @.str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
6 ; no arrays / no nested arrays
7 ; Requires no protector.
9 define void @foo(i8* %a) nounwind uwtable safestack {
11 ; CHECK-LABEL: define void @foo(
12 ; CHECK-NOT: __safestack_unsafe_stack_ptr
14 %a.addr = alloca i8*, align 8
15 store i8* %a, i8** %a.addr, align 8
16 %0 = load i8*, i8** %a.addr, align 8
17 %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* %0)
21 declare i32 @printf(i8*, ...)
23 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
24 target triple = "x86_64-unknown-linux-gnu"
26 define void @call_memset(i64 %len) safestack {
28 ; CHECK-LABEL: define void @call_memset
29 ; CHECK: @__safestack_unsafe_stack_ptr
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)
37 define void @call_constant_memset() safestack {
39 ; CHECK-LABEL: define void @call_constant_memset
40 ; CHECK-NOT: @__safestack_unsafe_stack_ptr
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)
48 define void @call_constant_overflow_memset() safestack {
50 ; CHECK-LABEL: define void @call_constant_overflow_memset
51 ; CHECK: @__safestack_unsafe_stack_ptr
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)
59 define void @call_constant_underflow_memset() safestack {
61 ; CHECK-LABEL: define void @call_constant_underflow_memset
62 ; CHECK: @__safestack_unsafe_stack_ptr
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)
70 ; Readnone nocapture -> safe
71 define void @call_readnone(i64 %len) safestack {
73 ; CHECK-LABEL: define void @call_readnone
74 ; CHECK-NOT: @__safestack_unsafe_stack_ptr
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)
82 ; Arg0 is readnone, arg1 is not. Pass alloca ptr as arg0 -> safe
83 define void @call_readnone0_0(i64 %len) safestack {
85 ; CHECK-LABEL: define void @call_readnone0_0
86 ; CHECK-NOT: @__safestack_unsafe_stack_ptr
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)
94 ; Arg0 is readnone, arg1 is not. Pass alloca ptr as arg1 -> unsafe
95 define void @call_readnone0_1(i64 %len) safestack {
97 ; CHECK-LABEL: define void @call_readnone0_1
98 ; CHECK: @__safestack_unsafe_stack_ptr
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)
106 ; Readonly nocapture -> unsafe
107 define void @call_readonly(i64 %len) safestack {
109 ; CHECK-LABEL: define void @call_readonly
110 ; CHECK: @__safestack_unsafe_stack_ptr
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)
118 ; Readonly nocapture -> unsafe
119 define void @call_arg_readonly(i64 %len) safestack {
121 ; CHECK-LABEL: define void @call_arg_readonly
122 ; CHECK: @__safestack_unsafe_stack_ptr
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)
130 ; Readwrite nocapture -> unsafe
131 define void @call_readwrite(i64 %len) safestack {
133 ; CHECK-LABEL: define void @call_readwrite
134 ; CHECK: @__safestack_unsafe_stack_ptr
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)
142 ; Captures the argument -> unsafe
143 define void @call_capture(i64 %len) safestack {
145 ; CHECK-LABEL: define void @call_capture
146 ; CHECK: @__safestack_unsafe_stack_ptr
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)
154 ; Lifetime intrinsics are always safe.
155 define void @call_lifetime(i32* %p) {
156 ; CHECK-LABEL: define void @call_lifetime
157 ; CHECK-NOT: @__safestack_unsafe_stack_ptr
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)
167 declare void @readonly(i8* nocapture) readonly
168 declare void @arg_readonly(i8* readonly nocapture)
169 declare void @readwrite(i8* nocapture)
170 declare void @capture(i8* readnone) readnone
172 declare void @readnone(i8* nocapture) readnone
173 declare void @readnone0(i8* nocapture readnone, i8* nocapture)
175 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i1) nounwind argmemonly
177 declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) nounwind argmemonly
178 declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) nounwind argmemonly