asan_stack.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //===-- asan_stack.h --------------------------------------------*- C++ -*-===//
  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 a part of AddressSanitizer, an address sanity checker.
  9. //
  10. // ASan-private header for asan_stack.cc.
  11. //===----------------------------------------------------------------------===//
  12. #ifndef ASAN_STACK_H
  13. #define ASAN_STACK_H
  14. #include "asan_flags.h"
  15. #include "asan_thread.h"
  16. #include "sanitizer_common/sanitizer_flags.h"
  17. #include "sanitizer_common/sanitizer_stacktrace.h"
  18. namespace __asan {
  19. // Get the stack trace with the given pc and bp.
  20. // The pc will be in the position 0 of the resulting stack trace.
  21. // The bp may refer to the current frame or to the caller's frame.
  22. ALWAYS_INLINE
  23. void GetStackTraceWithPcBpAndContext(BufferedStackTrace *stack, uptr max_depth,
  24. uptr pc, uptr bp, void *context,
  25. bool fast) {
  26. #if SANITIZER_WINDOWS
  27. stack->Unwind(max_depth, pc, bp, context, 0, 0, fast);
  28. #else
  29. AsanThread *t;
  30. stack->size = 0;
  31. if (LIKELY(asan_inited)) {
  32. if ((t = GetCurrentThread()) && !t->isUnwinding()) {
  33. // On FreeBSD the slow unwinding that leverages _Unwind_Backtrace()
  34. // yields the call stack of the signal's handler and not of the code
  35. // that raised the signal (as it does on Linux).
  36. if (SANITIZER_FREEBSD && t->isInDeadlySignal()) fast = true;
  37. uptr stack_top = t->stack_top();
  38. uptr stack_bottom = t->stack_bottom();
  39. ScopedUnwinding unwind_scope(t);
  40. stack->Unwind(max_depth, pc, bp, context, stack_top, stack_bottom, fast);
  41. } else if (t == 0 && !fast) {
  42. /* If GetCurrentThread() has failed, try to do slow unwind anyways. */
  43. stack->Unwind(max_depth, pc, bp, context, 0, 0, false);
  44. }
  45. }
  46. #endif // SANITIZER_WINDOWS
  47. }
  48. } // namespace __asan
  49. // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
  50. // as early as possible (in functions exposed to the user), as we generally
  51. // don't want stack trace to contain functions from ASan internals.
  52. #define GET_STACK_TRACE(max_size, fast) \
  53. BufferedStackTrace stack; \
  54. if (max_size <= 2) { \
  55. stack.size = max_size; \
  56. if (max_size > 0) { \
  57. stack.top_frame_bp = GET_CURRENT_FRAME(); \
  58. stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \
  59. if (max_size > 1) \
  60. stack.trace_buffer[1] = GET_CALLER_PC(); \
  61. } \
  62. } else { \
  63. GetStackTraceWithPcBpAndContext(&stack, max_size, \
  64. StackTrace::GetCurrentPc(), \
  65. GET_CURRENT_FRAME(), 0, fast); \
  66. }
  67. #define GET_STACK_TRACE_FATAL(pc, bp) \
  68. BufferedStackTrace stack; \
  69. GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, 0, \
  70. common_flags()->fast_unwind_on_fatal)
  71. #define GET_STACK_TRACE_SIGNAL(pc, bp, context) \
  72. BufferedStackTrace stack; \
  73. GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, context, \
  74. common_flags()->fast_unwind_on_fatal)
  75. #define GET_STACK_TRACE_FATAL_HERE \
  76. GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
  77. #define GET_STACK_TRACE_CHECK_HERE \
  78. GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check)
  79. #define GET_STACK_TRACE_THREAD \
  80. GET_STACK_TRACE(kStackTraceMax, true)
  81. #define GET_STACK_TRACE_MALLOC \
  82. GET_STACK_TRACE(common_flags()->malloc_context_size, \
  83. common_flags()->fast_unwind_on_malloc)
  84. #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
  85. #define PRINT_CURRENT_STACK() \
  86. { \
  87. GET_STACK_TRACE_FATAL_HERE; \
  88. stack.Print(); \
  89. }
  90. #define PRINT_CURRENT_STACK_CHECK() \
  91. { \
  92. GET_STACK_TRACE_CHECK_HERE; \
  93. stack.Print(); \
  94. }
  95. #endif // ASAN_STACK_H