xref: /llvm-project/compiler-rt/lib/gwp_asan/optional/backtrace.h (revision a8520f6970fd4d44ceae1ec5969563a0dbe30f54)
17339ca27SMitch Phillips //===-- backtrace.h ---------------------------------------------*- C++ -*-===//
27339ca27SMitch Phillips //
37339ca27SMitch Phillips // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47339ca27SMitch Phillips // See https://llvm.org/LICENSE.txt for license information.
57339ca27SMitch Phillips // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67339ca27SMitch Phillips //
77339ca27SMitch Phillips //===----------------------------------------------------------------------===//
87339ca27SMitch Phillips 
97339ca27SMitch Phillips #ifndef GWP_ASAN_OPTIONAL_BACKTRACE_H_
107339ca27SMitch Phillips #define GWP_ASAN_OPTIONAL_BACKTRACE_H_
117339ca27SMitch Phillips 
12*a8520f69SMitch Phillips #include "gwp_asan/optional/printf.h"
137339ca27SMitch Phillips #include "gwp_asan/options.h"
147339ca27SMitch Phillips 
157339ca27SMitch Phillips namespace gwp_asan {
16*a8520f69SMitch Phillips namespace backtrace {
17*a8520f69SMitch Phillips // ================================ Description ================================
18*a8520f69SMitch Phillips // This function shall take the backtrace provided in `TraceBuffer`, and print
19*a8520f69SMitch Phillips // it in a human-readable format using `Print`. Generally, this function shall
20*a8520f69SMitch Phillips // resolve raw pointers to section offsets and print them with the following
21*a8520f69SMitch Phillips // sanitizer-common format:
22*a8520f69SMitch Phillips //      "  #{frame_number} {pointer} in {function name} ({binary name}+{offset}"
23*a8520f69SMitch Phillips // e.g. "  #5 0x420459 in _start (/tmp/uaf+0x420459)"
24*a8520f69SMitch Phillips // This format allows the backtrace to be symbolized offline successfully using
25*a8520f69SMitch Phillips // llvm-symbolizer.
26*a8520f69SMitch Phillips // =================================== Notes ===================================
27*a8520f69SMitch Phillips // This function may directly or indirectly call malloc(), as the
28*a8520f69SMitch Phillips // GuardedPoolAllocator contains a reentrancy barrier to prevent infinite
29*a8520f69SMitch Phillips // recursion. Any allocation made inside this function will be served by the
30*a8520f69SMitch Phillips // supporting allocator, and will not have GWP-ASan protections.
31*a8520f69SMitch Phillips typedef void (*PrintBacktrace_t)(uintptr_t *TraceBuffer, size_t TraceLength,
32*a8520f69SMitch Phillips                                  Printf_t Print);
33*a8520f69SMitch Phillips 
34*a8520f69SMitch Phillips // Returns a function pointer to a backtrace function that's suitable for
35*a8520f69SMitch Phillips // unwinding through a signal handler. This is important primarily for frame-
36*a8520f69SMitch Phillips // pointer based unwinders, DWARF or other unwinders can simply provide the
37*a8520f69SMitch Phillips // normal backtrace function as the implementation here. On POSIX, SignalContext
38*a8520f69SMitch Phillips // should be the `ucontext_t` from the signal handler.
39*a8520f69SMitch Phillips typedef size_t (*SegvBacktrace_t)(uintptr_t *TraceBuffer, size_t Size,
40*a8520f69SMitch Phillips                                   void *SignalContext);
41*a8520f69SMitch Phillips 
42*a8520f69SMitch Phillips // Returns platform-specific provided implementations of Backtrace_t for use
43*a8520f69SMitch Phillips // inside the GWP-ASan core allocator.
44*a8520f69SMitch Phillips options::Backtrace_t getBacktraceFunction();
45*a8520f69SMitch Phillips 
46*a8520f69SMitch Phillips // Returns platform-specific provided implementations of PrintBacktrace_t and
47*a8520f69SMitch Phillips // SegvBacktrace_t for use in the optional SEGV handler.
48*a8520f69SMitch Phillips PrintBacktrace_t getPrintBacktraceFunction();
49*a8520f69SMitch Phillips SegvBacktrace_t getSegvBacktraceFunction();
50*a8520f69SMitch Phillips } // namespace backtrace
517339ca27SMitch Phillips } // namespace gwp_asan
527339ca27SMitch Phillips 
537339ca27SMitch Phillips #endif // GWP_ASAN_OPTIONAL_BACKTRACE_H_
54