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