1; Test basic address sanitizer instrumentation.
2;
3
4; RUN: opt < %s -passes='asan-pipeline' -S | FileCheck --check-prefixes=CHECK,CHECK-S3 %s
5; RUN: opt < %s -passes='asan-pipeline' -asan-mapping-scale=5 -S | FileCheck --check-prefixes=CHECK,CHECK-S5 %s
6
7target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
8target triple = "x86_64-unknown-linux-gnu"
9; CHECK: @llvm.used = appending global [1 x i8*] [i8* bitcast (void ()* @asan.module_ctor to i8*)]
10; CHECK: @llvm.global_ctors = {{.*}}{ i32 1, void ()* @asan.module_ctor, i8* bitcast (void ()* @asan.module_ctor to i8*) }
11
12define i32 @test_load(i32* %a) sanitize_address {
13; CHECK-LABEL: @test_load
14; CHECK-NOT: load
15; CHECK:   %[[LOAD_ADDR:[^ ]*]] = ptrtoint i32* %a to i64
16; CHECK-S3:   lshr i64 %[[LOAD_ADDR]], 3
17; CHECK-S5:   lshr i64 %[[LOAD_ADDR]], 5
18; CHECK:   {{or|add}}
19; CHECK:   %[[LOAD_SHADOW_PTR:[^ ]*]] = inttoptr
20; CHECK:   %[[LOAD_SHADOW:[^ ]*]] = load i8, i8* %[[LOAD_SHADOW_PTR]]
21; CHECK:   icmp ne i8
22; CHECK:   br i1 %{{.*}}, label %{{.*}}, label %{{.*}}!prof ![[PROF:[0-9]+]]
23;
24; First instrumentation block refines the shadow test.
25; CHECK-S3:   and i64 %[[LOAD_ADDR]], 7
26; CHECK-S5:   and i64 %[[LOAD_ADDR]], 31
27; CHECK:   add i64 %{{.*}}, 3
28; CHECK:   trunc i64 %{{.*}} to i8
29; CHECK:   icmp sge i8 %{{.*}}, %[[LOAD_SHADOW]]
30; CHECK:   br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
31;
32; The crash block reports the error.
33; CHECK:   call void @__asan_report_load4(i64 %[[LOAD_ADDR]])
34; CHECK:   unreachable
35;
36; The actual load.
37; CHECK:   %tmp1 = load i32, i32* %a
38; CHECK:   ret i32 %tmp1
39
40
41
42entry:
43  %tmp1 = load i32, i32* %a, align 4
44  ret i32 %tmp1
45}
46
47define void @test_store(i32* %a) sanitize_address {
48; CHECK-LABEL: @test_store
49; CHECK-NOT: store
50; CHECK:   %[[STORE_ADDR:[^ ]*]] = ptrtoint i32* %a to i64
51; CHECK-S3:   lshr i64 %[[STORE_ADDR]], 3
52; CHECK-S5:   lshr i64 %[[STORE_ADDR]], 5
53; CHECK:   {{or|add}}
54; CHECK:   %[[STORE_SHADOW_PTR:[^ ]*]] = inttoptr
55; CHECK:   %[[STORE_SHADOW:[^ ]*]] = load i8, i8* %[[STORE_SHADOW_PTR]]
56; CHECK:   icmp ne i8
57; CHECK:   br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
58;
59; First instrumentation block refines the shadow test.
60; CHECK-S3:   and i64 %[[STORE_ADDR]], 7
61; CHECK-S5:   and i64 %[[STORE_ADDR]], 31
62; CHECK:   add i64 %{{.*}}, 3
63; CHECK:   trunc i64 %{{.*}} to i8
64; CHECK:   icmp sge i8 %{{.*}}, %[[STORE_SHADOW]]
65; CHECK:   br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
66;
67; The crash block reports the error.
68; CHECK:   call void @__asan_report_store4(i64 %[[STORE_ADDR]])
69; CHECK:   unreachable
70;
71; The actual load.
72; CHECK:   store i32 42, i32* %a
73; CHECK:   ret void
74;
75
76entry:
77  store i32 42, i32* %a, align 4
78  ret void
79}
80
81; Check that asan leaves just one alloca.
82
83declare void @alloca_test_use([10 x i8]*)
84define void @alloca_test() sanitize_address {
85entry:
86  %x = alloca [10 x i8], align 1
87  %y = alloca [10 x i8], align 1
88  %z = alloca [10 x i8], align 1
89  call void @alloca_test_use([10 x i8]* %x)
90  call void @alloca_test_use([10 x i8]* %y)
91  call void @alloca_test_use([10 x i8]* %z)
92  ret void
93}
94
95; CHECK-LABEL: define void @alloca_test()
96; CHECK: %asan_local_stack_base = alloca
97; CHECK: = alloca
98; CHECK-NOT: = alloca
99; CHECK: ret void
100
101define void @LongDoubleTest(x86_fp80* nocapture %a) nounwind uwtable sanitize_address {
102entry:
103    store x86_fp80 0xK3FFF8000000000000000, x86_fp80* %a, align 16
104    ret void
105}
106
107; CHECK-LABEL: LongDoubleTest
108; CHECK: __asan_report_store_n
109; CHECK: __asan_report_store_n
110; CHECK: ret void
111
112
113define void @i40test(i40* %a, i40* %b) nounwind uwtable sanitize_address {
114  entry:
115  %t = load i40, i40* %a
116  store i40 %t, i40* %b, align 8
117  ret void
118}
119
120; CHECK-LABEL: i40test
121; CHECK: __asan_report_load_n{{.*}}, i64 5)
122; CHECK: __asan_report_load_n{{.*}}, i64 5)
123; CHECK: __asan_report_store_n{{.*}}, i64 5)
124; CHECK: __asan_report_store_n{{.*}}, i64 5)
125; CHECK: ret void
126
127define void @i64test_align1(i64* %b) nounwind uwtable sanitize_address {
128  entry:
129  store i64 0, i64* %b, align 1
130  ret void
131}
132
133; CHECK-LABEL: i64test_align1
134; CHECK: __asan_report_store_n{{.*}}, i64 8)
135; CHECK: __asan_report_store_n{{.*}}, i64 8)
136; CHECK: ret void
137
138
139define void @i80test(i80* %a, i80* %b) nounwind uwtable sanitize_address {
140  entry:
141  %t = load i80, i80* %a
142  store i80 %t, i80* %b, align 8
143  ret void
144}
145
146; CHECK-LABEL: i80test
147; CHECK: __asan_report_load_n{{.*}}, i64 10)
148; CHECK: __asan_report_load_n{{.*}}, i64 10)
149; CHECK: __asan_report_store_n{{.*}}, i64 10)
150; CHECK: __asan_report_store_n{{.*}}, i64 10)
151; CHECK: ret void
152
153; asan should not instrument functions with available_externally linkage.
154define available_externally i32 @f_available_externally(i32* %a) sanitize_address  {
155entry:
156  %tmp1 = load i32, i32* %a
157  ret i32 %tmp1
158}
159; CHECK-LABEL: @f_available_externally
160; CHECK-NOT: __asan_report
161; CHECK: ret i32
162
163
164; CHECK-LABEL: @test_swifterror
165; CHECK-NOT: __asan_report_load
166; CHECK: ret void
167define void @test_swifterror(i8** swifterror) sanitize_address {
168  %swifterror_ptr_value = load i8*, i8** %0
169  ret void
170}
171
172; CHECK-LABEL: @test_swifterror_2
173; CHECK-NOT: __asan_report_store
174; CHECK: ret void
175define void @test_swifterror_2(i8** swifterror) sanitize_address {
176  store i8* null, i8** %0
177  ret void
178}
179
180; CHECK-LABEL: @test_swifterror_3
181; CHECK-NOT: __asan_report_store
182; CHECK: ret void
183define void @test_swifterror_3() sanitize_address {
184  %swifterror_addr = alloca swifterror i8*
185  store i8* null, i8** %swifterror_addr
186  call void @test_swifterror_2(i8** swifterror %swifterror_addr)
187  ret void
188}
189
190;; ctor/dtor have the nounwind attribute. See uwtable.ll, they additionally have
191;; the uwtable attribute with the module flag "uwtable".
192; CHECK: define internal void @asan.module_ctor() #[[#ATTR:]] {{(comdat )?}}{
193; CHECK: call void @__asan_init()
194
195; CHECK: attributes #[[#ATTR]] = { nounwind }
196
197; PROF
198; CHECK: ![[PROF]] = !{!"branch_weights", i32 1, i32 100000}
199