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