Skip to content

Commit

Permalink
[ubsan] Fix print_stacktrace=1:fast_unwind_on_fatal=0 to correctly fa…
Browse files Browse the repository at this point in the history
…llback to fast unwinder

ubsan_GetStackTrace (from 52b7510) called by
~ScopeReport leaves top/bottom zeroes in the
`!WillUseFastUnwind(request_fast_unwind)` code path.
When BufferedStackTrace::Unwind falls back to UnwindFast,
`if (stack_top < 4096) return;` will return early, leaving just one frame in the stack trace.

Fix this by always initializing top/bottom like 261d6e0.

Reviewed By: eugenis, yln

Differential Revision: https://reviews.llvm.org/D123562
  • Loading branch information
MaskRay committed Apr 12, 2022
1 parent deadda7 commit fdd424e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
12 changes: 5 additions & 7 deletions compiler-rt/lib/ubsan/ubsan_diag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ using namespace __ubsan;
// Windows.
// TODO(yln): This is a temporary workaround. GetStackTrace functions will be
// removed in the future.
void ubsan_GetStackTrace(BufferedStackTrace *stack, uptr max_depth,
uptr pc, uptr bp, void *context, bool fast) {
void ubsan_GetStackTrace(BufferedStackTrace *stack, uptr max_depth, uptr pc,
uptr bp, void *context, bool request_fast) {
uptr top = 0;
uptr bottom = 0;
if (StackTrace::WillUseFastUnwind(fast)) {
GetThreadStackTopAndBottom(false, &top, &bottom);
stack->Unwind(max_depth, pc, bp, nullptr, top, bottom, true);
} else
stack->Unwind(max_depth, pc, bp, context, 0, 0, false);
GetThreadStackTopAndBottom(false, &top, &bottom);
bool fast = StackTrace::WillUseFastUnwind(request_fast);
stack->Unwind(max_depth, pc, bp, context, top, bottom, fast);
}

static void MaybePrintStackTrace(uptr pc, uptr bp) {
Expand Down
22 changes: 22 additions & 0 deletions compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// Fast unwinder does not work with Thumb code
// UNSUPPORTED: thumb

// RUN: %clangxx -fsanitize=return %gmlt -O2 -fno-omit-frame-pointer -fasynchronous-unwind-tables %s -o %t
// RUN: %env_ubsan_opts=print_stacktrace=1:fast_unwind_on_fatal=0 not %run %t 2>&1 | FileCheck %s
// RUN: %env_ubsan_opts=print_stacktrace=1:fast_unwind_on_fatal=1 not %run %t 2>&1 | FileCheck %s
// RUN: %clangxx -fsanitize=return %gmlt -O2 -fno-omit-frame-pointer -fno-exceptions -fno-asynchronous-unwind-tables %s -o %t
// RUN: %env_ubsan_opts=print_stacktrace=1:fast_unwind_on_fatal=0 not %run %t 2>&1 | FileCheck %s
// RUN: %env_ubsan_opts=print_stacktrace=1:fast_unwind_on_fatal=1 not %run %t 2>&1 | FileCheck %s

// CHECK: runtime error: execution reached the end of a value-returning function without returning a value
// CHECK-NEXT: #0 {{.*}}f() {{.*}}.cpp:[[#@LINE+1]]
__attribute__((noinline)) int f() {}

// CHECK-NEXT: #1 {{.*}}g() {{.*}}.cpp:[[#@LINE+1]]
__attribute__((noinline)) void g() { f(); }

// CHECK-NEXT: #2 {{.*}}h() {{.*}}.cpp:[[#@LINE+1]]
__attribute__((noinline)) void h() { g(); }

// CHECK-NEXT: #3 {{.*}}main {{.*}}.cpp:[[#@LINE+1]]
int main() { h(); }

0 comments on commit fdd424e

Please sign in to comment.