2016-01-21 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_stacktrace.cc
blob796d472a1eba948bbdad06aac920f4ba7d9e75ae
1 //===-- sanitizer_stacktrace.cc -------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries.
10 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common.h"
13 #include "sanitizer_flags.h"
14 #include "sanitizer_stacktrace.h"
16 namespace __sanitizer {
18 uptr StackTrace::GetNextInstructionPc(uptr pc) {
19 #if defined(__mips__)
20 return pc + 8;
21 #elif defined(__powerpc__)
22 return pc + 4;
23 #else
24 return pc + 1;
25 #endif
28 uptr StackTrace::GetCurrentPc() {
29 return GET_CALLER_PC();
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;
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);
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 GCC.
64 return bp_prev - 1;
65 #else
66 return (uhwptr*)bp;
67 #endif
70 void BufferedStackTrace::FastUnwindStack(uptr pc, uptr bp, uptr stack_top,
71 uptr stack_bottom, u32 max_depth) {
72 CHECK_GE(max_depth, 2);
73 trace_buffer[0] = pc;
74 size = 1;
75 if (stack_top < 4096) return; // Sanity check for stack top.
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;
80 // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
81 while (IsValidFrame((uptr)frame, stack_top, bottom) &&
82 IsAligned((uptr)frame, sizeof(*frame)) &&
83 size < max_depth) {
84 #ifdef __powerpc__
85 // PowerPC ABIs specify that the return address is saved at offset
86 // 16 of the *caller's* stack frame. Thus we must dereference the
87 // back chain to find the caller frame before extracting it.
88 uhwptr *caller_frame = (uhwptr*)frame[0];
89 if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) ||
90 !IsAligned((uptr)caller_frame, sizeof(uhwptr)))
91 break;
92 uhwptr pc1 = caller_frame[2];
93 #else
94 uhwptr pc1 = frame[1];
95 #endif
96 if (pc1 != pc) {
97 trace_buffer[size++] = (uptr) pc1;
99 bottom = (uptr)frame;
100 frame = GetCanonicFrame((uptr)frame[0], stack_top, bottom);
104 static bool MatchPc(uptr cur_pc, uptr trace_pc, uptr threshold) {
105 return cur_pc - trace_pc <= threshold || trace_pc - cur_pc <= threshold;
108 void BufferedStackTrace::PopStackFrames(uptr count) {
109 CHECK_LT(count, size);
110 size -= count;
111 for (uptr i = 0; i < size; ++i) {
112 trace_buffer[i] = trace_buffer[i + count];
116 uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
117 // Use threshold to find PC in stack trace, as PC we want to unwind from may
118 // slightly differ from return address in the actual unwinded stack trace.
119 const int kPcThreshold = 304;
120 for (uptr i = 0; i < size; ++i) {
121 if (MatchPc(pc, trace[i], kPcThreshold))
122 return i;
124 return 0;
127 } // namespace __sanitizer