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 #include <stdlib.h> 22 23 namespace __ubsan { 24 25 const char *MaybeCallUbsanDefaultOptions() { 26 return (&__ubsan_default_options) ? __ubsan_default_options() : ""; 27 } 28 29 static const char *GetFlag(const char *flag) { 30 // We cannot call getenv() from inside a preinit array initializer 31 if (SANITIZER_CAN_USE_PREINIT_ARRAY) { 32 return GetEnv(flag); 33 } else { 34 return getenv(flag); 35 } 36 } 37 38 Flags ubsan_flags; 39 40 void Flags::SetDefaults() { 41 #define UBSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue; 42 #include "ubsan_flags.inc" 43 #undef UBSAN_FLAG 44 } 45 46 void RegisterUbsanFlags(FlagParser *parser, Flags *f) { 47 #define UBSAN_FLAG(Type, Name, DefaultValue, Description) \ 48 RegisterFlag(parser, #Name, Description, &f->Name); 49 #include "ubsan_flags.inc" 50 #undef UBSAN_FLAG 51 } 52 53 void InitializeFlags() { 54 SetCommonFlagsDefaults(); 55 { 56 CommonFlags cf; 57 cf.CopyFrom(*common_flags()); 58 cf.print_summary = false; 59 cf.external_symbolizer_path = GetFlag("UBSAN_SYMBOLIZER_PATH"); 60 OverrideCommonFlags(cf); 61 } 62 63 Flags *f = flags(); 64 f->SetDefaults(); 65 66 FlagParser parser; 67 RegisterCommonFlags(&parser); 68 RegisterUbsanFlags(&parser, f); 69 70 // Override from user-specified string. 71 parser.ParseString(MaybeCallUbsanDefaultOptions()); 72 // Override from environment variable. 73 parser.ParseString(GetFlag("UBSAN_OPTIONS")); 74 InitializeCommonFlags(); 75 if (Verbosity()) ReportUnrecognizedFlags(); 76 77 if (common_flags()->help) parser.PrintFlagDescriptions(); 78 } 79 80 } // namespace __ubsan 81 82 SANITIZER_INTERFACE_WEAK_DEF(const char *, __ubsan_default_options, void) { 83 return ""; 84 } 85 86 #endif // CAN_SANITIZE_UB 87