File: origin_stack_trace.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 (108 lines) | stat: -rw-r--r-- 4,022 bytes parent folder | download | duplicates (20)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// RUN: %clang_dfsan -gmlt -mllvm -dfsan-track-origins=1 %s -o %t && \
// RUN:     %run %t >%t.out 2>&1
// RUN: FileCheck %s < %t.out
//
// RUN: %clang_dfsan -gmlt -mllvm -dfsan-track-origins=1 -mllvm -dfsan-instrument-with-call-threshold=0 %s -o %t && \
// RUN:     %run %t >%t.out 2>&1
// RUN: FileCheck %s < %t.out

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

#define NOINLINE __attribute__((noinline))

NOINLINE int foo(int a, int b) { return a + b; }

NOINLINE void bar(int depth, void *addr, int size) {
  if (depth) {
    bar(depth - 1, addr, size);
  } else {
    dfsan_set_label(1, addr, size);
  }
}

NOINLINE void baz(int depth, void *addr, int size) { bar(depth, addr, size); }

int main(int argc, char *argv[]) {
  int a = 10;
  int b = 20;
  baz(8, &a, sizeof(a));
  int c = foo(a, b);
  dfsan_print_origin_trace(&c, NULL);
  // CHECK: Taint value 0x1 {{.*}} origin tracking ()
  // CHECK: Origin value: {{.*}}, Taint value was stored to memory at
  // CHECK: #0 {{.*}} in main {{.*}}origin_stack_trace.c:[[@LINE-4]]

  // CHECK: Origin value: {{.*}}, Taint value was created at
  // CHECK: #0 {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-17]]
  // CHECK-COUNT-8: #{{[0-9]+}} {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-20]]
  // CHECK: #9 {{.*}} in baz.dfsan {{.*}}origin_stack_trace.c:[[@LINE-15]]

  // Test logic expects this buffer to be large enough.
  // String contains current paths, which could vary in length.
  // Make this buffer much larger than necessary to accomodate variation.
  const size_t kBufSize = 8192;
  char *buf = (char *)malloc(kBufSize);
  size_t length = dfsan_sprint_origin_trace(&c, NULL, buf, kBufSize);
  assert(kBufSize > length);

  printf("==OUTPUT==\n\n%s==EOS==\n", buf);
  // CHECK: ==OUTPUT==
  // CHECK: Taint value 0x1 {{.*}} origin tracking ()
  // CHECK: Origin value: {{.*}}, Taint value was stored to memory at
  // CHECK: #0 {{.*}} in main {{.*}}origin_stack_trace.c:[[@LINE-23]]

  // CHECK: Origin value: {{.*}}, Taint value was created at
  // CHECK: #0 {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-36]]
  // CHECK-COUNT-8: #{{[0-9]+}} {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-39]]
  // CHECK: #9 {{.*}} in baz.dfsan {{.*}}origin_stack_trace.c:[[@LINE-34]]
  // CHECK: ==EOS==

  char tinybuf[18];
  size_t same_length =
      dfsan_sprint_origin_trace(&c, NULL, tinybuf, sizeof(tinybuf));

  printf("==TRUNCATED OUTPUT==\n\n%s==EOS==\n", tinybuf);
  // CHECK: ==TRUNCATED OUTPUT==
  // CHECK: Taint value 0x1==EOS==

  printf("Returned length: %zu\n", length);
  printf("Actual length: %zu\n", strlen(buf));
  printf("Returned length with truncation: %zu\n", same_length);

  // CHECK: Returned length: [[#LEN:]]
  // CHECK: Actual length: [[#LEN]]
  // CHECK: Returned length with truncation: [[#LEN]]

  size_t length_with_desc =
      dfsan_sprint_origin_trace(&c, "DESCRIPTION", buf, kBufSize);
  assert(kBufSize > length_with_desc);

  printf("==OUTPUT==\n\n%s==EOS==\n", buf);
  // CHECK: ==OUTPUT==
  // CHECK: Taint value 0x1 {{.*}} origin tracking (DESCRIPTION)
  // CHECK: Origin value: {{.*}}, Taint value was stored to memory at
  // CHECK: #0 {{.*}} in main {{.*}}origin_stack_trace.c:[[@LINE-55]]

  // CHECK: Origin value: {{.*}}, Taint value was created at
  // CHECK: #0 {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-68]]
  // CHECK-COUNT-8: #{{[0-9]+}} {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-71]]
  // CHECK: #9 {{.*}} in baz.dfsan {{.*}}origin_stack_trace.c:[[@LINE-66]]
  // CHECK: ==EOS==

  printf("Returned length: %zu\n", length_with_desc);
  // COMM: Message length is increased by 11: the length of "DESCRIPTION".
  // CHECK: Returned length: [[#LEN + 11]]

  buf[0] = '\0';
  length = dfsan_sprint_origin_trace(&c, NULL, buf, 0);
  printf("Output=\"%s\"\n", buf);
  printf("Returned length: %zu\n", length);
  // CHECK: Output=""
  // CHECK: Returned length: [[#LEN]]

  free(buf);
}