stacktrace_aarch64-inl.inc 9.5 KB

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