xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.cpp (revision 0d8fe2373503aeac48492f28073049a8bfa4feb5)
1 //===-- sanitizer_stacktrace.cpp ------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries.
11 //===----------------------------------------------------------------------===//
12 
13 #include "sanitizer_stacktrace.h"
14 
15 #include "sanitizer_common.h"
16 #include "sanitizer_flags.h"
17 #include "sanitizer_platform.h"
18 
19 namespace __sanitizer {
20 
21 uptr StackTrace::GetNextInstructionPc(uptr pc) {
22 #if defined(__sparc__) || defined(__mips__)
23   return pc + 8;
24 #elif defined(__powerpc__) || defined(__arm__) || defined(__aarch64__)
25   return pc + 4;
26 #elif SANITIZER_RISCV64
27   // Current check order is 4 -> 2 -> 6 -> 8
28   u8 InsnByte = *(u8 *)(pc);
29   if (((InsnByte & 0x3) == 0x3) && ((InsnByte & 0x1c) != 0x1c)) {
30     // xxxxxxxxxxxbbb11 | 32 bit | bbb != 111
31     return pc + 4;
32   }
33   if ((InsnByte & 0x3) != 0x3) {
34     // xxxxxxxxxxxxxxaa | 16 bit | aa != 11
35     return pc + 2;
36   }
37   // RISC-V encoding allows instructions to be up to 8 bytes long
38   if ((InsnByte & 0x3f) == 0x1f) {
39     // xxxxxxxxxx011111 | 48 bit |
40     return pc + 6;
41   }
42   if ((InsnByte & 0x7f) == 0x3f) {
43     // xxxxxxxxx0111111 | 64 bit |
44     return pc + 8;
45   }
46   // bail-out if could not figure out the instruction size
47   return 0;
48 #else
49   return pc + 1;
50 #endif
51 }
52 
53 uptr StackTrace::GetCurrentPc() {
54   return GET_CALLER_PC();
55 }
56 
57 void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
58   size = cnt + !!extra_top_pc;
59   CHECK_LE(size, kStackTraceMax);
60   internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0]));
61   if (extra_top_pc)
62     trace_buffer[cnt] = extra_top_pc;
63   top_frame_bp = 0;
64 }
65 
66 // Sparc implemention is in its own file.
67 #if !defined(__sparc__)
68 
69 // In GCC on ARM bp points to saved lr, not fp, so we should check the next
70 // cell in stack to be a saved frame pointer. GetCanonicFrame returns the
71 // pointer to saved frame pointer in any case.
72 static inline uhwptr *GetCanonicFrame(uptr bp,
73                                       uptr stack_top,
74                                       uptr stack_bottom) {
75   CHECK_GT(stack_top, stack_bottom);
76 #ifdef __arm__
77   if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
78   uhwptr *bp_prev = (uhwptr *)bp;
79   if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
80   // The next frame pointer does not look right. This could be a GCC frame, step
81   // back by 1 word and try again.
82   if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
83     return bp_prev - 1;
84   // Nope, this does not look right either. This means the frame after next does
85   // not have a valid frame pointer, but we can still extract the caller PC.
86   // Unfortunately, there is no way to decide between GCC and LLVM frame
87   // layouts. Assume LLVM.
88   return bp_prev;
89 #else
90   return (uhwptr*)bp;
91 #endif
92 }
93 
94 void BufferedStackTrace::UnwindFast(uptr pc, uptr bp, uptr stack_top,
95                                     uptr stack_bottom, u32 max_depth) {
96   // TODO(yln): add arg sanity check for stack_top/stack_bottom
97   CHECK_GE(max_depth, 2);
98   const uptr kPageSize = GetPageSizeCached();
99   trace_buffer[0] = pc;
100   size = 1;
101   if (stack_top < 4096) return;  // Sanity check for stack top.
102   uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
103   // Lowest possible address that makes sense as the next frame pointer.
104   // Goes up as we walk the stack.
105   uptr bottom = stack_bottom;
106   // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
107   while (IsValidFrame((uptr)frame, stack_top, bottom) &&
108          IsAligned((uptr)frame, sizeof(*frame)) &&
109          size < max_depth) {
110 #ifdef __powerpc__
111     // PowerPC ABIs specify that the return address is saved at offset
112     // 16 of the *caller's* stack frame.  Thus we must dereference the
113     // back chain to find the caller frame before extracting it.
114     uhwptr *caller_frame = (uhwptr*)frame[0];
115     if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) ||
116         !IsAligned((uptr)caller_frame, sizeof(uhwptr)))
117       break;
118     uhwptr pc1 = caller_frame[2];
119 #elif defined(__s390__)
120     uhwptr pc1 = frame[14];
121 #elif defined(__riscv)
122     // frame[-1] contains the return address
123     uhwptr pc1 = frame[-1];
124 #else
125     uhwptr pc1 = frame[1];
126 #endif
127     // Let's assume that any pointer in the 0th page (i.e. <0x1000 on i386 and
128     // x86_64) is invalid and stop unwinding here.  If we're adding support for
129     // a platform where this isn't true, we need to reconsider this check.
130     if (pc1 < kPageSize)
131       break;
132     if (pc1 != pc) {
133       trace_buffer[size++] = (uptr) pc1;
134     }
135     bottom = (uptr)frame;
136 #if defined(__riscv)
137     // frame[-2] contain fp of the previous frame
138     uptr new_bp = (uptr)frame[-2];
139 #else
140     uptr new_bp = (uptr)frame[0];
141 #endif
142     frame = GetCanonicFrame(new_bp, stack_top, bottom);
143   }
144 }
145 
146 #endif  // !defined(__sparc__)
147 
148 void BufferedStackTrace::PopStackFrames(uptr count) {
149   CHECK_LT(count, size);
150   size -= count;
151   for (uptr i = 0; i < size; ++i) {
152     trace_buffer[i] = trace_buffer[i + count];
153   }
154 }
155 
156 static uptr Distance(uptr a, uptr b) { return a < b ? b - a : a - b; }
157 
158 uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
159   uptr best = 0;
160   for (uptr i = 1; i < size; ++i) {
161     if (Distance(trace[i], pc) < Distance(trace[best], pc)) best = i;
162   }
163   return best;
164 }
165 
166 }  // namespace __sanitizer
167