ubsan_init.cpp 2.1 KB

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