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