xref: /freebsd/contrib/llvm-project/compiler-rt/lib/gwp_asan/options.h (revision e25152834cdf3b353892835a4f3b157e066a8ed4)
10b57cec5SDimitry Andric //===-- options.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_OPTIONS_H_
100b57cec5SDimitry Andric #define GWP_ASAN_OPTIONS_H_
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include <stddef.h>
130b57cec5SDimitry Andric #include <stdint.h>
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric namespace gwp_asan {
160b57cec5SDimitry Andric namespace options {
17*68d75effSDimitry Andric // ================================ Requirements ===============================
18*68d75effSDimitry Andric // This function is required to be either implemented by the supporting
19*68d75effSDimitry Andric // allocator, or one of the two provided implementations may be used
20*68d75effSDimitry Andric // (RTGwpAsanBacktraceLibc or RTGwpAsanBacktraceSanitizerCommon).
21*68d75effSDimitry Andric // ================================ Description ================================
22*68d75effSDimitry Andric // This function shall collect the backtrace for the calling thread and place
23*68d75effSDimitry Andric // the result in `TraceBuffer`. This function should elide itself and all frames
24*68d75effSDimitry Andric // below itself from `TraceBuffer`, i.e. the caller's frame should be in
25*68d75effSDimitry Andric // TraceBuffer[0], and subsequent frames 1..n into TraceBuffer[1..n], where a
26*68d75effSDimitry Andric // maximum of `Size` frames are stored. Returns the number of frames stored into
27*68d75effSDimitry Andric // `TraceBuffer`, and zero on failure. If the return value of this function is
28*68d75effSDimitry Andric // equal to `Size`, it may indicate that the backtrace is truncated.
29*68d75effSDimitry Andric // =================================== Notes ===================================
30*68d75effSDimitry Andric // This function may directly or indirectly call malloc(), as the
31*68d75effSDimitry Andric // GuardedPoolAllocator contains a reentrancy barrier to prevent infinite
32*68d75effSDimitry Andric // recursion. Any allocation made inside this function will be served by the
33*68d75effSDimitry Andric // supporting allocator, and will not have GWP-ASan protections.
34*68d75effSDimitry Andric typedef size_t (*Backtrace_t)(uintptr_t *TraceBuffer, size_t Size);
35*68d75effSDimitry Andric 
360b57cec5SDimitry Andric struct Options {
370b57cec5SDimitry Andric   Backtrace_t Backtrace = nullptr;
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   // Read the options from the included definitions file.
400b57cec5SDimitry Andric #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description)                 \
410b57cec5SDimitry Andric   Type Name = DefaultValue;
420b57cec5SDimitry Andric #include "gwp_asan/options.inc"
430b57cec5SDimitry Andric #undef GWP_ASAN_OPTION
440b57cec5SDimitry Andric 
setDefaultsOptions450b57cec5SDimitry Andric   void setDefaults() {
460b57cec5SDimitry Andric #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description)                 \
470b57cec5SDimitry Andric   Name = DefaultValue;
480b57cec5SDimitry Andric #include "gwp_asan/options.inc"
490b57cec5SDimitry Andric #undef GWP_ASAN_OPTION
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric     Backtrace = nullptr;
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric };
540b57cec5SDimitry Andric } // namespace options
550b57cec5SDimitry Andric } // namespace gwp_asan
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric #endif // GWP_ASAN_OPTIONS_H_
58