Skip to content

Commit a204b52

Browse files
author
git apple-llvm automerger
committed
Merge commit '5d87665de956' from llvm.org/main into next
2 parents 6a438e8 + 5d87665 commit a204b52

File tree

3 files changed

+29
-45
lines changed

3 files changed

+29
-45
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ class ModuleAddressSanitizer {
815815
private:
816816
void initializeCallbacks(Module &M);
817817

818-
void InstrumentGlobals(IRBuilder<> &IRB, Module &M, bool *CtorComdat);
818+
bool InstrumentGlobals(IRBuilder<> &IRB, Module &M, bool *CtorComdat);
819819
void InstrumentGlobalsCOFF(IRBuilder<> &IRB, Module &M,
820820
ArrayRef<GlobalVariable *> ExtendedGlobals,
821821
ArrayRef<Constant *> MetadataInitializers);
@@ -2235,7 +2235,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsELF(
22352235

22362236
// We also need to unregister globals at the end, e.g., when a shared library
22372237
// gets closed.
2238-
if (DestructorKind != AsanDtorKind::None && !MetadataGlobals.empty()) {
2238+
if (DestructorKind != AsanDtorKind::None) {
22392239
IRBuilder<> IrbDtor(CreateAsanModuleDtor(M));
22402240
IrbDtor.CreateCall(AsanUnregisterElfGlobals,
22412241
{IRB.CreatePointerCast(RegisteredFlag, IntptrTy),
@@ -2341,8 +2341,10 @@ void ModuleAddressSanitizer::InstrumentGlobalsWithMetadataArray(
23412341
// redzones and inserts this function into llvm.global_ctors.
23422342
// Sets *CtorComdat to true if the global registration code emitted into the
23432343
// asan constructor is comdat-compatible.
2344-
void ModuleAddressSanitizer::InstrumentGlobals(IRBuilder<> &IRB, Module &M,
2344+
bool ModuleAddressSanitizer::InstrumentGlobals(IRBuilder<> &IRB, Module &M,
23452345
bool *CtorComdat) {
2346+
*CtorComdat = false;
2347+
23462348
// Build set of globals that are aliased by some GA, where
23472349
// getExcludedAliasedGlobal(GA) returns the relevant GlobalVariable.
23482350
SmallPtrSet<const GlobalVariable *, 16> AliasedGlobalExclusions;
@@ -2360,6 +2362,11 @@ void ModuleAddressSanitizer::InstrumentGlobals(IRBuilder<> &IRB, Module &M,
23602362
}
23612363

23622364
size_t n = GlobalsToChange.size();
2365+
if (n == 0) {
2366+
*CtorComdat = true;
2367+
return false;
2368+
}
2369+
23632370
auto &DL = M.getDataLayout();
23642371

23652372
// A global is described by a structure
@@ -2382,11 +2389,8 @@ void ModuleAddressSanitizer::InstrumentGlobals(IRBuilder<> &IRB, Module &M,
23822389

23832390
// We shouldn't merge same module names, as this string serves as unique
23842391
// module ID in runtime.
2385-
GlobalVariable *ModuleName =
2386-
n != 0
2387-
? createPrivateGlobalForString(M, M.getModuleIdentifier(),
2388-
/*AllowMerging*/ false, kAsanGenPrefix)
2389-
: nullptr;
2392+
GlobalVariable *ModuleName = createPrivateGlobalForString(
2393+
M, M.getModuleIdentifier(), /*AllowMerging*/ false, kAsanGenPrefix);
23902394

23912395
for (size_t i = 0; i < n; i++) {
23922396
GlobalVariable *G = GlobalsToChange[i];
@@ -2511,34 +2515,27 @@ void ModuleAddressSanitizer::InstrumentGlobals(IRBuilder<> &IRB, Module &M,
25112515
}
25122516
appendToCompilerUsed(M, ArrayRef<GlobalValue *>(GlobalsToAddToUsedList));
25132517

2514-
if (UseGlobalsGC && TargetTriple.isOSBinFormatELF()) {
2515-
// Use COMDAT and register globals even if n == 0 to ensure that (a) the
2516-
// linkage unit will only have one module constructor, and (b) the register
2517-
// function will be called. The module destructor is not created when n ==
2518-
// 0.
2518+
std::string ELFUniqueModuleId =
2519+
(UseGlobalsGC && TargetTriple.isOSBinFormatELF()) ? getUniqueModuleId(&M)
2520+
: "";
2521+
2522+
if (!ELFUniqueModuleId.empty()) {
2523+
InstrumentGlobalsELF(IRB, M, NewGlobals, Initializers, ELFUniqueModuleId);
25192524
*CtorComdat = true;
2520-
InstrumentGlobalsELF(IRB, M, NewGlobals, Initializers,
2521-
getUniqueModuleId(&M));
2522-
} else if (n == 0) {
2523-
// When UseGlobalsGC is false, COMDAT can still be used if n == 0, because
2524-
// all compile units will have identical module constructor/destructor.
2525-
*CtorComdat = TargetTriple.isOSBinFormatELF();
2525+
} else if (UseGlobalsGC && TargetTriple.isOSBinFormatCOFF()) {
2526+
InstrumentGlobalsCOFF(IRB, M, NewGlobals, Initializers);
2527+
} else if (UseGlobalsGC && ShouldUseMachOGlobalsSection()) {
2528+
InstrumentGlobalsMachO(IRB, M, NewGlobals, Initializers);
25262529
} else {
2527-
*CtorComdat = false;
2528-
if (UseGlobalsGC && TargetTriple.isOSBinFormatCOFF()) {
2529-
InstrumentGlobalsCOFF(IRB, M, NewGlobals, Initializers);
2530-
} else if (UseGlobalsGC && ShouldUseMachOGlobalsSection()) {
2531-
InstrumentGlobalsMachO(IRB, M, NewGlobals, Initializers);
2532-
} else {
2533-
InstrumentGlobalsWithMetadataArray(IRB, M, NewGlobals, Initializers);
2534-
}
2530+
InstrumentGlobalsWithMetadataArray(IRB, M, NewGlobals, Initializers);
25352531
}
25362532

25372533
// Create calls for poisoning before initializers run and unpoisoning after.
25382534
if (HasDynamicallyInitializedGlobals)
25392535
createInitializerPoisonCalls(M, ModuleName);
25402536

25412537
LLVM_DEBUG(dbgs() << M);
2538+
return true;
25422539
}
25432540

25442541
uint64_t

llvm/test/Instrumentation/AddressSanitizer/basic.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,8 @@ define void @test_swifterror_3() sanitize_address {
210210

211211
;; ctor/dtor have the nounwind attribute. See uwtable.ll, they additionally have
212212
;; the uwtable attribute with the module flag "uwtable".
213-
; CHECK: define internal void @asan.module_ctor() #[[#ATTR:]] comdat {
213+
; CHECK: define internal void @asan.module_ctor() #[[#ATTR:]] {{(comdat )?}}{
214214
; CHECK: call void @__asan_init()
215-
;; __asan_register_elf_globals is called even if this module does not contain global variables.
216-
; CHECK: call void @__asan_register_elf_globals(i64 ptrtoint (ptr @___asan_globals_registered to i64), i64 ptrtoint (ptr @__start_asan_globals to i64), i64 ptrtoint (ptr @__stop_asan_globals to i64))
217215

218216
; CHECK: attributes #[[#ATTR]] = { nounwind }
219217

llvm/test/Instrumentation/AddressSanitizer/global_metadata_array.ll

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
; RUN: rm -rf %t && split-file %s %t && cd %t
2-
; RUN: opt < a.ll -passes=asan -asan-globals-live-support=0 -mtriple=x86_64-unknown-linux-gnu -S | FileCheck --check-prefix=CHECK %s
3-
; RUN: opt < a.ll -passes=asan -asan-globals-live-support=0 -mtriple=x86_64-apple-macosx10.11.0 -S | FileCheck --check-prefix=CHECK %s
4-
; RUN: opt < a.ll -passes=asan -asan-globals-live-support=0 -mtriple=x86_64-pc-windows-msvc19.0.24215 -S | FileCheck --check-prefix=CHECK %s
5-
; RUN: opt < a.ll -passes=asan -asan-globals-live-support=0 -asan-mapping-scale=5 -mtriple=x86_64-unknown-linux-gnu -S | FileCheck --check-prefixes=CHECK,CHECK-S5 %s
1+
; RUN: opt < %s -passes=asan -asan-globals-live-support=0 -mtriple=x86_64-unknown-linux-gnu -S | FileCheck --check-prefix=CHECK %s
2+
; RUN: opt < %s -passes=asan -asan-globals-live-support=0 -mtriple=x86_64-apple-macosx10.11.0 -S | FileCheck --check-prefix=CHECK %s
3+
; RUN: opt < %s -passes=asan -asan-globals-live-support=0 -mtriple=x86_64-pc-windows-msvc19.0.24215 -S | FileCheck --check-prefix=CHECK %s
4+
; RUN: opt < %s -passes=asan -asan-globals-live-support=0 -asan-mapping-scale=5 -mtriple=x86_64-unknown-linux-gnu -S | FileCheck --check-prefixes=CHECK,CHECK-S5 %s
65

7-
; RUN: opt < empty.ll -passes=asan -asan-globals-live-support=0 -mtriple=x86_64-unknown-linux-gnu -S | FileCheck --check-prefix=ELF-NOGC %s
8-
9-
;--- a.ll
106
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
117

128
; Globals:
@@ -63,10 +59,3 @@ attributes #1 = { nounwind sanitize_address "less-precise-fpmad"="false" "frame-
6359
!7 = !{!"/tmp/asan-globals.cpp", i32 7, i32 5}
6460
!8 = !{!"/tmp/asan-globals.cpp", i32 12, i32 14}
6561
!9 = !{!"/tmp/asan-globals.cpp", i32 14, i32 25}
66-
67-
;; In the presence of instrumented global variables, asan.module_ctor do not use comdat.
68-
; CHECK: define internal void @asan.module_ctor() #[[#ATTR:]] {
69-
70-
; ELF-NOGC: define internal void @asan.module_ctor() #[[#ATTR:]] comdat {
71-
72-
;--- empty.ll

0 commit comments

Comments
 (0)