2019-08-01 01:51:05 +08:00
|
|
|
//===-- ubsan_init.cpp ----------------------------------------------------===//
|
2014-07-26 06:24:34 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-07-26 06:24:34 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Initialization of UBSan runtime.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-27 01:26:04 +08:00
|
|
|
#include "ubsan_platform.h"
|
|
|
|
#if CAN_SANITIZE_UB
|
2015-02-21 01:41:59 +08:00
|
|
|
#include "ubsan_diag.h"
|
2014-07-26 06:24:34 +08:00
|
|
|
#include "ubsan_init.h"
|
|
|
|
#include "ubsan_flags.h"
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
|
|
|
#include "sanitizer_common/sanitizer_mutex.h"
|
2014-07-26 09:41:45 +08:00
|
|
|
#include "sanitizer_common/sanitizer_symbolizer.h"
|
2014-07-26 06:24:34 +08:00
|
|
|
|
|
|
|
using namespace __ubsan;
|
|
|
|
|
2017-04-15 02:24:35 +08:00
|
|
|
const char *__ubsan::GetSanititizerToolName() {
|
|
|
|
return "UndefinedBehaviorSanitizer";
|
|
|
|
}
|
|
|
|
|
2017-09-21 08:35:22 +08:00
|
|
|
static bool ubsan_initialized;
|
2015-04-02 06:42:36 +08:00
|
|
|
static StaticSpinMutex ubsan_init_mu;
|
2014-07-26 06:24:34 +08:00
|
|
|
|
2015-04-02 06:42:36 +08:00
|
|
|
static void CommonInit() {
|
2015-02-21 01:41:59 +08:00
|
|
|
InitializeSuppressions();
|
2015-04-02 06:42:36 +08:00
|
|
|
}
|
|
|
|
|
2021-03-02 08:33:20 +08:00
|
|
|
static void UbsanDie() {
|
|
|
|
if (common_flags()->print_module_map >= 1)
|
|
|
|
DumpProcessMap();
|
|
|
|
}
|
|
|
|
|
2015-04-02 06:42:36 +08:00
|
|
|
static void CommonStandaloneInit() {
|
2017-04-15 02:24:35 +08:00
|
|
|
SanitizerToolName = GetSanititizerToolName();
|
2017-09-12 05:12:43 +08:00
|
|
|
CacheBinaryName();
|
2017-09-12 05:13:06 +08:00
|
|
|
InitializeFlags();
|
2020-04-21 09:52:26 +08:00
|
|
|
__sanitizer::InitializePlatformEarly();
|
2015-04-20 04:16:13 +08:00
|
|
|
__sanitizer_set_report_path(common_flags()->log_path);
|
2016-08-16 02:35:40 +08:00
|
|
|
AndroidLogInit();
|
2015-01-06 09:31:23 +08:00
|
|
|
InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
|
2015-04-02 06:42:36 +08:00
|
|
|
CommonInit();
|
2021-03-02 08:33:20 +08:00
|
|
|
|
|
|
|
// Only add die callback when running in standalone mode to avoid printing
|
|
|
|
// the same information from multiple sanitizers' output
|
|
|
|
AddDieCallback(UbsanDie);
|
2020-04-21 09:27:43 +08:00
|
|
|
Symbolizer::LateInitialize();
|
2014-07-26 06:24:34 +08:00
|
|
|
}
|
|
|
|
|
2015-04-02 06:42:36 +08:00
|
|
|
void __ubsan::InitAsStandalone() {
|
|
|
|
SpinMutexLock l(&ubsan_init_mu);
|
2017-09-21 08:35:22 +08:00
|
|
|
if (!ubsan_initialized) {
|
2015-04-02 06:42:36 +08:00
|
|
|
CommonStandaloneInit();
|
2017-09-21 08:35:22 +08:00
|
|
|
ubsan_initialized = true;
|
2015-04-02 06:42:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-21 08:35:22 +08:00
|
|
|
void __ubsan::InitAsStandaloneIfNecessary() { return InitAsStandalone(); }
|
|
|
|
|
2015-04-02 06:42:36 +08:00
|
|
|
void __ubsan::InitAsPlugin() {
|
|
|
|
SpinMutexLock l(&ubsan_init_mu);
|
2017-09-21 08:35:22 +08:00
|
|
|
if (!ubsan_initialized) {
|
|
|
|
CommonInit();
|
|
|
|
ubsan_initialized = true;
|
|
|
|
}
|
2015-04-02 06:42:36 +08:00
|
|
|
}
|
2015-03-27 01:26:04 +08:00
|
|
|
|
|
|
|
#endif // CAN_SANITIZE_UB
|