forked from OSchip/llvm-project
[WebAssembly] Lower ASan constructor priority on Emscripten
Summary: This change gives Emscripten the ability to use more than one constructor priorities that runs before ASan. By convention, constructor priorites 0-100 are reserved for use by the system. ASan on Emscripten now uses priority 50, leaving plenty of room for use by Emscripten before and after ASan. This change is done in response to: https://github.com/emscripten-core/emscripten/pull/9076#discussion_r310323723 Reviewers: kripken, tlively, aheejin Reviewed By: tlively Subscribers: cfe-commits, dschuff, sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits Tags: #llvm, #clang Differential Revision: https://reviews.llvm.org/D65684 llvm-svn: 368101
This commit is contained in:
parent
a0438305d0
commit
b3292a8469
|
@ -0,0 +1,5 @@
|
||||||
|
// RUN: %clang_cc1 -fsanitize=address -emit-llvm -o - %s | FileCheck %s
|
||||||
|
// RUN: %clang_cc1 -triple wasm32-unknown-emscripten -fsanitize=address -emit-llvm -o - %s | FileCheck %s --check-prefix=EMSCRIPTEN
|
||||||
|
|
||||||
|
// CHECK: @llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 1, void ()* @asan.module_ctor, i8* null }]
|
||||||
|
// EMSCRIPTEN: @llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 50, void ()* @asan.module_ctor, i8* null }]
|
|
@ -129,6 +129,8 @@ static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
|
||||||
static const char *const kAsanModuleCtorName = "asan.module_ctor";
|
static const char *const kAsanModuleCtorName = "asan.module_ctor";
|
||||||
static const char *const kAsanModuleDtorName = "asan.module_dtor";
|
static const char *const kAsanModuleDtorName = "asan.module_dtor";
|
||||||
static const uint64_t kAsanCtorAndDtorPriority = 1;
|
static const uint64_t kAsanCtorAndDtorPriority = 1;
|
||||||
|
// On Emscripten, the system needs more than one priorities for constructors.
|
||||||
|
static const uint64_t kAsanEmscriptenCtorAndDtorPriority = 50;
|
||||||
static const char *const kAsanReportErrorTemplate = "__asan_report_";
|
static const char *const kAsanReportErrorTemplate = "__asan_report_";
|
||||||
static const char *const kAsanRegisterGlobalsName = "__asan_register_globals";
|
static const char *const kAsanRegisterGlobalsName = "__asan_register_globals";
|
||||||
static const char *const kAsanUnregisterGlobalsName =
|
static const char *const kAsanUnregisterGlobalsName =
|
||||||
|
@ -530,6 +532,14 @@ static size_t RedzoneSizeForScale(int MappingScale) {
|
||||||
return std::max(32U, 1U << MappingScale);
|
return std::max(32U, 1U << MappingScale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint64_t GetCtorAndDtorPriority(Triple &TargetTriple) {
|
||||||
|
if (TargetTriple.isOSEmscripten()) {
|
||||||
|
return kAsanEmscriptenCtorAndDtorPriority;
|
||||||
|
} else {
|
||||||
|
return kAsanCtorAndDtorPriority;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
/// Module analysis for getting various metadata about the module.
|
/// Module analysis for getting various metadata about the module.
|
||||||
|
@ -1777,7 +1787,8 @@ void ModuleAddressSanitizer::createInitializerPoisonCalls(
|
||||||
if (F->getName() == kAsanModuleCtorName) continue;
|
if (F->getName() == kAsanModuleCtorName) continue;
|
||||||
ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
|
ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
|
||||||
// Don't instrument CTORs that will run before asan.module_ctor.
|
// Don't instrument CTORs that will run before asan.module_ctor.
|
||||||
if (Priority->getLimitedValue() <= kAsanCtorAndDtorPriority) continue;
|
if (Priority->getLimitedValue() <= GetCtorAndDtorPriority(TargetTriple))
|
||||||
|
continue;
|
||||||
poisonOneInitializer(*F, ModuleName);
|
poisonOneInitializer(*F, ModuleName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2429,22 +2440,22 @@ bool ModuleAddressSanitizer::instrumentModule(Module &M) {
|
||||||
Changed |= InstrumentGlobals(IRB, M, &CtorComdat);
|
Changed |= InstrumentGlobals(IRB, M, &CtorComdat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const uint64_t Priority = GetCtorAndDtorPriority(TargetTriple);
|
||||||
|
|
||||||
// Put the constructor and destructor in comdat if both
|
// Put the constructor and destructor in comdat if both
|
||||||
// (1) global instrumentation is not TU-specific
|
// (1) global instrumentation is not TU-specific
|
||||||
// (2) target is ELF.
|
// (2) target is ELF.
|
||||||
if (UseCtorComdat && TargetTriple.isOSBinFormatELF() && CtorComdat) {
|
if (UseCtorComdat && TargetTriple.isOSBinFormatELF() && CtorComdat) {
|
||||||
AsanCtorFunction->setComdat(M.getOrInsertComdat(kAsanModuleCtorName));
|
AsanCtorFunction->setComdat(M.getOrInsertComdat(kAsanModuleCtorName));
|
||||||
appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndDtorPriority,
|
appendToGlobalCtors(M, AsanCtorFunction, Priority, AsanCtorFunction);
|
||||||
AsanCtorFunction);
|
|
||||||
if (AsanDtorFunction) {
|
if (AsanDtorFunction) {
|
||||||
AsanDtorFunction->setComdat(M.getOrInsertComdat(kAsanModuleDtorName));
|
AsanDtorFunction->setComdat(M.getOrInsertComdat(kAsanModuleDtorName));
|
||||||
appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndDtorPriority,
|
appendToGlobalDtors(M, AsanDtorFunction, Priority, AsanDtorFunction);
|
||||||
AsanDtorFunction);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndDtorPriority);
|
appendToGlobalCtors(M, AsanCtorFunction, Priority);
|
||||||
if (AsanDtorFunction)
|
if (AsanDtorFunction)
|
||||||
appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndDtorPriority);
|
appendToGlobalDtors(M, AsanDtorFunction, Priority);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Changed;
|
return Changed;
|
||||||
|
|
Loading…
Reference in New Issue