2012-08-28 22:27:06 +08:00
|
|
|
//===-- sanitizer_stacktrace.cc -------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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) {
|
|
|
|
#if defined(__mips__)
|
|
|
|
return pc + 8;
|
2015-06-25 14:22:31 +08:00
|
|
|
#elif defined(__powerpc__)
|
|
|
|
return pc + 4;
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-04 05:10:44 +08:00
|
|
|
// Check if given pointer points into allocated stack area.
|
|
|
|
static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
|
|
|
|
return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
void BufferedStackTrace::FastUnwindStack(uptr pc, uptr bp, uptr stack_top,
|
2015-01-22 21:33:16 +08:00
|
|
|
uptr stack_bottom, u32 max_depth) {
|
2014-03-04 20:21:28 +08:00
|
|
|
CHECK_GE(max_depth, 2);
|
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__
|
|
|
|
uhwptr *caller_frame = (uhwptr*)frame[0];
|
|
|
|
if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) ||
|
|
|
|
!IsAligned((uptr)caller_frame, sizeof(uhwptr)))
|
|
|
|
break;
|
|
|
|
uhwptr pc1 = caller_frame[2];
|
|
|
|
#else
|
2014-05-21 15:34:03 +08:00
|
|
|
uhwptr pc1 = frame[1];
|
2015-07-29 03:41:49 +08:00
|
|
|
#endif
|
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;
|
|
|
|
frame = GetCanonicFrame((uptr)frame[0], stack_top, bottom);
|
2012-11-20 15:00:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-09 21:59:12 +08:00
|
|
|
static bool MatchPc(uptr cur_pc, uptr trace_pc, uptr threshold) {
|
|
|
|
return cur_pc - trace_pc <= threshold || trace_pc - cur_pc <= threshold;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
|
2013-11-13 23:20:10 +08:00
|
|
|
// Use threshold to find PC in stack trace, as PC we want to unwind from may
|
|
|
|
// slightly differ from return address in the actual unwinded stack trace.
|
2014-12-16 09:23:03 +08:00
|
|
|
const int kPcThreshold = 304;
|
2013-11-15 18:57:56 +08:00
|
|
|
for (uptr i = 0; i < size; ++i) {
|
2013-11-13 23:20:10 +08:00
|
|
|
if (MatchPc(pc, trace[i], kPcThreshold))
|
2013-11-09 21:59:12 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-28 22:27:06 +08:00
|
|
|
} // namespace __sanitizer
|