1 ; Test for the conservative assembly handling mode used by KMSAN.
2 ; RUN: opt < %s -msan-kernel=1 -msan-check-access-address=0 \
3 ; RUN: -msan-handle-asm-conservative=0 -S -passes=msan 2>&1 | FileCheck \
4 ; RUN: "-check-prefix=CHECK" %s
5 ; RUN: opt < %s -msan-kernel=1 -msan-check-access-address=0 \
6 ; RUN: -msan-handle-asm-conservative=1 -S -passes=msan 2>&1 | FileCheck \
7 ; RUN: "-check-prefixes=CHECK,CHECK-CONS" %s
9 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
10 target triple = "x86_64-unknown-linux-gnu"
12 ; The IR below was generated from the following source:
15 ; unsigned long value = 2;
17 ; unsigned long *addr = &value;
18 ; asm("btsq %2, %1; setc %0" : "=qm" (bit), "=m" (addr): "Ir" (nr));
25 ; In the regular instrumentation mode MSan is unable to understand that |bit|
26 ; is initialized by the asm() call, and therefore reports a false positive on
28 ; The conservative assembly handling mode initializes every memory location
29 ; passed by pointer into an asm() call. This prevents false positive reports,
30 ; but may introduce false negatives.
32 ; This test makes sure that the conservative mode unpoisons the shadow of |bit|
35 define dso_local i32 @main() sanitize_memory {
37 %retval = alloca i32, align 4
38 %bit = alloca i8, align 1
39 %value = alloca i64, align 8
40 %nr = alloca i64, align 8
41 %addr = alloca ptr, align 8
42 store i32 0, ptr %retval, align 4
43 store i64 2, ptr %value, align 8
44 store i64 0, ptr %nr, align 8
45 store ptr %value, ptr %addr, align 8
46 %0 = load i64, ptr %nr, align 8
47 call void asm "btsq $2, $1; setc $0", "=*qm,=*m,Ir,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(i8) %bit, ptr elementtype(ptr) %addr, i64 %0)
48 %1 = load i8, ptr %bit, align 1
49 %tobool = trunc i8 %1 to i1
50 br i1 %tobool, label %if.then, label %if.else
52 if.then: ; preds = %entry
55 if.else: ; preds = %entry
59 ; %nr is first poisoned, then unpoisoned (written to). Need to optimize this in the future.
60 ; CHECK: call void @__msan_poison_alloca(ptr %nr{{.*}})
61 ; CHECK: call { ptr, ptr } @__msan_metadata_ptr_for_store_8(ptr %nr)
63 ; Hooks for inputs usually go before the assembly statement. But here we have none,
64 ; because %nr is passed by value. However we check %nr for being initialized.
65 ; CHECK-CONS: call { ptr, ptr } @__msan_metadata_ptr_for_load_8(ptr %nr)
67 ; In the conservative mode, call the store hooks for %bit and %addr:
68 ; CHECK-CONS: call void @__msan_instrument_asm_store(ptr %bit, i64 1)
69 ; CHECK-CONS: call void @__msan_instrument_asm_store(ptr %addr, i64 8)
71 ; Landing pad for the %nr check above.
72 ; CHECK-CONS: call void @__msan_warning
74 ; CHECK: call void asm "btsq $2, $1; setc $0"
76 ; Calculating the shadow offset of %bit.
77 ; CHECKz: [[PTR:%.*]] = ptrtoint {{.*}} %bit to i64
78 ; CHECKz: [[SH_NUM:%.*]] = xor i64 [[PTR]]
79 ; CHECKz: [[SHADOW:%.*]] = inttoptr i64 [[SH_NUM]] {{.*}}
81 ; CHECK: [[META:%.*]] = call {{.*}} @__msan_metadata_ptr_for_load_1(ptr %bit)
82 ; CHECK: [[SHADOW:%.*]] = extractvalue { ptr, ptr } [[META]], 0
84 ; Now load the shadow value for the boolean.
85 ; CHECK: [[MSLD:%.*]] = load {{.*}} [[SHADOW]]
86 ; CHECK: [[MSPROP:%.*]] = trunc i8 [[MSLD]] to i1
88 ; Is the shadow poisoned?
89 ; CHECK: br i1 [[MSPROP]], label %[[IFTRUE:.*]], label {{.*}}
91 ; If yes, raise a warning.
93 ; CHECK: call void @__msan_warning