• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- 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 
14 #include "sanitizer_common.h"
15 #include "sanitizer_flags.h"
16 #include "sanitizer_stacktrace.h"
17 
18 namespace __sanitizer {
19 
GetNextInstructionPc(uptr pc)20 uptr StackTrace::GetNextInstructionPc(uptr pc) {
21 #if defined(__mips__)
22   return pc + 8;
23 #elif defined(__powerpc__)
24   return pc + 4;
25 #else
26   return pc + 1;
27 #endif
28 }
29 
GetCurrentPc()30 uptr StackTrace::GetCurrentPc() {
31   return GET_CALLER_PC();
32 }
33 
Init(const uptr * pcs,uptr cnt,uptr extra_top_pc)34 void 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 // In GCC on ARM bp points to saved lr, not fp, so we should check the next
44 // cell in stack to be a saved frame pointer. GetCanonicFrame returns the
45 // pointer to saved frame pointer in any case.
GetCanonicFrame(uptr bp,uptr stack_top,uptr stack_bottom)46 static inline uhwptr *GetCanonicFrame(uptr bp,
47                                       uptr stack_top,
48                                       uptr stack_bottom) {
49 #ifdef __arm__
50   if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
51   uhwptr *bp_prev = (uhwptr *)bp;
52   if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
53   // The next frame pointer does not look right. This could be a GCC frame, step
54   // back by 1 word and try again.
55   if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
56     return bp_prev - 1;
57   // Nope, this does not look right either. This means the frame after next does
58   // not have a valid frame pointer, but we can still extract the caller PC.
59   // Unfortunately, there is no way to decide between GCC and LLVM frame
60   // layouts. Assume LLVM.
61   return bp_prev;
62 #else
63   return (uhwptr*)bp;
64 #endif
65 }
66 
FastUnwindStack(uptr pc,uptr bp,uptr stack_top,uptr stack_bottom,u32 max_depth)67 void BufferedStackTrace::FastUnwindStack(uptr pc, uptr bp, uptr stack_top,
68                                          uptr stack_bottom, u32 max_depth) {
69   const uptr kPageSize = GetPageSizeCached();
70   CHECK_GE(max_depth, 2);
71   trace_buffer[0] = pc;
72   size = 1;
73   if (stack_top < 4096) return;  // Sanity check for stack top.
74   uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
75   // Lowest possible address that makes sense as the next frame pointer.
76   // Goes up as we walk the stack.
77   uptr bottom = stack_bottom;
78   // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
79   while (IsValidFrame((uptr)frame, stack_top, bottom) &&
80          IsAligned((uptr)frame, sizeof(*frame)) &&
81          size < max_depth) {
82 #ifdef __powerpc__
83     // PowerPC ABIs specify that the return address is saved at offset
84     // 16 of the *caller's* stack frame.  Thus we must dereference the
85     // back chain to find the caller frame before extracting it.
86     uhwptr *caller_frame = (uhwptr*)frame[0];
87     if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) ||
88         !IsAligned((uptr)caller_frame, sizeof(uhwptr)))
89       break;
90     uhwptr pc1 = caller_frame[2];
91 #elif defined(__s390__)
92     uhwptr pc1 = frame[14];
93 #else
94     uhwptr pc1 = frame[1];
95 #endif
96     // Let's assume that any pointer in the 0th page (i.e. <0x1000 on i386 and
97     // x86_64) is invalid and stop unwinding here.  If we're adding support for
98     // a platform where this isn't true, we need to reconsider this check.
99     if (pc1 < kPageSize)
100       break;
101     if (pc1 != pc) {
102       trace_buffer[size++] = (uptr) pc1;
103     }
104     bottom = (uptr)frame;
105     frame = GetCanonicFrame((uptr)frame[0], stack_top, bottom);
106   }
107 }
108 
MatchPc(uptr cur_pc,uptr trace_pc,uptr threshold)109 static bool MatchPc(uptr cur_pc, uptr trace_pc, uptr threshold) {
110   return cur_pc - trace_pc <= threshold || trace_pc - cur_pc <= threshold;
111 }
112 
PopStackFrames(uptr count)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 
LocatePcInTrace(uptr pc)121 uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
122   // Use threshold to find PC in stack trace, as PC we want to unwind from may
123   // slightly differ from return address in the actual unwinded stack trace.
124   const int kPcThreshold = 350;
125   for (uptr i = 0; i < size; ++i) {
126     if (MatchPc(pc, trace[i], kPcThreshold))
127       return i;
128   }
129   return 0;
130 }
131 
132 }  // namespace __sanitizer
133