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
4//
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
8
9#include <sanitizer/dfsan_interface.h>
10#include <stdio.h>
11#include <string.h>
12
13__attribute__((noinline)) int foo(int a, int b) { return a + b; }
14
15__attribute__((noinline)) void bar(int depth, void *addr, int size) {
16 if (depth) {
17 bar(depth: depth - 1, addr, size);
18 } else {
19 dfsan_set_label(label: 1, addr, size);
20 }
21}
22
23__attribute__((noinline)) void baz(int depth, void *addr, int size) {
24 bar(depth, addr, size);
25}
26
27int main(int argc, char *argv[]) {
28 int a = 10;
29 int b = 20;
30 baz(depth: 8, addr: &a, size: sizeof(a));
31 int c = foo(a, b);
32 dfsan_origin c_o = dfsan_get_origin(data: c);
33 dfsan_print_origin_id_trace(origin: c_o);
34 // CHECK: Origin value: {{.*}}, Taint value was created at
35 // CHECK: #0 {{.*}} in bar.dfsan {{.*}}origin_id_stack_trace.c:[[@LINE-16]]
36 // CHECK-COUNT-8: #{{[0-9]+}} {{.*}} in bar.dfsan {{.*}}origin_id_stack_trace.c:[[@LINE-19]]
37 // CHECK: #9 {{.*}} in baz.dfsan {{.*}}origin_id_stack_trace.c:[[@LINE-13]]
38
39 char buf[3000];
40 size_t length = dfsan_sprint_origin_id_trace(origin: c_o, out_buf: buf, out_buf_size: sizeof(buf));
41
42 printf(format: "==OUTPUT==\n\n%s==EOS==\n", buf);
43 // CHECK: ==OUTPUT==
44 // CHECK: Origin value: {{.*}}, Taint value was created at
45 // CHECK: #0 {{.*}} in bar.dfsan {{.*}}origin_id_stack_trace.c:[[@LINE-26]]
46 // CHECK-COUNT-8: #{{[0-9]+}} {{.*}} in bar.dfsan {{.*}}origin_id_stack_trace.c:[[@LINE-29]]
47 // CHECK: #9 {{.*}} in baz.dfsan {{.*}}origin_id_stack_trace.c:[[@LINE-23]]
48 // CHECK: ==EOS==
49
50 char tinybuf[20];
51 size_t same_length =
52 dfsan_sprint_origin_id_trace(origin: c_o, out_buf: tinybuf, out_buf_size: sizeof(tinybuf));
53
54 printf(format: "==TRUNCATED OUTPUT==\n\n%s==EOS==\n", tinybuf);
55 // CHECK: ==TRUNCATED OUTPUT==
56 // CHECK: Origin value: 0x1==EOS==
57
58 printf(format: "Returned length: %zu\n", length);
59 printf(format: "Actual length: %zu\n", strlen(s: buf));
60 printf(format: "Returned length with truncation: %zu\n", same_length);
61
62 // CHECK: Returned length: [[#LEN:]]
63 // CHECK: Actual length: [[#LEN]]
64 // CHECK: Returned length with truncation: [[#LEN]]
65
66 buf[0] = '\0';
67 length = dfsan_sprint_origin_id_trace(origin: c_o, out_buf: buf, out_buf_size: 0);
68 printf(format: "Output=\"%s\"\n", buf);
69 printf(format: "Returned length: %zu\n", length);
70 // CHECK: Output=""
71 // CHECK: Returned length: [[#LEN]]
72}
73

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