From ab41eef9aca3ad5e9321e045a57d36e288a75d17 Mon Sep 17 00:00:00 2001 From: Bjorn Pettersson Date: Wed, 15 Sep 2021 21:23:51 +0200 Subject: [PATCH] [NewPM] Use a separate struct for ModuleMemorySanitizerPass Split MemorySanitizerPass into MemorySanitizerPass (as a function pass) and ModuleMemorySanitizerPass (as a module pass). Main reason is to make sure that we have a unique mapping from ClassName to PassName in the new passmanager framework, making it possible to correctly identify the passes when dealing with options such as -print-after and -print-pipeline-passes. This is a follow-up to D105006 and D105007. --- clang/lib/CodeGen/BackendUtil.cpp | 2 +- .../llvm/Transforms/Instrumentation/MemorySanitizer.h | 17 ++++++++++++++++- llvm/lib/Passes/PassRegistry.def | 3 +-- llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp | 4 ++-- llvm/test/Other/new-pm-print-pipeline.ll | 5 ++--- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 9916207..3867ecf 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1119,7 +1119,7 @@ static void addSanitizers(const Triple &TargetTriple, bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); MPM.addPass( - MemorySanitizerPass({TrackOrigins, Recover, CompileKernel})); + ModuleMemorySanitizerPass({TrackOrigins, Recover, CompileKernel})); FunctionPassManager FPM; FPM.addPass( MemorySanitizerPass({TrackOrigins, Recover, CompileKernel})); diff --git a/llvm/include/llvm/Transforms/Instrumentation/MemorySanitizer.h b/llvm/include/llvm/Transforms/Instrumentation/MemorySanitizer.h index e2acd67..d47beb9 100644 --- a/llvm/include/llvm/Transforms/Instrumentation/MemorySanitizer.h +++ b/llvm/include/llvm/Transforms/Instrumentation/MemorySanitizer.h @@ -40,7 +40,6 @@ struct MemorySanitizerPass : public PassInfoMixin { MemorySanitizerPass(MemorySanitizerOptions Options) : Options(Options) {} PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM); - PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); void printPipeline(raw_ostream &OS, function_ref MapClassName2PassName); static bool isRequired() { return true; } @@ -48,6 +47,22 @@ struct MemorySanitizerPass : public PassInfoMixin { private: MemorySanitizerOptions Options; }; + +/// A module pass for msan instrumentation. +/// +/// Instruments functions to detect unitialized reads. This function pass +/// inserts calls to runtime library functions. If the functions aren't declared +/// yet, the pass inserts the declarations. Otherwise the existing globals are +/// used. +struct ModuleMemorySanitizerPass : public PassInfoMixin { + ModuleMemorySanitizerPass(MemorySanitizerOptions Options) : Options(Options) {} + + PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); + static bool isRequired() { return true; } + +private: + MemorySanitizerOptions Options; +}; } #endif /* LLVM_TRANSFORMS_INSTRUMENTATION_MEMORYSANITIZER_H */ diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def index e9a0ea8..6f107c3 100644 --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -110,7 +110,7 @@ MODULE_PASS("verify", VerifierPass()) MODULE_PASS("wholeprogramdevirt", WholeProgramDevirtPass()) MODULE_PASS("dfsan", DataFlowSanitizerPass()) MODULE_PASS("asan-module", ModuleAddressSanitizerPass(/*CompileKernel=*/false, false, true, false)) -MODULE_PASS("msan-module", MemorySanitizerPass({})) +MODULE_PASS("msan-module", ModuleMemorySanitizerPass({})) MODULE_PASS("tsan-module", ThreadSanitizerPass()) MODULE_PASS("kasan-module", ModuleAddressSanitizerPass(/*CompileKernel=*/true, false, true, false)) MODULE_PASS("sancov-module", ModuleSanitizerCoveragePass()) @@ -335,7 +335,6 @@ FUNCTION_PASS("verify", ScalarEvolutionVerifierPass()) FUNCTION_PASS("view-cfg", CFGViewerPass()) FUNCTION_PASS("view-cfg-only", CFGOnlyViewerPass()) FUNCTION_PASS("transform-warning", WarnMissedTransformationsPass()) -FUNCTION_PASS("msan", MemorySanitizerPass({})) FUNCTION_PASS("tsan", ThreadSanitizerPass()) FUNCTION_PASS("memprof", MemProfilerPass()) #undef FUNCTION_PASS diff --git a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp index d190005..fad2b27 100644 --- a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp @@ -673,8 +673,8 @@ PreservedAnalyses MemorySanitizerPass::run(Function &F, return PreservedAnalyses::all(); } -PreservedAnalyses MemorySanitizerPass::run(Module &M, - ModuleAnalysisManager &AM) { +PreservedAnalyses +ModuleMemorySanitizerPass::run(Module &M, ModuleAnalysisManager &AM) { if (Options.Kernel) return PreservedAnalyses::all(); insertModuleCtor(M); diff --git a/llvm/test/Other/new-pm-print-pipeline.ll b/llvm/test/Other/new-pm-print-pipeline.ll index 823348b..b0d2b14 100644 --- a/llvm/test/Other/new-pm-print-pipeline.ll +++ b/llvm/test/Other/new-pm-print-pipeline.ll @@ -40,9 +40,8 @@ ; RUN: opt -disable-output -disable-verify -print-pipeline-passes -passes='function(early-cse<>,early-cse)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-12 ; CHECK-12: function(early-cse<>,early-cse) -; RUN: opt -disable-output -disable-verify -print-pipeline-passes -passes='function(msan<>,msan)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-13 -;;; XXX: msan-module? this is one of the places where the ClassName to pass-name mapping fails. -; CHECK-13: function(msan-module,msan-module) +; RUN: opt -disable-output -disable-verify -print-pipeline-passes -passes='msan-module,function(msan,msan<>,msan)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-13 +; CHECK-13: msan-module,function(msan,msan,msan) ; RUN: opt -disable-output -disable-verify -print-pipeline-passes -passes='module(hwasan<>,hwasan)' < %s | FileCheck %s --match-full-lines --check-prefixes=CHECK-14 ; CHECK-14: hwasan<>,hwasan -- 2.7.4