llvm-project
64 строки · 1.8 Кб
1// RUN: %clang_dfsan -gmlt %s -o %t && %run %t >%t.out 2>&1
2// RUN: FileCheck %s < %t.out
3
4#include <assert.h>
5#include <sanitizer/dfsan_interface.h>
6#include <stdio.h>
7#include <string.h>
8
9#define NOINLINE __attribute__((noinline))
10
11NOINLINE size_t bar(int depth, char *buf, size_t len) {
12if (!depth) {
13return dfsan_sprint_stack_trace(buf, len);
14}
15
16return bar(depth - 1, buf, len);
17}
18
19NOINLINE size_t baz(int depth, char *buf, size_t len) {
20return bar(depth, buf, len);
21}
22
23int main(int argc, char *argv[]) {
24char buf[3000];
25size_t length = dfsan_sprint_stack_trace(buf, sizeof(buf));
26assert(length < sizeof(buf));
27printf("==OUTPUT==\n%s==EOS==\n", buf);
28
29// CHECK: ==OUTPUT==
30// CHECK: #0 {{.*}} in main [[FILEPATH:.*]]/stack_trace.c:[[# @LINE - 5 ]]
31// CHECK: ==EOS==
32
33length = baz(8, buf, sizeof(buf));
34printf("==OUTPUT==\n%s==EOS==\n", buf);
35
36// CHECK: ==OUTPUT==
37// CHECK: #0 {{.*}} in bar.dfsan [[FILEPATH]]/stack_trace.c:13
38// CHECK-COUNT-8: #{{[1-9]+}} {{.*}} in bar.dfsan [[FILEPATH]]/stack_trace.c:16
39// CHECK: #9 {{.*}} in baz.dfsan [[FILEPATH]]/stack_trace.c:20
40// CHECK: #10 {{.*}} in main [[FILEPATH]]/stack_trace.c:[[# @LINE - 7 ]]
41// CHECK: ==EOS==
42
43char tinybuf[8];
44size_t same_length = baz(8, tinybuf, sizeof(tinybuf));
45
46printf("==TRUNCATED OUTPUT==\n%s==EOS==\n", tinybuf);
47// CHECK: ==TRUNCATED OUTPUT==
48// CHECK: #0 ==EOS==
49
50printf("Returned length: %zu\n", length);
51printf("Actual length: %zu\n", strlen(buf));
52printf("Returned length with truncation: %zu\n", same_length);
53
54// CHECK: Returned length: [[#LEN:]]
55// CHECK: Actual length: [[#LEN]]
56// CHECK: Returned length with truncation: [[#LEN]]
57
58buf[0] = '\0';
59length = baz(8, buf, 0);
60printf("Output=\"%s\"\n", buf);
61printf("Returned length: %zu\n", length);
62// CHECK: Output=""
63// CHECK: Returned length: [[#LEN]]
64}
65