1*0fca6ea1SDimitry Andric //===-- nsan_flags.cc -----------------------------------------------------===//
2*0fca6ea1SDimitry Andric //
3*0fca6ea1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0fca6ea1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0fca6ea1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0fca6ea1SDimitry Andric //
7*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
8*0fca6ea1SDimitry Andric //
9*0fca6ea1SDimitry Andric // This file is a part of NumericalStabilitySanitizer.
10*0fca6ea1SDimitry Andric //
11*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
12*0fca6ea1SDimitry Andric
13*0fca6ea1SDimitry Andric #include "nsan_flags.h"
14*0fca6ea1SDimitry Andric
15*0fca6ea1SDimitry Andric #include "sanitizer_common/sanitizer_flag_parser.h"
16*0fca6ea1SDimitry Andric #include "sanitizer_common/sanitizer_flags.h"
17*0fca6ea1SDimitry Andric
18*0fca6ea1SDimitry Andric using namespace __sanitizer;
19*0fca6ea1SDimitry Andric using namespace __nsan;
20*0fca6ea1SDimitry Andric
SANITIZER_INTERFACE_WEAK_DEF(const char *,__nsan_default_options,void)21*0fca6ea1SDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(const char *, __nsan_default_options, void) {
22*0fca6ea1SDimitry Andric return "";
23*0fca6ea1SDimitry Andric }
24*0fca6ea1SDimitry Andric
25*0fca6ea1SDimitry Andric Flags __nsan::flags_data;
26*0fca6ea1SDimitry Andric
SetDefaults()27*0fca6ea1SDimitry Andric void Flags::SetDefaults() {
28*0fca6ea1SDimitry Andric #define NSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
29*0fca6ea1SDimitry Andric #include "nsan_flags.inc"
30*0fca6ea1SDimitry Andric #undef NSAN_FLAG
31*0fca6ea1SDimitry Andric }
32*0fca6ea1SDimitry Andric
PopulateCache()33*0fca6ea1SDimitry Andric void Flags::PopulateCache() {
34*0fca6ea1SDimitry Andric cached_absolute_error_threshold =
35*0fca6ea1SDimitry Andric 1.0 / (1ull << log2_absolute_error_threshold);
36*0fca6ea1SDimitry Andric }
37*0fca6ea1SDimitry Andric
RegisterNSanFlags(FlagParser * parser,Flags * f)38*0fca6ea1SDimitry Andric static void RegisterNSanFlags(FlagParser *parser, Flags *f) {
39*0fca6ea1SDimitry Andric #define NSAN_FLAG(Type, Name, DefaultValue, Description) \
40*0fca6ea1SDimitry Andric RegisterFlag(parser, #Name, Description, &f->Name);
41*0fca6ea1SDimitry Andric #include "nsan_flags.inc"
42*0fca6ea1SDimitry Andric #undef NSAN_FLAG
43*0fca6ea1SDimitry Andric }
44*0fca6ea1SDimitry Andric
MaybeCallNsanDefaultOptions()45*0fca6ea1SDimitry Andric static const char *MaybeCallNsanDefaultOptions() {
46*0fca6ea1SDimitry Andric return (&__nsan_default_options) ? __nsan_default_options() : "";
47*0fca6ea1SDimitry Andric }
48*0fca6ea1SDimitry Andric
InitializeFlags()49*0fca6ea1SDimitry Andric void __nsan::InitializeFlags() {
50*0fca6ea1SDimitry Andric SetCommonFlagsDefaults();
51*0fca6ea1SDimitry Andric {
52*0fca6ea1SDimitry Andric CommonFlags cf;
53*0fca6ea1SDimitry Andric cf.CopyFrom(*common_flags());
54*0fca6ea1SDimitry Andric cf.external_symbolizer_path = GetEnv("NSAN_SYMBOLIZER_PATH");
55*0fca6ea1SDimitry Andric OverrideCommonFlags(cf);
56*0fca6ea1SDimitry Andric }
57*0fca6ea1SDimitry Andric
58*0fca6ea1SDimitry Andric flags().SetDefaults();
59*0fca6ea1SDimitry Andric
60*0fca6ea1SDimitry Andric FlagParser parser;
61*0fca6ea1SDimitry Andric RegisterCommonFlags(&parser);
62*0fca6ea1SDimitry Andric RegisterNSanFlags(&parser, &flags());
63*0fca6ea1SDimitry Andric
64*0fca6ea1SDimitry Andric const char *nsan_default_options = MaybeCallNsanDefaultOptions();
65*0fca6ea1SDimitry Andric parser.ParseString(nsan_default_options);
66*0fca6ea1SDimitry Andric
67*0fca6ea1SDimitry Andric parser.ParseString(GetEnv("NSAN_OPTIONS"));
68*0fca6ea1SDimitry Andric InitializeCommonFlags();
69*0fca6ea1SDimitry Andric if (Verbosity())
70*0fca6ea1SDimitry Andric ReportUnrecognizedFlags();
71*0fca6ea1SDimitry Andric if (common_flags()->help)
72*0fca6ea1SDimitry Andric parser.PrintFlagDescriptions();
73*0fca6ea1SDimitry Andric
74*0fca6ea1SDimitry Andric flags().PopulateCache();
75*0fca6ea1SDimitry Andric }
76