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 
22 __attribute__((noinline))
fn_g(int a)23 void fn_g(int a) {
24   x = a;
25 }
26 
27 __attribute__((noinline))
fn_f(int a)28 void fn_f(int a) {
29   fn_g(a);
30 }
31 
32 __attribute__((noinline))
fn_h()33 void fn_h() {
34   y = x;
35 }
36 
main(int argc,char * argv[])37 int main(int argc, char *argv[]) {
38 #ifdef HEAP
39   int * volatile zz = new int;
40   int z = *zz;
41 #else
42   int volatile z;
43 #endif
44   fn_f(z);
45   fn_h();
46   return y;
47 }
48 
49 // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
50 // CHECK: {{#0 .* in main.*chained_origin.cpp:}}[[@LINE-4]]
51 
52 // CHECK: Uninitialized value was stored to memory at
53 // CHECK-FULL-STACK: {{#0 .* in fn_h.*chained_origin.cpp:}}[[@LINE-19]]
54 // CHECK-FULL-STACK: {{#1 .* in main.*chained_origin.cpp:}}[[@LINE-9]]
55 // CHECK-SHORT-STACK: {{#0 .* in fn_h.*chained_origin.cpp:}}[[@LINE-21]]
56 
57 // CHECK: Uninitialized value was stored to memory at
58 // CHECK-FULL-STACK: {{#0 .* in fn_g.*chained_origin.cpp:}}[[@LINE-34]]
59 // CHECK-FULL-STACK: {{#1 .* in fn_f.*chained_origin.cpp:}}[[@LINE-30]]
60 // CHECK-FULL-STACK: {{#2 .* in main.*chained_origin.cpp:}}[[@LINE-16]]
61 // CHECK-SHORT-STACK: {{#0 .* in fn_g.*chained_origin.cpp:}}[[@LINE-37]]
62 
63 // CHECK-STACK: Uninitialized value was created by an allocation of 'z' in the stack frame of function 'main'
64 // CHECK-STACK: {{#0 .* in main.*chained_origin.cpp:}}[[@LINE-27]]
65 
66 // CHECK-HEAP: Uninitialized value was created by a heap allocation
67 // CHECK-HEAP: {{#1 .* in main.*chained_origin.cpp:}}[[@LINE-28]]
68