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