1// RUN: %clang_dfsan -fno-sanitize=dataflow -O2 -fPIE -DCALLBACKS -c %s -o %t-callbacks.o
2// RUN: %clang_dfsan -gmlt -fsanitize-ignorelist=%S/Inputs/flags_abilist.txt -O2 -mllvm -dfsan-reaches-function-callbacks=1 %s %t-callbacks.o -o %t
3// RUN: %run %t 2>&1 | FileCheck %s
4
5// RUN: %clang_dfsan -fno-sanitize=dataflow -O2 -fPIE -DCALLBACKS -DORIGIN_TRACKING -c %s -o %t-callbacks.o
6// RUN: %clang_dfsan -gmlt -fsanitize-ignorelist=%S/Inputs/flags_abilist.txt -O2 -mllvm -dfsan-reaches-function-callbacks=1 -mllvm -dfsan-track-origins=2 %s %t-callbacks.o -o %t
7// RUN: %run %t 2>&1 | FileCheck --check-prefix=CHECK-ORIGIN-TRACKING %s
8
9// Tests that callbacks are inserted for reached functions when
10// -dfsan-reaches-function-callbacks is specified.
11
12#include <assert.h>
13#include <stdio.h>
14#include <string.h>
15#include <sanitizer/dfsan_interface.h>
16
17#ifdef CALLBACKS
18// Compile this code without DFSan to avoid recursive instrumentation.
19
20void my_dfsan_reaches_function_callback(dfsan_label label, dfsan_origin origin,
21 const char *file, unsigned int line,
22 const char *function) {
23#ifdef ORIGIN_TRACKING
24 dfsan_print_origin_id_trace(origin);
25#else
26 printf("%s:%d %s\n", file, line, function);
27#endif
28}
29
30#else
31
32__attribute__((noinline)) uint64_t add(uint64_t *a, uint64_t *b) {
33
34 return *a + *b;
35 // CHECK: {{.*}}compiler-rt/test/dfsan/reaches_function.c:[[# @LINE - 1]] add.dfsan
36 // CHECK-ORIGIN-TRACKING: Origin value: 0x10000002, Taint value was stored to memory at
37 // CHECK-ORIGIN-TRACKING: #0 {{.*}} in add.dfsan {{.*}}compiler-rt/test/dfsan/reaches_function.c:[[# @LINE - 3]]:{{.*}}
38 // CHECK-ORIGIN-TRACKING: Origin value: 0x1, Taint value was created at
39 // CHECK-ORIGIN-TRACKING: #0 {{.*}} in main {{.*}}compiler-rt/test/dfsan/reaches_function.c:{{.*}}
40}
41
42extern void my_dfsan_reaches_function_callback(dfsan_label label,
43 dfsan_origin origin,
44 const char *file,
45 unsigned int line,
46 const char *function);
47
48int main(int argc, char *argv[]) {
49
50 dfsan_set_reaches_function_callback(callback: my_dfsan_reaches_function_callback);
51
52 uint64_t a = 0;
53 uint64_t b = 0;
54
55 dfsan_set_label(label: 8, addr: &a, size: sizeof(a));
56 uint64_t c = add(a: &a, b: &b);
57 // CHECK: {{.*}}compiler-rt/test/dfsan/reaches_function.c:[[# @LINE - 1]] main
58 // CHECK-ORIGIN-TRACKING: Origin value: 0x10000002, Taint value was stored to memory at
59 // CHECK-ORIGIN-TRACKING: #0 {{.*}} in add.dfsan {{.*}}compiler-rt/test/dfsan/reaches_function.c:{{.*}}
60 // CHECK-ORIGIN-TRACKING: Origin value: 0x1, Taint value was created at
61 // CHECK-ORIGIN-TRACKING: #0 {{.*}} in main {{.*}}compiler-rt/test/dfsan/reaches_function.c:[[# @LINE - 6]]:{{.*}}
62 return c;
63}
64
65#endif // #ifdef CALLBACKS
66

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