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 ; RUN: opt -passes=safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
4 ; RUN: opt -passes=safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
6 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
7 target triple = "x86_64-unknown-linux-gnu"
9 @.str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
11 ; no arrays / no nested arrays
12 ; Requires no protector.
14 define void @foo(ptr %a) nounwind uwtable safestack {
16 ; CHECK-LABEL: define void @foo(
17 ; CHECK-NOT: __safestack_unsafe_stack_ptr
19 %a.addr = alloca ptr, align 8
20 store ptr %a, ptr %a.addr, align 8
21 %0 = load ptr, ptr %a.addr, align 8
22 %call = call i32 (ptr, ...) @printf(ptr @.str, ptr %0)
26 declare i32 @printf(ptr, ...)
28 define void @call_memset(i64 %len) safestack {
30 ; CHECK-LABEL: define void @call_memset
31 ; CHECK: @__safestack_unsafe_stack_ptr
33 %q = alloca [10 x i8], align 1
34 call void @llvm.memset.p0.i64(ptr %q, i8 1, i64 %len, i1 false)
38 define void @call_constant_memset() safestack {
40 ; CHECK-LABEL: define void @call_constant_memset
41 ; CHECK-NOT: @__safestack_unsafe_stack_ptr
43 %q = alloca [10 x i8], align 1
44 %arraydecay = getelementptr inbounds [10 x i8], ptr %q, i32 0, i32 2
45 call void @llvm.memset.p0.i64(ptr %arraydecay, i8 1, i64 7, i1 false)
49 define void @call_constant_overflow_memset() safestack {
51 ; CHECK-LABEL: define void @call_constant_overflow_memset
52 ; CHECK: @__safestack_unsafe_stack_ptr
54 %q = alloca [10 x i8], align 1
55 %arraydecay = getelementptr inbounds [10 x i8], ptr %q, i32 0, i32 7
56 call void @llvm.memset.p0.i64(ptr %arraydecay, i8 1, i64 5, i1 false)
60 define void @call_constant_underflow_memset() safestack {
62 ; CHECK-LABEL: define void @call_constant_underflow_memset
63 ; CHECK: @__safestack_unsafe_stack_ptr
65 %q = alloca [10 x i8], align 1
66 %arraydecay = getelementptr [10 x i8], ptr %q, i32 0, i32 -1
67 call void @llvm.memset.p0.i64(ptr %arraydecay, i8 1, i64 3, i1 false)
71 ; Readnone nocapture -> safe
72 define void @call_readnone(i64 %len) safestack {
74 ; CHECK-LABEL: define void @call_readnone
75 ; CHECK-NOT: @__safestack_unsafe_stack_ptr
77 %q = alloca [10 x i8], align 1
78 call void @readnone(ptr %q)
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 call void @readnone0(ptr %q, ptr zeroinitializer)
93 ; Arg0 is readnone, arg1 is not. Pass alloca ptr as arg1 -> unsafe
94 define void @call_readnone0_1(i64 %len) safestack {
96 ; CHECK-LABEL: define void @call_readnone0_1
97 ; CHECK: @__safestack_unsafe_stack_ptr
99 %q = alloca [10 x i8], align 1
100 call void @readnone0(ptr zeroinitializer, ptr %q)
104 ; Readonly nocapture -> unsafe
105 define void @call_readonly(i64 %len) safestack {
107 ; CHECK-LABEL: define void @call_readonly
108 ; CHECK: @__safestack_unsafe_stack_ptr
110 %q = alloca [10 x i8], align 1
111 call void @readonly(ptr %q)
115 ; Readonly nocapture -> unsafe
116 define void @call_arg_readonly(i64 %len) safestack {
118 ; CHECK-LABEL: define void @call_arg_readonly
119 ; CHECK: @__safestack_unsafe_stack_ptr
121 %q = alloca [10 x i8], align 1
122 call void @arg_readonly(ptr %q)
126 ; Readwrite nocapture -> unsafe
127 define void @call_readwrite(i64 %len) safestack {
129 ; CHECK-LABEL: define void @call_readwrite
130 ; CHECK: @__safestack_unsafe_stack_ptr
132 %q = alloca [10 x i8], align 1
133 call void @readwrite(ptr %q)
137 ; Captures the argument -> unsafe
138 define void @call_capture(i64 %len) safestack {
140 ; CHECK-LABEL: define void @call_capture
141 ; CHECK: @__safestack_unsafe_stack_ptr
143 %q = alloca [10 x i8], align 1
144 call void @capture(ptr %q)
148 ; Lifetime intrinsics are always safe.
149 define void @call_lifetime(ptr %p) {
150 ; CHECK-LABEL: define void @call_lifetime
151 ; CHECK-NOT: @__safestack_unsafe_stack_ptr
154 %q = alloca [100 x i8], align 16
155 call void @llvm.lifetime.start.p0(i64 100, ptr %q)
156 call void @llvm.lifetime.end.p0(i64 100, ptr %q)
160 declare void @readonly(ptr nocapture) readonly
161 declare void @arg_readonly(ptr readonly nocapture)
162 declare void @readwrite(ptr nocapture)
163 declare void @capture(ptr readnone) readnone
165 declare void @readnone(ptr nocapture) readnone
166 declare void @readnone0(ptr nocapture readnone, ptr nocapture)
168 declare void @llvm.memset.p0.i64(ptr nocapture, i8, i64, i1) nounwind argmemonly
170 declare void @llvm.lifetime.start.p0(i64, ptr nocapture) nounwind argmemonly
171 declare void @llvm.lifetime.end.p0(i64, ptr nocapture) nounwind argmemonly