1#ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_AARCH64_INL_H_ 2#define ABSL_DEBUGGING_INTERNAL_STACKTRACE_AARCH64_INL_H_ 3 4// Generate stack tracer for aarch64 5 6#if defined(__linux__) 7#include <sys/mman.h> 8#include <ucontext.h> 9#include <unistd.h> 10#endif 11 12#include <atomic> 13#include <cassert> 14#include <cstdint> 15#include <iostream> 16 17#include "absl/base/attributes.h" 18#include "absl/debugging/internal/address_is_readable.h" 19#include "absl/debugging/internal/vdso_support.h" // a no-op on non-elf or non-glibc systems 20#include "absl/debugging/stacktrace.h" 21 22static const uintptr_t kUnknownFrameSize = 0; 23 24#if defined(__linux__) 25// Returns the address of the VDSO __kernel_rt_sigreturn function, if present. 26static const unsigned char* GetKernelRtSigreturnAddress() { 27 constexpr uintptr_t kImpossibleAddress = 1; 28 ABSL_CONST_INIT static std::atomic<uintptr_t> memoized{kImpossibleAddress}; 29 uintptr_t address = memoized.load(std::memory_order_relaxed); 30 if (address != kImpossibleAddress) { 31 return reinterpret_cast<const unsigned char*>(address); 32 } 33 34 address = reinterpret_cast<uintptr_t>(nullptr); 35 36#ifdef ABSL_HAVE_VDSO_SUPPORT 37 absl::debugging_internal::VDSOSupport vdso; 38 if (vdso.IsPresent()) { 39 absl::debugging_internal::VDSOSupport::SymbolInfo symbol_info; 40 if (!vdso.LookupSymbol("__kernel_rt_sigreturn", "LINUX_2.6.39", STT_FUNC, 41 &symbol_info) || 42 symbol_info.address == nullptr) { 43 // Unexpected: VDSO is present, yet the expected symbol is missing 44 // or null. 45 assert(false && "VDSO is present, but doesn't have expected symbol"); 46 } else { 47 if (reinterpret_cast<uintptr_t>(symbol_info.address) != 48 kImpossibleAddress) { 49 address = reinterpret_cast<uintptr_t>(symbol_info.address); 50 } else { 51 assert(false && "VDSO returned invalid address"); 52 } 53 } 54 } 55#endif 56 57 memoized.store(address, std::memory_order_relaxed); 58 return reinterpret_cast<const unsigned char*>(address); 59} 60#endif // __linux__ 61 62// Compute the size of a stack frame in [low..high). We assume that 63// low < high. Return size of kUnknownFrameSize. 64template<typename T> 65static inline uintptr_t ComputeStackFrameSize(const T* low, 66 const T* high) { 67 const char* low_char_ptr = reinterpret_cast<const char *>(low); 68 const char* high_char_ptr = reinterpret_cast<const char *>(high); 69 return low < high ? high_char_ptr - low_char_ptr : kUnknownFrameSize; 70} 71 72// Given a pointer to a stack frame, locate and return the calling 73// stackframe, or return null if no stackframe can be found. Perform sanity 74// checks (the strictness of which is controlled by the boolean parameter 75// "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned. 76template<bool STRICT_UNWINDING, bool WITH_CONTEXT> 77ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS // May read random elements from stack. 78ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY // May read random elements from stack. 79static void **NextStackFrame(void **old_frame_pointer, const void *uc) { 80 void **new_frame_pointer = reinterpret_cast<void**>(*old_frame_pointer); 81 bool check_frame_size = true; 82 83#if defined(__linux__) 84 if (WITH_CONTEXT && uc != nullptr) { 85 // Check to see if next frame's return address is __kernel_rt_sigreturn. 86 if (old_frame_pointer[1] == GetKernelRtSigreturnAddress()) { 87 const ucontext_t *ucv = static_cast<const ucontext_t *>(uc); 88 // old_frame_pointer[0] is not suitable for unwinding, look at 89 // ucontext to discover frame pointer before signal. 90 void **const pre_signal_frame_pointer = 91 reinterpret_cast<void **>(ucv->uc_mcontext.regs[29]); 92 93 // Check that alleged frame pointer is actually readable. This is to 94 // prevent "double fault" in case we hit the first fault due to e.g. 95 // stack corruption. 96 if (!absl::debugging_internal::AddressIsReadable( 97 pre_signal_frame_pointer)) 98 return nullptr; 99 100 // Alleged frame pointer is readable, use it for further unwinding. 101 new_frame_pointer = pre_signal_frame_pointer; 102 103 // Skip frame size check if we return from a signal. We may be using a 104 // an alternate stack for signals. 105 check_frame_size = false; 106 } 107 } 108#endif 109 110 // aarch64 ABI requires stack pointer to be 16-byte-aligned. 111 if ((reinterpret_cast<uintptr_t>(new_frame_pointer) & 15) != 0) 112 return nullptr; 113 114 // Check frame size. In strict mode, we assume frames to be under 115 // 100,000 bytes. In non-strict mode, we relax the limit to 1MB. 116 if (check_frame_size) { 117 const uintptr_t max_size = STRICT_UNWINDING ? 100000 : 1000000; 118 const uintptr_t frame_size = 119 ComputeStackFrameSize(old_frame_pointer, new_frame_pointer); 120 if (frame_size == kUnknownFrameSize || frame_size > max_size) 121 return nullptr; 122 } 123 124 return new_frame_pointer; 125} 126 127template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT> 128ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS // May read random elements from stack. 129ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY // May read random elements from stack. 130static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count, 131 const void *ucp, int *min_dropped_frames) { 132#ifdef __GNUC__ 133 void **frame_pointer = reinterpret_cast<void**>(__builtin_frame_address(0)); 134#else 135# error reading stack point not yet supported on this platform. 136#endif 137 138 skip_count++; // Skip the frame for this function. 139 int n = 0; 140 141 // The frame pointer points to low address of a frame. The first 64-bit 142 // word of a frame points to the next frame up the call chain, which normally 143 // is just after the high address of the current frame. The second word of 144 // a frame contains return adress of to the caller. To find a pc value 145 // associated with the current frame, we need to go down a level in the call 146 // chain. So we remember return the address of the last frame seen. This 147 // does not work for the first stack frame, which belongs to UnwindImp() but 148 // we skip the frame for UnwindImp() anyway. 149 void* prev_return_address = nullptr; 150 151 while (frame_pointer && n < max_depth) { 152 // The absl::GetStackFrames routine is called when we are in some 153 // informational context (the failure signal handler for example). 154 // Use the non-strict unwinding rules to produce a stack trace 155 // that is as complete as possible (even if it contains a few bogus 156 // entries in some rare cases). 157 void **next_frame_pointer = 158 NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(frame_pointer, ucp); 159 160 if (skip_count > 0) { 161 skip_count--; 162 } else { 163 result[n] = prev_return_address; 164 if (IS_STACK_FRAMES) { 165 sizes[n] = ComputeStackFrameSize(frame_pointer, next_frame_pointer); 166 } 167 n++; 168 } 169 prev_return_address = frame_pointer[1]; 170 frame_pointer = next_frame_pointer; 171 } 172 if (min_dropped_frames != nullptr) { 173 // Implementation detail: we clamp the max of frames we are willing to 174 // count, so as not to spend too much time in the loop below. 175 const int kMaxUnwind = 200; 176 int j = 0; 177 for (; frame_pointer != nullptr && j < kMaxUnwind; j++) { 178 frame_pointer = 179 NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(frame_pointer, ucp); 180 } 181 *min_dropped_frames = j; 182 } 183 return n; 184} 185 186namespace absl { 187ABSL_NAMESPACE_BEGIN 188namespace debugging_internal { 189bool StackTraceWorksForTest() { 190 return true; 191} 192} // namespace debugging_internal 193ABSL_NAMESPACE_END 194} // namespace absl 195 196#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_AARCH64_INL_H_ 197