sanitizer_stacktrace.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //===-- sanitizer_stacktrace.cc -------------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is shared between AddressSanitizer and ThreadSanitizer
  9. // run-time libraries.
  10. //===----------------------------------------------------------------------===//
  11. #include "sanitizer_common.h"
  12. #include "sanitizer_flags.h"
  13. #include "sanitizer_stacktrace.h"
  14. namespace __sanitizer {
  15. uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
  16. #if defined(__arm__)
  17. // Cancel Thumb bit.
  18. pc = pc & (~1);
  19. #endif
  20. #if defined(__powerpc__) || defined(__powerpc64__)
  21. // PCs are always 4 byte aligned.
  22. return pc - 4;
  23. #elif defined(__sparc__) || defined(__mips__)
  24. return pc - 8;
  25. #else
  26. return pc - 1;
  27. #endif
  28. }
  29. uptr StackTrace::GetCurrentPc() {
  30. return GET_CALLER_PC();
  31. }
  32. void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
  33. size = cnt + !!extra_top_pc;
  34. CHECK_LE(size, kStackTraceMax);
  35. internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0]));
  36. if (extra_top_pc)
  37. trace_buffer[cnt] = extra_top_pc;
  38. top_frame_bp = 0;
  39. }
  40. // Check if given pointer points into allocated stack area.
  41. static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
  42. return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr);
  43. }
  44. // In GCC on ARM bp points to saved lr, not fp, so we should check the next
  45. // cell in stack to be a saved frame pointer. GetCanonicFrame returns the
  46. // pointer to saved frame pointer in any case.
  47. static inline uhwptr *GetCanonicFrame(uptr bp,
  48. uptr stack_top,
  49. uptr stack_bottom) {
  50. #ifdef __arm__
  51. if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
  52. uhwptr *bp_prev = (uhwptr *)bp;
  53. if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
  54. // The next frame pointer does not look right. This could be a GCC frame, step
  55. // back by 1 word and try again.
  56. if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
  57. return bp_prev - 1;
  58. // Nope, this does not look right either. This means the frame after next does
  59. // not have a valid frame pointer, but we can still extract the caller PC.
  60. // Unfortunately, there is no way to decide between GCC and LLVM frame
  61. // layouts. Assume LLVM.
  62. return bp_prev;
  63. #else
  64. return (uhwptr*)bp;
  65. #endif
  66. }
  67. void BufferedStackTrace::FastUnwindStack(uptr pc, uptr bp, uptr stack_top,
  68. uptr stack_bottom, uptr max_depth) {
  69. CHECK_GE(max_depth, 2);
  70. trace_buffer[0] = pc;
  71. size = 1;
  72. if (stack_top < 4096) return; // Sanity check for stack top.
  73. uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
  74. // Lowest possible address that makes sense as the next frame pointer.
  75. // Goes up as we walk the stack.
  76. uptr bottom = stack_bottom;
  77. // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
  78. while (IsValidFrame((uptr)frame, stack_top, bottom) &&
  79. IsAligned((uptr)frame, sizeof(*frame)) &&
  80. size < max_depth) {
  81. uhwptr pc1 = frame[1];
  82. if (pc1 != pc) {
  83. trace_buffer[size++] = (uptr) pc1;
  84. }
  85. bottom = (uptr)frame;
  86. frame = GetCanonicFrame((uptr)frame[0], stack_top, bottom);
  87. }
  88. }
  89. static bool MatchPc(uptr cur_pc, uptr trace_pc, uptr threshold) {
  90. return cur_pc - trace_pc <= threshold || trace_pc - cur_pc <= threshold;
  91. }
  92. void BufferedStackTrace::PopStackFrames(uptr count) {
  93. CHECK_LT(count, size);
  94. size -= count;
  95. for (uptr i = 0; i < size; ++i) {
  96. trace_buffer[i] = trace_buffer[i + count];
  97. }
  98. }
  99. uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
  100. // Use threshold to find PC in stack trace, as PC we want to unwind from may
  101. // slightly differ from return address in the actual unwinded stack trace.
  102. const int kPcThreshold = 288;
  103. for (uptr i = 0; i < size; ++i) {
  104. if (MatchPc(pc, trace[i], kPcThreshold))
  105. return i;
  106. }
  107. return 0;
  108. }
  109. } // namespace __sanitizer