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