backtrace.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //===-- backtrace.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. #ifndef GWP_ASAN_OPTIONAL_BACKTRACE_H_
  9. #define GWP_ASAN_OPTIONAL_BACKTRACE_H_
  10. #include "gwp_asan/optional/printf.h"
  11. #include "gwp_asan/options.h"
  12. namespace gwp_asan {
  13. namespace backtrace {
  14. // ================================ Description ================================
  15. // This function shall take the backtrace provided in `TraceBuffer`, and print
  16. // it in a human-readable format using `Print`. Generally, this function shall
  17. // resolve raw pointers to section offsets and print them with the following
  18. // sanitizer-common format:
  19. // " #{frame_number} {pointer} in {function name} ({binary name}+{offset}"
  20. // e.g. " #5 0x420459 in _start (/tmp/uaf+0x420459)"
  21. // This format allows the backtrace to be symbolized offline successfully using
  22. // llvm-symbolizer.
  23. // =================================== Notes ===================================
  24. // This function may directly or indirectly call malloc(), as the
  25. // GuardedPoolAllocator contains a reentrancy barrier to prevent infinite
  26. // recursion. Any allocation made inside this function will be served by the
  27. // supporting allocator, and will not have GWP-ASan protections.
  28. typedef void (*PrintBacktrace_t)(uintptr_t *TraceBuffer, size_t TraceLength,
  29. Printf_t Print);
  30. // Returns a function pointer to a backtrace function that's suitable for
  31. // unwinding through a signal handler. This is important primarily for frame-
  32. // pointer based unwinders, DWARF or other unwinders can simply provide the
  33. // normal backtrace function as the implementation here. On POSIX, SignalContext
  34. // should be the `ucontext_t` from the signal handler.
  35. typedef size_t (*SegvBacktrace_t)(uintptr_t *TraceBuffer, size_t Size,
  36. void *SignalContext);
  37. // Returns platform-specific provided implementations of Backtrace_t for use
  38. // inside the GWP-ASan core allocator.
  39. options::Backtrace_t getBacktraceFunction();
  40. // Returns platform-specific provided implementations of PrintBacktrace_t and
  41. // SegvBacktrace_t for use in the optional SEGV handler.
  42. PrintBacktrace_t getPrintBacktraceFunction();
  43. SegvBacktrace_t getSegvBacktraceFunction();
  44. } // namespace backtrace
  45. } // namespace gwp_asan
  46. #endif // GWP_ASAN_OPTIONAL_BACKTRACE_H_