xref: /openbsd-src/gnu/llvm/compiler-rt/lib/gwp_asan/options.h (revision 1f9cb04fc6f537ca6cf5a53c28927340cba218a2)
1*3cab2bb3Spatrick //===-- options.h -----------------------------------------------*- C++ -*-===//
2*3cab2bb3Spatrick //
3*3cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*3cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*3cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*3cab2bb3Spatrick //
7*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
8*3cab2bb3Spatrick 
9*3cab2bb3Spatrick #ifndef GWP_ASAN_OPTIONS_H_
10*3cab2bb3Spatrick #define GWP_ASAN_OPTIONS_H_
11*3cab2bb3Spatrick 
12*3cab2bb3Spatrick #include <stddef.h>
13*3cab2bb3Spatrick #include <stdint.h>
14*3cab2bb3Spatrick 
15*3cab2bb3Spatrick namespace gwp_asan {
16*3cab2bb3Spatrick namespace options {
17*3cab2bb3Spatrick // ================================ Requirements ===============================
18*3cab2bb3Spatrick // This function is required to be either implemented by the supporting
19*3cab2bb3Spatrick // allocator, or one of the two provided implementations may be used
20*3cab2bb3Spatrick // (RTGwpAsanBacktraceLibc or RTGwpAsanBacktraceSanitizerCommon).
21*3cab2bb3Spatrick // ================================ Description ================================
22*3cab2bb3Spatrick // This function shall collect the backtrace for the calling thread and place
23*3cab2bb3Spatrick // the result in `TraceBuffer`. This function should elide itself and all frames
24*3cab2bb3Spatrick // below itself from `TraceBuffer`, i.e. the caller's frame should be in
25*3cab2bb3Spatrick // TraceBuffer[0], and subsequent frames 1..n into TraceBuffer[1..n], where a
26*3cab2bb3Spatrick // maximum of `Size` frames are stored. Returns the number of frames stored into
27*3cab2bb3Spatrick // `TraceBuffer`, and zero on failure. If the return value of this function is
28*3cab2bb3Spatrick // equal to `Size`, it may indicate that the backtrace is truncated.
29*3cab2bb3Spatrick // =================================== Notes ===================================
30*3cab2bb3Spatrick // This function may directly or indirectly call malloc(), as the
31*3cab2bb3Spatrick // GuardedPoolAllocator contains a reentrancy barrier to prevent infinite
32*3cab2bb3Spatrick // recursion. Any allocation made inside this function will be served by the
33*3cab2bb3Spatrick // supporting allocator, and will not have GWP-ASan protections.
34*3cab2bb3Spatrick typedef size_t (*Backtrace_t)(uintptr_t *TraceBuffer, size_t Size);
35*3cab2bb3Spatrick 
36*3cab2bb3Spatrick struct Options {
37*3cab2bb3Spatrick   Backtrace_t Backtrace = nullptr;
38*3cab2bb3Spatrick 
39*3cab2bb3Spatrick   // Read the options from the included definitions file.
40*3cab2bb3Spatrick #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description)                 \
41*3cab2bb3Spatrick   Type Name = DefaultValue;
42*3cab2bb3Spatrick #include "gwp_asan/options.inc"
43*3cab2bb3Spatrick #undef GWP_ASAN_OPTION
44*3cab2bb3Spatrick 
setDefaultsOptions45*3cab2bb3Spatrick   void setDefaults() {
46*3cab2bb3Spatrick #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description)                 \
47*3cab2bb3Spatrick   Name = DefaultValue;
48*3cab2bb3Spatrick #include "gwp_asan/options.inc"
49*3cab2bb3Spatrick #undef GWP_ASAN_OPTION
50*3cab2bb3Spatrick 
51*3cab2bb3Spatrick     Backtrace = nullptr;
52*3cab2bb3Spatrick   }
53*3cab2bb3Spatrick };
54*3cab2bb3Spatrick } // namespace options
55*3cab2bb3Spatrick } // namespace gwp_asan
56*3cab2bb3Spatrick 
57*3cab2bb3Spatrick #endif // GWP_ASAN_OPTIONS_H_
58