1// RUN: %clang_dfsan -gmlt -DTEST64 -DALIGN=8 -mllvm -dfsan-track-origins=1 %s -o %t && \
2// RUN: %run %t >%t.out 2>&1
3// RUN: FileCheck %s < %t.out
4//
5// RUN: %clang_dfsan -gmlt -DTEST32 -DALIGN=4 -mllvm -dfsan-track-origins=1 %s -o %t && \
6// RUN: %run %t >%t.out 2>&1
7// RUN: FileCheck %s < %t.out
8//
9// RUN: %clang_dfsan -gmlt -DALIGN=2 -mllvm -dfsan-track-origins=1 %s -o %t && \
10// RUN: %run %t >%t.out 2>&1
11// RUN: FileCheck %s < %t.out
12//
13// RUN: %clang_dfsan -gmlt -DTEST64 -DALIGN=4 -mllvm -dfsan-track-origins=1 %s -o %t && \
14// RUN: %run %t >%t.out 2>&1
15// RUN: FileCheck %s < %t.out
16//
17// RUN: %clang_dfsan -gmlt -DTEST32 -DALIGN=2 -mllvm -dfsan-track-origins=1 %s -o %t && \
18// RUN: %run %t >%t.out 2>&1
19// RUN: FileCheck %s < %t.out
20//
21// RUN: %clang_dfsan -gmlt -DALIGN=1 -mllvm -dfsan-track-origins=1 %s -o %t && \
22// RUN: %run %t >%t.out 2>&1
23// RUN: FileCheck %s < %t.out
24//
25// Test origin tracking is accurate in terms of partial store/load, and
26// different aligments. Do not test alignments that are not power of 2.
27// Compilers do not always allow this.
28
29#include <sanitizer/dfsan_interface.h>
30
31#ifdef TEST64
32typedef uint64_t FULL_TYPE;
33typedef uint32_t HALF_TYPE;
34#elif defined(TEST32)
35typedef uint32_t FULL_TYPE;
36typedef uint16_t HALF_TYPE;
37#else
38typedef uint16_t FULL_TYPE;
39typedef uint8_t HALF_TYPE;
40#endif
41
42__attribute__((noinline)) FULL_TYPE foo(FULL_TYPE a, FULL_TYPE b) { return a + b; }
43
44int main(int argc, char *argv[]) {
45 char x __attribute__((aligned(ALIGN))) = 1, y = 2;
46 dfsan_set_label(label: 8, addr: &x, size: sizeof(x));
47 char z __attribute__((aligned(ALIGN))) = x + y;
48 dfsan_print_origin_trace(addr: &z, NULL);
49
50 FULL_TYPE a __attribute__((aligned(ALIGN))) = 1;
51 FULL_TYPE b = 10;
52 dfsan_set_label(label: 4, addr: (HALF_TYPE *)&a + 1, size: sizeof(HALF_TYPE));
53 FULL_TYPE c __attribute__((aligned(ALIGN))) = foo(a, b);
54 dfsan_print_origin_trace(addr: &c, NULL);
55 dfsan_print_origin_trace(addr: (HALF_TYPE *)&c + 1, NULL);
56}
57
58// CHECK: Taint value 0x8 {{.*}} origin tracking ()
59// CHECK: Origin value: {{.*}}, Taint value was stored to memory at
60// CHECK: #0 {{.*}} in main {{.*}}origin_ldst.c:[[@LINE-13]]
61
62// CHECK: Origin value: {{.*}}, Taint value was created at
63// CHECK: #0 {{.*}} in main {{.*}}origin_ldst.c:[[@LINE-17]]
64
65// CHECK: Taint value 0x4 {{.*}} origin tracking ()
66// CHECK: Origin value: {{.*}}, Taint value was stored to memory at
67// CHECK: #0 {{.*}} in main {{.*}}origin_ldst.c:[[@LINE-14]]
68
69// CHECK: Origin value: {{.*}}, Taint value was created at
70// CHECK: #0 {{.*}} in main {{.*}}origin_ldst.c:[[@LINE-18]]
71
72// CHECK: Taint value 0x4 {{.*}} origin tracking ()
73// CHECK: Origin value: {{.*}}, Taint value was stored to memory at
74// CHECK: #0 {{.*}} in main {{.*}}origin_ldst.c:[[@LINE-21]]
75
76// CHECK: Origin value: {{.*}}, Taint value was created at
77// CHECK: #0 {{.*}} in main {{.*}}origin_ldst.c:[[@LINE-25]]
78

source code of compiler-rt/test/dfsan/origin_ldst.c