]>
Commit | Line | Data |
---|---|---|
b667dd70 | 1 | //===-- ubsan_flags.cpp ---------------------------------------------------===// |
866e32ad | 2 | // |
b667dd70 ML |
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 | |
866e32ad KS |
6 | // |
7 | //===----------------------------------------------------------------------===// | |
8 | // | |
9 | // Runtime flags for UndefinedBehaviorSanitizer. | |
10 | // | |
11 | //===----------------------------------------------------------------------===// | |
12 | ||
696d846a MO |
13 | #include "ubsan_platform.h" |
14 | #if CAN_SANITIZE_UB | |
866e32ad KS |
15 | #include "ubsan_flags.h" |
16 | #include "sanitizer_common/sanitizer_common.h" | |
17 | #include "sanitizer_common/sanitizer_flags.h" | |
696d846a | 18 | #include "sanitizer_common/sanitizer_flag_parser.h" |
866e32ad | 19 | |
eac97531 ML |
20 | #include <stdlib.h> |
21 | ||
866e32ad KS |
22 | namespace __ubsan { |
23 | ||
696d846a | 24 | const char *MaybeCallUbsanDefaultOptions() { |
866e32ad KS |
25 | return (&__ubsan_default_options) ? __ubsan_default_options() : ""; |
26 | } | |
27 | ||
eac97531 ML |
28 | static const char *GetFlag(const char *flag) { |
29 | // We cannot call getenv() from inside a preinit array initializer | |
30 | if (SANITIZER_CAN_USE_PREINIT_ARRAY) { | |
31 | return GetEnv(flag); | |
32 | } else { | |
33 | return getenv(flag); | |
34 | } | |
35 | } | |
36 | ||
866e32ad KS |
37 | Flags ubsan_flags; |
38 | ||
696d846a MO |
39 | void Flags::SetDefaults() { |
40 | #define UBSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue; | |
41 | #include "ubsan_flags.inc" | |
42 | #undef UBSAN_FLAG | |
43 | } | |
44 | ||
45 | void RegisterUbsanFlags(FlagParser *parser, Flags *f) { | |
46 | #define UBSAN_FLAG(Type, Name, DefaultValue, Description) \ | |
47 | RegisterFlag(parser, #Name, Description, &f->Name); | |
48 | #include "ubsan_flags.inc" | |
49 | #undef UBSAN_FLAG | |
866e32ad KS |
50 | } |
51 | ||
52 | void InitializeFlags() { | |
696d846a MO |
53 | SetCommonFlagsDefaults(); |
54 | { | |
55 | CommonFlags cf; | |
56 | cf.CopyFrom(*common_flags()); | |
f18ab180 | 57 | cf.print_summary = false; |
eac97531 | 58 | cf.external_symbolizer_path = GetFlag("UBSAN_SYMBOLIZER_PATH"); |
696d846a MO |
59 | OverrideCommonFlags(cf); |
60 | } | |
61 | ||
866e32ad | 62 | Flags *f = flags(); |
696d846a MO |
63 | f->SetDefaults(); |
64 | ||
65 | FlagParser parser; | |
66 | RegisterCommonFlags(&parser); | |
67 | RegisterUbsanFlags(&parser, f); | |
68 | ||
866e32ad | 69 | // Override from user-specified string. |
696d846a | 70 | parser.ParseString(MaybeCallUbsanDefaultOptions()); |
866e32ad | 71 | // Override from environment variable. |
b667dd70 | 72 | parser.ParseStringFromEnv("UBSAN_OPTIONS"); |
10189819 | 73 | InitializeCommonFlags(); |
696d846a MO |
74 | if (Verbosity()) ReportUnrecognizedFlags(); |
75 | ||
76 | if (common_flags()->help) parser.PrintFlagDescriptions(); | |
866e32ad KS |
77 | } |
78 | ||
79 | } // namespace __ubsan | |
80 | ||
5d3805fc JJ |
81 | SANITIZER_INTERFACE_WEAK_DEF(const char *, __ubsan_default_options, void) { |
82 | return ""; | |
83 | } | |
696d846a MO |
84 | |
85 | #endif // CAN_SANITIZE_UB |