asan_stack.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //===-- asan_stack.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 a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // ASan-private header for asan_stack.cpp.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef ASAN_STACK_H
  14. #define ASAN_STACK_H
  15. #include "asan_flags.h"
  16. #include "asan_thread.h"
  17. #include "sanitizer_common/sanitizer_flags.h"
  18. #include "sanitizer_common/sanitizer_stacktrace.h"
  19. namespace __asan {
  20. static const u32 kDefaultMallocContextSize = 30;
  21. void SetMallocContextSize(u32 size);
  22. u32 GetMallocContextSize();
  23. } // namespace __asan
  24. // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
  25. // as early as possible (in functions exposed to the user), as we generally
  26. // don't want stack trace to contain functions from ASan internals.
  27. #define GET_STACK_TRACE(max_size, fast) \
  28. BufferedStackTrace stack; \
  29. if (max_size <= 2) { \
  30. stack.size = max_size; \
  31. if (max_size > 0) { \
  32. stack.top_frame_bp = GET_CURRENT_FRAME(); \
  33. stack.trace_buffer[0] = StackTrace::GetCurrentPc(); \
  34. if (max_size > 1) stack.trace_buffer[1] = GET_CALLER_PC(); \
  35. } \
  36. } else { \
  37. stack.Unwind(StackTrace::GetCurrentPc(), \
  38. GET_CURRENT_FRAME(), nullptr, fast, max_size); \
  39. }
  40. #define GET_STACK_TRACE_FATAL(pc, bp) \
  41. BufferedStackTrace stack; \
  42. stack.Unwind(pc, bp, nullptr, \
  43. common_flags()->fast_unwind_on_fatal)
  44. #define GET_STACK_TRACE_FATAL_HERE \
  45. GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_fatal)
  46. #define GET_STACK_TRACE_THREAD \
  47. GET_STACK_TRACE(kStackTraceMax, true)
  48. #define GET_STACK_TRACE_MALLOC \
  49. GET_STACK_TRACE(GetMallocContextSize(), common_flags()->fast_unwind_on_malloc)
  50. #define GET_STACK_TRACE_FREE GET_STACK_TRACE_MALLOC
  51. #define PRINT_CURRENT_STACK() \
  52. { \
  53. GET_STACK_TRACE_FATAL_HERE; \
  54. stack.Print(); \
  55. }
  56. #endif // ASAN_STACK_H