]> git.ipfire.org Git - thirdparty/gcc.git/blob - libsanitizer/sanitizer_common/sanitizer_stacktrace.cc
Update GCC to autoconf 2.69, automake 1.15.1 (PR bootstrap/82856).
[thirdparty/gcc.git] / libsanitizer / sanitizer_common / sanitizer_stacktrace.cc
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 //===----------------------------------------------------------------------===//
11
12 #include "sanitizer_common.h"
13 #include "sanitizer_flags.h"
14 #include "sanitizer_stacktrace.h"
15
16 namespace __sanitizer {
17
18 uptr StackTrace::GetNextInstructionPc(uptr pc) {
19 #if defined(__mips__)
20 return pc + 8;
21 #elif defined(__powerpc__) || defined(__sparc__) || defined(__arm__) || \
22 defined(__aarch64__)
23 return pc + 4;
24 #else
25 return pc + 1;
26 #endif
27 }
28
29 uptr StackTrace::GetCurrentPc() {
30 return GET_CALLER_PC();
31 }
32
33 void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
34 size = cnt + !!extra_top_pc;
35 CHECK_LE(size, kStackTraceMax);
36 internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0]));
37 if (extra_top_pc)
38 trace_buffer[cnt] = extra_top_pc;
39 top_frame_bp = 0;
40 }
41
42 // Sparc implemention is in its own file.
43 #if !defined(__sparc__)
44
45 // In GCC on ARM bp points to saved lr, not fp, so we should check the next
46 // cell in stack to be a saved frame pointer. GetCanonicFrame returns the
47 // pointer to saved frame pointer in any case.
48 static inline uhwptr *GetCanonicFrame(uptr bp,
49 uptr stack_top,
50 uptr stack_bottom) {
51 #ifdef __arm__
52 if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
53 uhwptr *bp_prev = (uhwptr *)bp;
54 if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
55 // The next frame pointer does not look right. This could be a GCC frame, step
56 // back by 1 word and try again.
57 if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
58 return bp_prev - 1;
59 // Nope, this does not look right either. This means the frame after next does
60 // not have a valid frame pointer, but we can still extract the caller PC.
61 // Unfortunately, there is no way to decide between GCC and LLVM frame
62 // layouts. Assume GCC.
63 return bp_prev - 1;
64 #else
65 return (uhwptr*)bp;
66 #endif
67 }
68
69 void BufferedStackTrace::FastUnwindStack(uptr pc, uptr bp, uptr stack_top,
70 uptr stack_bottom, u32 max_depth) {
71 const uptr kPageSize = GetPageSizeCached();
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 #elif defined(__s390__)
94 uhwptr pc1 = frame[14];
95 #else
96 uhwptr pc1 = frame[1];
97 #endif
98 // Let's assume that any pointer in the 0th page (i.e. <0x1000 on i386 and
99 // x86_64) is invalid and stop unwinding here. If we're adding support for
100 // a platform where this isn't true, we need to reconsider this check.
101 if (pc1 < kPageSize)
102 break;
103 if (pc1 != pc) {
104 trace_buffer[size++] = (uptr) pc1;
105 }
106 bottom = (uptr)frame;
107 frame = GetCanonicFrame((uptr)frame[0], stack_top, bottom);
108 }
109 }
110
111 #endif // !defined(__sparc__)
112
113 void BufferedStackTrace::PopStackFrames(uptr count) {
114 CHECK_LT(count, size);
115 size -= count;
116 for (uptr i = 0; i < size; ++i) {
117 trace_buffer[i] = trace_buffer[i + count];
118 }
119 }
120
121 static uptr Distance(uptr a, uptr b) { return a < b ? b - a : a - b; }
122
123 uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
124 uptr best = 0;
125 for (uptr i = 1; i < size; ++i) {
126 if (Distance(trace[i], pc) < Distance(trace[best], pc)) best = i;
127 }
128 return best;
129 }
130
131 } // namespace __sanitizer