function.c (assign_parm_adjust_stack_rtl): Revise STRICT_ALIGNMENT check to use targe...
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_stacktrace.cc
blobf1c514d49520c8f67cd06fd1be5171f0ed799f90
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(__sparc__) || defined(__mips__)
20 return pc + 8;
21 #elif defined(__powerpc__) || defined(__arm__) || defined(__aarch64__)
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 // Sparc implemention is in its own file.
42 #if !defined(__sparc__)
44 // In GCC on ARM bp points to saved lr, not fp, so we should check the next
45 // cell in stack to be a saved frame pointer. GetCanonicFrame returns the
46 // pointer to saved frame pointer in any case.
47 static inline uhwptr *GetCanonicFrame(uptr bp,
48 uptr stack_top,
49 uptr stack_bottom) {
50 #ifdef __arm__
51 if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
52 uhwptr *bp_prev = (uhwptr *)bp;
53 if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
54 // The next frame pointer does not look right. This could be a GCC frame, step
55 // back by 1 word and try again.
56 if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
57 return bp_prev - 1;
58 // Nope, this does not look right either. This means the frame after next does
59 // not have a valid frame pointer, but we can still extract the caller PC.
60 // Unfortunately, there is no way to decide between GCC and LLVM frame
61 // layouts. Assume GCC.
62 return bp_prev - 1;
63 #else
64 return (uhwptr*)bp;
65 #endif
68 void BufferedStackTrace::FastUnwindStack(uptr pc, uptr bp, uptr stack_top,
69 uptr stack_bottom, u32 max_depth) {
70 const uptr kPageSize = GetPageSizeCached();
71 CHECK_GE(max_depth, 2);
72 trace_buffer[0] = pc;
73 size = 1;
74 if (stack_top < 4096) return; // Sanity check for stack top.
75 uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
76 // Lowest possible address that makes sense as the next frame pointer.
77 // Goes up as we walk the stack.
78 uptr bottom = stack_bottom;
79 // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
80 while (IsValidFrame((uptr)frame, stack_top, bottom) &&
81 IsAligned((uptr)frame, sizeof(*frame)) &&
82 size < max_depth) {
83 #ifdef __powerpc__
84 // PowerPC ABIs specify that the return address is saved on the
85 // *caller's* stack frame. Thus we must dereference the back chain
86 // to find the caller frame before extracting it.
87 uhwptr *caller_frame = (uhwptr*)frame[0];
88 if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) ||
89 !IsAligned((uptr)caller_frame, sizeof(uhwptr)))
90 break;
91 // For most ABIs the offset where the return address is saved is two
92 // register sizes. The exception is the SVR4 ABI, which uses an
93 // offset of only one register size.
94 #ifdef _CALL_SYSV
95 uhwptr pc1 = caller_frame[1];
96 #else
97 uhwptr pc1 = caller_frame[2];
98 #endif
99 #elif defined(__s390__)
100 uhwptr pc1 = frame[14];
101 #else
102 uhwptr pc1 = frame[1];
103 #endif
104 // Let's assume that any pointer in the 0th page (i.e. <0x1000 on i386 and
105 // x86_64) is invalid and stop unwinding here. If we're adding support for
106 // a platform where this isn't true, we need to reconsider this check.
107 if (pc1 < kPageSize)
108 break;
109 if (pc1 != pc) {
110 trace_buffer[size++] = (uptr) pc1;
112 bottom = (uptr)frame;
113 frame = GetCanonicFrame((uptr)frame[0], stack_top, bottom);
117 #endif // !defined(__sparc__)
119 void BufferedStackTrace::PopStackFrames(uptr count) {
120 CHECK_LT(count, size);
121 size -= count;
122 for (uptr i = 0; i < size; ++i) {
123 trace_buffer[i] = trace_buffer[i + count];
127 static uptr Distance(uptr a, uptr b) { return a < b ? b - a : a - b; }
129 uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
130 uptr best = 0;
131 for (uptr i = 1; i < size; ++i) {
132 if (Distance(trace[i], pc) < Distance(trace[best], pc)) best = i;
134 return best;
137 } // namespace __sanitizer