Skip to content

Commit d383329

Browse files
committed
Revert 374481 "[tsan,msan] Insert module constructors in a module pass"
CodeGen/sanitizer-module-constructor.c fails on mac and windows, see e.g. http://lab.llvm.org:8011/builders/clang-x64-windows-msvc/builds/11424 llvm-svn: 374503
1 parent c1f8e04 commit d383329

File tree

9 files changed

+55
-102
lines changed

9 files changed

+55
-102
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,6 @@ static void addSanitizersAtO0(ModulePassManager &MPM,
974974
}
975975

976976
if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
977-
MPM.addPass(MemorySanitizerPass({}));
978977
MPM.addPass(createModuleToFunctionPassAdaptor(MemorySanitizerPass({})));
979978
}
980979

@@ -984,7 +983,6 @@ static void addSanitizersAtO0(ModulePassManager &MPM,
984983
}
985984

986985
if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
987-
MPM.addPass(ThreadSanitizerPass());
988986
MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
989987
}
990988
}
@@ -1164,23 +1162,16 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
11641162
[](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
11651163
FPM.addPass(BoundsCheckingPass());
11661164
});
1167-
if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
1168-
PB.registerPipelineStartEPCallback([](ModulePassManager &MPM) {
1169-
MPM.addPass(MemorySanitizerPass({}));
1170-
});
1165+
if (LangOpts.Sanitize.has(SanitizerKind::Memory))
11711166
PB.registerOptimizerLastEPCallback(
11721167
[](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
11731168
FPM.addPass(MemorySanitizerPass({}));
11741169
});
1175-
}
1176-
if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
1177-
PB.registerPipelineStartEPCallback(
1178-
[](ModulePassManager &MPM) { MPM.addPass(ThreadSanitizerPass()); });
1170+
if (LangOpts.Sanitize.has(SanitizerKind::Thread))
11791171
PB.registerOptimizerLastEPCallback(
11801172
[](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
11811173
FPM.addPass(ThreadSanitizerPass());
11821174
});
1183-
}
11841175
if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
11851176
PB.registerPipelineStartEPCallback([&](ModulePassManager &MPM) {
11861177
MPM.addPass(

clang/test/CodeGen/sanitizer-module-constructor.c

Lines changed: 0 additions & 22 deletions
This file was deleted.

llvm/include/llvm/Transforms/Instrumentation/MemorySanitizer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ struct MemorySanitizerPass : public PassInfoMixin<MemorySanitizerPass> {
4040
MemorySanitizerPass(MemorySanitizerOptions Options) : Options(Options) {}
4141

4242
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
43-
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
4443

4544
private:
4645
MemorySanitizerOptions Options;

llvm/include/llvm/Transforms/Instrumentation/ThreadSanitizer.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ FunctionPass *createThreadSanitizerLegacyPassPass();
2727
/// yet, the pass inserts the declarations. Otherwise the existing globals are
2828
struct ThreadSanitizerPass : public PassInfoMixin<ThreadSanitizerPass> {
2929
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
30-
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
3130
};
32-
3331
} // namespace llvm
3432
#endif /* LLVM_TRANSFORMS_INSTRUMENTATION_THREADSANITIZER_H */

llvm/lib/Passes/PassRegistry.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ MODULE_PASS("synthetic-counts-propagation", SyntheticCountsPropagation())
8686
MODULE_PASS("wholeprogramdevirt", WholeProgramDevirtPass(nullptr, nullptr))
8787
MODULE_PASS("verify", VerifierPass())
8888
MODULE_PASS("asan-module", ModuleAddressSanitizerPass(/*CompileKernel=*/false, false, true, false))
89-
MODULE_PASS("msan-module", MemorySanitizerPass({}))
90-
MODULE_PASS("tsan-module", ThreadSanitizerPass())
9189
MODULE_PASS("kasan-module", ModuleAddressSanitizerPass(/*CompileKernel=*/true, false, true, false))
9290
MODULE_PASS("sancov-module", ModuleSanitizerCoveragePass())
9391
MODULE_PASS("poison-checking", PoisonCheckingPass())

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -587,25 +587,9 @@ class MemorySanitizer {
587587

588588
/// An empty volatile inline asm that prevents callback merge.
589589
InlineAsm *EmptyAsm;
590-
};
591590

592-
void insertModuleCtor(Module &M) {
593-
getOrCreateSanitizerCtorAndInitFunctions(
594-
M, kMsanModuleCtorName, kMsanInitName,
595-
/*InitArgTypes=*/{},
596-
/*InitArgs=*/{},
597-
// This callback is invoked when the functions are created the first
598-
// time. Hook them into the global ctors list in that case:
599-
[&](Function *Ctor, FunctionCallee) {
600-
if (!ClWithComdat) {
601-
appendToGlobalCtors(M, Ctor, 0);
602-
return;
603-
}
604-
Comdat *MsanCtorComdat = M.getOrInsertComdat(kMsanModuleCtorName);
605-
Ctor->setComdat(MsanCtorComdat);
606-
appendToGlobalCtors(M, Ctor, 0, Ctor);
607-
});
608-
}
591+
Function *MsanCtorFunction;
592+
};
609593

610594
/// A legacy function pass for msan instrumentation.
611595
///
@@ -651,14 +635,6 @@ PreservedAnalyses MemorySanitizerPass::run(Function &F,
651635
return PreservedAnalyses::all();
652636
}
653637

654-
PreservedAnalyses MemorySanitizerPass::run(Module &M,
655-
ModuleAnalysisManager &AM) {
656-
if (Options.Kernel)
657-
return PreservedAnalyses::all();
658-
insertModuleCtor(M);
659-
return PreservedAnalyses::none();
660-
}
661-
662638
char MemorySanitizerLegacyPass::ID = 0;
663639

664640
INITIALIZE_PASS_BEGIN(MemorySanitizerLegacyPass, "msan",
@@ -944,6 +920,23 @@ void MemorySanitizer::initializeModule(Module &M) {
944920
OriginStoreWeights = MDBuilder(*C).createBranchWeights(1, 1000);
945921

946922
if (!CompileKernel) {
923+
std::tie(MsanCtorFunction, std::ignore) =
924+
getOrCreateSanitizerCtorAndInitFunctions(
925+
M, kMsanModuleCtorName, kMsanInitName,
926+
/*InitArgTypes=*/{},
927+
/*InitArgs=*/{},
928+
// This callback is invoked when the functions are created the first
929+
// time. Hook them into the global ctors list in that case:
930+
[&](Function *Ctor, FunctionCallee) {
931+
if (!ClWithComdat) {
932+
appendToGlobalCtors(M, Ctor, 0);
933+
return;
934+
}
935+
Comdat *MsanCtorComdat = M.getOrInsertComdat(kMsanModuleCtorName);
936+
Ctor->setComdat(MsanCtorComdat);
937+
appendToGlobalCtors(M, Ctor, 0, Ctor);
938+
});
939+
947940
if (TrackOrigins)
948941
M.getOrInsertGlobal("__msan_track_origins", IRB.getInt32Ty(), [&] {
949942
return new GlobalVariable(
@@ -961,8 +954,6 @@ void MemorySanitizer::initializeModule(Module &M) {
961954
}
962955

963956
bool MemorySanitizerLegacyPass::doInitialization(Module &M) {
964-
if (!Options.Kernel)
965-
insertModuleCtor(M);
966957
MSan.emplace(M, Options);
967958
return true;
968959
}
@@ -4587,9 +4578,8 @@ static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
45874578
}
45884579

45894580
bool MemorySanitizer::sanitizeFunction(Function &F, TargetLibraryInfo &TLI) {
4590-
if (!CompileKernel && F.getName() == kMsanModuleCtorName)
4581+
if (!CompileKernel && (&F == MsanCtorFunction))
45914582
return false;
4592-
45934583
MemorySanitizerVisitor Visitor(F, *this, TLI);
45944584

45954585
// Clear out readonly/readnone attributes.

llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ namespace {
9292
/// ensures the __tsan_init function is in the list of global constructors for
9393
/// the module.
9494
struct ThreadSanitizer {
95+
ThreadSanitizer(Module &M);
9596
bool sanitizeFunction(Function &F, const TargetLibraryInfo &TLI);
9697

9798
private:
98-
void initialize(Module &M);
99+
void initializeCallbacks(Module &M);
99100
bool instrumentLoadOrStore(Instruction *I, const DataLayout &DL);
100101
bool instrumentAtomic(Instruction *I, const DataLayout &DL);
101102
bool instrumentMemIntrinsic(Instruction *I);
@@ -107,6 +108,8 @@ struct ThreadSanitizer {
107108
void InsertRuntimeIgnores(Function &F);
108109

109110
Type *IntptrTy;
111+
IntegerType *OrdTy;
112+
// Callbacks to run-time library are computed in doInitialization.
110113
FunctionCallee TsanFuncEntry;
111114
FunctionCallee TsanFuncExit;
112115
FunctionCallee TsanIgnoreBegin;
@@ -127,6 +130,7 @@ struct ThreadSanitizer {
127130
FunctionCallee TsanVptrUpdate;
128131
FunctionCallee TsanVptrLoad;
129132
FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;
133+
Function *TsanCtorFunction;
130134
};
131135

132136
struct ThreadSanitizerLegacyPass : FunctionPass {
@@ -139,32 +143,16 @@ struct ThreadSanitizerLegacyPass : FunctionPass {
139143
private:
140144
Optional<ThreadSanitizer> TSan;
141145
};
142-
143-
void insertModuleCtor(Module &M) {
144-
getOrCreateSanitizerCtorAndInitFunctions(
145-
M, kTsanModuleCtorName, kTsanInitName, /*InitArgTypes=*/{},
146-
/*InitArgs=*/{},
147-
// This callback is invoked when the functions are created the first
148-
// time. Hook them into the global ctors list in that case:
149-
[&](Function *Ctor, FunctionCallee) { appendToGlobalCtors(M, Ctor, 0); });
150-
}
151-
152146
} // namespace
153147

154148
PreservedAnalyses ThreadSanitizerPass::run(Function &F,
155149
FunctionAnalysisManager &FAM) {
156-
ThreadSanitizer TSan;
150+
ThreadSanitizer TSan(*F.getParent());
157151
if (TSan.sanitizeFunction(F, FAM.getResult<TargetLibraryAnalysis>(F)))
158152
return PreservedAnalyses::none();
159153
return PreservedAnalyses::all();
160154
}
161155

162-
PreservedAnalyses ThreadSanitizerPass::run(Module &M,
163-
ModuleAnalysisManager &MAM) {
164-
insertModuleCtor(M);
165-
return PreservedAnalyses::none();
166-
}
167-
168156
char ThreadSanitizerLegacyPass::ID = 0;
169157
INITIALIZE_PASS_BEGIN(ThreadSanitizerLegacyPass, "tsan",
170158
"ThreadSanitizer: detects data races.", false, false)
@@ -181,8 +169,7 @@ void ThreadSanitizerLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
181169
}
182170

183171
bool ThreadSanitizerLegacyPass::doInitialization(Module &M) {
184-
insertModuleCtor(M);
185-
TSan.emplace();
172+
TSan.emplace(M);
186173
return true;
187174
}
188175

@@ -196,10 +183,7 @@ FunctionPass *llvm::createThreadSanitizerLegacyPassPass() {
196183
return new ThreadSanitizerLegacyPass();
197184
}
198185

199-
void ThreadSanitizer::initialize(Module &M) {
200-
const DataLayout &DL = M.getDataLayout();
201-
IntptrTy = DL.getIntPtrType(M.getContext());
202-
186+
void ThreadSanitizer::initializeCallbacks(Module &M) {
203187
IRBuilder<> IRB(M.getContext());
204188
AttributeList Attr;
205189
Attr = Attr.addAttribute(M.getContext(), AttributeList::FunctionIndex,
@@ -213,7 +197,7 @@ void ThreadSanitizer::initialize(Module &M) {
213197
IRB.getVoidTy());
214198
TsanIgnoreEnd =
215199
M.getOrInsertFunction("__tsan_ignore_thread_end", Attr, IRB.getVoidTy());
216-
IntegerType *OrdTy = IRB.getInt32Ty();
200+
OrdTy = IRB.getInt32Ty();
217201
for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
218202
const unsigned ByteSize = 1U << i;
219203
const unsigned BitSize = ByteSize * 8;
@@ -296,6 +280,20 @@ void ThreadSanitizer::initialize(Module &M) {
296280
IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy);
297281
}
298282

283+
ThreadSanitizer::ThreadSanitizer(Module &M) {
284+
const DataLayout &DL = M.getDataLayout();
285+
IntptrTy = DL.getIntPtrType(M.getContext());
286+
std::tie(TsanCtorFunction, std::ignore) =
287+
getOrCreateSanitizerCtorAndInitFunctions(
288+
M, kTsanModuleCtorName, kTsanInitName, /*InitArgTypes=*/{},
289+
/*InitArgs=*/{},
290+
// This callback is invoked when the functions are created the first
291+
// time. Hook them into the global ctors list in that case:
292+
[&](Function *Ctor, FunctionCallee) {
293+
appendToGlobalCtors(M, Ctor, 0);
294+
});
295+
}
296+
299297
static bool isVtableAccess(Instruction *I) {
300298
if (MDNode *Tag = I->getMetadata(LLVMContext::MD_tbaa))
301299
return Tag->isTBAAVtableAccess();
@@ -438,9 +436,9 @@ bool ThreadSanitizer::sanitizeFunction(Function &F,
438436
const TargetLibraryInfo &TLI) {
439437
// This is required to prevent instrumenting call to __tsan_init from within
440438
// the module constructor.
441-
if (F.getName() == kTsanModuleCtorName)
439+
if (&F == TsanCtorFunction)
442440
return false;
443-
initialize(*F.getParent());
441+
initializeCallbacks(*F.getParent());
444442
SmallVector<Instruction*, 8> AllLoadsAndStores;
445443
SmallVector<Instruction*, 8> LocalLoadsAndStores;
446444
SmallVector<Instruction*, 8> AtomicAccesses;

llvm/test/Instrumentation/MemorySanitizer/msan_basic.ll

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
; RUN: opt < %s -msan-check-access-address=0 -S -passes='module(msan-module),function(msan)' 2>&1 | FileCheck -allow-deprecated-dag-overlap %s
2-
; RUN: opt < %s --passes='module(msan-module),function(msan)' -msan-check-access-address=0 -S | FileCheck -allow-deprecated-dag-overlap %s
3-
; RUN: opt < %s -msan-check-access-address=0 -msan-track-origins=1 -S -passes='module(msan-module),function(msan)' 2>&1 | \
4-
; RUN: FileCheck -allow-deprecated-dag-overlap -check-prefixes=CHECK,CHECK-ORIGINS %s
5-
; RUN: opt < %s -passes='module(msan-module),function(msan)' -msan-check-access-address=0 -msan-track-origins=1 -S | \
6-
; RUN: FileCheck -allow-deprecated-dag-overlap -check-prefixes=CHECK,CHECK-ORIGINS %s
1+
; RUN: opt < %s -msan-check-access-address=0 -S -passes=msan 2>&1 | FileCheck \
2+
; RUN: -allow-deprecated-dag-overlap %s
3+
; RUN: opt < %s -msan -msan-check-access-address=0 -S | FileCheck -allow-deprecated-dag-overlap %s
4+
; RUN: opt < %s -msan-check-access-address=0 -msan-track-origins=1 -S \
5+
; RUN: -passes=msan 2>&1 | FileCheck -allow-deprecated-dag-overlap \
6+
; RUN: -check-prefix=CHECK -check-prefix=CHECK-ORIGINS %s
7+
; RUN: opt < %s -msan -msan-check-access-address=0 -msan-track-origins=1 -S | FileCheck -allow-deprecated-dag-overlap -check-prefix=CHECK -check-prefix=CHECK-ORIGINS %s
78

89
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
910
target triple = "x86_64-unknown-linux-gnu"

llvm/test/Instrumentation/ThreadSanitizer/tsan_basic.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; RUN: opt < %s -tsan -S | FileCheck %s
2-
; RUN: opt < %s -passes='function(tsan),module(tsan-module)' -S | FileCheck %s
2+
; RUN: opt < %s -passes=tsan -S | FileCheck %s
33

44
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
55
target triple = "x86_64-unknown-linux-gnu"

0 commit comments

Comments
 (0)