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 ; RUN: opt < %s -msan -msan-check-access-address=1 -S | FileCheck %s -check-prefix=CHECK-AA
5 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"
6 target triple = "x86_64-unknown-linux-gnu"
8 ; Check the presence of __msan_init
9 ; CHECK: @llvm.global_ctors {{.*}} @__msan_init
11 ; Check the presence and the linkage type of __msan_track_origins and
12 ; other interface symbols.
13 ; CHECK: @__msan_track_origins = weak_odr constant i32 0
14 ; CHECK: @__msan_keep_going = weak_odr constant i32 0
15 ; CHECK: @__msan_retval_tls = external thread_local(initialexec) global [{{.*}}]
16 ; CHECK: @__msan_retval_origin_tls = external thread_local(initialexec) global i32
17 ; CHECK: @__msan_param_tls = external thread_local(initialexec) global [{{.*}}]
18 ; CHECK: @__msan_param_origin_tls = external thread_local(initialexec) global [{{.*}}]
19 ; CHECK: @__msan_va_arg_tls = external thread_local(initialexec) global [{{.*}}]
20 ; CHECK: @__msan_va_arg_overflow_size_tls = external thread_local(initialexec) global i64
21 ; CHECK: @__msan_origin_tls = external thread_local(initialexec) global i32
24 ; Check instrumentation of stores
26 define void @Store(i32* nocapture %p, i32 %x) nounwind uwtable sanitize_memory {
28 store i32 %x, i32* %p, align 4
33 ; CHECK: load {{.*}} @__msan_param_tls
37 ; CHECK-ORIGINS: @Store
38 ; CHECK-ORIGINS: load {{.*}} @__msan_param_tls
39 ; CHECK-ORIGINS: store
41 ; CHECK-ORIGINS: br i1
42 ; CHECK-ORIGINS: <label>
43 ; CHECK-ORIGINS: store
44 ; CHECK-ORIGINS: br label
45 ; CHECK-ORIGINS: <label>
46 ; CHECK-ORIGINS: store
47 ; CHECK-ORIGINS: ret void
50 ; Check instrumentation of aligned stores
51 ; Shadow store has the same alignment as the original store; origin store
52 ; does not specify explicit alignment.
54 define void @AlignedStore(i32* nocapture %p, i32 %x) nounwind uwtable sanitize_memory {
56 store i32 %x, i32* %p, align 32
60 ; CHECK: @AlignedStore
61 ; CHECK: load {{.*}} @__msan_param_tls
62 ; CHECK: store {{.*}} align 32
63 ; CHECK: store {{.*}} align 32
65 ; CHECK-ORIGINS: @AlignedStore
66 ; CHECK-ORIGINS: load {{.*}} @__msan_param_tls
67 ; CHECK-ORIGINS: store {{.*}} align 32
69 ; CHECK-ORIGINS: br i1
70 ; CHECK-ORIGINS: <label>
71 ; CHECK-ORIGINS: store {{.*}} align 32
72 ; CHECK-ORIGINS: br label
73 ; CHECK-ORIGINS: <label>
74 ; CHECK-ORIGINS: store {{.*}} align 32
75 ; CHECK-ORIGINS: ret void
78 ; load followed by cmp: check that we load the shadow and call __msan_warning.
79 define void @LoadAndCmp(i32* nocapture %a) nounwind uwtable sanitize_memory {
81 %0 = load i32* %a, align 4
82 %tobool = icmp eq i32 %0, 0
83 br i1 %tobool, label %if.end, label %if.then
85 if.then: ; preds = %entry
86 tail call void (...)* @foo() nounwind
89 if.end: ; preds = %entry, %if.then
93 declare void @foo(...)
98 ; CHECK: call void @__msan_warning_noreturn()
99 ; CHECK-NEXT: call void asm sideeffect
100 ; CHECK-NEXT: unreachable
103 ; Check that we store the shadow for the retval.
104 define i32 @ReturnInt() nounwind uwtable readnone sanitize_memory {
110 ; CHECK: store i32 0,{{.*}}__msan_retval_tls
113 ; Check that we get the shadow for the retval.
114 define void @CopyRetVal(i32* nocapture %a) nounwind uwtable sanitize_memory {
116 %call = tail call i32 @ReturnInt() nounwind
117 store i32 %call, i32* %a, align 4
122 ; CHECK: load{{.*}}__msan_retval_tls
128 ; Check that we generate PHIs for shadow.
129 define void @FuncWithPhi(i32* nocapture %a, i32* %b, i32* nocapture %c) nounwind uwtable sanitize_memory {
131 %tobool = icmp eq i32* %b, null
132 br i1 %tobool, label %if.else, label %if.then
134 if.then: ; preds = %entry
135 %0 = load i32* %b, align 4
138 if.else: ; preds = %entry
139 %1 = load i32* %c, align 4
142 if.end: ; preds = %if.else, %if.then
143 %t.0 = phi i32 [ %0, %if.then ], [ %1, %if.else ]
144 store i32 %t.0, i32* %a, align 4
148 ; CHECK: @FuncWithPhi
155 ; Compute shadow for "x << 10"
156 define void @ShlConst(i32* nocapture %x) nounwind uwtable sanitize_memory {
158 %0 = load i32* %x, align 4
160 store i32 %1, i32* %x, align 4
173 ; Compute shadow for "10 << x": it should have 'sext i1'.
174 define void @ShlNonConst(i32* nocapture %x) nounwind uwtable sanitize_memory {
176 %0 = load i32* %x, align 4
178 store i32 %1, i32* %x, align 4
182 ; CHECK: @ShlNonConst
191 define void @SExt(i32* nocapture %a, i16* nocapture %b) nounwind uwtable sanitize_memory {
193 %0 = load i16* %b, align 2
194 %1 = sext i16 %0 to i32
195 store i32 %1, i32* %a, align 4
210 define void @MemSet(i8* nocapture %x) nounwind uwtable sanitize_memory {
212 call void @llvm.memset.p0i8.i64(i8* %x, i8 42, i64 10, i32 1, i1 false)
216 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
219 ; CHECK: call i8* @__msan_memset
224 define void @MemCpy(i8* nocapture %x, i8* nocapture %y) nounwind uwtable sanitize_memory {
226 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %x, i8* %y, i64 10, i32 1, i1 false)
230 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
233 ; CHECK: call i8* @__msan_memcpy
237 ; memmove is lowered to a call
238 define void @MemMove(i8* nocapture %x, i8* nocapture %y) nounwind uwtable sanitize_memory {
240 call void @llvm.memmove.p0i8.p0i8.i64(i8* %x, i8* %y, i64 10, i32 1, i1 false)
244 declare void @llvm.memmove.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
247 ; CHECK: call i8* @__msan_memmove
251 ; Check that we propagate shadow for "select"
253 define i32 @Select(i32 %a, i32 %b, i32 %c) nounwind uwtable readnone sanitize_memory {
255 %tobool = icmp ne i32 %c, 0
256 %cond = select i1 %tobool, i32 %a, i32 %b
266 ; Check that we propagate origin for "select" with vector condition.
267 ; Select condition is flattened to i1, which is then used to select one of the
270 define <8 x i16> @SelectVector(<8 x i16> %a, <8 x i16> %b, <8 x i1> %c) nounwind uwtable readnone sanitize_memory {
272 %cond = select <8 x i1> %c, <8 x i16> %a, <8 x i16> %b
276 ; CHECK-ORIGINS: @SelectVector
277 ; CHECK-ORIGINS: bitcast <8 x i1> {{.*}} to i8
278 ; CHECK-ORIGINS: icmp ne i8
279 ; CHECK-ORIGINS: select i1
280 ; CHECK-ORIGINS: ret <8 x i16>
283 define i8* @IntToPtr(i64 %x) nounwind uwtable readnone sanitize_memory {
285 %0 = inttoptr i64 %x to i8*
290 ; CHECK: load i64*{{.*}}__msan_param_tls
291 ; CHECK-NEXT: inttoptr
292 ; CHECK-NEXT: store i64{{.*}}__msan_retval_tls
296 define i8* @IntToPtr_ZExt(i16 %x) nounwind uwtable readnone sanitize_memory {
298 %0 = inttoptr i16 %x to i8*
302 ; CHECK: @IntToPtr_ZExt
304 ; CHECK-NEXT: inttoptr
308 ; Check that we insert exactly one check on udiv
309 ; (2nd arg shadow is checked, 1st arg shadow is propagated)
311 define i32 @Div(i32 %a, i32 %b) nounwind uwtable readnone sanitize_memory {
313 %div = udiv i32 %a, %b
319 ; CHECK: call void @__msan_warning
326 ; Check that we propagate shadow for x<0, x>=0, etc (i.e. sign bit tests)
328 define zeroext i1 @ICmpSLT(i32 %x) nounwind uwtable readnone sanitize_memory {
329 %1 = icmp slt i32 %x, 0
335 ; CHECK-NOT: call void @__msan_warning
337 ; CHECK-NOT: call void @__msan_warning
340 define zeroext i1 @ICmpSGE(i32 %x) nounwind uwtable readnone sanitize_memory {
341 %1 = icmp sge i32 %x, 0
347 ; CHECK-NOT: call void @__msan_warning
349 ; CHECK-NOT: call void @__msan_warning
352 define zeroext i1 @ICmpSGT(i32 %x) nounwind uwtable readnone sanitize_memory {
353 %1 = icmp sgt i32 0, %x
359 ; CHECK-NOT: call void @__msan_warning
361 ; CHECK-NOT: call void @__msan_warning
364 define zeroext i1 @ICmpSLE(i32 %x) nounwind uwtable readnone sanitize_memory {
365 %1 = icmp sle i32 0, %x
371 ; CHECK-NOT: call void @__msan_warning
373 ; CHECK-NOT: call void @__msan_warning
377 ; Check that we propagate shadow for x<0, x>=0, etc (i.e. sign bit tests)
378 ; of the vector arguments.
380 define <2 x i1> @ICmpSLT_vector(<2 x i32*> %x) nounwind uwtable readnone sanitize_memory {
381 %1 = icmp slt <2 x i32*> %x, zeroinitializer
385 ; CHECK: @ICmpSLT_vector
386 ; CHECK: icmp slt <2 x i64>
387 ; CHECK-NOT: call void @__msan_warning
388 ; CHECK: icmp slt <2 x i32*>
389 ; CHECK-NOT: call void @__msan_warning
390 ; CHECK: ret <2 x i1>
393 ; Check that we propagate shadow for unsigned relational comparisons with
396 define zeroext i1 @ICmpUGTConst(i32 %x) nounwind uwtable readnone sanitize_memory {
398 %cmp = icmp ugt i32 %x, 7
402 ; CHECK: @ICmpUGTConst
403 ; CHECK: icmp ugt i32
404 ; CHECK-NOT: call void @__msan_warning
405 ; CHECK: icmp ugt i32
406 ; CHECK-NOT: call void @__msan_warning
407 ; CHECK: icmp ugt i32
408 ; CHECK-NOT: call void @__msan_warning
412 ; Check that loads of shadow have the same aligment as the original loads.
413 ; Check that loads of origin have the aligment of max(4, original alignment).
415 define i32 @ShadowLoadAlignmentLarge() nounwind uwtable sanitize_memory {
416 %y = alloca i32, align 64
417 %1 = load volatile i32* %y, align 64
421 ; CHECK: @ShadowLoadAlignmentLarge
422 ; CHECK: load i32* {{.*}} align 64
423 ; CHECK: load volatile i32* {{.*}} align 64
426 define i32 @ShadowLoadAlignmentSmall() nounwind uwtable sanitize_memory {
427 %y = alloca i32, align 2
428 %1 = load volatile i32* %y, align 2
432 ; CHECK: @ShadowLoadAlignmentSmall
433 ; CHECK: load i32* {{.*}} align 2
434 ; CHECK: load volatile i32* {{.*}} align 2
437 ; CHECK-ORIGINS: @ShadowLoadAlignmentSmall
438 ; CHECK-ORIGINS: load i32* {{.*}} align 2
439 ; CHECK-ORIGINS: load i32* {{.*}} align 4
440 ; CHECK-ORIGINS: load volatile i32* {{.*}} align 2
441 ; CHECK-ORIGINS: ret i32
444 ; Test vector manipulation instructions.
445 ; Check that the same bit manipulation is applied to the shadow values.
446 ; Check that there is a zero test of the shadow of %idx argument, where present.
448 define i32 @ExtractElement(<4 x i32> %vec, i32 %idx) sanitize_memory {
449 %x = extractelement <4 x i32> %vec, i32 %idx
453 ; CHECK: @ExtractElement
454 ; CHECK: extractelement
455 ; CHECK: call void @__msan_warning
456 ; CHECK: extractelement
459 define <4 x i32> @InsertElement(<4 x i32> %vec, i32 %idx, i32 %x) sanitize_memory {
460 %vec1 = insertelement <4 x i32> %vec, i32 %x, i32 %idx
464 ; CHECK: @InsertElement
465 ; CHECK: insertelement
466 ; CHECK: call void @__msan_warning
467 ; CHECK: insertelement
468 ; CHECK: ret <4 x i32>
470 define <4 x i32> @ShuffleVector(<4 x i32> %vec, <4 x i32> %vec1) sanitize_memory {
471 %vec2 = shufflevector <4 x i32> %vec, <4 x i32> %vec1,
472 <4 x i32> <i32 0, i32 4, i32 1, i32 5>
476 ; CHECK: @ShuffleVector
477 ; CHECK: shufflevector
478 ; CHECK-NOT: call void @__msan_warning
479 ; CHECK: shufflevector
480 ; CHECK: ret <4 x i32>
483 ; Test bswap intrinsic instrumentation
484 define i32 @BSwap(i32 %x) nounwind uwtable readnone sanitize_memory {
485 %y = tail call i32 @llvm.bswap.i32(i32 %x)
489 declare i32 @llvm.bswap.i32(i32) nounwind readnone
492 ; CHECK-NOT: call void @__msan_warning
493 ; CHECK: @llvm.bswap.i32
494 ; CHECK-NOT: call void @__msan_warning
495 ; CHECK: @llvm.bswap.i32
496 ; CHECK-NOT: call void @__msan_warning
502 define void @StoreIntrinsic(i8* %p, <4 x float> %x) nounwind uwtable sanitize_memory {
503 call void @llvm.x86.sse.storeu.ps(i8* %p, <4 x float> %x)
507 declare void @llvm.x86.sse.storeu.ps(i8*, <4 x float>) nounwind
509 ; CHECK: @StoreIntrinsic
512 ; CHECK: store <4 x i32> {{.*}} align 1
513 ; CHECK: call void @llvm.x86.sse.storeu.ps
519 define <16 x i8> @LoadIntrinsic(i8* %p) nounwind uwtable sanitize_memory {
520 %call = call <16 x i8> @llvm.x86.sse3.ldu.dq(i8* %p)
524 declare <16 x i8> @llvm.x86.sse3.ldu.dq(i8* %p) nounwind
526 ; CHECK: @LoadIntrinsic
527 ; CHECK: load <16 x i8>* {{.*}} align 1
530 ; CHECK: call <16 x i8> @llvm.x86.sse3.ldu.dq
531 ; CHECK: store <16 x i8> {{.*}} @__msan_retval_tls
532 ; CHECK: ret <16 x i8>
534 ; CHECK-ORIGINS: @LoadIntrinsic
535 ; CHECK-ORIGINS: [[ORIGIN:%[01-9a-z]+]] = load i32* {{.*}}
536 ; CHECK-ORIGINS: call <16 x i8> @llvm.x86.sse3.ldu.dq
537 ; CHECK-ORIGINS: store i32 {{.*}}[[ORIGIN]], i32* @__msan_retval_origin_tls
538 ; CHECK-ORIGINS: ret <16 x i8>
541 ; Simple NoMem intrinsic
542 ; Check that shadow is OR'ed, and origin is Select'ed
543 ; And no shadow checks!
545 define <8 x i16> @Paddsw128(<8 x i16> %a, <8 x i16> %b) nounwind uwtable sanitize_memory {
546 %call = call <8 x i16> @llvm.x86.sse2.padds.w(<8 x i16> %a, <8 x i16> %b)
550 declare <8 x i16> @llvm.x86.sse2.padds.w(<8 x i16> %a, <8 x i16> %b) nounwind
553 ; CHECK-NEXT: load <8 x i16>* {{.*}} @__msan_param_tls
554 ; CHECK-NEXT: load <8 x i16>* {{.*}} @__msan_param_tls
555 ; CHECK-NEXT: = or <8 x i16>
556 ; CHECK-NEXT: call <8 x i16> @llvm.x86.sse2.padds.w
557 ; CHECK-NEXT: store <8 x i16> {{.*}} @__msan_retval_tls
558 ; CHECK-NEXT: ret <8 x i16>
560 ; CHECK-ORIGINS: @Paddsw128
561 ; CHECK-ORIGINS: load i32* {{.*}} @__msan_param_origin_tls
562 ; CHECK-ORIGINS: load i32* {{.*}} @__msan_param_origin_tls
563 ; CHECK-ORIGINS: = bitcast <8 x i16> {{.*}} to i128
564 ; CHECK-ORIGINS-NEXT: = icmp ne i128 {{.*}}, 0
565 ; CHECK-ORIGINS-NEXT: = select i1 {{.*}}, i32 {{.*}}, i32
566 ; CHECK-ORIGINS: call <8 x i16> @llvm.x86.sse2.padds.w
567 ; CHECK-ORIGINS: store i32 {{.*}} @__msan_retval_origin_tls
568 ; CHECK-ORIGINS: ret <8 x i16>
571 ; Test handling of vectors of pointers.
572 ; Check that shadow of such vector is a vector of integers.
574 define <8 x i8*> @VectorOfPointers(<8 x i8*>* %p) nounwind uwtable sanitize_memory {
575 %x = load <8 x i8*>* %p
579 ; CHECK: @VectorOfPointers
580 ; CHECK: load <8 x i64>*
581 ; CHECK: load <8 x i8*>*
582 ; CHECK: store <8 x i64> {{.*}} @__msan_retval_tls
583 ; CHECK: ret <8 x i8*>
585 ; Test handling of va_copy.
587 declare void @llvm.va_copy(i8*, i8*) nounwind
589 define void @VACopy(i8* %p1, i8* %p2) nounwind uwtable sanitize_memory {
590 call void @llvm.va_copy(i8* %p1, i8* %p2) nounwind
595 ; CHECK: call void @llvm.memset.p0i8.i64({{.*}}, i8 0, i64 24, i32 8, i1 false)
599 ; Test handling of volatile stores.
600 ; Check that MemorySanitizer does not add a check of the value being stored.
602 define void @VolatileStore(i32* nocapture %p, i32 %x) nounwind uwtable sanitize_memory {
604 store volatile i32 %x, i32* %p, align 4
608 ; CHECK: @VolatileStore
609 ; CHECK-NOT: @__msan_warning
613 ; Test that checks are omitted but shadow propagation is kept if
614 ; sanitize_memory attribute is missing.
616 define i32 @NoSanitizeMemory(i32 %x) uwtable {
618 %tobool = icmp eq i32 %x, 0
619 br i1 %tobool, label %if.end, label %if.then
621 if.then: ; preds = %entry
622 tail call void @bar()
625 if.end: ; preds = %entry, %if.then
631 ; CHECK: @NoSanitizeMemory
632 ; CHECK-NOT: @__msan_warning
633 ; CHECK: load i32* {{.*}} @__msan_param_tls
634 ; CHECK-NOT: @__msan_warning
635 ; CHECK: store {{.*}} @__msan_retval_tls
636 ; CHECK-NOT: @__msan_warning
640 ; Test argument shadow alignment
642 define <2 x i64> @ArgumentShadowAlignment(i64 %a, <2 x i64> %b) sanitize_memory {
647 ; CHECK: @ArgumentShadowAlignment
648 ; CHECK: load <2 x i64>* {{.*}} @__msan_param_tls {{.*}}, align 8
649 ; CHECK: store <2 x i64> {{.*}} @__msan_retval_tls {{.*}}, align 8
650 ; CHECK: ret <2 x i64>
653 ; Test byval argument shadow alignment
655 define <2 x i64> @ByValArgumentShadowLargeAlignment(<2 x i64>* byval %p) sanitize_memory {
657 %x = load <2 x i64>* %p
661 ; CHECK-AA: @ByValArgumentShadowLargeAlignment
662 ; CHECK-AA: call void @llvm.memcpy.p0i8.p0i8.i64(i8* {{.*}}, i8* {{.*}}, i64 16, i32 8, i1 false)
663 ; CHECK-AA: ret <2 x i64>
666 define i16 @ByValArgumentShadowSmallAlignment(i16* byval %p) sanitize_memory {
672 ; CHECK-AA: @ByValArgumentShadowSmallAlignment
673 ; CHECK-AA: call void @llvm.memcpy.p0i8.p0i8.i64(i8* {{.*}}, i8* {{.*}}, i64 2, i32 2, i1 false)