@@ -819,11 +819,11 @@ class ModuleAddressSanitizer {
819
819
private:
820
820
void initializeCallbacks (Module &M);
821
821
822
- void instrumentGlobals (IRBuilder<> &IRB, Module &M, bool *CtorComdat);
822
+ bool InstrumentGlobals (IRBuilder<> &IRB, Module &M, bool *CtorComdat);
823
823
void InstrumentGlobalsCOFF (IRBuilder<> &IRB, Module &M,
824
824
ArrayRef<GlobalVariable *> ExtendedGlobals,
825
825
ArrayRef<Constant *> MetadataInitializers);
826
- void instrumentGlobalsELF (IRBuilder<> &IRB, Module &M,
826
+ void InstrumentGlobalsELF (IRBuilder<> &IRB, Module &M,
827
827
ArrayRef<GlobalVariable *> ExtendedGlobals,
828
828
ArrayRef<Constant *> MetadataInitializers,
829
829
const std::string &UniqueModuleId);
@@ -2177,7 +2177,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsCOFF(
2177
2177
appendToCompilerUsed (M, MetadataGlobals);
2178
2178
}
2179
2179
2180
- void ModuleAddressSanitizer::instrumentGlobalsELF (
2180
+ void ModuleAddressSanitizer::InstrumentGlobalsELF (
2181
2181
IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2182
2182
ArrayRef<Constant *> MetadataInitializers,
2183
2183
const std::string &UniqueModuleId) {
@@ -2187,7 +2187,7 @@ void ModuleAddressSanitizer::instrumentGlobalsELF(
2187
2187
// false negative odr violations at link time. If odr indicators are used, we
2188
2188
// keep the comdat sections, as link time odr violations will be dectected on
2189
2189
// the odr indicator symbols.
2190
- bool UseComdatForGlobalsGC = UseOdrIndicator && !UniqueModuleId. empty () ;
2190
+ bool UseComdatForGlobalsGC = UseOdrIndicator;
2191
2191
2192
2192
SmallVector<GlobalValue *, 16 > MetadataGlobals (ExtendedGlobals.size ());
2193
2193
for (size_t i = 0 ; i < ExtendedGlobals.size (); i++) {
@@ -2237,7 +2237,7 @@ void ModuleAddressSanitizer::instrumentGlobalsELF(
2237
2237
2238
2238
// We also need to unregister globals at the end, e.g., when a shared library
2239
2239
// gets closed.
2240
- if (DestructorKind != AsanDtorKind::None && !MetadataGlobals. empty () ) {
2240
+ if (DestructorKind != AsanDtorKind::None) {
2241
2241
IRBuilder<> IrbDtor (CreateAsanModuleDtor (M));
2242
2242
IrbDtor.CreateCall (AsanUnregisterElfGlobals,
2243
2243
{IRB.CreatePointerCast (RegisteredFlag, IntptrTy),
@@ -2343,8 +2343,10 @@ void ModuleAddressSanitizer::InstrumentGlobalsWithMetadataArray(
2343
2343
// redzones and inserts this function into llvm.global_ctors.
2344
2344
// Sets *CtorComdat to true if the global registration code emitted into the
2345
2345
// asan constructor is comdat-compatible.
2346
- void ModuleAddressSanitizer::instrumentGlobals (IRBuilder<> &IRB, Module &M,
2346
+ bool ModuleAddressSanitizer::InstrumentGlobals (IRBuilder<> &IRB, Module &M,
2347
2347
bool *CtorComdat) {
2348
+ *CtorComdat = false ;
2349
+
2348
2350
// Build set of globals that are aliased by some GA, where
2349
2351
// getExcludedAliasedGlobal(GA) returns the relevant GlobalVariable.
2350
2352
SmallPtrSet<const GlobalVariable *, 16 > AliasedGlobalExclusions;
@@ -2362,6 +2364,11 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
2362
2364
}
2363
2365
2364
2366
size_t n = GlobalsToChange.size ();
2367
+ if (n == 0 ) {
2368
+ *CtorComdat = true ;
2369
+ return false ;
2370
+ }
2371
+
2365
2372
auto &DL = M.getDataLayout ();
2366
2373
2367
2374
// A global is described by a structure
@@ -2384,11 +2391,8 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
2384
2391
2385
2392
// We shouldn't merge same module names, as this string serves as unique
2386
2393
// module ID in runtime.
2387
- GlobalVariable *ModuleName =
2388
- n != 0
2389
- ? createPrivateGlobalForString (M, M.getModuleIdentifier (),
2390
- /* AllowMerging*/ false , kAsanGenPrefix )
2391
- : nullptr ;
2394
+ GlobalVariable *ModuleName = createPrivateGlobalForString (
2395
+ M, M.getModuleIdentifier (), /* AllowMerging*/ false , kAsanGenPrefix );
2392
2396
2393
2397
for (size_t i = 0 ; i < n; i++) {
2394
2398
GlobalVariable *G = GlobalsToChange[i];
@@ -2513,34 +2517,27 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
2513
2517
}
2514
2518
appendToCompilerUsed (M, ArrayRef<GlobalValue *>(GlobalsToAddToUsedList));
2515
2519
2516
- if (UseGlobalsGC && TargetTriple.isOSBinFormatELF ()) {
2517
- // Use COMDAT and register globals even if n == 0 to ensure that (a) the
2518
- // linkage unit will only have one module constructor, and (b) the register
2519
- // function will be called. The module destructor is not created when n ==
2520
- // 0.
2520
+ std::string ELFUniqueModuleId =
2521
+ (UseGlobalsGC && TargetTriple.isOSBinFormatELF ()) ? getUniqueModuleId (&M)
2522
+ : " " ;
2523
+
2524
+ if (!ELFUniqueModuleId.empty ()) {
2525
+ InstrumentGlobalsELF (IRB, M, NewGlobals, Initializers, ELFUniqueModuleId);
2521
2526
*CtorComdat = true ;
2522
- instrumentGlobalsELF (IRB, M, NewGlobals, Initializers,
2523
- getUniqueModuleId (&M));
2524
- } else if (n == 0 ) {
2525
- // When UseGlobalsGC is false, COMDAT can still be used if n == 0, because
2526
- // all compile units will have identical module constructor/destructor.
2527
- *CtorComdat = TargetTriple.isOSBinFormatELF ();
2527
+ } else if (UseGlobalsGC && TargetTriple.isOSBinFormatCOFF ()) {
2528
+ InstrumentGlobalsCOFF (IRB, M, NewGlobals, Initializers);
2529
+ } else if (UseGlobalsGC && ShouldUseMachOGlobalsSection ()) {
2530
+ InstrumentGlobalsMachO (IRB, M, NewGlobals, Initializers);
2528
2531
} else {
2529
- *CtorComdat = false ;
2530
- if (UseGlobalsGC && TargetTriple.isOSBinFormatCOFF ()) {
2531
- InstrumentGlobalsCOFF (IRB, M, NewGlobals, Initializers);
2532
- } else if (UseGlobalsGC && ShouldUseMachOGlobalsSection ()) {
2533
- InstrumentGlobalsMachO (IRB, M, NewGlobals, Initializers);
2534
- } else {
2535
- InstrumentGlobalsWithMetadataArray (IRB, M, NewGlobals, Initializers);
2536
- }
2532
+ InstrumentGlobalsWithMetadataArray (IRB, M, NewGlobals, Initializers);
2537
2533
}
2538
2534
2539
2535
// Create calls for poisoning before initializers run and unpoisoning after.
2540
2536
if (HasDynamicallyInitializedGlobals)
2541
2537
createInitializerPoisonCalls (M, ModuleName);
2542
2538
2543
2539
LLVM_DEBUG (dbgs () << M);
2540
+ return true ;
2544
2541
}
2545
2542
2546
2543
uint64_t
@@ -2604,10 +2601,10 @@ bool ModuleAddressSanitizer::instrumentModule(Module &M) {
2604
2601
assert (AsanCtorFunction || ConstructorKind == AsanCtorKind::None);
2605
2602
if (AsanCtorFunction) {
2606
2603
IRBuilder<> IRB (AsanCtorFunction->getEntryBlock ().getTerminator ());
2607
- instrumentGlobals (IRB, M, &CtorComdat);
2604
+ InstrumentGlobals (IRB, M, &CtorComdat);
2608
2605
} else {
2609
2606
IRBuilder<> IRB (*C);
2610
- instrumentGlobals (IRB, M, &CtorComdat);
2607
+ InstrumentGlobals (IRB, M, &CtorComdat);
2611
2608
}
2612
2609
}
2613
2610
0 commit comments