ubsan_init.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //===-- ubsan_init.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. // Initialization of UBSan runtime.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ubsan_platform.h"
  13. #if CAN_SANITIZE_UB
  14. #include "ubsan_diag.h"
  15. #include "ubsan_init.h"
  16. #include "ubsan_flags.h"
  17. #include "sanitizer_common/sanitizer_common.h"
  18. #include "sanitizer_common/sanitizer_libc.h"
  19. #include "sanitizer_common/sanitizer_mutex.h"
  20. #include "sanitizer_common/sanitizer_symbolizer.h"
  21. using namespace __ubsan;
  22. const char *__ubsan::GetSanititizerToolName() {
  23. return "UndefinedBehaviorSanitizer";
  24. }
  25. static bool ubsan_initialized;
  26. static StaticSpinMutex ubsan_init_mu;
  27. static void CommonInit() {
  28. InitializeSuppressions();
  29. }
  30. static void UbsanDie() {
  31. if (common_flags()->print_module_map >= 1)
  32. DumpProcessMap();
  33. }
  34. static void CommonStandaloneInit() {
  35. SanitizerToolName = GetSanititizerToolName();
  36. CacheBinaryName();
  37. InitializeFlags();
  38. __sanitizer::InitializePlatformEarly();
  39. __sanitizer_set_report_path(common_flags()->log_path);
  40. AndroidLogInit();
  41. InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
  42. CommonInit();
  43. // Only add die callback when running in standalone mode to avoid printing
  44. // the same information from multiple sanitizers' output
  45. AddDieCallback(UbsanDie);
  46. Symbolizer::LateInitialize();
  47. }
  48. void __ubsan::InitAsStandalone() {
  49. SpinMutexLock l(&ubsan_init_mu);
  50. if (!ubsan_initialized) {
  51. CommonStandaloneInit();
  52. ubsan_initialized = true;
  53. }
  54. }
  55. void __ubsan::InitAsStandaloneIfNecessary() { return InitAsStandalone(); }
  56. void __ubsan::InitAsPlugin() {
  57. SpinMutexLock l(&ubsan_init_mu);
  58. if (!ubsan_initialized) {
  59. CommonInit();
  60. ubsan_initialized = true;
  61. }
  62. }
  63. #endif // CAN_SANITIZE_UB