1 // RUN: %clang_dfsan -gmlt -mllvm -dfsan-track-origins=1 %s -o %t && \
2 // RUN: %run %t >%t.out 2>&1
3 // RUN: FileCheck %s < %t.out
5 // RUN: %clang_dfsan -gmlt -mllvm -dfsan-track-origins=1 -mllvm -dfsan-instrument-with-call-threshold=0 %s -o %t && \
6 // RUN: %run %t >%t.out 2>&1
7 // RUN: FileCheck %s < %t.out
11 #include <sanitizer/dfsan_interface.h>
15 #define NOINLINE __attribute__((noinline))
17 NOINLINE
int foo(int a
, int b
) { return a
+ b
; }
19 NOINLINE
void bar(int depth
, void *addr
, int size
) {
21 bar(depth
- 1, addr
, size
);
23 dfsan_set_label(1, addr
, size
);
27 NOINLINE
void baz(int depth
, void *addr
, int size
) { bar(depth
, addr
, size
); }
29 int main(int argc
, char *argv
[]) {
32 baz(8, &a
, sizeof(a
));
34 dfsan_print_origin_trace(&c
, NULL
);
35 // CHECK: Taint value 0x1 {{.*}} origin tracking ()
36 // CHECK: Origin value: {{.*}}, Taint value was stored to memory at
37 // CHECK: #0 {{.*}} in main {{.*}}origin_stack_trace.c:[[@LINE-4]]
39 // CHECK: Origin value: {{.*}}, Taint value was created at
40 // CHECK: #0 {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-17]]
41 // CHECK-COUNT-8: #{{[0-9]+}} {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-20]]
42 // CHECK: #9 {{.*}} in baz.dfsan {{.*}}origin_stack_trace.c:[[@LINE-15]]
44 // Test logic expects this buffer to be large enough.
45 // String contains current paths, which could vary in length.
46 // Make this buffer much larger than necessary to accomodate variation.
47 const size_t kBufSize
= 8192;
48 char *buf
= (char *)malloc(kBufSize
);
49 size_t length
= dfsan_sprint_origin_trace(&c
, NULL
, buf
, kBufSize
);
50 assert(kBufSize
> length
);
52 printf("==OUTPUT==\n\n%s==EOS==\n", buf
);
54 // CHECK: Taint value 0x1 {{.*}} origin tracking ()
55 // CHECK: Origin value: {{.*}}, Taint value was stored to memory at
56 // CHECK: #0 {{.*}} in main {{.*}}origin_stack_trace.c:[[@LINE-23]]
58 // CHECK: Origin value: {{.*}}, Taint value was created at
59 // CHECK: #0 {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-36]]
60 // CHECK-COUNT-8: #{{[0-9]+}} {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-39]]
61 // CHECK: #9 {{.*}} in baz.dfsan {{.*}}origin_stack_trace.c:[[@LINE-34]]
66 dfsan_sprint_origin_trace(&c
, NULL
, tinybuf
, sizeof(tinybuf
));
68 printf("==TRUNCATED OUTPUT==\n\n%s==EOS==\n", tinybuf
);
69 // CHECK: ==TRUNCATED OUTPUT==
70 // CHECK: Taint value 0x1==EOS==
72 printf("Returned length: %zu\n", length
);
73 printf("Actual length: %zu\n", strlen(buf
));
74 printf("Returned length with truncation: %zu\n", same_length
);
76 // CHECK: Returned length: [[#LEN:]]
77 // CHECK: Actual length: [[#LEN]]
78 // CHECK: Returned length with truncation: [[#LEN]]
80 size_t length_with_desc
=
81 dfsan_sprint_origin_trace(&c
, "DESCRIPTION", buf
, kBufSize
);
82 assert(kBufSize
> length_with_desc
);
84 printf("==OUTPUT==\n\n%s==EOS==\n", buf
);
86 // CHECK: Taint value 0x1 {{.*}} origin tracking (DESCRIPTION)
87 // CHECK: Origin value: {{.*}}, Taint value was stored to memory at
88 // CHECK: #0 {{.*}} in main {{.*}}origin_stack_trace.c:[[@LINE-55]]
90 // CHECK: Origin value: {{.*}}, Taint value was created at
91 // CHECK: #0 {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-68]]
92 // CHECK-COUNT-8: #{{[0-9]+}} {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-71]]
93 // CHECK: #9 {{.*}} in baz.dfsan {{.*}}origin_stack_trace.c:[[@LINE-66]]
96 printf("Returned length: %zu\n", length_with_desc
);
97 // COMM: Message length is increased by 11: the length of "DESCRIPTION".
98 // CHECK: Returned length: [[#LEN + 11]]
101 length
= dfsan_sprint_origin_trace(&c
, NULL
, buf
, 0);
102 printf("Output=\"%s\"\n", buf
);
103 printf("Returned length: %zu\n", length
);
105 // CHECK: Returned length: [[#LEN]]