Skip to content

Commit 20d7fa1

Browse files
authored
[TySan] Added a 'print_stacktrace' flag for more detailed errors (#121756)
Raised in issue #121697
1 parent 30b7da7 commit 20d7fa1

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

compiler-rt/lib/tysan/tysan.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,14 @@ static void reportError(void *Addr, int Size, tysan_type_descriptor *TD,
197197
Printf("\n");
198198

199199
if (pc) {
200+
uptr top = 0;
201+
uptr bottom = 0;
202+
if (flags().print_stacktrace)
203+
GetThreadStackTopAndBottom(false, &top, &bottom);
200204

201205
bool request_fast = StackTrace::WillUseFastUnwind(true);
202206
BufferedStackTrace ST;
203-
ST.Unwind(kStackTraceMax, pc, bp, 0, 0, 0, request_fast);
207+
ST.Unwind(kStackTraceMax, pc, bp, 0, top, bottom, request_fast);
204208
ST.Print();
205209
} else {
206210
Printf("\n");

compiler-rt/lib/tysan/tysan_flags.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@
1515

1616
// TYSAN_FLAG(Type, Name, DefaultValue, Description)
1717
// See COMMON_FLAG in sanitizer_flags.inc for more details.
18+
19+
TYSAN_FLAG(bool, print_stacktrace, false,
20+
"Include full stacktrace into an error report")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_tysan -O0 %s -o %t && %run %t >%t.out 2>&1
2+
// RUN: FileCheck --check-prefixes=CHECK,CHECK-SHORT %s < %t.out
3+
4+
// RUN: %env_tysan_opts=print_stacktrace=1 %run %t >%t.out 2>&1
5+
// RUN: FileCheck --check-prefixes=CHECK,CHECK-LONG %s < %t.out
6+
7+
float *P;
8+
void zero_array() {
9+
int i;
10+
for (i = 0; i < 1; ++i)
11+
P[i] = 0.0f;
12+
// CHECK: ERROR: TypeSanitizer: type-aliasing-violation
13+
// CHECK: WRITE of size 4 at {{.*}} with type float accesses an existing object of type p1 float
14+
// CHECK: {{#0 0x.* in zero_array .*print_stacktrace.c:}}[[@LINE-3]]
15+
// CHECK-SHORT-NOT: {{#1 0x.* in main .*print_stacktrace.c}}
16+
// CHECK-LONG-NEXT: {{#1 0x.* in main .*print_stacktrace.c}}
17+
}
18+
19+
int main() {
20+
P = (float *)&P;
21+
zero_array();
22+
}

0 commit comments

Comments
 (0)