1*9356374aSAndroid Build Coastguard Worker#ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_AARCH64_INL_H_ 2*9356374aSAndroid Build Coastguard Worker#define ABSL_DEBUGGING_INTERNAL_STACKTRACE_AARCH64_INL_H_ 3*9356374aSAndroid Build Coastguard Worker 4*9356374aSAndroid Build Coastguard Worker// Generate stack tracer for aarch64 5*9356374aSAndroid Build Coastguard Worker 6*9356374aSAndroid Build Coastguard Worker#if defined(__linux__) 7*9356374aSAndroid Build Coastguard Worker#include <signal.h> 8*9356374aSAndroid Build Coastguard Worker#include <sys/mman.h> 9*9356374aSAndroid Build Coastguard Worker#include <ucontext.h> 10*9356374aSAndroid Build Coastguard Worker#include <unistd.h> 11*9356374aSAndroid Build Coastguard Worker#endif 12*9356374aSAndroid Build Coastguard Worker 13*9356374aSAndroid Build Coastguard Worker#include <atomic> 14*9356374aSAndroid Build Coastguard Worker#include <cassert> 15*9356374aSAndroid Build Coastguard Worker#include <cstdint> 16*9356374aSAndroid Build Coastguard Worker#include <iostream> 17*9356374aSAndroid Build Coastguard Worker#include <limits> 18*9356374aSAndroid Build Coastguard Worker 19*9356374aSAndroid Build Coastguard Worker#include "absl/base/attributes.h" 20*9356374aSAndroid Build Coastguard Worker#include "absl/debugging/internal/address_is_readable.h" 21*9356374aSAndroid Build Coastguard Worker#include "absl/debugging/internal/vdso_support.h" // a no-op on non-elf or non-glibc systems 22*9356374aSAndroid Build Coastguard Worker#include "absl/debugging/stacktrace.h" 23*9356374aSAndroid Build Coastguard Worker 24*9356374aSAndroid Build Coastguard Workerstatic const size_t kUnknownFrameSize = 0; 25*9356374aSAndroid Build Coastguard Worker// Stack end to use when we don't know the actual stack end 26*9356374aSAndroid Build Coastguard Worker// (effectively just the end of address space). 27*9356374aSAndroid Build Coastguard Workerconstexpr uintptr_t kUnknownStackEnd = 28*9356374aSAndroid Build Coastguard Worker std::numeric_limits<size_t>::max() - sizeof(void *); 29*9356374aSAndroid Build Coastguard Worker 30*9356374aSAndroid Build Coastguard Worker#if defined(__linux__) 31*9356374aSAndroid Build Coastguard Worker// Returns the address of the VDSO __kernel_rt_sigreturn function, if present. 32*9356374aSAndroid Build Coastguard Workerstatic const unsigned char* GetKernelRtSigreturnAddress() { 33*9356374aSAndroid Build Coastguard Worker constexpr uintptr_t kImpossibleAddress = 1; 34*9356374aSAndroid Build Coastguard Worker ABSL_CONST_INIT static std::atomic<uintptr_t> memoized{kImpossibleAddress}; 35*9356374aSAndroid Build Coastguard Worker uintptr_t address = memoized.load(std::memory_order_relaxed); 36*9356374aSAndroid Build Coastguard Worker if (address != kImpossibleAddress) { 37*9356374aSAndroid Build Coastguard Worker return reinterpret_cast<const unsigned char*>(address); 38*9356374aSAndroid Build Coastguard Worker } 39*9356374aSAndroid Build Coastguard Worker 40*9356374aSAndroid Build Coastguard Worker address = reinterpret_cast<uintptr_t>(nullptr); 41*9356374aSAndroid Build Coastguard Worker 42*9356374aSAndroid Build Coastguard Worker#ifdef ABSL_HAVE_VDSO_SUPPORT 43*9356374aSAndroid Build Coastguard Worker absl::debugging_internal::VDSOSupport vdso; 44*9356374aSAndroid Build Coastguard Worker if (vdso.IsPresent()) { 45*9356374aSAndroid Build Coastguard Worker absl::debugging_internal::VDSOSupport::SymbolInfo symbol_info; 46*9356374aSAndroid Build Coastguard Worker auto lookup = [&](int type) { 47*9356374aSAndroid Build Coastguard Worker return vdso.LookupSymbol("__kernel_rt_sigreturn", "LINUX_2.6.39", type, 48*9356374aSAndroid Build Coastguard Worker &symbol_info); 49*9356374aSAndroid Build Coastguard Worker }; 50*9356374aSAndroid Build Coastguard Worker if ((!lookup(STT_FUNC) && !lookup(STT_NOTYPE)) || 51*9356374aSAndroid Build Coastguard Worker symbol_info.address == nullptr) { 52*9356374aSAndroid Build Coastguard Worker // Unexpected: VDSO is present, yet the expected symbol is missing 53*9356374aSAndroid Build Coastguard Worker // or null. 54*9356374aSAndroid Build Coastguard Worker assert(false && "VDSO is present, but doesn't have expected symbol"); 55*9356374aSAndroid Build Coastguard Worker } else { 56*9356374aSAndroid Build Coastguard Worker if (reinterpret_cast<uintptr_t>(symbol_info.address) != 57*9356374aSAndroid Build Coastguard Worker kImpossibleAddress) { 58*9356374aSAndroid Build Coastguard Worker address = reinterpret_cast<uintptr_t>(symbol_info.address); 59*9356374aSAndroid Build Coastguard Worker } else { 60*9356374aSAndroid Build Coastguard Worker assert(false && "VDSO returned invalid address"); 61*9356374aSAndroid Build Coastguard Worker } 62*9356374aSAndroid Build Coastguard Worker } 63*9356374aSAndroid Build Coastguard Worker } 64*9356374aSAndroid Build Coastguard Worker#endif 65*9356374aSAndroid Build Coastguard Worker 66*9356374aSAndroid Build Coastguard Worker memoized.store(address, std::memory_order_relaxed); 67*9356374aSAndroid Build Coastguard Worker return reinterpret_cast<const unsigned char*>(address); 68*9356374aSAndroid Build Coastguard Worker} 69*9356374aSAndroid Build Coastguard Worker#endif // __linux__ 70*9356374aSAndroid Build Coastguard Worker 71*9356374aSAndroid Build Coastguard Worker// Compute the size of a stack frame in [low..high). We assume that 72*9356374aSAndroid Build Coastguard Worker// low < high. Return size of kUnknownFrameSize. 73*9356374aSAndroid Build Coastguard Workertemplate<typename T> 74*9356374aSAndroid Build Coastguard Workerstatic size_t ComputeStackFrameSize(const T* low, 75*9356374aSAndroid Build Coastguard Worker const T* high) { 76*9356374aSAndroid Build Coastguard Worker const char* low_char_ptr = reinterpret_cast<const char *>(low); 77*9356374aSAndroid Build Coastguard Worker const char* high_char_ptr = reinterpret_cast<const char *>(high); 78*9356374aSAndroid Build Coastguard Worker return low < high ? static_cast<size_t>(high_char_ptr - low_char_ptr) 79*9356374aSAndroid Build Coastguard Worker : kUnknownFrameSize; 80*9356374aSAndroid Build Coastguard Worker} 81*9356374aSAndroid Build Coastguard Worker 82*9356374aSAndroid Build Coastguard Worker// Saves stack info that is expensive to calculate to avoid recalculating per frame. 83*9356374aSAndroid Build Coastguard Workerstruct StackInfo { 84*9356374aSAndroid Build Coastguard Worker uintptr_t stack_low; 85*9356374aSAndroid Build Coastguard Worker uintptr_t stack_high; 86*9356374aSAndroid Build Coastguard Worker uintptr_t sig_stack_low; 87*9356374aSAndroid Build Coastguard Worker uintptr_t sig_stack_high; 88*9356374aSAndroid Build Coastguard Worker}; 89*9356374aSAndroid Build Coastguard Worker 90*9356374aSAndroid Build Coastguard Workerstatic bool InsideSignalStack(void** ptr, const StackInfo* stack_info) { 91*9356374aSAndroid Build Coastguard Worker uintptr_t comparable_ptr = reinterpret_cast<uintptr_t>(ptr); 92*9356374aSAndroid Build Coastguard Worker if (stack_info->sig_stack_high == kUnknownStackEnd) 93*9356374aSAndroid Build Coastguard Worker return false; 94*9356374aSAndroid Build Coastguard Worker return (comparable_ptr >= stack_info->sig_stack_low && 95*9356374aSAndroid Build Coastguard Worker comparable_ptr < stack_info->sig_stack_high); 96*9356374aSAndroid Build Coastguard Worker} 97*9356374aSAndroid Build Coastguard Worker 98*9356374aSAndroid Build Coastguard Worker// Given a pointer to a stack frame, locate and return the calling 99*9356374aSAndroid Build Coastguard Worker// stackframe, or return null if no stackframe can be found. Perform sanity 100*9356374aSAndroid Build Coastguard Worker// checks (the strictness of which is controlled by the boolean parameter 101*9356374aSAndroid Build Coastguard Worker// "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned. 102*9356374aSAndroid Build Coastguard Workertemplate<bool STRICT_UNWINDING, bool WITH_CONTEXT> 103*9356374aSAndroid Build Coastguard WorkerABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS // May read random elements from stack. 104*9356374aSAndroid Build Coastguard WorkerABSL_ATTRIBUTE_NO_SANITIZE_MEMORY // May read random elements from stack. 105*9356374aSAndroid Build Coastguard Workerstatic void **NextStackFrame(void **old_frame_pointer, const void *uc, 106*9356374aSAndroid Build Coastguard Worker const StackInfo *stack_info) { 107*9356374aSAndroid Build Coastguard Worker void **new_frame_pointer = reinterpret_cast<void**>(*old_frame_pointer); 108*9356374aSAndroid Build Coastguard Worker 109*9356374aSAndroid Build Coastguard Worker#if defined(__linux__) 110*9356374aSAndroid Build Coastguard Worker if (WITH_CONTEXT && uc != nullptr) { 111*9356374aSAndroid Build Coastguard Worker // Check to see if next frame's return address is __kernel_rt_sigreturn. 112*9356374aSAndroid Build Coastguard Worker if (old_frame_pointer[1] == GetKernelRtSigreturnAddress()) { 113*9356374aSAndroid Build Coastguard Worker const ucontext_t *ucv = static_cast<const ucontext_t *>(uc); 114*9356374aSAndroid Build Coastguard Worker // old_frame_pointer[0] is not suitable for unwinding, look at 115*9356374aSAndroid Build Coastguard Worker // ucontext to discover frame pointer before signal. 116*9356374aSAndroid Build Coastguard Worker void **const pre_signal_frame_pointer = 117*9356374aSAndroid Build Coastguard Worker reinterpret_cast<void **>(ucv->uc_mcontext.regs[29]); 118*9356374aSAndroid Build Coastguard Worker 119*9356374aSAndroid Build Coastguard Worker // The most recent signal always needs special handling to find the frame 120*9356374aSAndroid Build Coastguard Worker // pointer, but a nested signal does not. If pre_signal_frame_pointer is 121*9356374aSAndroid Build Coastguard Worker // earlier in the stack than the old_frame_pointer, then use it. If it is 122*9356374aSAndroid Build Coastguard Worker // later, then we have already unwound through it and it needs no special 123*9356374aSAndroid Build Coastguard Worker // handling. 124*9356374aSAndroid Build Coastguard Worker if (pre_signal_frame_pointer >= old_frame_pointer) { 125*9356374aSAndroid Build Coastguard Worker new_frame_pointer = pre_signal_frame_pointer; 126*9356374aSAndroid Build Coastguard Worker } 127*9356374aSAndroid Build Coastguard Worker } 128*9356374aSAndroid Build Coastguard Worker#endif 129*9356374aSAndroid Build Coastguard Worker 130*9356374aSAndroid Build Coastguard Worker // The frame pointer should be 8-byte aligned. 131*9356374aSAndroid Build Coastguard Worker if ((reinterpret_cast<uintptr_t>(new_frame_pointer) & 7) != 0) 132*9356374aSAndroid Build Coastguard Worker return nullptr; 133*9356374aSAndroid Build Coastguard Worker 134*9356374aSAndroid Build Coastguard Worker // Check that alleged frame pointer is actually readable. This is to 135*9356374aSAndroid Build Coastguard Worker // prevent "double fault" in case we hit the first fault due to e.g. 136*9356374aSAndroid Build Coastguard Worker // stack corruption. 137*9356374aSAndroid Build Coastguard Worker if (!absl::debugging_internal::AddressIsReadable( 138*9356374aSAndroid Build Coastguard Worker new_frame_pointer)) 139*9356374aSAndroid Build Coastguard Worker return nullptr; 140*9356374aSAndroid Build Coastguard Worker } 141*9356374aSAndroid Build Coastguard Worker 142*9356374aSAndroid Build Coastguard Worker // Only check the size if both frames are in the same stack. 143*9356374aSAndroid Build Coastguard Worker if (InsideSignalStack(new_frame_pointer, stack_info) == 144*9356374aSAndroid Build Coastguard Worker InsideSignalStack(old_frame_pointer, stack_info)) { 145*9356374aSAndroid Build Coastguard Worker // Check frame size. In strict mode, we assume frames to be under 146*9356374aSAndroid Build Coastguard Worker // 100,000 bytes. In non-strict mode, we relax the limit to 1MB. 147*9356374aSAndroid Build Coastguard Worker const size_t max_size = STRICT_UNWINDING ? 100000 : 1000000; 148*9356374aSAndroid Build Coastguard Worker const size_t frame_size = 149*9356374aSAndroid Build Coastguard Worker ComputeStackFrameSize(old_frame_pointer, new_frame_pointer); 150*9356374aSAndroid Build Coastguard Worker if (frame_size == kUnknownFrameSize) 151*9356374aSAndroid Build Coastguard Worker return nullptr; 152*9356374aSAndroid Build Coastguard Worker // A very large frame may mean corrupt memory or an erroneous frame 153*9356374aSAndroid Build Coastguard Worker // pointer. But also maybe just a plain-old large frame. Assume that if the 154*9356374aSAndroid Build Coastguard Worker // frame is within a known stack, then it is valid. 155*9356374aSAndroid Build Coastguard Worker if (frame_size > max_size) { 156*9356374aSAndroid Build Coastguard Worker size_t stack_low = stack_info->stack_low; 157*9356374aSAndroid Build Coastguard Worker size_t stack_high = stack_info->stack_high; 158*9356374aSAndroid Build Coastguard Worker if (InsideSignalStack(new_frame_pointer, stack_info)) { 159*9356374aSAndroid Build Coastguard Worker stack_low = stack_info->sig_stack_low; 160*9356374aSAndroid Build Coastguard Worker stack_high = stack_info->sig_stack_high; 161*9356374aSAndroid Build Coastguard Worker } 162*9356374aSAndroid Build Coastguard Worker if (stack_high < kUnknownStackEnd && 163*9356374aSAndroid Build Coastguard Worker static_cast<size_t>(getpagesize()) < stack_low) { 164*9356374aSAndroid Build Coastguard Worker const uintptr_t new_fp_u = 165*9356374aSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(new_frame_pointer); 166*9356374aSAndroid Build Coastguard Worker // Stack bounds are known. 167*9356374aSAndroid Build Coastguard Worker if (!(stack_low < new_fp_u && new_fp_u <= stack_high)) { 168*9356374aSAndroid Build Coastguard Worker // new_frame_pointer is not within a known stack. 169*9356374aSAndroid Build Coastguard Worker return nullptr; 170*9356374aSAndroid Build Coastguard Worker } 171*9356374aSAndroid Build Coastguard Worker } else { 172*9356374aSAndroid Build Coastguard Worker // Stack bounds are unknown, prefer truncated stack to possible crash. 173*9356374aSAndroid Build Coastguard Worker return nullptr; 174*9356374aSAndroid Build Coastguard Worker } 175*9356374aSAndroid Build Coastguard Worker } 176*9356374aSAndroid Build Coastguard Worker } 177*9356374aSAndroid Build Coastguard Worker 178*9356374aSAndroid Build Coastguard Worker return new_frame_pointer; 179*9356374aSAndroid Build Coastguard Worker} 180*9356374aSAndroid Build Coastguard Worker 181*9356374aSAndroid Build Coastguard Workertemplate <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT> 182*9356374aSAndroid Build Coastguard Worker// We count on the bottom frame being this one. See the comment 183*9356374aSAndroid Build Coastguard Worker// at prev_return_address 184*9356374aSAndroid Build Coastguard WorkerABSL_ATTRIBUTE_NOINLINE 185*9356374aSAndroid Build Coastguard WorkerABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS // May read random elements from stack. 186*9356374aSAndroid Build Coastguard WorkerABSL_ATTRIBUTE_NO_SANITIZE_MEMORY // May read random elements from stack. 187*9356374aSAndroid Build Coastguard Workerstatic int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count, 188*9356374aSAndroid Build Coastguard Worker const void *ucp, int *min_dropped_frames) { 189*9356374aSAndroid Build Coastguard Worker#ifdef __GNUC__ 190*9356374aSAndroid Build Coastguard Worker void **frame_pointer = reinterpret_cast<void**>(__builtin_frame_address(0)); 191*9356374aSAndroid Build Coastguard Worker#else 192*9356374aSAndroid Build Coastguard Worker# error reading stack point not yet supported on this platform. 193*9356374aSAndroid Build Coastguard Worker#endif 194*9356374aSAndroid Build Coastguard Worker skip_count++; // Skip the frame for this function. 195*9356374aSAndroid Build Coastguard Worker int n = 0; 196*9356374aSAndroid Build Coastguard Worker 197*9356374aSAndroid Build Coastguard Worker // Assume that the first page is not stack. 198*9356374aSAndroid Build Coastguard Worker StackInfo stack_info; 199*9356374aSAndroid Build Coastguard Worker stack_info.stack_low = static_cast<uintptr_t>(getpagesize()); 200*9356374aSAndroid Build Coastguard Worker stack_info.stack_high = kUnknownStackEnd; 201*9356374aSAndroid Build Coastguard Worker stack_info.sig_stack_low = stack_info.stack_low; 202*9356374aSAndroid Build Coastguard Worker stack_info.sig_stack_high = kUnknownStackEnd; 203*9356374aSAndroid Build Coastguard Worker 204*9356374aSAndroid Build Coastguard Worker // The frame pointer points to low address of a frame. The first 64-bit 205*9356374aSAndroid Build Coastguard Worker // word of a frame points to the next frame up the call chain, which normally 206*9356374aSAndroid Build Coastguard Worker // is just after the high address of the current frame. The second word of 207*9356374aSAndroid Build Coastguard Worker // a frame contains return address of to the caller. To find a pc value 208*9356374aSAndroid Build Coastguard Worker // associated with the current frame, we need to go down a level in the call 209*9356374aSAndroid Build Coastguard Worker // chain. So we remember return the address of the last frame seen. This 210*9356374aSAndroid Build Coastguard Worker // does not work for the first stack frame, which belongs to UnwindImp() but 211*9356374aSAndroid Build Coastguard Worker // we skip the frame for UnwindImp() anyway. 212*9356374aSAndroid Build Coastguard Worker void* prev_return_address = nullptr; 213*9356374aSAndroid Build Coastguard Worker // The nth frame size is the difference between the nth frame pointer and the 214*9356374aSAndroid Build Coastguard Worker // the frame pointer below it in the call chain. There is no frame below the 215*9356374aSAndroid Build Coastguard Worker // leaf frame, but this function is the leaf anyway, and we skip it. 216*9356374aSAndroid Build Coastguard Worker void** prev_frame_pointer = nullptr; 217*9356374aSAndroid Build Coastguard Worker 218*9356374aSAndroid Build Coastguard Worker while (frame_pointer && n < max_depth) { 219*9356374aSAndroid Build Coastguard Worker if (skip_count > 0) { 220*9356374aSAndroid Build Coastguard Worker skip_count--; 221*9356374aSAndroid Build Coastguard Worker } else { 222*9356374aSAndroid Build Coastguard Worker result[n] = prev_return_address; 223*9356374aSAndroid Build Coastguard Worker if (IS_STACK_FRAMES) { 224*9356374aSAndroid Build Coastguard Worker sizes[n] = static_cast<int>( 225*9356374aSAndroid Build Coastguard Worker ComputeStackFrameSize(prev_frame_pointer, frame_pointer)); 226*9356374aSAndroid Build Coastguard Worker } 227*9356374aSAndroid Build Coastguard Worker n++; 228*9356374aSAndroid Build Coastguard Worker } 229*9356374aSAndroid Build Coastguard Worker prev_return_address = frame_pointer[1]; 230*9356374aSAndroid Build Coastguard Worker prev_frame_pointer = frame_pointer; 231*9356374aSAndroid Build Coastguard Worker // The absl::GetStackFrames routine is called when we are in some 232*9356374aSAndroid Build Coastguard Worker // informational context (the failure signal handler for example). 233*9356374aSAndroid Build Coastguard Worker // Use the non-strict unwinding rules to produce a stack trace 234*9356374aSAndroid Build Coastguard Worker // that is as complete as possible (even if it contains a few bogus 235*9356374aSAndroid Build Coastguard Worker // entries in some rare cases). 236*9356374aSAndroid Build Coastguard Worker frame_pointer = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>( 237*9356374aSAndroid Build Coastguard Worker frame_pointer, ucp, &stack_info); 238*9356374aSAndroid Build Coastguard Worker } 239*9356374aSAndroid Build Coastguard Worker 240*9356374aSAndroid Build Coastguard Worker if (min_dropped_frames != nullptr) { 241*9356374aSAndroid Build Coastguard Worker // Implementation detail: we clamp the max of frames we are willing to 242*9356374aSAndroid Build Coastguard Worker // count, so as not to spend too much time in the loop below. 243*9356374aSAndroid Build Coastguard Worker const int kMaxUnwind = 200; 244*9356374aSAndroid Build Coastguard Worker int num_dropped_frames = 0; 245*9356374aSAndroid Build Coastguard Worker for (int j = 0; frame_pointer != nullptr && j < kMaxUnwind; j++) { 246*9356374aSAndroid Build Coastguard Worker if (skip_count > 0) { 247*9356374aSAndroid Build Coastguard Worker skip_count--; 248*9356374aSAndroid Build Coastguard Worker } else { 249*9356374aSAndroid Build Coastguard Worker num_dropped_frames++; 250*9356374aSAndroid Build Coastguard Worker } 251*9356374aSAndroid Build Coastguard Worker frame_pointer = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>( 252*9356374aSAndroid Build Coastguard Worker frame_pointer, ucp, &stack_info); 253*9356374aSAndroid Build Coastguard Worker } 254*9356374aSAndroid Build Coastguard Worker *min_dropped_frames = num_dropped_frames; 255*9356374aSAndroid Build Coastguard Worker } 256*9356374aSAndroid Build Coastguard Worker return n; 257*9356374aSAndroid Build Coastguard Worker} 258*9356374aSAndroid Build Coastguard Worker 259*9356374aSAndroid Build Coastguard Workernamespace absl { 260*9356374aSAndroid Build Coastguard WorkerABSL_NAMESPACE_BEGIN 261*9356374aSAndroid Build Coastguard Workernamespace debugging_internal { 262*9356374aSAndroid Build Coastguard Workerbool StackTraceWorksForTest() { 263*9356374aSAndroid Build Coastguard Worker return true; 264*9356374aSAndroid Build Coastguard Worker} 265*9356374aSAndroid Build Coastguard Worker} // namespace debugging_internal 266*9356374aSAndroid Build Coastguard WorkerABSL_NAMESPACE_END 267*9356374aSAndroid Build Coastguard Worker} // namespace absl 268*9356374aSAndroid Build Coastguard Worker 269*9356374aSAndroid Build Coastguard Worker#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_AARCH64_INL_H_ 270