stacktrace_aarch64-inl.inc 10 KB

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