llvm-project
72 строки · 2.4 Кб
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) {
16if (depth) {
17bar(depth - 1, addr, size);
18} else {
19dfsan_set_label(1, addr, size);
20}
21}
22
23__attribute__((noinline)) void baz(int depth, void *addr, int size) {
24bar(depth, addr, size);
25}
26
27int main(int argc, char *argv[]) {
28int a = 10;
29int b = 20;
30baz(8, &a, sizeof(a));
31int c = foo(a, b);
32dfsan_origin c_o = dfsan_get_origin(c);
33dfsan_print_origin_id_trace(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
39char buf[3000];
40size_t length = dfsan_sprint_origin_id_trace(c_o, buf, sizeof(buf));
41
42printf("==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
50char tinybuf[20];
51size_t same_length =
52dfsan_sprint_origin_id_trace(c_o, tinybuf, sizeof(tinybuf));
53
54printf("==TRUNCATED OUTPUT==\n\n%s==EOS==\n", tinybuf);
55// CHECK: ==TRUNCATED OUTPUT==
56// CHECK: Origin value: 0x1==EOS==
57
58printf("Returned length: %zu\n", length);
59printf("Actual length: %zu\n", strlen(buf));
60printf("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
66buf[0] = '\0';
67length = dfsan_sprint_origin_id_trace(c_o, buf, 0);
68printf("Output=\"%s\"\n", buf);
69printf("Returned length: %zu\n", length);
70// CHECK: Output=""
71// CHECK: Returned length: [[#LEN]]
72}
73