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 #include "interface.h"
13 
14 namespace scudo {
15 
16 Flags *getFlags() {
17   static Flags F;
18   return &F;
19 }
20 
21 void Flags::setDefaults() {
22 #define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
23 #include "flags.inc"
24 #undef SCUDO_FLAG
25 
26 #ifdef GWP_ASAN_HOOKS
27 #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description)                 \
28   GWP_ASAN_##Name = DefaultValue;
29 #include "gwp_asan/options.inc"
30 #undef GWP_ASAN_OPTION
31 #endif // GWP_ASAN_HOOKS
32 }
33 
34 void registerFlags(FlagParser *Parser, Flags *F) {
35 #define SCUDO_FLAG(Type, Name, DefaultValue, Description)                      \
36   Parser->registerFlag(#Name, Description, FlagType::FT_##Type,                \
37                        reinterpret_cast<void *>(&F->Name));
38 #include "flags.inc"
39 #undef SCUDO_FLAG
40 
41 #ifdef GWP_ASAN_HOOKS
42 #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description)                 \
43   Parser->registerFlag("GWP_ASAN_" #Name, Description, FlagType::FT_##Type,    \
44                        reinterpret_cast<void *>(&F->GWP_ASAN_##Name));
45 #include "gwp_asan/options.inc"
46 #undef GWP_ASAN_OPTION
47 #endif // GWP_ASAN_HOOKS
48 }
49 
50 static const char *getCompileDefinitionScudoDefaultOptions() {
51 #ifdef SCUDO_DEFAULT_OPTIONS
52   return STRINGIFY(SCUDO_DEFAULT_OPTIONS);
53 #else
54   return "";
55 #endif
56 }
57 
58 static const char *getScudoDefaultOptions() {
59   return (&__scudo_default_options) ? __scudo_default_options() : "";
60 }
61 
62 void initFlags() {
63   Flags *F = getFlags();
64   F->setDefaults();
65   FlagParser Parser;
66   registerFlags(&Parser, F);
67   Parser.parseString(getCompileDefinitionScudoDefaultOptions());
68   Parser.parseString(getScudoDefaultOptions());
69   Parser.parseString(getEnv("SCUDO_OPTIONS"));
70 }
71 
72 } // namespace scudo
73