1 // RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - %s | FileCheck -check-prefixes CHECK,WITHOUT %s
2 // RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - %s -fsanitize=memory | FileCheck -check-prefixes CHECK,MSAN %s
3 // RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - %s -fsanitize=kernel-memory | FileCheck -check-prefixes CHECK,KMSAN %s
4 
5 // Instrumented function.
6 // MSan uses memset(addr, -1, size) to poison allocas and stores shadow of the return value in
7 // __msan_retval_tls. KMSAN uses __msan_poison_alloca() to poison allocas and calls
8 // __msan_get_context_state() at function prologue to access the task context struct (including the
9 // shadow of the return value).
10 //
11 // CHECK-LABEL: i32 @instrumented1
12 // KMSAN: __msan_get_context_state
13 // WITHOUT-NOT: __msan_poison_alloca
14 // WITHOUT-NOT: @llvm.memset
15 // MSAN: @llvm.memset{{.*}}({{.*}}, i8 -1
16 // KMSAN: __msan_poison_alloca
17 // WITHOUT-NOT: __msan_retval_tls
18 // MSAN: __msan_retval_tls
19 // CHECK: ret i32
instrumented1(int * a)20 int instrumented1(int *a) {
21   volatile char buf[8];
22   return *a;
23 }
24 
25 // Function with no_sanitize("memory")/no_sanitize("kernel-memory"): no shadow propagation, but
26 // unpoisons memory to prevent false positives.
27 // MSan uses memset(addr, 0, size) to unpoison locals, KMSAN uses __msan_unpoison_alloca(). Both
28 // tools still access the retval shadow to write 0 to it.
29 //
30 // CHECK-LABEL: i32 @no_false_positives1
31 // KMSAN: __msan_get_context_state
32 // WITHOUT-NOT: __msan_unpoison_alloca
33 // WITHOUT-NOT: @llvm.memset
34 // MSAN: @llvm.memset{{.*}}({{.*}}, i8 0
35 // KMSAN: __msan_unpoison_alloca
36 // WITHOUT-NOT: __msan_retval_tls
37 // MSAN: __msan_retval_tls
38 // CHECK: ret i32
no_false_positives1(int * a)39 __attribute__((no_sanitize("memory"))) __attribute__((no_sanitize("kernel-memory"))) int no_false_positives1(int *a) {
40   volatile char buf[8];
41   return *a;
42 }
43 
44 // Function with disable_sanitizer_instrumentation: no instrumentation at all.
45 //
46 // CHECK-LABEL: i32 @no_instrumentation1
47 // KMSAN-NOT: __msan_get_context_state
48 // WITHOUT-NOT: __msan_poison_alloca
49 // WITHOUT-NOT: @llvm.memset
50 // MSAN-NOT: @llvm.memset{{.*}}({{.*}}, i8 0
51 // KMSAN-NOT: __msan_unpoison_alloca
52 // WITHOUT-NOT: __msan_retval_tls
53 // MSAN-NOT: __msan_retval_tls
54 // CHECK: ret i32
no_instrumentation1(int * a)55 __attribute__((disable_sanitizer_instrumentation)) int no_instrumentation1(int *a) {
56   volatile char buf[8];
57   return *a;
58 }
59