[msan] Heuristically instrument unknown intrinsics.
[oota-llvm.git] / test / Instrumentation / MemorySanitizer / msan_basic.ll
1 ; RUN: opt < %s -msan -msan-check-access-address=0 -S | FileCheck %s
2 ; RUN: opt < %s -msan -msan-check-access-address=0 -msan-track-origins=1 -S | FileCheck -check-prefix=CHECK-ORIGINS %s
3 target 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-S128"
4
5 ; Check the presence of __msan_init
6 ; CHECK: @llvm.global_ctors {{.*}} @__msan_init
7
8 ; Check the presence and the linkage type of __msan_track_origins
9 ; CHECK: @__msan_track_origins = weak_odr constant i32 0
10
11
12 ; Check instrumentation of stores
13
14 define void @Store(i32* nocapture %p, i32 %x) nounwind uwtable {
15 entry:
16   store i32 %x, i32* %p, align 4
17   ret void
18 }
19
20 ; CHECK: @Store
21 ; CHECK: load {{.*}} @__msan_param_tls
22 ; CHECK: store
23 ; CHECK: store
24 ; CHECK: ret void
25 ; CHECK-ORIGINS: @Store
26 ; CHECK-ORIGINS: load {{.*}} @__msan_param_tls
27 ; CHECK-ORIGINS: store
28 ; CHECK-ORIGINS: icmp
29 ; CHECK-ORIGINS: br i1
30 ; CHECK-ORIGINS: <label>
31 ; CHECK-ORIGINS: store
32 ; CHECK-ORIGINS: br label
33 ; CHECK-ORIGINS: <label>
34 ; CHECK-ORIGINS: store
35 ; CHECK-ORIGINS: ret void
36
37
38 ; Check instrumentation of aligned stores
39 ; Shadow store has the same alignment as the original store; origin store
40 ; does not specify explicit alignment.
41
42 define void @AlignedStore(i32* nocapture %p, i32 %x) nounwind uwtable {
43 entry:
44   store i32 %x, i32* %p, align 32
45   ret void
46 }
47
48 ; CHECK: @AlignedStore
49 ; CHECK: load {{.*}} @__msan_param_tls
50 ; CHECK: store {{.*}} align 32
51 ; CHECK: store {{.*}} align 32
52 ; CHECK: ret void
53 ; CHECK-ORIGINS: @AlignedStore
54 ; CHECK-ORIGINS: load {{.*}} @__msan_param_tls
55 ; CHECK-ORIGINS: store {{.*}} align 32
56 ; CHECK-ORIGINS: icmp
57 ; CHECK-ORIGINS: br i1
58 ; CHECK-ORIGINS: <label>
59 ; CHECK-ORIGINS-NOT: store {{.*}} align
60 ; CHECK-ORIGINS: br label
61 ; CHECK-ORIGINS: <label>
62 ; CHECK-ORIGINS: store {{.*}} align 32
63 ; CHECK-ORIGINS: ret void
64
65
66 ; load followed by cmp: check that we load the shadow and call __msan_warning.
67 define void @LoadAndCmp(i32* nocapture %a) nounwind uwtable {
68 entry:
69   %0 = load i32* %a, align 4
70   %tobool = icmp eq i32 %0, 0
71   br i1 %tobool, label %if.end, label %if.then
72
73 if.then:                                          ; preds = %entry
74   tail call void (...)* @foo() nounwind
75   br label %if.end
76
77 if.end:                                           ; preds = %entry, %if.then
78   ret void
79 }
80
81 declare void @foo(...)
82
83 ; CHECK: @LoadAndCmp
84 ; CHECK: = load
85 ; CHECK: = load
86 ; CHECK: call void @__msan_warning_noreturn()
87 ; CHECK-NEXT: call void asm sideeffect
88 ; CHECK-NEXT: unreachable
89 ; CHECK: ret void
90
91 ; Check that we store the shadow for the retval.
92 define i32 @ReturnInt() nounwind uwtable readnone {
93 entry:
94   ret i32 123
95 }
96
97 ; CHECK: @ReturnInt
98 ; CHECK: store i32 0,{{.*}}__msan_retval_tls
99 ; CHECK: ret i32
100
101 ; Check that we get the shadow for the retval.
102 define void @CopyRetVal(i32* nocapture %a) nounwind uwtable {
103 entry:
104   %call = tail call i32 @ReturnInt() nounwind
105   store i32 %call, i32* %a, align 4
106   ret void
107 }
108
109 ; CHECK: @CopyRetVal
110 ; CHECK: load{{.*}}__msan_retval_tls
111 ; CHECK: store
112 ; CHECK: store
113 ; CHECK: ret void
114
115
116 ; Check that we generate PHIs for shadow.
117 define void @FuncWithPhi(i32* nocapture %a, i32* %b, i32* nocapture %c) nounwind uwtable {
118 entry:
119   %tobool = icmp eq i32* %b, null
120   br i1 %tobool, label %if.else, label %if.then
121
122   if.then:                                          ; preds = %entry
123   %0 = load i32* %b, align 4
124   br label %if.end
125
126   if.else:                                          ; preds = %entry
127   %1 = load i32* %c, align 4
128   br label %if.end
129
130   if.end:                                           ; preds = %if.else, %if.then
131   %t.0 = phi i32 [ %0, %if.then ], [ %1, %if.else ]
132   store i32 %t.0, i32* %a, align 4
133   ret void
134 }
135
136 ; CHECK: @FuncWithPhi
137 ; CHECK: = phi
138 ; CHECK-NEXT: = phi
139 ; CHECK: store
140 ; CHECK: store
141 ; CHECK: ret void
142
143 ; Compute shadow for "x << 10"
144 define void @ShlConst(i32* nocapture %x) nounwind uwtable {
145 entry:
146   %0 = load i32* %x, align 4
147   %1 = shl i32 %0, 10
148   store i32 %1, i32* %x, align 4
149   ret void
150 }
151
152 ; CHECK: @ShlConst
153 ; CHECK: = load
154 ; CHECK: = load
155 ; CHECK: shl
156 ; CHECK: shl
157 ; CHECK: store
158 ; CHECK: store
159 ; CHECK: ret void
160
161 ; Compute shadow for "10 << x": it should have 'sext i1'.
162 define void @ShlNonConst(i32* nocapture %x) nounwind uwtable {
163 entry:
164   %0 = load i32* %x, align 4
165   %1 = shl i32 10, %0
166   store i32 %1, i32* %x, align 4
167   ret void
168 }
169
170 ; CHECK: @ShlNonConst
171 ; CHECK: = load
172 ; CHECK: = load
173 ; CHECK: = sext i1
174 ; CHECK: store
175 ; CHECK: store
176 ; CHECK: ret void
177
178 ; SExt
179 define void @SExt(i32* nocapture %a, i16* nocapture %b) nounwind uwtable {
180 entry:
181   %0 = load i16* %b, align 2
182   %1 = sext i16 %0 to i32
183   store i32 %1, i32* %a, align 4
184   ret void
185 }
186
187 ; CHECK: @SExt
188 ; CHECK: = load
189 ; CHECK: = load
190 ; CHECK: = sext
191 ; CHECK: = sext
192 ; CHECK: store
193 ; CHECK: store
194 ; CHECK: ret void
195
196
197 ; memset
198 define void @MemSet(i8* nocapture %x) nounwind uwtable {
199 entry:
200   call void @llvm.memset.p0i8.i64(i8* %x, i8 42, i64 10, i32 1, i1 false)
201   ret void
202 }
203
204 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
205
206 ; CHECK: @MemSet
207 ; CHECK: call i8* @__msan_memset
208 ; CHECK: ret void
209
210
211 ; memcpy
212 define void @MemCpy(i8* nocapture %x, i8* nocapture %y) nounwind uwtable {
213 entry:
214   call void @llvm.memcpy.p0i8.p0i8.i64(i8* %x, i8* %y, i64 10, i32 1, i1 false)
215   ret void
216 }
217
218 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
219
220 ; CHECK: @MemCpy
221 ; CHECK: call i8* @__msan_memcpy
222 ; CHECK: ret void
223
224
225 ; memmove is lowered to a call
226 define void @MemMove(i8* nocapture %x, i8* nocapture %y) nounwind uwtable {
227 entry:
228   call void @llvm.memmove.p0i8.p0i8.i64(i8* %x, i8* %y, i64 10, i32 1, i1 false)
229   ret void
230 }
231
232 declare void @llvm.memmove.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
233
234 ; CHECK: @MemMove
235 ; CHECK: call i8* @__msan_memmove
236 ; CHECK: ret void
237
238
239 ; Check that we propagate shadow for "select"
240
241 define i32 @Select(i32 %a, i32 %b, i32 %c) nounwind uwtable readnone {
242 entry:
243   %tobool = icmp ne i32 %c, 0
244   %cond = select i1 %tobool, i32 %a, i32 %b
245   ret i32 %cond
246 }
247
248 ; CHECK: @Select
249 ; CHECK: select
250 ; CHECK-NEXT: select
251 ; CHECK: ret i32
252
253
254 define i8* @IntToPtr(i64 %x) nounwind uwtable readnone {
255 entry:
256   %0 = inttoptr i64 %x to i8*
257   ret i8* %0
258 }
259
260 ; CHECK: @IntToPtr
261 ; CHECK: load i64*{{.*}}__msan_param_tls
262 ; CHECK-NEXT: inttoptr
263 ; CHECK-NEXT: store i64{{.*}}__msan_retval_tls
264 ; CHECK: ret i8
265
266
267 define i8* @IntToPtr_ZExt(i16 %x) nounwind uwtable readnone {
268 entry:
269   %0 = inttoptr i16 %x to i8*
270   ret i8* %0
271 }
272
273 ; CHECK: @IntToPtr_ZExt
274 ; CHECK: zext
275 ; CHECK-NEXT: inttoptr
276 ; CHECK: ret i8
277
278
279 ; Check that we insert exactly one check on udiv
280 ; (2nd arg shadow is checked, 1st arg shadow is propagated)
281
282 define i32 @Div(i32 %a, i32 %b) nounwind uwtable readnone {
283 entry:
284   %div = udiv i32 %a, %b
285   ret i32 %div
286 }
287
288 ; CHECK: @Div
289 ; CHECK: icmp
290 ; CHECK: call void @__msan_warning
291 ; CHECK-NOT: icmp
292 ; CHECK: udiv
293 ; CHECK-NOT: icmp
294 ; CHECK: ret i32
295
296
297 ; Check that we propagate shadow for x<0, x>=0, etc (i.e. sign bit tests)
298
299 define zeroext i1 @ICmpSLT(i32 %x) nounwind uwtable readnone {
300   %1 = icmp slt i32 %x, 0
301   ret i1 %1
302 }
303
304 ; CHECK: @ICmpSLT
305 ; CHECK: icmp slt
306 ; CHECK-NOT: call void @__msan_warning
307 ; CHECK: icmp slt
308 ; CHECK-NOT: call void @__msan_warning
309 ; CHECK: ret i1
310
311 define zeroext i1 @ICmpSGE(i32 %x) nounwind uwtable readnone {
312   %1 = icmp sge i32 %x, 0
313   ret i1 %1
314 }
315
316 ; CHECK: @ICmpSGE
317 ; CHECK: icmp slt
318 ; CHECK-NOT: call void @__msan_warning
319 ; CHECK: icmp sge
320 ; CHECK-NOT: call void @__msan_warning
321 ; CHECK: ret i1
322
323 define zeroext i1 @ICmpSGT(i32 %x) nounwind uwtable readnone {
324   %1 = icmp sgt i32 0, %x
325   ret i1 %1
326 }
327
328 ; CHECK: @ICmpSGT
329 ; CHECK: icmp slt
330 ; CHECK-NOT: call void @__msan_warning
331 ; CHECK: icmp sgt
332 ; CHECK-NOT: call void @__msan_warning
333 ; CHECK: ret i1
334
335 define zeroext i1 @ICmpSLE(i32 %x) nounwind uwtable readnone {
336   %1 = icmp sle i32 0, %x
337   ret i1 %1
338 }
339
340 ; CHECK: @ICmpSLE
341 ; CHECK: icmp slt
342 ; CHECK-NOT: call void @__msan_warning
343 ; CHECK: icmp sle
344 ; CHECK-NOT: call void @__msan_warning
345 ; CHECK: ret i1
346
347
348 ; Check that loads from shadow have the same aligment as the original loads.
349
350 define i32 @ShadowLoadAlignmentLarge() nounwind uwtable {
351   %y = alloca i32, align 64
352   %1 = load volatile i32* %y, align 64
353   ret i32 %1
354 }
355
356 ; CHECK: @ShadowLoadAlignmentLarge
357 ; CHECK: load i32* {{.*}} align 64
358 ; CHECK: load volatile i32* {{.*}} align 64
359 ; CHECK: ret i32
360
361 define i32 @ShadowLoadAlignmentSmall() nounwind uwtable {
362   %y = alloca i32, align 2
363   %1 = load volatile i32* %y, align 2
364   ret i32 %1
365 }
366
367 ; CHECK: @ShadowLoadAlignmentSmall
368 ; CHECK: load i32* {{.*}} align 2
369 ; CHECK: load volatile i32* {{.*}} align 2
370 ; CHECK: ret i32
371
372
373 ; Test vector manipulation instructions.
374 ; Check that the same bit manipulation is applied to the shadow values.
375 ; Check that there is a zero test of the shadow of %idx argument, where present.
376
377 define i32 @ExtractElement(<4 x i32> %vec, i32 %idx) {
378   %x = extractelement <4 x i32> %vec, i32 %idx
379   ret i32 %x
380 }
381
382 ; CHECK: @ExtractElement
383 ; CHECK: extractelement
384 ; CHECK: call void @__msan_warning
385 ; CHECK: extractelement
386 ; CHECK: ret i32
387
388 define <4 x i32> @InsertElement(<4 x i32> %vec, i32 %idx, i32 %x) {
389   %vec1 = insertelement <4 x i32> %vec, i32 %x, i32 %idx
390   ret <4 x i32> %vec1
391 }
392
393 ; CHECK: @InsertElement
394 ; CHECK: insertelement
395 ; CHECK: call void @__msan_warning
396 ; CHECK: insertelement
397 ; CHECK: ret <4 x i32>
398
399 define <4 x i32> @ShuffleVector(<4 x i32> %vec, <4 x i32> %vec1) {
400   %vec2 = shufflevector <4 x i32> %vec, <4 x i32> %vec1,
401                         <4 x i32> <i32 0, i32 4, i32 1, i32 5>
402   ret <4 x i32> %vec2
403 }
404
405 ; CHECK: @ShuffleVector
406 ; CHECK: shufflevector
407 ; CHECK-NOT: call void @__msan_warning
408 ; CHECK: shufflevector
409 ; CHECK: ret <4 x i32>
410
411
412 ; Test bswap intrinsic instrumentation
413 define i32 @BSwap(i32 %x) nounwind uwtable readnone {
414   %y = tail call i32 @llvm.bswap.i32(i32 %x)
415   ret i32 %y
416 }
417
418 declare i32 @llvm.bswap.i32(i32) nounwind readnone
419
420 ; CHECK: @BSwap
421 ; CHECK-NOT: call void @__msan_warning
422 ; CHECK: @llvm.bswap.i32
423 ; CHECK-NOT: call void @__msan_warning
424 ; CHECK: @llvm.bswap.i32
425 ; CHECK-NOT: call void @__msan_warning
426 ; CHECK: ret i32
427
428
429 ; Store intrinsic.
430
431 define void @StoreIntrinsic(i8* %p, <4 x float> %x) nounwind uwtable {
432   call void @llvm.x86.sse.storeu.ps(i8* %p, <4 x float> %x)
433   ret void
434 }
435
436 declare void @llvm.x86.sse.storeu.ps(i8*, <4 x float>) nounwind
437
438 ; CHECK: @StoreIntrinsic
439 ; CHECK-NOT: br
440 ; CHECK-NOT: = or
441 ; CHECK: store <4 x i32> {{.*}} align 1
442 ; CHECK: call void @llvm.x86.sse.storeu.ps
443 ; CHECK: ret void
444
445
446 ; Load intrinsic.
447
448 define <16 x i8> @LoadIntrinsic(i8* %p) nounwind uwtable {
449   %call = call <16 x i8> @llvm.x86.sse3.ldu.dq(i8* %p)
450   ret <16 x i8> %call
451 }
452
453 declare <16 x i8> @llvm.x86.sse3.ldu.dq(i8* %p) nounwind
454
455 ; CHECK: @LoadIntrinsic
456 ; CHECK: load <16 x i8>* {{.*}} align 1
457 ; CHECK-NOT: br
458 ; CHECK-NOT: = or
459 ; CHECK: call <16 x i8> @llvm.x86.sse3.ldu.dq
460 ; CHECK: store <16 x i8> {{.*}} @__msan_retval_tls
461 ; CHECK: ret <16 x i8>
462
463 ; CHECK-ORIGINS: @LoadIntrinsic
464 ; CHECK-ORIGINS: [[ORIGIN:%[01-9a-z]+]] = load i32* {{.*}}
465 ; CHECK-ORIGINS: call <16 x i8> @llvm.x86.sse3.ldu.dq
466 ; CHECK-ORIGINS: store i32 {{.*}}[[ORIGIN]], i32* @__msan_retval_origin_tls
467 ; CHECK-ORIGINS: ret <16 x i8>
468
469
470 ; Simple NoMem intrinsic
471 ; Check that shadow is OR'ed, and origin is Select'ed
472 ; And no shadow checks!
473
474 define <8 x i16> @Paddsw128(<8 x i16> %a, <8 x i16> %b) nounwind uwtable {
475   %call = call <8 x i16> @llvm.x86.sse2.padds.w(<8 x i16> %a, <8 x i16> %b)
476   ret <8 x i16> %call
477 }
478
479 declare <8 x i16> @llvm.x86.sse2.padds.w(<8 x i16> %a, <8 x i16> %b) nounwind
480
481 ; CHECK: @Paddsw128
482 ; CHECK-NEXT: load <8 x i16>* {{.*}} @__msan_param_tls
483 ; CHECK-NEXT: load <8 x i16>* {{.*}} @__msan_param_tls
484 ; CHECK-NEXT: = or <8 x i16>
485 ; CHECK-NEXT: call <8 x i16> @llvm.x86.sse2.padds.w
486 ; CHECK-NEXT: store <8 x i16> {{.*}} @__msan_retval_tls
487 ; CHECK-NEXT: ret <8 x i16>
488
489 ; CHECK-ORIGINS: @Paddsw128
490 ; CHECK-ORIGINS: load i32* {{.*}} @__msan_param_origin_tls
491 ; CHECK-ORIGINS: load i32* {{.*}} @__msan_param_origin_tls
492 ; CHECK-ORIGINS: = bitcast <8 x i16> {{.*}} to i128
493 ; CHECK-ORIGINS-NEXT: = icmp ne i128 {{.*}}, 0
494 ; CHECK-ORIGINS-NEXT: = select i1 {{.*}}, i32 {{.*}}, i32
495 ; CHECK-ORIGINS: call <8 x i16> @llvm.x86.sse2.padds.w
496 ; CHECK-ORIGINS: store i32 {{.*}} @__msan_retval_origin_tls
497 ; CHECK-ORIGINS: ret <8 x i16>