Skip to content

Commit 3368a32

Browse files
authored
[sanitizer_common] Fix UnwindFast on SPARC (#101634)
``` UBSan-Standalone-sparc :: TestCases/Misc/Linux/diag-stacktrace.cpp ``` `FAIL`s on 32 and 64-bit Linux/sparc64 (and on Solaris/sparcv9, too: the test isn't Linux-specific at all). With `UBSAN_OPTIONS=fast_unwind_on_fatal=1`, the stack trace shows a duplicate innermost frame: ``` compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:14:31: runtime error: execution reached the end of a value-returning function without returning a value #0 0x7003a708 in f() compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:14:35 #1 0x7003a708 in f() compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:14:35 #2 0x7003a714 in g() compiler-rt/test/ubsan/TestCases/Misc/Linux/diag-stacktrace.cpp:17:38 ``` which isn't seen with `fast_unwind_on_fatal=0`. This turns out to be another fallout from fixing `__builtin_return_address`/`__builtin_extract_return_addr` on SPARC. In `sanitizer_stacktrace_sparc.cpp` (`BufferedStackTrace::UnwindFast`) the `pc` arg is the return address, while `pc1` from the stack frame (`fr_savpc`) is the address of the `call` insn, leading to a double entry for the innermost frame in `trace_buffer[]`. This patch fixes this by moving the adjustment before all uses. Tested on `sparc64-unknown-linux-gnu` and `sparcv9-sun-solaris2.11` (with the `ubsan/TestCases/Misc/Linux` tests enabled).
1 parent 63a7786 commit 3368a32

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_stacktrace_sparc.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,16 @@ void BufferedStackTrace::UnwindFast(uptr pc, uptr bp, uptr stack_top,
5858
// Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
5959
while (IsValidFrame(bp, stack_top, bottom) && IsAligned(bp, sizeof(uhwptr)) &&
6060
size < max_depth) {
61-
uhwptr pc1 = ((uhwptr *)bp)[15];
61+
// %o7 contains the address of the call instruction and not the
62+
// return address, so we need to compensate.
63+
uhwptr pc1 = GetNextInstructionPc(((uhwptr *)bp)[15]);
6264
// Let's assume that any pointer in the 0th page is invalid and
6365
// stop unwinding here. If we're adding support for a platform
6466
// where this isn't true, we need to reconsider this check.
6567
if (pc1 < kPageSize)
6668
break;
67-
if (pc1 != pc) {
68-
// %o7 contains the address of the call instruction and not the
69-
// return address, so we need to compensate.
70-
trace_buffer[size++] = GetNextInstructionPc((uptr)pc1);
71-
}
69+
if (pc1 != pc)
70+
trace_buffer[size++] = pc1;
7271
bottom = bp;
7372
bp = (uptr)((uhwptr *)bp)[14] + STACK_BIAS;
7473
}

0 commit comments

Comments
 (0)