2019-08-01 02:51:27 +08:00
|
|
|
//===-- sanitizer_stacktrace.cpp ------------------------------------------===//
|
2012-08-28 22:27:06 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-08-28 22:27:06 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer
|
|
|
|
// run-time libraries.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-08-28 22:48:28 +08:00
|
|
|
#include "sanitizer_common.h"
|
2013-10-04 16:55:03 +08:00
|
|
|
#include "sanitizer_flags.h"
|
2012-08-28 22:48:28 +08:00
|
|
|
#include "sanitizer_stacktrace.h"
|
2012-08-28 22:27:06 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2014-11-20 05:42:33 +08:00
|
|
|
uptr StackTrace::GetNextInstructionPc(uptr pc) {
|
SanitizerCommon: fixes for unwinding & backtrace on SPARC
Summary:
This patch contains various fixes for the unwinding and backtrace machinery on the SPARC, which doesn't work correctly in various cases. It was tested with GCC on SPARC/Solaris and SPARC/Linux.
Patch by Eric Botcazou.
Reviewers: #sanitizers, vitalybuka
Reviewed By: #sanitizers, vitalybuka
Subscribers: jrtc27, delcypher, vitalybuka, ro, jyknight, kubamracek, fedor.sergeev, jdoerfert, llvm-commits, #sanitizers
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D58431
llvm-svn: 355965
2019-03-13 04:31:53 +08:00
|
|
|
#if defined(__sparc__) || defined(__mips__)
|
2014-11-20 05:42:33 +08:00
|
|
|
return pc + 8;
|
SanitizerCommon: fixes for unwinding & backtrace on SPARC
Summary:
This patch contains various fixes for the unwinding and backtrace machinery on the SPARC, which doesn't work correctly in various cases. It was tested with GCC on SPARC/Solaris and SPARC/Linux.
Patch by Eric Botcazou.
Reviewers: #sanitizers, vitalybuka
Reviewed By: #sanitizers, vitalybuka
Subscribers: jrtc27, delcypher, vitalybuka, ro, jyknight, kubamracek, fedor.sergeev, jdoerfert, llvm-commits, #sanitizers
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D58431
llvm-svn: 355965
2019-03-13 04:31:53 +08:00
|
|
|
#elif defined(__powerpc__) || defined(__arm__) || defined(__aarch64__)
|
2015-06-25 14:22:31 +08:00
|
|
|
return pc + 4;
|
2020-09-24 15:23:45 +08:00
|
|
|
#elif SANITIZER_RISCV64
|
|
|
|
// Current check order is 4 -> 2 -> 6 -> 8
|
|
|
|
u8 InsnByte = *(u8 *)(pc);
|
|
|
|
if (((InsnByte & 0x3) == 0x3) && ((InsnByte & 0x1c) != 0x1c)) {
|
|
|
|
// xxxxxxxxxxxbbb11 | 32 bit | bbb != 111
|
|
|
|
return pc + 4;
|
|
|
|
}
|
|
|
|
if ((InsnByte & 0x3) != 0x3) {
|
|
|
|
// xxxxxxxxxxxxxxaa | 16 bit | aa != 11
|
|
|
|
return pc + 2;
|
|
|
|
}
|
|
|
|
// RISC-V encoding allows instructions to be up to 8 bytes long
|
|
|
|
if ((InsnByte & 0x3f) == 0x1f) {
|
|
|
|
// xxxxxxxxxx011111 | 48 bit |
|
|
|
|
return pc + 6;
|
|
|
|
}
|
|
|
|
if ((InsnByte & 0x7f) == 0x3f) {
|
|
|
|
// xxxxxxxxx0111111 | 64 bit |
|
|
|
|
return pc + 8;
|
|
|
|
}
|
|
|
|
// bail-out if could not figure out the instruction size
|
|
|
|
return 0;
|
2014-11-20 05:42:33 +08:00
|
|
|
#else
|
|
|
|
return pc + 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-08-28 22:27:06 +08:00
|
|
|
uptr StackTrace::GetCurrentPc() {
|
|
|
|
return GET_CALLER_PC();
|
|
|
|
}
|
|
|
|
|
2014-11-04 06:23:44 +08:00
|
|
|
void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
|
|
|
|
size = cnt + !!extra_top_pc;
|
|
|
|
CHECK_LE(size, kStackTraceMax);
|
|
|
|
internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0]));
|
|
|
|
if (extra_top_pc)
|
|
|
|
trace_buffer[cnt] = extra_top_pc;
|
|
|
|
top_frame_bp = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-11 05:40:16 +08:00
|
|
|
// Sparc implemention is in its own file.
|
|
|
|
#if !defined(__sparc__)
|
|
|
|
|
2014-09-04 05:10:44 +08:00
|
|
|
// In GCC on ARM bp points to saved lr, not fp, so we should check the next
|
|
|
|
// cell in stack to be a saved frame pointer. GetCanonicFrame returns the
|
|
|
|
// pointer to saved frame pointer in any case.
|
|
|
|
static inline uhwptr *GetCanonicFrame(uptr bp,
|
|
|
|
uptr stack_top,
|
|
|
|
uptr stack_bottom) {
|
[Sanitizer] On Darwin `__sanitizer_print_stack_trace` only prints topmost frame
In compiler-rt we have the notion of a `fast` and a `slow` stack
unwinder. Darwin currently only supports the fast unwinder.
From reading the code, my understanding is that
`BufferedStackTrace::Unwind` can be called with `bp=0, stack_top=0,
stack_bottom=0, request_fast_unwind=false`. If
`request_fast_unwind=true`, then we alos need to supply bp, stack_top,
and stack_bottom.
However, `BufferedStackTrace::Unwind` uses
`StackTrace::WillUseFastUnwind` which will adapt `request_fast_unwind`
if the requested unwinder is not supported. On Darwin, the result is
that we don't pass actual values for bp, stack_top, and stack_bottom,
but end up using the fast unwinder. The tests then fail because we only
print the topmost stack frame.
This patch adds a check to `WillUseFastUnwind` at the point of usage to
avoid the mismatch between `request_fast_unwind` and what `Unwind`
actually does. I am also interested in cleaning up the
`request_fast_unwind` machinery so this patch just the simplest thing
possible so I can enable the tests.
Reviewers: vitalybuka, vsk
Differential Revision: https://reviews.llvm.org/D58156
llvm-svn: 354282
2019-02-19 02:47:49 +08:00
|
|
|
CHECK_GT(stack_top, stack_bottom);
|
2014-09-04 05:10:44 +08:00
|
|
|
#ifdef __arm__
|
|
|
|
if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
|
|
|
|
uhwptr *bp_prev = (uhwptr *)bp;
|
|
|
|
if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
|
2014-10-14 21:46:07 +08:00
|
|
|
// The next frame pointer does not look right. This could be a GCC frame, step
|
|
|
|
// back by 1 word and try again.
|
|
|
|
if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
|
|
|
|
return bp_prev - 1;
|
|
|
|
// Nope, this does not look right either. This means the frame after next does
|
|
|
|
// not have a valid frame pointer, but we can still extract the caller PC.
|
|
|
|
// Unfortunately, there is no way to decide between GCC and LLVM frame
|
|
|
|
// layouts. Assume LLVM.
|
|
|
|
return bp_prev;
|
2014-09-04 05:10:44 +08:00
|
|
|
#else
|
|
|
|
return (uhwptr*)bp;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-02-23 06:03:09 +08:00
|
|
|
void BufferedStackTrace::UnwindFast(uptr pc, uptr bp, uptr stack_top,
|
|
|
|
uptr stack_bottom, u32 max_depth) {
|
2019-02-27 09:38:54 +08:00
|
|
|
// TODO(yln): add arg sanity check for stack_top/stack_bottom
|
2014-03-04 20:21:28 +08:00
|
|
|
CHECK_GE(max_depth, 2);
|
2019-02-23 10:36:23 +08:00
|
|
|
const uptr kPageSize = GetPageSizeCached();
|
2014-10-26 11:35:14 +08:00
|
|
|
trace_buffer[0] = pc;
|
2012-08-28 22:27:06 +08:00
|
|
|
size = 1;
|
2013-06-18 22:47:40 +08:00
|
|
|
if (stack_top < 4096) return; // Sanity check for stack top.
|
2014-09-04 05:10:44 +08:00
|
|
|
uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
|
2014-10-14 21:46:07 +08:00
|
|
|
// Lowest possible address that makes sense as the next frame pointer.
|
|
|
|
// Goes up as we walk the stack.
|
|
|
|
uptr bottom = stack_bottom;
|
2013-02-21 04:29:48 +08:00
|
|
|
// Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
|
2014-10-14 21:46:07 +08:00
|
|
|
while (IsValidFrame((uptr)frame, stack_top, bottom) &&
|
2013-04-04 14:52:40 +08:00
|
|
|
IsAligned((uptr)frame, sizeof(*frame)) &&
|
2013-10-12 20:40:47 +08:00
|
|
|
size < max_depth) {
|
2015-07-29 03:41:49 +08:00
|
|
|
#ifdef __powerpc__
|
2017-03-17 09:15:39 +08:00
|
|
|
// PowerPC ABIs specify that the return address is saved at offset
|
|
|
|
// 16 of the *caller's* stack frame. Thus we must dereference the
|
|
|
|
// back chain to find the caller frame before extracting it.
|
|
|
|
uhwptr *caller_frame = (uhwptr*)frame[0];
|
|
|
|
if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) ||
|
|
|
|
!IsAligned((uptr)caller_frame, sizeof(uhwptr)))
|
|
|
|
break;
|
2015-07-29 03:41:49 +08:00
|
|
|
uhwptr pc1 = caller_frame[2];
|
2016-04-15 05:19:27 +08:00
|
|
|
#elif defined(__s390__)
|
|
|
|
uhwptr pc1 = frame[14];
|
2020-10-04 15:31:30 +08:00
|
|
|
#elif defined(__riscv)
|
|
|
|
// frame[-1] contains the return address
|
|
|
|
uhwptr pc1 = frame[-1];
|
2015-07-29 03:41:49 +08:00
|
|
|
#else
|
2014-05-21 15:34:03 +08:00
|
|
|
uhwptr pc1 = frame[1];
|
2015-07-29 03:41:49 +08:00
|
|
|
#endif
|
2016-06-28 20:53:20 +08:00
|
|
|
// Let's assume that any pointer in the 0th page (i.e. <0x1000 on i386 and
|
|
|
|
// x86_64) is invalid and stop unwinding here. If we're adding support for
|
|
|
|
// a platform where this isn't true, we need to reconsider this check.
|
2016-06-27 23:32:18 +08:00
|
|
|
if (pc1 < kPageSize)
|
|
|
|
break;
|
2012-08-28 22:27:06 +08:00
|
|
|
if (pc1 != pc) {
|
2014-10-26 11:35:14 +08:00
|
|
|
trace_buffer[size++] = (uptr) pc1;
|
2012-08-28 22:27:06 +08:00
|
|
|
}
|
2014-10-14 21:46:07 +08:00
|
|
|
bottom = (uptr)frame;
|
2020-10-04 15:31:30 +08:00
|
|
|
#if defined(__riscv)
|
|
|
|
// frame[-2] contain fp of the previous frame
|
|
|
|
uptr new_bp = (uptr)frame[-2];
|
|
|
|
#else
|
|
|
|
uptr new_bp = (uptr)frame[0];
|
|
|
|
#endif
|
|
|
|
frame = GetCanonicFrame(new_bp, stack_top, bottom);
|
2012-11-20 15:00:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 05:40:16 +08:00
|
|
|
#endif // !defined(__sparc__)
|
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
void BufferedStackTrace::PopStackFrames(uptr count) {
|
2013-12-10 16:30:39 +08:00
|
|
|
CHECK_LT(count, size);
|
2013-12-10 03:52:39 +08:00
|
|
|
size -= count;
|
|
|
|
for (uptr i = 0; i < size; ++i) {
|
2014-10-26 11:35:14 +08:00
|
|
|
trace_buffer[i] = trace_buffer[i + count];
|
2013-12-10 03:52:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 05:04:47 +08:00
|
|
|
static uptr Distance(uptr a, uptr b) { return a < b ? b - a : a - b; }
|
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
|
2017-01-06 05:04:47 +08:00
|
|
|
uptr best = 0;
|
|
|
|
for (uptr i = 1; i < size; ++i) {
|
|
|
|
if (Distance(trace[i], pc) < Distance(trace[best], pc)) best = i;
|
2013-11-09 21:59:12 +08:00
|
|
|
}
|
2017-01-06 05:04:47 +08:00
|
|
|
return best;
|
2013-11-09 21:59:12 +08:00
|
|
|
}
|
|
|
|
|
2012-08-28 22:27:06 +08:00
|
|
|
} // namespace __sanitizer
|