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-prefixes=CHECK,CHECK-NONCONS" %s
5 ; RUN: opt < %s -msan -msan-kernel=1 -msan-check-access-address=0 -msan-handle-asm-conservative=0 -S | FileCheck -check-prefixes=CHECK,CHECK-NONCONS %s
6 ; RUN: opt < %s -msan-kernel=1 -msan-check-access-address=0 \
7 ; RUN: -msan-handle-asm-conservative=1 -S -passes=msan 2>&1 | FileCheck \
8 ; RUN: "-check-prefixes=CHECK,CHECK-CONS" %s
9 ; RUN: opt < %s -msan -msan-kernel=1 -msan-check-access-address=0 -msan-handle-asm-conservative=1 -S | FileCheck -check-prefixes=CHECK,CHECK-CONS %s
11 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
12 target triple = "x86_64-unknown-linux-gnu"
14 ; The IR below was generated from the following source:
17 ; unsigned long value = 2;
19 ; unsigned long *addr = &value;
20 ; asm("btsq %2, %1; setc %0" : "=qm" (bit), "=m" (addr): "Ir" (nr));
27 ; In the regular instrumentation mode MSan is unable to understand that |bit|
28 ; is initialized by the asm() call, and therefore reports a false positive on
30 ; The conservative assembly handling mode initializes every memory location
31 ; passed by pointer into an asm() call. This prevents false positive reports,
32 ; but may introduce false negatives.
34 ; This test makes sure that the conservative mode unpoisons the shadow of |bit|
37 define dso_local i32 @main() sanitize_memory {
39 %retval = alloca i32, align 4
40 %bit = alloca i8, align 1
41 %value = alloca i64, align 8
42 %nr = alloca i64, align 8
43 %addr = alloca i64*, align 8
44 store i32 0, i32* %retval, align 4
45 store i64 2, i64* %value, align 8
46 store i64 0, i64* %nr, align 8
47 store i64* %value, i64** %addr, align 8
48 %0 = load i64, i64* %nr, align 8
49 call void asm "btsq $2, $1; setc $0", "=*qm,=*m,Ir,~{dirflag},~{fpsr},~{flags}"(i8* %bit, i64** %addr, i64 %0)
50 %1 = load i8, i8* %bit, align 1
51 %tobool = trunc i8 %1 to i1
52 br i1 %tobool, label %if.then, label %if.else
54 if.then: ; preds = %entry
57 if.else: ; preds = %entry
61 ; %nr is first poisoned, then unpoisoned (written to). Need to optimize this in the future.
62 ; CHECK: [[NRC1:%.*]] = bitcast i64* %nr to i8*
63 ; CHECK: call void @__msan_poison_alloca(i8* [[NRC1]]{{.*}})
64 ; CHECK: [[NRC2:%.*]] = bitcast i64* %nr to i8*
65 ; CHECK: call { i8*, i32* } @__msan_metadata_ptr_for_store_8(i8* [[NRC2]])
67 ; Hooks for inputs usually go before the assembly statement. But here we have none,
68 ; because %nr is passed by value. However we check %nr for being initialized.
69 ; CHECK-CONS: [[NRC3:%.*]] = bitcast i64* %nr to i8*
70 ; CHECK-CONS: call { i8*, i32* } @__msan_metadata_ptr_for_load_8(i8* [[NRC3]])
72 ; In the conservative mode, call the store hooks for %bit and %addr:
73 ; CHECK-CONS: call void @__msan_instrument_asm_store(i8* %bit, i64 1)
74 ; CHECK-CONS: [[ADDR8S:%.*]] = bitcast i64** %addr to i8*
75 ; CHECK-CONS: call void @__msan_instrument_asm_store(i8* [[ADDR8S]], i64 8)
77 ; Landing pad for the %nr check above.
78 ; CHECK-CONS: call void @__msan_warning
80 ; CHECK: call void asm "btsq $2, $1; setc $0"
82 ; Calculating the shadow offset of %bit.
83 ; CHECKz: [[PTR:%.*]] = ptrtoint {{.*}} %bit to i64
84 ; CHECKz: [[SH_NUM:%.*]] = xor i64 [[PTR]]
85 ; CHECKz: [[SHADOW:%.*]] = inttoptr i64 [[SH_NUM]] {{.*}}
87 ; CHECK: [[META:%.*]] = call {{.*}} @__msan_metadata_ptr_for_load_1(i8* %bit)
88 ; CHECK: [[SHADOW:%.*]] = extractvalue { i8*, i32* } [[META]], 0
90 ; Now load the shadow value for the boolean.
91 ; CHECK: [[MSLD:%.*]] = load {{.*}} [[SHADOW]]
92 ; CHECK: [[MSPROP:%.*]] = trunc i8 [[MSLD]] to i1
94 ; Is the shadow poisoned?
95 ; CHECK: [[MSCMP:%.*]] = icmp ne i1 [[MSPROP]], false
96 ; CHECK: br i1 [[MSCMP]], label %[[IFTRUE:.*]], label {{.*}}
98 ; If yes, raise a warning.
100 ; CHECK: call void @__msan_warning