blob: 7862575b37bbd22d548d279e902688f02a2f8584 [file] [log] [blame]
Kostya Serebryanyc7be4072012-08-28 14:27:06 +00001//===-- 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 Serebryany9ada1f32012-08-28 14:48:28 +000014#include "sanitizer_common.h"
Alexey Samsonov90b0f1e2013-10-04 08:55:03 +000015#include "sanitizer_flags.h"
Kostya Serebryany9ada1f32012-08-28 14:48:28 +000016#include "sanitizer_stacktrace.h"
Kostya Serebryanyc7be4072012-08-28 14:27:06 +000017
18namespace __sanitizer {
Kostya Serebryanyc7be4072012-08-28 14:27:06 +000019
Stephen Hines6d186232014-11-26 17:56:19 -080020uptr StackTrace::GetNextInstructionPc(uptr pc) {
21#if defined(__mips__)
22 return pc + 8;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080023#elif defined(__powerpc__)
24 return pc + 4;
Stephen Hines6d186232014-11-26 17:56:19 -080025#else
26 return pc + 1;
27#endif
28}
29
Kostya Serebryanyc7be4072012-08-28 14:27:06 +000030uptr StackTrace::GetCurrentPc() {
31 return GET_CALLER_PC();
32}
33
Stephen Hines6d186232014-11-26 17:56:19 -080034void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
35 size = cnt + !!extra_top_pc;
36 CHECK_LE(size, kStackTraceMax);
37 internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0]));
38 if (extra_top_pc)
39 trace_buffer[cnt] = extra_top_pc;
40 top_frame_bp = 0;
41}
42
43// Check if given pointer points into allocated stack area.
44static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
45 return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr);
46}
47
48// In GCC on ARM bp points to saved lr, not fp, so we should check the next
49// cell in stack to be a saved frame pointer. GetCanonicFrame returns the
50// pointer to saved frame pointer in any case.
51static inline uhwptr *GetCanonicFrame(uptr bp,
52 uptr stack_top,
53 uptr stack_bottom) {
54#ifdef __arm__
55 if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
56 uhwptr *bp_prev = (uhwptr *)bp;
57 if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
58 // The next frame pointer does not look right. This could be a GCC frame, step
59 // back by 1 word and try again.
60 if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
61 return bp_prev - 1;
62 // Nope, this does not look right either. This means the frame after next does
63 // not have a valid frame pointer, but we can still extract the caller PC.
64 // Unfortunately, there is no way to decide between GCC and LLVM frame
65 // layouts. Assume LLVM.
66 return bp_prev;
67#else
68 return (uhwptr*)bp;
69#endif
70}
71
72void BufferedStackTrace::FastUnwindStack(uptr pc, uptr bp, uptr stack_top,
Stephen Hines86277eb2015-03-23 12:06:32 -070073 uptr stack_bottom, u32 max_depth) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -070074 CHECK_GE(max_depth, 2);
Stephen Hines6d186232014-11-26 17:56:19 -080075 trace_buffer[0] = pc;
Kostya Serebryanyc7be4072012-08-28 14:27:06 +000076 size = 1;
Kostya Serebryany8301c732013-06-18 14:47:40 +000077 if (stack_top < 4096) return; // Sanity check for stack top.
Stephen Hines6d186232014-11-26 17:56:19 -080078 uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
79 // Lowest possible address that makes sense as the next frame pointer.
80 // Goes up as we walk the stack.
81 uptr bottom = stack_bottom;
Reid Kleckner20aed572013-02-20 20:29:48 +000082 // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
Stephen Hines6d186232014-11-26 17:56:19 -080083 while (IsValidFrame((uptr)frame, stack_top, bottom) &&
Kostya Serebryany583025d2013-04-04 06:52:40 +000084 IsAligned((uptr)frame, sizeof(*frame)) &&
Alexey Samsonov3e0b8ff2013-10-12 12:40:47 +000085 size < max_depth) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080086#ifdef __powerpc__
87 // PowerPC ABIs specify that the return address is saved at offset
88 // 16 of the *caller's* stack frame. Thus we must dereference the
89 // back chain to find the caller frame before extracting it.
90 uhwptr *caller_frame = (uhwptr*)frame[0];
91 if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) ||
92 !IsAligned((uptr)caller_frame, sizeof(uhwptr)))
93 break;
94 uhwptr pc1 = caller_frame[2];
95#else
Bill Wendling7f790ca2013-12-16 02:36:46 +000096 uhwptr pc1 = frame[1];
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080097#endif
Kostya Serebryanyc7be4072012-08-28 14:27:06 +000098 if (pc1 != pc) {
Stephen Hines6d186232014-11-26 17:56:19 -080099 trace_buffer[size++] = (uptr) pc1;
Kostya Serebryanyc7be4072012-08-28 14:27:06 +0000100 }
Stephen Hines6d186232014-11-26 17:56:19 -0800101 bottom = (uptr)frame;
102 frame = GetCanonicFrame((uptr)frame[0], stack_top, bottom);
Bill Wendling7f790ca2013-12-16 02:36:46 +0000103 }
104}
105
Timur Iskhodzhanov7177d2b2013-11-09 13:59:12 +0000106static bool MatchPc(uptr cur_pc, uptr trace_pc, uptr threshold) {
107 return cur_pc - trace_pc <= threshold || trace_pc - cur_pc <= threshold;
108}
109
Stephen Hines6d186232014-11-26 17:56:19 -0800110void BufferedStackTrace::PopStackFrames(uptr count) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700111 CHECK_LT(count, size);
112 size -= count;
113 for (uptr i = 0; i < size; ++i) {
Stephen Hines6d186232014-11-26 17:56:19 -0800114 trace_buffer[i] = trace_buffer[i + count];
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700115 }
116}
117
Stephen Hines6d186232014-11-26 17:56:19 -0800118uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
Alexey Samsonov24d775e2013-11-13 15:20:10 +0000119 // Use threshold to find PC in stack trace, as PC we want to unwind from may
120 // slightly differ from return address in the actual unwinded stack trace.
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800121 const int kPcThreshold = 320;
Alexey Samsonov6a58b002013-11-15 10:57:56 +0000122 for (uptr i = 0; i < size; ++i) {
Alexey Samsonov24d775e2013-11-13 15:20:10 +0000123 if (MatchPc(pc, trace[i], kPcThreshold))
Timur Iskhodzhanov7177d2b2013-11-09 13:59:12 +0000124 return i;
125 }
126 return 0;
127}
128
Kostya Serebryanyc7be4072012-08-28 14:27:06 +0000129} // namespace __sanitizer