]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.cc
Reintegrate head revisions r273096-r277147
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_stacktrace.cc
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
20 uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
21 #if defined(__arm__)
22   // Cancel Thumb bit.
23   pc = pc & (~1);
24 #endif
25 #if defined(__powerpc__) || defined(__powerpc64__)
26   // PCs are always 4 byte aligned.
27   return pc - 4;
28 #elif defined(__sparc__) || defined(__mips__)
29   return pc - 8;
30 #else
31   return pc - 1;
32 #endif
33 }
34
35 uptr StackTrace::GetNextInstructionPc(uptr pc) {
36 #if defined(__mips__)
37   return pc + 8;
38 #else
39   return pc + 1;
40 #endif
41 }
42
43 uptr StackTrace::GetCurrentPc() {
44   return GET_CALLER_PC();
45 }
46
47 void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
48   size = cnt + !!extra_top_pc;
49   CHECK_LE(size, kStackTraceMax);
50   internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0]));
51   if (extra_top_pc)
52     trace_buffer[cnt] = extra_top_pc;
53   top_frame_bp = 0;
54 }
55
56 // Check if given pointer points into allocated stack area.
57 static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
58   return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr);
59 }
60
61 // In GCC on ARM bp points to saved lr, not fp, so we should check the next
62 // cell in stack to be a saved frame pointer. GetCanonicFrame returns the
63 // pointer to saved frame pointer in any case.
64 static inline uhwptr *GetCanonicFrame(uptr bp,
65                                       uptr stack_top,
66                                       uptr stack_bottom) {
67 #ifdef __arm__
68   if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
69   uhwptr *bp_prev = (uhwptr *)bp;
70   if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
71   // The next frame pointer does not look right. This could be a GCC frame, step
72   // back by 1 word and try again.
73   if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
74     return bp_prev - 1;
75   // Nope, this does not look right either. This means the frame after next does
76   // not have a valid frame pointer, but we can still extract the caller PC.
77   // Unfortunately, there is no way to decide between GCC and LLVM frame
78   // layouts. Assume LLVM.
79   return bp_prev;
80 #else
81   return (uhwptr*)bp;
82 #endif
83 }
84
85 void BufferedStackTrace::FastUnwindStack(uptr pc, uptr bp, uptr stack_top,
86                                          uptr stack_bottom, uptr max_depth) {
87   CHECK_GE(max_depth, 2);
88   trace_buffer[0] = pc;
89   size = 1;
90   if (stack_top < 4096) return;  // Sanity check for stack top.
91   uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
92   // Lowest possible address that makes sense as the next frame pointer.
93   // Goes up as we walk the stack.
94   uptr bottom = stack_bottom;
95   // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
96   while (IsValidFrame((uptr)frame, stack_top, bottom) &&
97          IsAligned((uptr)frame, sizeof(*frame)) &&
98          size < max_depth) {
99     uhwptr pc1 = frame[1];
100     if (pc1 != pc) {
101       trace_buffer[size++] = (uptr) pc1;
102     }
103     bottom = (uptr)frame;
104     frame = GetCanonicFrame((uptr)frame[0], stack_top, bottom);
105   }
106 }
107
108 static bool MatchPc(uptr cur_pc, uptr trace_pc, uptr threshold) {
109   return cur_pc - trace_pc <= threshold || trace_pc - cur_pc <= threshold;
110 }
111
112 void BufferedStackTrace::PopStackFrames(uptr count) {
113   CHECK_LT(count, size);
114   size -= count;
115   for (uptr i = 0; i < size; ++i) {
116     trace_buffer[i] = trace_buffer[i + count];
117   }
118 }
119
120 uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
121   // Use threshold to find PC in stack trace, as PC we want to unwind from may
122   // slightly differ from return address in the actual unwinded stack trace.
123   const int kPcThreshold = 288;
124   for (uptr i = 0; i < size; ++i) {
125     if (MatchPc(pc, trace[i], kPcThreshold))
126       return i;
127   }
128   return 0;
129 }
130
131 }  // namespace __sanitizer