sanitizer_stacktrace.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is shared between AddressSanitizer and ThreadSanitizer
  10. // run-time libraries.
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_STACKTRACE_H
  13. #define SANITIZER_STACKTRACE_H
  14. #include "sanitizer_common.h"
  15. #include "sanitizer_internal_defs.h"
  16. #include "sanitizer_platform.h"
  17. namespace __sanitizer {
  18. struct BufferedStackTrace;
  19. static const u32 kStackTraceMax = 255;
  20. #if SANITIZER_LINUX && defined(__mips__)
  21. # define SANITIZER_CAN_FAST_UNWIND 0
  22. #elif SANITIZER_WINDOWS
  23. # define SANITIZER_CAN_FAST_UNWIND 0
  24. #else
  25. # define SANITIZER_CAN_FAST_UNWIND 1
  26. #endif
  27. // Fast unwind is the only option on Mac for now; we will need to
  28. // revisit this macro when slow unwind works on Mac, see
  29. // https://github.com/google/sanitizers/issues/137
  30. #if SANITIZER_APPLE
  31. # define SANITIZER_CAN_SLOW_UNWIND 0
  32. #else
  33. # define SANITIZER_CAN_SLOW_UNWIND 1
  34. #endif
  35. struct StackTrace {
  36. const uptr *trace;
  37. u32 size;
  38. u32 tag;
  39. static const int TAG_UNKNOWN = 0;
  40. static const int TAG_ALLOC = 1;
  41. static const int TAG_DEALLOC = 2;
  42. static const int TAG_CUSTOM = 100; // Tool specific tags start here.
  43. StackTrace() : trace(nullptr), size(0), tag(0) {}
  44. StackTrace(const uptr *trace, u32 size) : trace(trace), size(size), tag(0) {}
  45. StackTrace(const uptr *trace, u32 size, u32 tag)
  46. : trace(trace), size(size), tag(tag) {}
  47. // Prints a symbolized stacktrace, followed by an empty line.
  48. void Print() const;
  49. // Prints a symbolized stacktrace to the output string, followed by an empty
  50. // line.
  51. void PrintTo(InternalScopedString *output) const;
  52. // Prints a symbolized stacktrace to the output buffer, followed by an empty
  53. // line. Returns the number of symbols that should have been written to buffer
  54. // (not including trailing '\0'). Thus, the string is truncated iff return
  55. // value is not less than "out_buf_size".
  56. uptr PrintTo(char *out_buf, uptr out_buf_size) const;
  57. static bool WillUseFastUnwind(bool request_fast_unwind) {
  58. if (!SANITIZER_CAN_FAST_UNWIND)
  59. return false;
  60. if (!SANITIZER_CAN_SLOW_UNWIND)
  61. return true;
  62. return request_fast_unwind;
  63. }
  64. static uptr GetCurrentPc();
  65. static inline uptr GetPreviousInstructionPc(uptr pc);
  66. static uptr GetNextInstructionPc(uptr pc);
  67. };
  68. // Performance-critical, must be in the header.
  69. ALWAYS_INLINE
  70. uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
  71. #if defined(__arm__)
  72. // T32 (Thumb) branch instructions might be 16 or 32 bit long,
  73. // so we return (pc-2) in that case in order to be safe.
  74. // For A32 mode we return (pc-4) because all instructions are 32 bit long.
  75. return (pc - 3) & (~1);
  76. #elif defined(__sparc__) || defined(__mips__)
  77. return pc - 8;
  78. #elif SANITIZER_RISCV64
  79. // RV-64 has variable instruciton length...
  80. // C extentions gives us 2-byte instructoins
  81. // RV-64 has 4-byte instructions
  82. // + RISCV architecture allows instructions up to 8 bytes
  83. // It seems difficult to figure out the exact instruction length -
  84. // pc - 2 seems like a safe option for the purposes of stack tracing
  85. return pc - 2;
  86. #elif SANITIZER_S390 || SANITIZER_I386 || SANITIZER_X32 || SANITIZER_X64
  87. return pc - 1;
  88. #else
  89. return pc - 4;
  90. #endif
  91. }
  92. // StackTrace that owns the buffer used to store the addresses.
  93. struct BufferedStackTrace : public StackTrace {
  94. uptr trace_buffer[kStackTraceMax];
  95. uptr top_frame_bp; // Optional bp of a top frame.
  96. BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
  97. void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
  98. // Get the stack trace with the given pc and bp.
  99. // The pc will be in the position 0 of the resulting stack trace.
  100. // The bp may refer to the current frame or to the caller's frame.
  101. void Unwind(uptr pc, uptr bp, void *context, bool request_fast,
  102. u32 max_depth = kStackTraceMax) {
  103. top_frame_bp = (max_depth > 0) ? bp : 0;
  104. // Small max_depth optimization
  105. if (max_depth <= 1) {
  106. if (max_depth == 1)
  107. trace_buffer[0] = pc;
  108. size = max_depth;
  109. return;
  110. }
  111. UnwindImpl(pc, bp, context, request_fast, max_depth);
  112. }
  113. void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
  114. uptr stack_bottom, bool request_fast_unwind);
  115. void Reset() {
  116. *static_cast<StackTrace *>(this) = StackTrace(trace_buffer, 0);
  117. top_frame_bp = 0;
  118. }
  119. private:
  120. // Every runtime defines its own implementation of this method
  121. void UnwindImpl(uptr pc, uptr bp, void *context, bool request_fast,
  122. u32 max_depth);
  123. // UnwindFast/Slow have platform-specific implementations
  124. void UnwindFast(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
  125. u32 max_depth);
  126. void UnwindSlow(uptr pc, u32 max_depth);
  127. void UnwindSlow(uptr pc, void *context, u32 max_depth);
  128. void PopStackFrames(uptr count);
  129. uptr LocatePcInTrace(uptr pc);
  130. BufferedStackTrace(const BufferedStackTrace &) = delete;
  131. void operator=(const BufferedStackTrace &) = delete;
  132. friend class FastUnwindTest;
  133. };
  134. #if defined(__s390x__)
  135. static const uptr kFrameSize = 160;
  136. #elif defined(__s390__)
  137. static const uptr kFrameSize = 96;
  138. #else
  139. static const uptr kFrameSize = 2 * sizeof(uhwptr);
  140. #endif
  141. // Check if given pointer points into allocated stack area.
  142. static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
  143. return frame > stack_bottom && frame < stack_top - kFrameSize;
  144. }
  145. } // namespace __sanitizer
  146. // Use this macro if you want to print stack trace with the caller
  147. // of the current function in the top frame.
  148. #define GET_CALLER_PC_BP \
  149. uptr bp = GET_CURRENT_FRAME(); \
  150. uptr pc = GET_CALLER_PC();
  151. #define GET_CALLER_PC_BP_SP \
  152. GET_CALLER_PC_BP; \
  153. uptr local_stack; \
  154. uptr sp = (uptr)&local_stack
  155. // Use this macro if you want to print stack trace with the current
  156. // function in the top frame.
  157. #define GET_CURRENT_PC_BP \
  158. uptr bp = GET_CURRENT_FRAME(); \
  159. uptr pc = StackTrace::GetCurrentPc()
  160. #define GET_CURRENT_PC_BP_SP \
  161. GET_CURRENT_PC_BP; \
  162. uptr local_stack; \
  163. uptr sp = (uptr)&local_stack
  164. // GET_CURRENT_PC() is equivalent to StackTrace::GetCurrentPc().
  165. // Optimized x86 version is faster than GetCurrentPc because
  166. // it does not involve a function call, instead it reads RIP register.
  167. // Reads of RIP by an instruction return RIP pointing to the next
  168. // instruction, which is exactly what we want here, thus 0 offset.
  169. // It needs to be a macro because otherwise we will get the name
  170. // of this function on the top of most stacks. Attribute artificial
  171. // does not do what it claims to do, unfortunatley. And attribute
  172. // __nodebug__ is clang-only. If we would have an attribute that
  173. // would remove this function from debug info, we could simply make
  174. // StackTrace::GetCurrentPc() faster.
  175. #if defined(__x86_64__)
  176. # define GET_CURRENT_PC() \
  177. (__extension__({ \
  178. uptr pc; \
  179. asm("lea 0(%%rip), %0" : "=r"(pc)); \
  180. pc; \
  181. }))
  182. #else
  183. # define GET_CURRENT_PC() StackTrace::GetCurrentPc()
  184. #endif
  185. #endif // SANITIZER_STACKTRACE_H