File: reaches_function.c

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (65 lines) | stat: -rw-r--r-- 2,913 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// RUN: %clang_dfsan -fno-sanitize=dataflow -O2 -fPIE -DCALLBACKS -c %s -o %t-callbacks.o
// RUN: %clang_dfsan -gmlt -fsanitize-ignorelist=%S/Inputs/flags_abilist.txt -O2 -mllvm -dfsan-reaches-function-callbacks=1 %s %t-callbacks.o -o %t
// RUN: %run %t 2>&1 | FileCheck %s

// RUN: %clang_dfsan -fno-sanitize=dataflow -O2 -fPIE -DCALLBACKS -DORIGIN_TRACKING -c %s -o %t-callbacks.o
// 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
// RUN: %run %t 2>&1 | FileCheck --check-prefix=CHECK-ORIGIN-TRACKING %s

// Tests that callbacks are inserted for reached functions when
// -dfsan-reaches-function-callbacks is specified.

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sanitizer/dfsan_interface.h>

#ifdef CALLBACKS
// Compile this code without DFSan to avoid recursive instrumentation.

void my_dfsan_reaches_function_callback(dfsan_label label, dfsan_origin origin,
                                        const char *file, unsigned int line,
                                        const char *function) {
#ifdef ORIGIN_TRACKING
  dfsan_print_origin_id_trace(origin);
#else
  printf("%s:%d %s\n", file, line, function);
#endif
}

#else

__attribute__((noinline)) uint64_t add(uint64_t *a, uint64_t *b) {

  return *a + *b;
  // CHECK: {{.*}}compiler-rt/test/dfsan/reaches_function.c:[[# @LINE - 1]] add.dfsan
  // CHECK-ORIGIN-TRACKING: Origin value: 0x10000002, Taint value was stored to memory at
  // CHECK-ORIGIN-TRACKING: #0 {{.*}} in add.dfsan {{.*}}compiler-rt/test/dfsan/reaches_function.c:[[# @LINE - 3]]:{{.*}}
  // CHECK-ORIGIN-TRACKING: Origin value: 0x1, Taint value was created at
  // CHECK-ORIGIN-TRACKING: #0 {{.*}} in main {{.*}}compiler-rt/test/dfsan/reaches_function.c:{{.*}}
}

extern void my_dfsan_reaches_function_callback(dfsan_label label,
                                               dfsan_origin origin,
                                               const char *file,
                                               unsigned int line,
                                               const char *function);

int main(int argc, char *argv[]) {

  dfsan_set_reaches_function_callback(my_dfsan_reaches_function_callback);

  uint64_t a = 0;
  uint64_t b = 0;

  dfsan_set_label(8, &a, sizeof(a));
  uint64_t c = add(&a, &b);
  // CHECK: {{.*}}compiler-rt/test/dfsan/reaches_function.c:[[# @LINE - 1]] main
  // CHECK-ORIGIN-TRACKING: Origin value: 0x10000002, Taint value was stored to memory at
  // CHECK-ORIGIN-TRACKING: #0 {{.*}} in add.dfsan {{.*}}compiler-rt/test/dfsan/reaches_function.c:{{.*}}
  // CHECK-ORIGIN-TRACKING: Origin value: 0x1, Taint value was created at
  // CHECK-ORIGIN-TRACKING: #0 {{.*}} in main {{.*}}compiler-rt/test/dfsan/reaches_function.c:[[# @LINE - 6]]:{{.*}}
  return c;
}

#endif // #ifdef CALLBACKS