sanitizer_stacktrace.cpp 5.7 KB

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