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