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