xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
168d75effSDimitry Andric //===-- sanitizer_stacktrace.cpp ------------------------------------------===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // This file is shared between AddressSanitizer and ThreadSanitizer
1068d75effSDimitry Andric // run-time libraries.
1168d75effSDimitry Andric //===----------------------------------------------------------------------===//
1268d75effSDimitry Andric 
13*e8d8bef9SDimitry Andric #include "sanitizer_stacktrace.h"
14*e8d8bef9SDimitry Andric 
1568d75effSDimitry Andric #include "sanitizer_common.h"
1668d75effSDimitry Andric #include "sanitizer_flags.h"
17*e8d8bef9SDimitry Andric #include "sanitizer_platform.h"
1868d75effSDimitry Andric 
1968d75effSDimitry Andric namespace __sanitizer {
2068d75effSDimitry Andric 
2168d75effSDimitry Andric uptr StackTrace::GetNextInstructionPc(uptr pc) {
2268d75effSDimitry Andric #if defined(__sparc__) || defined(__mips__)
2368d75effSDimitry Andric   return pc + 8;
2468d75effSDimitry Andric #elif defined(__powerpc__) || defined(__arm__) || defined(__aarch64__)
2568d75effSDimitry Andric   return pc + 4;
26*e8d8bef9SDimitry Andric #elif SANITIZER_RISCV64
27*e8d8bef9SDimitry Andric   // Current check order is 4 -> 2 -> 6 -> 8
28*e8d8bef9SDimitry Andric   u8 InsnByte = *(u8 *)(pc);
29*e8d8bef9SDimitry Andric   if (((InsnByte & 0x3) == 0x3) && ((InsnByte & 0x1c) != 0x1c)) {
30*e8d8bef9SDimitry Andric     // xxxxxxxxxxxbbb11 | 32 bit | bbb != 111
31*e8d8bef9SDimitry Andric     return pc + 4;
32*e8d8bef9SDimitry Andric   }
33*e8d8bef9SDimitry Andric   if ((InsnByte & 0x3) != 0x3) {
34*e8d8bef9SDimitry Andric     // xxxxxxxxxxxxxxaa | 16 bit | aa != 11
35*e8d8bef9SDimitry Andric     return pc + 2;
36*e8d8bef9SDimitry Andric   }
37*e8d8bef9SDimitry Andric   // RISC-V encoding allows instructions to be up to 8 bytes long
38*e8d8bef9SDimitry Andric   if ((InsnByte & 0x3f) == 0x1f) {
39*e8d8bef9SDimitry Andric     // xxxxxxxxxx011111 | 48 bit |
40*e8d8bef9SDimitry Andric     return pc + 6;
41*e8d8bef9SDimitry Andric   }
42*e8d8bef9SDimitry Andric   if ((InsnByte & 0x7f) == 0x3f) {
43*e8d8bef9SDimitry Andric     // xxxxxxxxx0111111 | 64 bit |
44*e8d8bef9SDimitry Andric     return pc + 8;
45*e8d8bef9SDimitry Andric   }
46*e8d8bef9SDimitry Andric   // bail-out if could not figure out the instruction size
47*e8d8bef9SDimitry Andric   return 0;
4868d75effSDimitry Andric #else
4968d75effSDimitry Andric   return pc + 1;
5068d75effSDimitry Andric #endif
5168d75effSDimitry Andric }
5268d75effSDimitry Andric 
5368d75effSDimitry Andric uptr StackTrace::GetCurrentPc() {
5468d75effSDimitry Andric   return GET_CALLER_PC();
5568d75effSDimitry Andric }
5668d75effSDimitry Andric 
5768d75effSDimitry Andric void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
5868d75effSDimitry Andric   size = cnt + !!extra_top_pc;
5968d75effSDimitry Andric   CHECK_LE(size, kStackTraceMax);
6068d75effSDimitry Andric   internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0]));
6168d75effSDimitry Andric   if (extra_top_pc)
6268d75effSDimitry Andric     trace_buffer[cnt] = extra_top_pc;
6368d75effSDimitry Andric   top_frame_bp = 0;
6468d75effSDimitry Andric }
6568d75effSDimitry Andric 
6668d75effSDimitry Andric // Sparc implemention is in its own file.
6768d75effSDimitry Andric #if !defined(__sparc__)
6868d75effSDimitry Andric 
6968d75effSDimitry Andric // In GCC on ARM bp points to saved lr, not fp, so we should check the next
7068d75effSDimitry Andric // cell in stack to be a saved frame pointer. GetCanonicFrame returns the
7168d75effSDimitry Andric // pointer to saved frame pointer in any case.
7268d75effSDimitry Andric static inline uhwptr *GetCanonicFrame(uptr bp,
7368d75effSDimitry Andric                                       uptr stack_top,
7468d75effSDimitry Andric                                       uptr stack_bottom) {
7568d75effSDimitry Andric   CHECK_GT(stack_top, stack_bottom);
7668d75effSDimitry Andric #ifdef __arm__
7768d75effSDimitry Andric   if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
7868d75effSDimitry Andric   uhwptr *bp_prev = (uhwptr *)bp;
7968d75effSDimitry Andric   if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
8068d75effSDimitry Andric   // The next frame pointer does not look right. This could be a GCC frame, step
8168d75effSDimitry Andric   // back by 1 word and try again.
8268d75effSDimitry Andric   if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
8368d75effSDimitry Andric     return bp_prev - 1;
8468d75effSDimitry Andric   // Nope, this does not look right either. This means the frame after next does
8568d75effSDimitry Andric   // not have a valid frame pointer, but we can still extract the caller PC.
8668d75effSDimitry Andric   // Unfortunately, there is no way to decide between GCC and LLVM frame
8768d75effSDimitry Andric   // layouts. Assume LLVM.
8868d75effSDimitry Andric   return bp_prev;
8968d75effSDimitry Andric #else
9068d75effSDimitry Andric   return (uhwptr*)bp;
9168d75effSDimitry Andric #endif
9268d75effSDimitry Andric }
9368d75effSDimitry Andric 
9468d75effSDimitry Andric void BufferedStackTrace::UnwindFast(uptr pc, uptr bp, uptr stack_top,
9568d75effSDimitry Andric                                     uptr stack_bottom, u32 max_depth) {
9668d75effSDimitry Andric   // TODO(yln): add arg sanity check for stack_top/stack_bottom
9768d75effSDimitry Andric   CHECK_GE(max_depth, 2);
9868d75effSDimitry Andric   const uptr kPageSize = GetPageSizeCached();
9968d75effSDimitry Andric   trace_buffer[0] = pc;
10068d75effSDimitry Andric   size = 1;
10168d75effSDimitry Andric   if (stack_top < 4096) return;  // Sanity check for stack top.
10268d75effSDimitry Andric   uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
10368d75effSDimitry Andric   // Lowest possible address that makes sense as the next frame pointer.
10468d75effSDimitry Andric   // Goes up as we walk the stack.
10568d75effSDimitry Andric   uptr bottom = stack_bottom;
10668d75effSDimitry Andric   // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
10768d75effSDimitry Andric   while (IsValidFrame((uptr)frame, stack_top, bottom) &&
10868d75effSDimitry Andric          IsAligned((uptr)frame, sizeof(*frame)) &&
10968d75effSDimitry Andric          size < max_depth) {
11068d75effSDimitry Andric #ifdef __powerpc__
11168d75effSDimitry Andric     // PowerPC ABIs specify that the return address is saved at offset
11268d75effSDimitry Andric     // 16 of the *caller's* stack frame.  Thus we must dereference the
11368d75effSDimitry Andric     // back chain to find the caller frame before extracting it.
11468d75effSDimitry Andric     uhwptr *caller_frame = (uhwptr*)frame[0];
11568d75effSDimitry Andric     if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) ||
11668d75effSDimitry Andric         !IsAligned((uptr)caller_frame, sizeof(uhwptr)))
11768d75effSDimitry Andric       break;
11868d75effSDimitry Andric     uhwptr pc1 = caller_frame[2];
11968d75effSDimitry Andric #elif defined(__s390__)
12068d75effSDimitry Andric     uhwptr pc1 = frame[14];
121*e8d8bef9SDimitry Andric #elif defined(__riscv)
122*e8d8bef9SDimitry Andric     // frame[-1] contains the return address
123*e8d8bef9SDimitry Andric     uhwptr pc1 = frame[-1];
12468d75effSDimitry Andric #else
12568d75effSDimitry Andric     uhwptr pc1 = frame[1];
12668d75effSDimitry Andric #endif
12768d75effSDimitry Andric     // Let's assume that any pointer in the 0th page (i.e. <0x1000 on i386 and
12868d75effSDimitry Andric     // x86_64) is invalid and stop unwinding here.  If we're adding support for
12968d75effSDimitry Andric     // a platform where this isn't true, we need to reconsider this check.
13068d75effSDimitry Andric     if (pc1 < kPageSize)
13168d75effSDimitry Andric       break;
13268d75effSDimitry Andric     if (pc1 != pc) {
13368d75effSDimitry Andric       trace_buffer[size++] = (uptr) pc1;
13468d75effSDimitry Andric     }
13568d75effSDimitry Andric     bottom = (uptr)frame;
136*e8d8bef9SDimitry Andric #if defined(__riscv)
137*e8d8bef9SDimitry Andric     // frame[-2] contain fp of the previous frame
138*e8d8bef9SDimitry Andric     uptr new_bp = (uptr)frame[-2];
139*e8d8bef9SDimitry Andric #else
140*e8d8bef9SDimitry Andric     uptr new_bp = (uptr)frame[0];
141*e8d8bef9SDimitry Andric #endif
142*e8d8bef9SDimitry Andric     frame = GetCanonicFrame(new_bp, stack_top, bottom);
14368d75effSDimitry Andric   }
14468d75effSDimitry Andric }
14568d75effSDimitry Andric 
14668d75effSDimitry Andric #endif  // !defined(__sparc__)
14768d75effSDimitry Andric 
14868d75effSDimitry Andric void BufferedStackTrace::PopStackFrames(uptr count) {
14968d75effSDimitry Andric   CHECK_LT(count, size);
15068d75effSDimitry Andric   size -= count;
15168d75effSDimitry Andric   for (uptr i = 0; i < size; ++i) {
15268d75effSDimitry Andric     trace_buffer[i] = trace_buffer[i + count];
15368d75effSDimitry Andric   }
15468d75effSDimitry Andric }
15568d75effSDimitry Andric 
15668d75effSDimitry Andric static uptr Distance(uptr a, uptr b) { return a < b ? b - a : a - b; }
15768d75effSDimitry Andric 
15868d75effSDimitry Andric uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
15968d75effSDimitry Andric   uptr best = 0;
16068d75effSDimitry Andric   for (uptr i = 1; i < size; ++i) {
16168d75effSDimitry Andric     if (Distance(trace[i], pc) < Distance(trace[best], pc)) best = i;
16268d75effSDimitry Andric   }
16368d75effSDimitry Andric   return best;
16468d75effSDimitry Andric }
16568d75effSDimitry Andric 
16668d75effSDimitry Andric }  // namespace __sanitizer
167