]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.cc
Update compiler-rt to 3.7.0 release. This also includes the sanitizer
[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::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
30 uptr StackTrace::GetCurrentPc() {
31   return GET_CALLER_PC();
32 }
33
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 // Check if given pointer points into allocated stack area.
44 static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
45   return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr);
46 }
47
48 // In GCC on ARM bp points to saved lr, not fp, so we should check the next
49 // cell in stack to be a saved frame pointer. GetCanonicFrame returns the
50 // pointer to saved frame pointer in any case.
51 static inline uhwptr *GetCanonicFrame(uptr bp,
52                                       uptr stack_top,
53                                       uptr stack_bottom) {
54 #ifdef __arm__
55   if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
56   uhwptr *bp_prev = (uhwptr *)bp;
57   if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
58   // The next frame pointer does not look right. This could be a GCC frame, step
59   // back by 1 word and try again.
60   if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
61     return bp_prev - 1;
62   // Nope, this does not look right either. This means the frame after next does
63   // not have a valid frame pointer, but we can still extract the caller PC.
64   // Unfortunately, there is no way to decide between GCC and LLVM frame
65   // layouts. Assume LLVM.
66   return bp_prev;
67 #else
68   return (uhwptr*)bp;
69 #endif
70 }
71
72 void BufferedStackTrace::FastUnwindStack(uptr pc, uptr bp, uptr stack_top,
73                                          uptr stack_bottom, u32 max_depth) {
74   CHECK_GE(max_depth, 2);
75   trace_buffer[0] = pc;
76   size = 1;
77   if (stack_top < 4096) return;  // Sanity check for stack top.
78   uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
79   // Lowest possible address that makes sense as the next frame pointer.
80   // Goes up as we walk the stack.
81   uptr bottom = stack_bottom;
82   // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
83   while (IsValidFrame((uptr)frame, stack_top, bottom) &&
84          IsAligned((uptr)frame, sizeof(*frame)) &&
85          size < max_depth) {
86     uhwptr pc1 = frame[1];
87     if (pc1 != pc) {
88       trace_buffer[size++] = (uptr) pc1;
89     }
90     bottom = (uptr)frame;
91     frame = GetCanonicFrame((uptr)frame[0], stack_top, bottom);
92   }
93 }
94
95 static bool MatchPc(uptr cur_pc, uptr trace_pc, uptr threshold) {
96   return cur_pc - trace_pc <= threshold || trace_pc - cur_pc <= threshold;
97 }
98
99 void BufferedStackTrace::PopStackFrames(uptr count) {
100   CHECK_LT(count, size);
101   size -= count;
102   for (uptr i = 0; i < size; ++i) {
103     trace_buffer[i] = trace_buffer[i + count];
104   }
105 }
106
107 uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
108   // Use threshold to find PC in stack trace, as PC we want to unwind from may
109   // slightly differ from return address in the actual unwinded stack trace.
110   const int kPcThreshold = 304;
111   for (uptr i = 0; i < size; ++i) {
112     if (MatchPc(pc, trace[i], kPcThreshold))
113       return i;
114   }
115   return 0;
116 }
117
118 }  // namespace __sanitizer