Skip to content
This repository was archived by the owner on Jan 1, 2023. It is now read-only.

Commit e0afa7b

Browse files
committed
[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: emscripten-core/emscripten#9076 (comment) 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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@368101 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 493f8f6 commit e0afa7b

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
129129
static const char *const kAsanModuleCtorName = "asan.module_ctor";
130130
static const char *const kAsanModuleDtorName = "asan.module_dtor";
131131
static const uint64_t kAsanCtorAndDtorPriority = 1;
132+
// On Emscripten, the system needs more than one priorities for constructors.
133+
static const uint64_t kAsanEmscriptenCtorAndDtorPriority = 50;
132134
static const char *const kAsanReportErrorTemplate = "__asan_report_";
133135
static const char *const kAsanRegisterGlobalsName = "__asan_register_globals";
134136
static const char *const kAsanUnregisterGlobalsName =
@@ -530,6 +532,14 @@ static size_t RedzoneSizeForScale(int MappingScale) {
530532
return std::max(32U, 1U << MappingScale);
531533
}
532534

535+
static uint64_t GetCtorAndDtorPriority(Triple &TargetTriple) {
536+
if (TargetTriple.isOSEmscripten()) {
537+
return kAsanEmscriptenCtorAndDtorPriority;
538+
} else {
539+
return kAsanCtorAndDtorPriority;
540+
}
541+
}
542+
533543
namespace {
534544

535545
/// Module analysis for getting various metadata about the module.
@@ -1777,7 +1787,8 @@ void ModuleAddressSanitizer::createInitializerPoisonCalls(
17771787
if (F->getName() == kAsanModuleCtorName) continue;
17781788
ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
17791789
// Don't instrument CTORs that will run before asan.module_ctor.
1780-
if (Priority->getLimitedValue() <= kAsanCtorAndDtorPriority) continue;
1790+
if (Priority->getLimitedValue() <= GetCtorAndDtorPriority(TargetTriple))
1791+
continue;
17811792
poisonOneInitializer(*F, ModuleName);
17821793
}
17831794
}
@@ -2429,22 +2440,22 @@ bool ModuleAddressSanitizer::instrumentModule(Module &M) {
24292440
Changed |= InstrumentGlobals(IRB, M, &CtorComdat);
24302441
}
24312442

2443+
const uint64_t Priority = GetCtorAndDtorPriority(TargetTriple);
2444+
24322445
// Put the constructor and destructor in comdat if both
24332446
// (1) global instrumentation is not TU-specific
24342447
// (2) target is ELF.
24352448
if (UseCtorComdat && TargetTriple.isOSBinFormatELF() && CtorComdat) {
24362449
AsanCtorFunction->setComdat(M.getOrInsertComdat(kAsanModuleCtorName));
2437-
appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndDtorPriority,
2438-
AsanCtorFunction);
2450+
appendToGlobalCtors(M, AsanCtorFunction, Priority, AsanCtorFunction);
24392451
if (AsanDtorFunction) {
24402452
AsanDtorFunction->setComdat(M.getOrInsertComdat(kAsanModuleDtorName));
2441-
appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndDtorPriority,
2442-
AsanDtorFunction);
2453+
appendToGlobalDtors(M, AsanDtorFunction, Priority, AsanDtorFunction);
24432454
}
24442455
} else {
2445-
appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndDtorPriority);
2456+
appendToGlobalCtors(M, AsanCtorFunction, Priority);
24462457
if (AsanDtorFunction)
2447-
appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndDtorPriority);
2458+
appendToGlobalDtors(M, AsanDtorFunction, Priority);
24482459
}
24492460

24502461
return Changed;

0 commit comments

Comments
 (0)