xref: /llvm-project/compiler-rt/test/msan/chained_origin.cpp (revision 15805c030f31b8d112ee4a4706ff4f5725c794df)
1 // RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -O3 %s -o %t && \
2 // RUN:     not %run %t >%t.out 2>&1
3 // RUN: FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%short-stack --check-prefix=CHECK-STACK < %t.out
4 
5 // RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -DHEAP=1 -O3 %s -o %t && \
6 // RUN:     not %run %t >%t.out 2>&1
7 // RUN: FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%short-stack --check-prefix=CHECK-HEAP < %t.out
8 
9 
10 // RUN: %clangxx_msan -mllvm -msan-instrumentation-with-call-threshold=0 -fsanitize-memory-track-origins=2 -O3 %s -o %t && \
11 // RUN:     not %run %t >%t.out 2>&1
12 // RUN: FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%short-stack --check-prefix=CHECK-STACK < %t.out
13 
14 // RUN: %clangxx_msan -mllvm -msan-instrumentation-with-call-threshold=0 -fsanitize-memory-track-origins=2 -DHEAP=1 -O3 %s -o %t && \
15 // RUN:     not %run %t >%t.out 2>&1
16 // RUN: FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-HEAP < %t.out
17 
18 #include <stdio.h>
19 
20 volatile int x, y;
21 
fn_g(int & a)22 __attribute__((noinline)) void fn_g(int &a) { x = a; }
23 
fn_f(int & a)24 __attribute__((noinline)) void fn_f(int &a) { fn_g(a); }
25 
fn_h()26 __attribute__((noinline)) void fn_h() { y = x; }
27 
main(int argc,char * argv[])28 int main(int argc, char *argv[]) {
29 #ifdef HEAP
30   int * volatile zz = new int;
31   int z = *zz;
32 #else
33   int volatile z;
34 #endif
35   fn_f((int &)z);
36   fn_h();
37   return y;
38 }
39 
40 // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
41 // CHECK: {{#0 .* in main.*chained_origin.cpp:}}[[@LINE-4]]
42 
43 // CHECK: Uninitialized value was stored to memory at
44 // CHECK-FULL-STACK: {{#0 .* in fn_h.*chained_origin.cpp:}}[[@LINE-18]]
45 // CHECK-FULL-STACK: {{#1 .* in main.*chained_origin.cpp:}}[[@LINE-9]]
46 // CHECK-SHORT-STACK: {{#0 .* in fn_h.*chained_origin.cpp:}}[[@LINE-21]]
47 
48 // CHECK: Uninitialized value was stored to memory at
49 // CHECK-FULL-STACK: {{#0 .* in fn_g.*chained_origin.cpp:}}[[@LINE-27]]
50 // CHECK-FULL-STACK: {{#1 .* in fn_f.*chained_origin.cpp:}}[[@LINE-26]]
51 // CHECK-FULL-STACK: {{#2 .* in main.*chained_origin.cpp:}}[[@LINE-16]]
52 // CHECK-SHORT-STACK: {{#0 .* in fn_g.*chained_origin.cpp:}}[[@LINE-37]]
53 
54 // CHECK-STACK: Uninitialized value was created by an allocation of 'z' in the stack frame
55 // CHECK-STACK: {{#0 .* in main.*chained_origin.cpp:}}[[@LINE-22]]
56 
57 // CHECK-HEAP: Uninitialized value was created by a heap allocation
58 // CHECK-HEAP: {{#1 .* in main.*chained_origin.cpp:}}[[@LINE-28]]
59