xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.cpp (revision fcaf7f8644a9988098ac6be2165bce3ea4786e91)
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 
13e8d8bef9SDimitry Andric #include "sanitizer_stacktrace.h"
14e8d8bef9SDimitry Andric 
1568d75effSDimitry Andric #include "sanitizer_common.h"
1668d75effSDimitry Andric #include "sanitizer_flags.h"
17e8d8bef9SDimitry Andric #include "sanitizer_platform.h"
18fe6060f1SDimitry Andric #include "sanitizer_ptrauth.h"
1968d75effSDimitry Andric 
2068d75effSDimitry Andric namespace __sanitizer {
2168d75effSDimitry Andric 
GetNextInstructionPc(uptr pc)2268d75effSDimitry Andric uptr StackTrace::GetNextInstructionPc(uptr pc) {
2381ad6265SDimitry Andric #if defined(__aarch64__)
24349cc55cSDimitry Andric   return STRIP_PAC_PC((void *)pc) + 4;
2581ad6265SDimitry Andric #elif defined(__sparc__) || defined(__mips__)
2681ad6265SDimitry Andric   return pc + 8;
27e8d8bef9SDimitry Andric #elif SANITIZER_RISCV64
28e8d8bef9SDimitry Andric   // Current check order is 4 -> 2 -> 6 -> 8
29e8d8bef9SDimitry Andric   u8 InsnByte = *(u8 *)(pc);
30e8d8bef9SDimitry Andric   if (((InsnByte & 0x3) == 0x3) && ((InsnByte & 0x1c) != 0x1c)) {
31e8d8bef9SDimitry Andric     // xxxxxxxxxxxbbb11 | 32 bit | bbb != 111
32e8d8bef9SDimitry Andric     return pc + 4;
33e8d8bef9SDimitry Andric   }
34e8d8bef9SDimitry Andric   if ((InsnByte & 0x3) != 0x3) {
35e8d8bef9SDimitry Andric     // xxxxxxxxxxxxxxaa | 16 bit | aa != 11
36e8d8bef9SDimitry Andric     return pc + 2;
37e8d8bef9SDimitry Andric   }
38e8d8bef9SDimitry Andric   // RISC-V encoding allows instructions to be up to 8 bytes long
39e8d8bef9SDimitry Andric   if ((InsnByte & 0x3f) == 0x1f) {
40e8d8bef9SDimitry Andric     // xxxxxxxxxx011111 | 48 bit |
41e8d8bef9SDimitry Andric     return pc + 6;
42e8d8bef9SDimitry Andric   }
43e8d8bef9SDimitry Andric   if ((InsnByte & 0x7f) == 0x3f) {
44e8d8bef9SDimitry Andric     // xxxxxxxxx0111111 | 64 bit |
45e8d8bef9SDimitry Andric     return pc + 8;
46e8d8bef9SDimitry Andric   }
47e8d8bef9SDimitry Andric   // bail-out if could not figure out the instruction size
48e8d8bef9SDimitry Andric   return 0;
4981ad6265SDimitry Andric #elif SANITIZER_S390 || SANITIZER_I386 || SANITIZER_X32 || SANITIZER_X64
5068d75effSDimitry Andric   return pc + 1;
5181ad6265SDimitry Andric #else
5281ad6265SDimitry Andric   return pc + 4;
5368d75effSDimitry Andric #endif
5468d75effSDimitry Andric }
5568d75effSDimitry Andric 
GetCurrentPc()5668d75effSDimitry Andric uptr StackTrace::GetCurrentPc() {
5768d75effSDimitry Andric   return GET_CALLER_PC();
5868d75effSDimitry Andric }
5968d75effSDimitry Andric 
Init(const uptr * pcs,uptr cnt,uptr extra_top_pc)6068d75effSDimitry Andric void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
6168d75effSDimitry Andric   size = cnt + !!extra_top_pc;
6268d75effSDimitry Andric   CHECK_LE(size, kStackTraceMax);
6368d75effSDimitry Andric   internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0]));
6468d75effSDimitry Andric   if (extra_top_pc)
6568d75effSDimitry Andric     trace_buffer[cnt] = extra_top_pc;
6668d75effSDimitry Andric   top_frame_bp = 0;
6768d75effSDimitry Andric }
6868d75effSDimitry Andric 
69349cc55cSDimitry Andric // Sparc implementation is in its own file.
7068d75effSDimitry Andric #if !defined(__sparc__)
7168d75effSDimitry Andric 
7268d75effSDimitry Andric // In GCC on ARM bp points to saved lr, not fp, so we should check the next
7368d75effSDimitry Andric // cell in stack to be a saved frame pointer. GetCanonicFrame returns the
7468d75effSDimitry Andric // pointer to saved frame pointer in any case.
GetCanonicFrame(uptr bp,uptr stack_top,uptr stack_bottom)7568d75effSDimitry Andric static inline uhwptr *GetCanonicFrame(uptr bp,
7668d75effSDimitry Andric                                       uptr stack_top,
7768d75effSDimitry Andric                                       uptr stack_bottom) {
7868d75effSDimitry Andric   CHECK_GT(stack_top, stack_bottom);
7968d75effSDimitry Andric #ifdef __arm__
8068d75effSDimitry Andric   if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
8168d75effSDimitry Andric   uhwptr *bp_prev = (uhwptr *)bp;
8268d75effSDimitry Andric   if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
8368d75effSDimitry Andric   // The next frame pointer does not look right. This could be a GCC frame, step
8468d75effSDimitry Andric   // back by 1 word and try again.
8568d75effSDimitry Andric   if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
8668d75effSDimitry Andric     return bp_prev - 1;
8768d75effSDimitry Andric   // Nope, this does not look right either. This means the frame after next does
8868d75effSDimitry Andric   // not have a valid frame pointer, but we can still extract the caller PC.
8968d75effSDimitry Andric   // Unfortunately, there is no way to decide between GCC and LLVM frame
9068d75effSDimitry Andric   // layouts. Assume LLVM.
9168d75effSDimitry Andric   return bp_prev;
9268d75effSDimitry Andric #else
9368d75effSDimitry Andric   return (uhwptr*)bp;
9468d75effSDimitry Andric #endif
9568d75effSDimitry Andric }
9668d75effSDimitry Andric 
UnwindFast(uptr pc,uptr bp,uptr stack_top,uptr stack_bottom,u32 max_depth)9768d75effSDimitry Andric void BufferedStackTrace::UnwindFast(uptr pc, uptr bp, uptr stack_top,
9868d75effSDimitry Andric                                     uptr stack_bottom, u32 max_depth) {
9968d75effSDimitry Andric   // TODO(yln): add arg sanity check for stack_top/stack_bottom
10068d75effSDimitry Andric   CHECK_GE(max_depth, 2);
10168d75effSDimitry Andric   const uptr kPageSize = GetPageSizeCached();
10268d75effSDimitry Andric   trace_buffer[0] = pc;
10368d75effSDimitry Andric   size = 1;
10468d75effSDimitry Andric   if (stack_top < 4096) return;  // Sanity check for stack top.
10568d75effSDimitry Andric   uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
10668d75effSDimitry Andric   // Lowest possible address that makes sense as the next frame pointer.
10768d75effSDimitry Andric   // Goes up as we walk the stack.
10868d75effSDimitry Andric   uptr bottom = stack_bottom;
10968d75effSDimitry Andric   // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
11068d75effSDimitry Andric   while (IsValidFrame((uptr)frame, stack_top, bottom) &&
11168d75effSDimitry Andric          IsAligned((uptr)frame, sizeof(*frame)) &&
11268d75effSDimitry Andric          size < max_depth) {
11368d75effSDimitry Andric #ifdef __powerpc__
11468d75effSDimitry Andric     // PowerPC ABIs specify that the return address is saved at offset
11568d75effSDimitry Andric     // 16 of the *caller's* stack frame.  Thus we must dereference the
11668d75effSDimitry Andric     // back chain to find the caller frame before extracting it.
11768d75effSDimitry Andric     uhwptr *caller_frame = (uhwptr*)frame[0];
11868d75effSDimitry Andric     if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) ||
11968d75effSDimitry Andric         !IsAligned((uptr)caller_frame, sizeof(uhwptr)))
12068d75effSDimitry Andric       break;
12168d75effSDimitry Andric     uhwptr pc1 = caller_frame[2];
12268d75effSDimitry Andric #elif defined(__s390__)
12368d75effSDimitry Andric     uhwptr pc1 = frame[14];
124*fcaf7f86SDimitry Andric #elif defined(__loongarch__) || defined(__riscv)
125e8d8bef9SDimitry Andric     // frame[-1] contains the return address
126e8d8bef9SDimitry Andric     uhwptr pc1 = frame[-1];
12768d75effSDimitry Andric #else
128fe6060f1SDimitry Andric     uhwptr pc1 = STRIP_PAC_PC((void *)frame[1]);
12968d75effSDimitry Andric #endif
13068d75effSDimitry Andric     // Let's assume that any pointer in the 0th page (i.e. <0x1000 on i386 and
13168d75effSDimitry Andric     // x86_64) is invalid and stop unwinding here.  If we're adding support for
13268d75effSDimitry Andric     // a platform where this isn't true, we need to reconsider this check.
13368d75effSDimitry Andric     if (pc1 < kPageSize)
13468d75effSDimitry Andric       break;
13568d75effSDimitry Andric     if (pc1 != pc) {
13668d75effSDimitry Andric       trace_buffer[size++] = (uptr) pc1;
13768d75effSDimitry Andric     }
13868d75effSDimitry Andric     bottom = (uptr)frame;
139*fcaf7f86SDimitry Andric #if defined(__loongarch__) || defined(__riscv)
140e8d8bef9SDimitry Andric     // frame[-2] contain fp of the previous frame
141e8d8bef9SDimitry Andric     uptr new_bp = (uptr)frame[-2];
142e8d8bef9SDimitry Andric #else
143e8d8bef9SDimitry Andric     uptr new_bp = (uptr)frame[0];
144e8d8bef9SDimitry Andric #endif
145e8d8bef9SDimitry Andric     frame = GetCanonicFrame(new_bp, stack_top, bottom);
14668d75effSDimitry Andric   }
14768d75effSDimitry Andric }
14868d75effSDimitry Andric 
14968d75effSDimitry Andric #endif  // !defined(__sparc__)
15068d75effSDimitry Andric 
PopStackFrames(uptr count)15168d75effSDimitry Andric void BufferedStackTrace::PopStackFrames(uptr count) {
15268d75effSDimitry Andric   CHECK_LT(count, size);
15368d75effSDimitry Andric   size -= count;
15468d75effSDimitry Andric   for (uptr i = 0; i < size; ++i) {
15568d75effSDimitry Andric     trace_buffer[i] = trace_buffer[i + count];
15668d75effSDimitry Andric   }
15768d75effSDimitry Andric }
15868d75effSDimitry Andric 
Distance(uptr a,uptr b)15968d75effSDimitry Andric static uptr Distance(uptr a, uptr b) { return a < b ? b - a : a - b; }
16068d75effSDimitry Andric 
LocatePcInTrace(uptr pc)16168d75effSDimitry Andric uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
16268d75effSDimitry Andric   uptr best = 0;
16368d75effSDimitry Andric   for (uptr i = 1; i < size; ++i) {
16468d75effSDimitry Andric     if (Distance(trace[i], pc) < Distance(trace[best], pc)) best = i;
16568d75effSDimitry Andric   }
16668d75effSDimitry Andric   return best;
16768d75effSDimitry Andric }
16868d75effSDimitry Andric 
16968d75effSDimitry Andric }  // namespace __sanitizer
170