sanitizer_stacktrace.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //===-- sanitizer_stacktrace.cpp ------------------------------------------===//
  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. #include "sanitizer_stacktrace.h"
  13. #include "sanitizer_common.h"
  14. #include "sanitizer_flags.h"
  15. #include "sanitizer_platform.h"
  16. #include "sanitizer_ptrauth.h"
  17. namespace __sanitizer {
  18. uptr StackTrace::GetNextInstructionPc(uptr pc) {
  19. #if defined(__sparc__) || defined(__mips__)
  20. return pc + 8;
  21. #elif defined(__powerpc__) || defined(__arm__) || defined(__aarch64__) || \
  22. defined(__hexagon__)
  23. return STRIP_PAC_PC((void *)pc) + 4;
  24. #elif SANITIZER_RISCV64
  25. // Current check order is 4 -> 2 -> 6 -> 8
  26. u8 InsnByte = *(u8 *)(pc);
  27. if (((InsnByte & 0x3) == 0x3) && ((InsnByte & 0x1c) != 0x1c)) {
  28. // xxxxxxxxxxxbbb11 | 32 bit | bbb != 111
  29. return pc + 4;
  30. }
  31. if ((InsnByte & 0x3) != 0x3) {
  32. // xxxxxxxxxxxxxxaa | 16 bit | aa != 11
  33. return pc + 2;
  34. }
  35. // RISC-V encoding allows instructions to be up to 8 bytes long
  36. if ((InsnByte & 0x3f) == 0x1f) {
  37. // xxxxxxxxxx011111 | 48 bit |
  38. return pc + 6;
  39. }
  40. if ((InsnByte & 0x7f) == 0x3f) {
  41. // xxxxxxxxx0111111 | 64 bit |
  42. return pc + 8;
  43. }
  44. // bail-out if could not figure out the instruction size
  45. return 0;
  46. #else
  47. return pc + 1;
  48. #endif
  49. }
  50. uptr StackTrace::GetCurrentPc() {
  51. return GET_CALLER_PC();
  52. }
  53. void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
  54. size = cnt + !!extra_top_pc;
  55. CHECK_LE(size, kStackTraceMax);
  56. internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0]));
  57. if (extra_top_pc)
  58. trace_buffer[cnt] = extra_top_pc;
  59. top_frame_bp = 0;
  60. }
  61. // Sparc implementation is in its own file.
  62. #if !defined(__sparc__)
  63. // In GCC on ARM bp points to saved lr, not fp, so we should check the next
  64. // cell in stack to be a saved frame pointer. GetCanonicFrame returns the
  65. // pointer to saved frame pointer in any case.
  66. static inline uhwptr *GetCanonicFrame(uptr bp,
  67. uptr stack_top,
  68. uptr stack_bottom) {
  69. CHECK_GT(stack_top, stack_bottom);
  70. #ifdef __arm__
  71. if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
  72. uhwptr *bp_prev = (uhwptr *)bp;
  73. if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
  74. // The next frame pointer does not look right. This could be a GCC frame, step
  75. // back by 1 word and try again.
  76. if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
  77. return bp_prev - 1;
  78. // Nope, this does not look right either. This means the frame after next does
  79. // not have a valid frame pointer, but we can still extract the caller PC.
  80. // Unfortunately, there is no way to decide between GCC and LLVM frame
  81. // layouts. Assume LLVM.
  82. return bp_prev;
  83. #else
  84. return (uhwptr*)bp;
  85. #endif
  86. }
  87. void BufferedStackTrace::UnwindFast(uptr pc, uptr bp, uptr stack_top,
  88. uptr stack_bottom, u32 max_depth) {
  89. // TODO(yln): add arg sanity check for stack_top/stack_bottom
  90. CHECK_GE(max_depth, 2);
  91. const uptr kPageSize = GetPageSizeCached();
  92. trace_buffer[0] = pc;
  93. size = 1;
  94. if (stack_top < 4096) return; // Sanity check for stack top.
  95. uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
  96. // Lowest possible address that makes sense as the next frame pointer.
  97. // Goes up as we walk the stack.
  98. uptr bottom = stack_bottom;
  99. // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
  100. while (IsValidFrame((uptr)frame, stack_top, bottom) &&
  101. IsAligned((uptr)frame, sizeof(*frame)) &&
  102. size < max_depth) {
  103. #ifdef __powerpc__
  104. // PowerPC ABIs specify that the return address is saved at offset
  105. // 16 of the *caller's* stack frame. Thus we must dereference the
  106. // back chain to find the caller frame before extracting it.
  107. uhwptr *caller_frame = (uhwptr*)frame[0];
  108. if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) ||
  109. !IsAligned((uptr)caller_frame, sizeof(uhwptr)))
  110. break;
  111. uhwptr pc1 = caller_frame[2];
  112. #elif defined(__s390__)
  113. uhwptr pc1 = frame[14];
  114. #elif defined(__riscv)
  115. // frame[-1] contains the return address
  116. uhwptr pc1 = frame[-1];
  117. #else
  118. uhwptr pc1 = STRIP_PAC_PC((void *)frame[1]);
  119. #endif
  120. // Let's assume that any pointer in the 0th page (i.e. <0x1000 on i386 and
  121. // x86_64) is invalid and stop unwinding here. If we're adding support for
  122. // a platform where this isn't true, we need to reconsider this check.
  123. if (pc1 < kPageSize)
  124. break;
  125. if (pc1 != pc) {
  126. trace_buffer[size++] = (uptr) pc1;
  127. }
  128. bottom = (uptr)frame;
  129. #if defined(__riscv)
  130. // frame[-2] contain fp of the previous frame
  131. uptr new_bp = (uptr)frame[-2];
  132. #else
  133. uptr new_bp = (uptr)frame[0];
  134. #endif
  135. frame = GetCanonicFrame(new_bp, stack_top, bottom);
  136. }
  137. }
  138. #endif // !defined(__sparc__)
  139. void BufferedStackTrace::PopStackFrames(uptr count) {
  140. CHECK_LT(count, size);
  141. size -= count;
  142. for (uptr i = 0; i < size; ++i) {
  143. trace_buffer[i] = trace_buffer[i + count];
  144. }
  145. }
  146. static uptr Distance(uptr a, uptr b) { return a < b ? b - a : a - b; }
  147. uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
  148. uptr best = 0;
  149. for (uptr i = 1; i < size; ++i) {
  150. if (Distance(trace[i], pc) < Distance(trace[best], pc)) best = i;
  151. }
  152. return best;
  153. }
  154. } // namespace __sanitizer