xref: /freebsd/contrib/llvm-project/compiler-rt/lib/scudo/standalone/flags.cpp (revision 2f9966ff63d65bd474478888c9088eeae3f9c669)
1 //===-- flags.cpp -----------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "flags.h"
10 #include "common.h"
11 #include "flags_parser.h"
12 
13 #include "scudo/interface.h"
14 
15 namespace scudo {
16 
17 Flags *getFlags() {
18   static Flags F;
19   return &F;
20 }
21 
22 void Flags::setDefaults() {
23 #define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
24 #include "flags.inc"
25 #undef SCUDO_FLAG
26 
27 #ifdef GWP_ASAN_HOOKS
28 #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description)                 \
29   GWP_ASAN_##Name = DefaultValue;
30 #include "gwp_asan/options.inc"
31 #undef GWP_ASAN_OPTION
32 #endif // GWP_ASAN_HOOKS
33 }
34 
35 void registerFlags(FlagParser *Parser, Flags *F) {
36 #define SCUDO_FLAG(Type, Name, DefaultValue, Description)                      \
37   Parser->registerFlag(#Name, Description, FlagType::FT_##Type,                \
38                        reinterpret_cast<void *>(&F->Name));
39 #include "flags.inc"
40 #undef SCUDO_FLAG
41 
42 #ifdef GWP_ASAN_HOOKS
43 #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description)                 \
44   Parser->registerFlag("GWP_ASAN_" #Name, Description, FlagType::FT_##Type,    \
45                        reinterpret_cast<void *>(&F->GWP_ASAN_##Name));
46 #include "gwp_asan/options.inc"
47 #undef GWP_ASAN_OPTION
48 #endif // GWP_ASAN_HOOKS
49 }
50 
51 static const char *getCompileDefinitionScudoDefaultOptions() {
52 #ifdef SCUDO_DEFAULT_OPTIONS
53   return STRINGIFY(SCUDO_DEFAULT_OPTIONS);
54 #else
55   return "";
56 #endif
57 }
58 
59 static const char *getScudoDefaultOptions() {
60   return (&__scudo_default_options) ? __scudo_default_options() : "";
61 }
62 
63 void initFlags() {
64   Flags *F = getFlags();
65   F->setDefaults();
66   FlagParser Parser;
67   registerFlags(&Parser, F);
68   Parser.parseString(getCompileDefinitionScudoDefaultOptions());
69   Parser.parseString(getScudoDefaultOptions());
70   Parser.parseString(getEnv("SCUDO_OPTIONS"));
71   if (const char *V = getEnv("SCUDO_ALLOCATION_RING_BUFFER_SIZE")) {
72     Parser.parseStringPair("allocation_ring_buffer_size", V);
73   }
74 }
75 
76 } // namespace scudo
77