Kostya Serebryany | c7be407 | 2012-08-28 14:27:06 +0000 | [diff] [blame] | 1 | //===-- sanitizer_stacktrace.cc -------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is shared between AddressSanitizer and ThreadSanitizer |
| 11 | // run-time libraries. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Kostya Serebryany | 9ada1f3 | 2012-08-28 14:48:28 +0000 | [diff] [blame] | 14 | #include "sanitizer_common.h" |
Alexey Samsonov | 90b0f1e | 2013-10-04 08:55:03 +0000 | [diff] [blame] | 15 | #include "sanitizer_flags.h" |
Kostya Serebryany | 9ada1f3 | 2012-08-28 14:48:28 +0000 | [diff] [blame] | 16 | #include "sanitizer_stacktrace.h" |
Kostya Serebryany | c7be407 | 2012-08-28 14:27:06 +0000 | [diff] [blame] | 17 | |
| 18 | namespace __sanitizer { |
Kostya Serebryany | c7be407 | 2012-08-28 14:27:06 +0000 | [diff] [blame] | 19 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 20 | uptr StackTrace::GetNextInstructionPc(uptr pc) { |
| 21 | #if defined(__mips__) |
| 22 | return pc + 8; |
| 23 | #else |
| 24 | return pc + 1; |
| 25 | #endif |
| 26 | } |
| 27 | |
Kostya Serebryany | c7be407 | 2012-08-28 14:27:06 +0000 | [diff] [blame] | 28 | uptr StackTrace::GetCurrentPc() { |
| 29 | return GET_CALLER_PC(); |
| 30 | } |
| 31 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 32 | void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) { |
| 33 | size = cnt + !!extra_top_pc; |
| 34 | CHECK_LE(size, kStackTraceMax); |
| 35 | internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0])); |
| 36 | if (extra_top_pc) |
| 37 | trace_buffer[cnt] = extra_top_pc; |
| 38 | top_frame_bp = 0; |
| 39 | } |
| 40 | |
| 41 | // Check if given pointer points into allocated stack area. |
| 42 | static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) { |
| 43 | return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr); |
| 44 | } |
| 45 | |
| 46 | // In GCC on ARM bp points to saved lr, not fp, so we should check the next |
| 47 | // cell in stack to be a saved frame pointer. GetCanonicFrame returns the |
| 48 | // pointer to saved frame pointer in any case. |
| 49 | static inline uhwptr *GetCanonicFrame(uptr bp, |
| 50 | uptr stack_top, |
| 51 | uptr stack_bottom) { |
| 52 | #ifdef __arm__ |
| 53 | if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0; |
| 54 | uhwptr *bp_prev = (uhwptr *)bp; |
| 55 | if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev; |
| 56 | // The next frame pointer does not look right. This could be a GCC frame, step |
| 57 | // back by 1 word and try again. |
| 58 | if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom)) |
| 59 | return bp_prev - 1; |
| 60 | // Nope, this does not look right either. This means the frame after next does |
| 61 | // not have a valid frame pointer, but we can still extract the caller PC. |
| 62 | // Unfortunately, there is no way to decide between GCC and LLVM frame |
| 63 | // layouts. Assume LLVM. |
| 64 | return bp_prev; |
| 65 | #else |
| 66 | return (uhwptr*)bp; |
| 67 | #endif |
| 68 | } |
| 69 | |
| 70 | void BufferedStackTrace::FastUnwindStack(uptr pc, uptr bp, uptr stack_top, |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 71 | uptr stack_bottom, u32 max_depth) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 72 | CHECK_GE(max_depth, 2); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 73 | trace_buffer[0] = pc; |
Kostya Serebryany | c7be407 | 2012-08-28 14:27:06 +0000 | [diff] [blame] | 74 | size = 1; |
Kostya Serebryany | 8301c73 | 2013-06-18 14:47:40 +0000 | [diff] [blame] | 75 | if (stack_top < 4096) return; // Sanity check for stack top. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 76 | uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom); |
| 77 | // Lowest possible address that makes sense as the next frame pointer. |
| 78 | // Goes up as we walk the stack. |
| 79 | uptr bottom = stack_bottom; |
Reid Kleckner | 20aed57 | 2013-02-20 20:29:48 +0000 | [diff] [blame] | 80 | // Avoid infinite loop when frame == frame[0] by using frame > prev_frame. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 81 | while (IsValidFrame((uptr)frame, stack_top, bottom) && |
Kostya Serebryany | 583025d | 2013-04-04 06:52:40 +0000 | [diff] [blame] | 82 | IsAligned((uptr)frame, sizeof(*frame)) && |
Alexey Samsonov | 3e0b8ff | 2013-10-12 12:40:47 +0000 | [diff] [blame] | 83 | size < max_depth) { |
Bill Wendling | 7f790ca | 2013-12-16 02:36:46 +0000 | [diff] [blame] | 84 | uhwptr pc1 = frame[1]; |
Kostya Serebryany | c7be407 | 2012-08-28 14:27:06 +0000 | [diff] [blame] | 85 | if (pc1 != pc) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 86 | trace_buffer[size++] = (uptr) pc1; |
Kostya Serebryany | c7be407 | 2012-08-28 14:27:06 +0000 | [diff] [blame] | 87 | } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 88 | bottom = (uptr)frame; |
| 89 | frame = GetCanonicFrame((uptr)frame[0], stack_top, bottom); |
Bill Wendling | 7f790ca | 2013-12-16 02:36:46 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Timur Iskhodzhanov | 7177d2b | 2013-11-09 13:59:12 +0000 | [diff] [blame] | 93 | static bool MatchPc(uptr cur_pc, uptr trace_pc, uptr threshold) { |
| 94 | return cur_pc - trace_pc <= threshold || trace_pc - cur_pc <= threshold; |
| 95 | } |
| 96 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 97 | void BufferedStackTrace::PopStackFrames(uptr count) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 98 | CHECK_LT(count, size); |
| 99 | size -= count; |
| 100 | for (uptr i = 0; i < size; ++i) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 101 | trace_buffer[i] = trace_buffer[i + count]; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 105 | uptr BufferedStackTrace::LocatePcInTrace(uptr pc) { |
Alexey Samsonov | 24d775e | 2013-11-13 15:20:10 +0000 | [diff] [blame] | 106 | // Use threshold to find PC in stack trace, as PC we want to unwind from may |
| 107 | // slightly differ from return address in the actual unwinded stack trace. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 108 | const int kPcThreshold = 304; |
Alexey Samsonov | 6a58b00 | 2013-11-15 10:57:56 +0000 | [diff] [blame] | 109 | for (uptr i = 0; i < size; ++i) { |
Alexey Samsonov | 24d775e | 2013-11-13 15:20:10 +0000 | [diff] [blame] | 110 | if (MatchPc(pc, trace[i], kPcThreshold)) |
Timur Iskhodzhanov | 7177d2b | 2013-11-09 13:59:12 +0000 | [diff] [blame] | 111 | return i; |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
Kostya Serebryany | c7be407 | 2012-08-28 14:27:06 +0000 | [diff] [blame] | 116 | } // namespace __sanitizer |