1 //===-- options_parser.cpp --------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "gwp_asan/optional/options_parser.h"
16 #include "gwp_asan/options.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_flag_parser.h"
19 #include "sanitizer_common/sanitizer_flags.h"
24 void registerGwpAsanFlags(__sanitizer::FlagParser *parser, Options *o) {
25 #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description) \
26 RegisterFlag(parser, #Name, Description, &o->Name);
27 #include "gwp_asan/options.inc"
28 #undef GWP_ASAN_OPTION
31 const char *getCompileDefinitionGwpAsanDefaultOptions() {
32 #ifdef GWP_ASAN_DEFAULT_OPTIONS
33 return SANITIZER_STRINGIFY(GWP_ASAN_DEFAULT_OPTIONS);
39 const char *getGwpAsanDefaultOptions() {
40 return (__gwp_asan_default_options) ? __gwp_asan_default_options() : "";
43 Options *getOptionsInternal() {
44 static Options GwpAsanFlags;
47 } // anonymous namespace
50 __sanitizer::SetCommonFlagsDefaults();
52 Options *o = getOptionsInternal();
55 __sanitizer::FlagParser Parser;
56 registerGwpAsanFlags(&Parser, o);
58 // Override from compile definition.
59 Parser.ParseString(getCompileDefinitionGwpAsanDefaultOptions());
61 // Override from user-specified string.
62 Parser.ParseString(getGwpAsanDefaultOptions());
64 // Override from environment.
65 Parser.ParseString(__sanitizer::GetEnv("GWP_ASAN_OPTIONS"));
67 __sanitizer::InitializeCommonFlags();
68 if (__sanitizer::Verbosity())
69 __sanitizer::ReportUnrecognizedFlags();
74 // Sanity checks for the parameters.
75 if (o->MaxSimultaneousAllocations <= 0) {
76 __sanitizer::Printf("GWP-ASan ERROR: MaxSimultaneousAllocations must be > "
77 "0 when GWP-ASan is enabled.\n");
81 if (o->SampleRate < 1) {
83 "GWP-ASan ERROR: SampleRate must be > 0 when GWP-ASan is enabled.\n");
87 o->Printf = __sanitizer::Printf;
90 Options &getOptions() { return *getOptionsInternal(); }
92 } // namespace options
93 } // namespace gwp_asan