File: 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 (64 lines) | stat: -rw-r--r-- 1,873 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
// RUN: %clang_dfsan -gmlt %s -o %t && %run %t >%t.out 2>&1
// RUN: FileCheck %s < %t.out

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

#define NOINLINE __attribute__((noinline))

NOINLINE size_t bar(int depth, char *buf, size_t len) {
  if (!depth) {
    return dfsan_sprint_stack_trace(buf, len);
  }

  return bar(depth - 1, buf, len);
}

NOINLINE size_t baz(int depth, char *buf, size_t len) {
  return bar(depth, buf, len);
}

int main(int argc, char *argv[]) {
  char buf[3000];
  size_t length = dfsan_sprint_stack_trace(buf, sizeof(buf));
  assert(length < sizeof(buf));
  printf("==OUTPUT==\n%s==EOS==\n", buf);

  // CHECK: ==OUTPUT==
  // CHECK: #0 {{.*}} in main [[FILEPATH:.*]]/stack_trace.c:[[# @LINE - 5 ]]
  // CHECK: ==EOS==

  length = baz(8, buf, sizeof(buf));
  printf("==OUTPUT==\n%s==EOS==\n", buf);

  // CHECK: ==OUTPUT==
  // CHECK: #0 {{.*}} in bar.dfsan [[FILEPATH]]/stack_trace.c:13
  // CHECK-COUNT-8: #{{[1-9]+}} {{.*}} in bar.dfsan [[FILEPATH]]/stack_trace.c:16
  // CHECK: #9 {{.*}} in baz.dfsan [[FILEPATH]]/stack_trace.c:20
  // CHECK: #10 {{.*}} in main [[FILEPATH]]/stack_trace.c:[[# @LINE - 7 ]]
  // CHECK: ==EOS==

  char tinybuf[8];
  size_t same_length = baz(8, tinybuf, sizeof(tinybuf));

  printf("==TRUNCATED OUTPUT==\n%s==EOS==\n", tinybuf);
  // CHECK: ==TRUNCATED OUTPUT==
  // CHECK:     #0 ==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]]

  buf[0] = '\0';
  length = baz(8, buf, 0);
  printf("Output=\"%s\"\n", buf);
  printf("Returned length: %zu\n", length);
  // CHECK: Output=""
  // CHECK: Returned length: [[#LEN]]
}