ubsan_flags.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //===-- ubsan_flags.cpp ---------------------------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Runtime flags for UndefinedBehaviorSanitizer.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ubsan_platform.h"
  13. #if CAN_SANITIZE_UB
  14. #include "ubsan_flags.h"
  15. #include "sanitizer_common/sanitizer_common.h"
  16. #include "sanitizer_common/sanitizer_flags.h"
  17. #include "sanitizer_common/sanitizer_flag_parser.h"
  18. #include <stdlib.h>
  19. namespace __ubsan {
  20. static const char *GetFlag(const char *flag) {
  21. // We cannot call getenv() from inside a preinit array initializer
  22. if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
  23. return GetEnv(flag);
  24. } else {
  25. return getenv(flag);
  26. }
  27. }
  28. Flags ubsan_flags;
  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. void RegisterUbsanFlags(FlagParser *parser, Flags *f) {
  35. #define UBSAN_FLAG(Type, Name, DefaultValue, Description) \
  36. RegisterFlag(parser, #Name, Description, &f->Name);
  37. #include "ubsan_flags.inc"
  38. #undef UBSAN_FLAG
  39. }
  40. void InitializeFlags() {
  41. SetCommonFlagsDefaults();
  42. {
  43. CommonFlags cf;
  44. cf.CopyFrom(*common_flags());
  45. cf.external_symbolizer_path = GetFlag("UBSAN_SYMBOLIZER_PATH");
  46. OverrideCommonFlags(cf);
  47. }
  48. Flags *f = flags();
  49. f->SetDefaults();
  50. FlagParser parser;
  51. RegisterCommonFlags(&parser);
  52. RegisterUbsanFlags(&parser, f);
  53. // Override from user-specified string.
  54. parser.ParseString(__ubsan_default_options());
  55. // Override from environment variable.
  56. parser.ParseStringFromEnv("UBSAN_OPTIONS");
  57. InitializeCommonFlags();
  58. if (Verbosity()) ReportUnrecognizedFlags();
  59. if (common_flags()->help) parser.PrintFlagDescriptions();
  60. }
  61. } // namespace __ubsan
  62. SANITIZER_INTERFACE_WEAK_DEF(const char *, __ubsan_default_options, void) {
  63. return "";
  64. }
  65. #endif // CAN_SANITIZE_UB