File: sanitize-memory-disable.c

package info (click to toggle)
llvm-toolchain-16 1%3A16.0.6-15~deb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,634,820 kB
  • sloc: cpp: 6,179,261; ansic: 1,216,205; asm: 741,319; python: 196,614; objc: 75,325; f90: 49,640; lisp: 32,396; pascal: 12,286; sh: 9,394; perl: 7,442; ml: 5,494; awk: 3,523; makefile: 2,723; javascript: 1,206; xml: 886; fortran: 581; cs: 573
file content (59 lines) | stat: -rw-r--r-- 2,498 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - %s | FileCheck -check-prefixes CHECK,WITHOUT %s
// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - %s -fsanitize=memory | FileCheck -check-prefixes CHECK,MSAN %s
// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - %s -fsanitize=kernel-memory | FileCheck -check-prefixes CHECK,KMSAN %s
// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - %s -fsanitize=memory -fno-sanitize-memory-param-retval | FileCheck -check-prefixes CHECK,MSAN,RETTLS %s

// Instrumented function.
// MSan uses memset(addr, -1, size) to poison allocas and stores shadow of the return value in
// __msan_retval_tls. KMSAN uses __msan_poison_alloca() to poison allocas and calls
// __msan_get_context_state() at function prologue to access the task context struct (including the
// shadow of the return value).
//
// CHECK-LABEL: i32 @instrumented1
// KMSAN: __msan_get_context_state
// WITHOUT-NOT: __msan_poison_alloca
// WITHOUT-NOT: @llvm.memset
// MSAN: @llvm.memset{{.*}}({{.*}}, i8 -1
// KMSAN: __msan_poison_alloca
// WITHOUT-NOT: __msan_retval_tls
// RETTLS: __msan_retval_tls
// CHECK: ret i32
int instrumented1(int *a) {
  volatile char buf[8];
  return *a;
}

// Function with no_sanitize("memory")/no_sanitize("kernel-memory"): no shadow propagation, but
// unpoisons memory to prevent false positives.
// MSan uses memset(addr, 0, size) to unpoison locals, KMSAN uses __msan_unpoison_alloca(). Both
// tools still access the retval shadow to write 0 to it.
//
// CHECK-LABEL: i32 @no_false_positives1
// KMSAN: __msan_get_context_state
// WITHOUT-NOT: __msan_unpoison_alloca
// WITHOUT-NOT: @llvm.memset
// MSAN: @llvm.memset{{.*}}({{.*}}, i8 0
// KMSAN: __msan_unpoison_alloca
// WITHOUT-NOT: __msan_retval_tls
// RETTLS: __msan_retval_tls
// CHECK: ret i32
__attribute__((no_sanitize("memory"))) __attribute__((no_sanitize("kernel-memory"))) int no_false_positives1(int *a) {
  volatile char buf[8];
  return *a;
}

// Function with disable_sanitizer_instrumentation: no instrumentation at all.
//
// CHECK-LABEL: i32 @no_instrumentation1
// KMSAN-NOT: __msan_get_context_state
// WITHOUT-NOT: __msan_poison_alloca
// WITHOUT-NOT: @llvm.memset
// MSAN-NOT: @llvm.memset{{.*}}({{.*}}, i8 0
// KMSAN-NOT: __msan_unpoison_alloca
// WITHOUT-NOT: __msan_retval_tls
// MSAN-NOT: __msan_retval_tls
// CHECK: ret i32
__attribute__((disable_sanitizer_instrumentation)) int no_instrumentation1(int *a) {
  volatile char buf[8];
  return *a;
}