1 //===-- ubsan_flags.cc ----------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Runtime flags for UndefinedBehaviorSanitizer.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ubsan_platform.h"
15 #if CAN_SANITIZE_UB
16 #include "ubsan_flags.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_flags.h"
19 #include "sanitizer_common/sanitizer_flag_parser.h"
20 
21 namespace __ubsan {
22 
MaybeCallUbsanDefaultOptions()23 const char *MaybeCallUbsanDefaultOptions() {
24   return (&__ubsan_default_options) ? __ubsan_default_options() : "";
25 }
26 
27 Flags ubsan_flags;
28 
SetDefaults()29 void Flags::SetDefaults() {
30 #define UBSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
31 #include "ubsan_flags.inc"
32 #undef UBSAN_FLAG
33 }
34 
RegisterUbsanFlags(FlagParser * parser,Flags * f)35 void RegisterUbsanFlags(FlagParser *parser, Flags *f) {
36 #define UBSAN_FLAG(Type, Name, DefaultValue, Description) \
37   RegisterFlag(parser, #Name, Description, &f->Name);
38 #include "ubsan_flags.inc"
39 #undef UBSAN_FLAG
40 }
41 
InitializeFlags()42 void InitializeFlags() {
43   SetCommonFlagsDefaults();
44   {
45     CommonFlags cf;
46     cf.CopyFrom(*common_flags());
47     cf.print_summary = false;
48     OverrideCommonFlags(cf);
49   }
50 
51   Flags *f = flags();
52   f->SetDefaults();
53 
54   FlagParser parser;
55   RegisterCommonFlags(&parser);
56   RegisterUbsanFlags(&parser, f);
57 
58   // Override from user-specified string.
59   parser.ParseString(MaybeCallUbsanDefaultOptions());
60   // Override from environment variable.
61   parser.ParseString(GetEnv("UBSAN_OPTIONS"));
62   SetVerbosity(common_flags()->verbosity);
63   if (Verbosity()) ReportUnrecognizedFlags();
64 
65   if (common_flags()->help) parser.PrintFlagDescriptions();
66 }
67 
68 }  // namespace __ubsan
69 
70 extern "C" {
71 
72 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
73 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
__ubsan_default_options()74 const char *__ubsan_default_options() { return ""; }
75 #endif
76 
77 #if SANITIZER_WINDOWS
__ubsan_default_default_options()78 const char *__ubsan_default_default_options() { return ""; }
79 # ifdef _WIN64
80 #  pragma comment(linker, "/alternatename:__ubsan_default_options=__ubsan_default_default_options")
81 # else
82 #  pragma comment(linker, "/alternatename:___ubsan_default_options=___ubsan_default_default_options")
83 # endif
84 #endif
85 
86 }  // extern "C"
87 
88 #endif  // CAN_SANITIZE_UB
89