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" 18*fe6060f1SDimitry Andric #include "sanitizer_ptrauth.h" 1968d75effSDimitry Andric 2068d75effSDimitry Andric namespace __sanitizer { 2168d75effSDimitry Andric 2268d75effSDimitry Andric uptr StackTrace::GetNextInstructionPc(uptr pc) { 2368d75effSDimitry Andric #if defined(__sparc__) || defined(__mips__) 2468d75effSDimitry Andric return pc + 8; 2568d75effSDimitry Andric #elif defined(__powerpc__) || defined(__arm__) || defined(__aarch64__) 2668d75effSDimitry Andric return pc + 4; 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; 4968d75effSDimitry Andric #else 5068d75effSDimitry Andric return pc + 1; 5168d75effSDimitry Andric #endif 5268d75effSDimitry Andric } 5368d75effSDimitry Andric 5468d75effSDimitry Andric uptr StackTrace::GetCurrentPc() { 5568d75effSDimitry Andric return GET_CALLER_PC(); 5668d75effSDimitry Andric } 5768d75effSDimitry Andric 5868d75effSDimitry Andric void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) { 5968d75effSDimitry Andric size = cnt + !!extra_top_pc; 6068d75effSDimitry Andric CHECK_LE(size, kStackTraceMax); 6168d75effSDimitry Andric internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0])); 6268d75effSDimitry Andric if (extra_top_pc) 6368d75effSDimitry Andric trace_buffer[cnt] = extra_top_pc; 6468d75effSDimitry Andric top_frame_bp = 0; 6568d75effSDimitry Andric } 6668d75effSDimitry Andric 6768d75effSDimitry Andric // Sparc implemention is in its own file. 6868d75effSDimitry Andric #if !defined(__sparc__) 6968d75effSDimitry Andric 7068d75effSDimitry Andric // In GCC on ARM bp points to saved lr, not fp, so we should check the next 7168d75effSDimitry Andric // cell in stack to be a saved frame pointer. GetCanonicFrame returns the 7268d75effSDimitry Andric // pointer to saved frame pointer in any case. 7368d75effSDimitry Andric static inline uhwptr *GetCanonicFrame(uptr bp, 7468d75effSDimitry Andric uptr stack_top, 7568d75effSDimitry Andric uptr stack_bottom) { 7668d75effSDimitry Andric CHECK_GT(stack_top, stack_bottom); 7768d75effSDimitry Andric #ifdef __arm__ 7868d75effSDimitry Andric if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0; 7968d75effSDimitry Andric uhwptr *bp_prev = (uhwptr *)bp; 8068d75effSDimitry Andric if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev; 8168d75effSDimitry Andric // The next frame pointer does not look right. This could be a GCC frame, step 8268d75effSDimitry Andric // back by 1 word and try again. 8368d75effSDimitry Andric if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom)) 8468d75effSDimitry Andric return bp_prev - 1; 8568d75effSDimitry Andric // Nope, this does not look right either. This means the frame after next does 8668d75effSDimitry Andric // not have a valid frame pointer, but we can still extract the caller PC. 8768d75effSDimitry Andric // Unfortunately, there is no way to decide between GCC and LLVM frame 8868d75effSDimitry Andric // layouts. Assume LLVM. 8968d75effSDimitry Andric return bp_prev; 9068d75effSDimitry Andric #else 9168d75effSDimitry Andric return (uhwptr*)bp; 9268d75effSDimitry Andric #endif 9368d75effSDimitry Andric } 9468d75effSDimitry Andric 9568d75effSDimitry Andric void BufferedStackTrace::UnwindFast(uptr pc, uptr bp, uptr stack_top, 9668d75effSDimitry Andric uptr stack_bottom, u32 max_depth) { 9768d75effSDimitry Andric // TODO(yln): add arg sanity check for stack_top/stack_bottom 9868d75effSDimitry Andric CHECK_GE(max_depth, 2); 9968d75effSDimitry Andric const uptr kPageSize = GetPageSizeCached(); 10068d75effSDimitry Andric trace_buffer[0] = pc; 10168d75effSDimitry Andric size = 1; 10268d75effSDimitry Andric if (stack_top < 4096) return; // Sanity check for stack top. 10368d75effSDimitry Andric uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom); 10468d75effSDimitry Andric // Lowest possible address that makes sense as the next frame pointer. 10568d75effSDimitry Andric // Goes up as we walk the stack. 10668d75effSDimitry Andric uptr bottom = stack_bottom; 10768d75effSDimitry Andric // Avoid infinite loop when frame == frame[0] by using frame > prev_frame. 10868d75effSDimitry Andric while (IsValidFrame((uptr)frame, stack_top, bottom) && 10968d75effSDimitry Andric IsAligned((uptr)frame, sizeof(*frame)) && 11068d75effSDimitry Andric size < max_depth) { 11168d75effSDimitry Andric #ifdef __powerpc__ 11268d75effSDimitry Andric // PowerPC ABIs specify that the return address is saved at offset 11368d75effSDimitry Andric // 16 of the *caller's* stack frame. Thus we must dereference the 11468d75effSDimitry Andric // back chain to find the caller frame before extracting it. 11568d75effSDimitry Andric uhwptr *caller_frame = (uhwptr*)frame[0]; 11668d75effSDimitry Andric if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) || 11768d75effSDimitry Andric !IsAligned((uptr)caller_frame, sizeof(uhwptr))) 11868d75effSDimitry Andric break; 11968d75effSDimitry Andric uhwptr pc1 = caller_frame[2]; 12068d75effSDimitry Andric #elif defined(__s390__) 12168d75effSDimitry Andric uhwptr pc1 = frame[14]; 122e8d8bef9SDimitry Andric #elif defined(__riscv) 123e8d8bef9SDimitry Andric // frame[-1] contains the return address 124e8d8bef9SDimitry Andric uhwptr pc1 = frame[-1]; 12568d75effSDimitry Andric #else 126*fe6060f1SDimitry Andric uhwptr pc1 = STRIP_PAC_PC((void *)frame[1]); 12768d75effSDimitry Andric #endif 12868d75effSDimitry Andric // Let's assume that any pointer in the 0th page (i.e. <0x1000 on i386 and 12968d75effSDimitry Andric // x86_64) is invalid and stop unwinding here. If we're adding support for 13068d75effSDimitry Andric // a platform where this isn't true, we need to reconsider this check. 13168d75effSDimitry Andric if (pc1 < kPageSize) 13268d75effSDimitry Andric break; 13368d75effSDimitry Andric if (pc1 != pc) { 13468d75effSDimitry Andric trace_buffer[size++] = (uptr) pc1; 13568d75effSDimitry Andric } 13668d75effSDimitry Andric bottom = (uptr)frame; 137e8d8bef9SDimitry Andric #if defined(__riscv) 138e8d8bef9SDimitry Andric // frame[-2] contain fp of the previous frame 139e8d8bef9SDimitry Andric uptr new_bp = (uptr)frame[-2]; 140e8d8bef9SDimitry Andric #else 141e8d8bef9SDimitry Andric uptr new_bp = (uptr)frame[0]; 142e8d8bef9SDimitry Andric #endif 143e8d8bef9SDimitry Andric frame = GetCanonicFrame(new_bp, stack_top, bottom); 14468d75effSDimitry Andric } 14568d75effSDimitry Andric } 14668d75effSDimitry Andric 14768d75effSDimitry Andric #endif // !defined(__sparc__) 14868d75effSDimitry Andric 14968d75effSDimitry Andric void BufferedStackTrace::PopStackFrames(uptr count) { 15068d75effSDimitry Andric CHECK_LT(count, size); 15168d75effSDimitry Andric size -= count; 15268d75effSDimitry Andric for (uptr i = 0; i < size; ++i) { 15368d75effSDimitry Andric trace_buffer[i] = trace_buffer[i + count]; 15468d75effSDimitry Andric } 15568d75effSDimitry Andric } 15668d75effSDimitry Andric 15768d75effSDimitry Andric static uptr Distance(uptr a, uptr b) { return a < b ? b - a : a - b; } 15868d75effSDimitry Andric 15968d75effSDimitry Andric uptr BufferedStackTrace::LocatePcInTrace(uptr pc) { 16068d75effSDimitry Andric uptr best = 0; 16168d75effSDimitry Andric for (uptr i = 1; i < size; ++i) { 16268d75effSDimitry Andric if (Distance(trace[i], pc) < Distance(trace[best], pc)) best = i; 16368d75effSDimitry Andric } 16468d75effSDimitry Andric return best; 16568d75effSDimitry Andric } 16668d75effSDimitry Andric 16768d75effSDimitry Andric } // namespace __sanitizer 168