diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1187,9 +1187,7 @@ MPM.addPass(RequireAnalysisPass()); MPM.addPass(ModuleAddressSanitizerPass( CompileKernel, Recover, ModuleUseAfterScope, UseOdrIndicator, - DestructorKind)); - MPM.addPass(createModuleToFunctionPassAdaptor(AddressSanitizerPass( - {CompileKernel, Recover, UseAfterScope, UseAfterReturn}))); + DestructorKind, UseAfterScope, UseAfterReturn)); } }; ASanPass(SanitizerKind::Address, false); diff --git a/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h b/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h --- a/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h +++ b/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h @@ -89,40 +89,6 @@ static AnalysisKey Key; }; -struct AddressSanitizerOptions { - AddressSanitizerOptions() - : AddressSanitizerOptions(false, false, false, - AsanDetectStackUseAfterReturnMode::Runtime){}; - AddressSanitizerOptions(bool CompileKernel, bool Recover, bool UseAfterScope, - AsanDetectStackUseAfterReturnMode UseAfterReturn) - : CompileKernel(CompileKernel), Recover(Recover), - UseAfterScope(UseAfterScope), UseAfterReturn(UseAfterReturn){}; - bool CompileKernel; - bool Recover; - bool UseAfterScope; - AsanDetectStackUseAfterReturnMode UseAfterReturn; -}; - -/// Public interface to the address sanitizer pass for instrumenting code to -/// check for various memory errors at runtime. -/// -/// The sanitizer itself is a function pass that works by inserting various -/// calls to the ASan runtime library functions. The runtime library essentially -/// replaces malloc() and free() with custom implementations that allow regions -/// surrounding requested memory to be checked for invalid accesses. -class AddressSanitizerPass : public PassInfoMixin { -public: - explicit AddressSanitizerPass(AddressSanitizerOptions Options) - : Options(Options){}; - PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); - void printPipeline(raw_ostream &OS, - function_ref MapClassName2PassName); - static bool isRequired() { return true; } - -private: - AddressSanitizerOptions Options; -}; - /// Public interface to the address sanitizer module pass for instrumenting code /// to check for various memory errors. /// @@ -134,7 +100,10 @@ explicit ModuleAddressSanitizerPass( bool CompileKernel = false, bool Recover = false, bool UseGlobalGC = true, bool UseOdrIndicator = false, - AsanDtorKind DestructorKind = AsanDtorKind::Global); + AsanDtorKind DestructorKind = AsanDtorKind::Global, + bool UseAfterScope = false, + AsanDetectStackUseAfterReturnMode UseAfterReturn = + AsanDetectStackUseAfterReturnMode::Runtime); PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); void printPipeline(raw_ostream &OS, function_ref MapClassName2PassName); @@ -146,6 +115,8 @@ bool UseGlobalGC; bool UseOdrIndicator; AsanDtorKind DestructorKind; + bool UseAfterScope; + AsanDetectStackUseAfterReturnMode UseAfterReturn; }; // Insert AddressSanitizer (address sanity checking) instrumentation diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -393,13 +393,6 @@ "no-profile-peeling;profile-peeling;" "no-runtime;runtime;" "no-upperbound;upperbound") -FUNCTION_PASS_WITH_PARAMS("asan", - "AddressSanitizerPass", - [](AddressSanitizerOptions Opts) { - return AddressSanitizerPass(Opts); - }, - parseASanPassOptions, - "kernel") FUNCTION_PASS_WITH_PARAMS("msan", "MemorySanitizerPass", [](MemorySanitizerOptions Opts) { diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp --- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -1254,35 +1254,6 @@ return GlobalsMetadata(M); } -PreservedAnalyses AddressSanitizerPass::run(Function &F, - AnalysisManager &AM) { - auto &MAMProxy = AM.getResult(F); - Module &M = *F.getParent(); - if (auto *R = MAMProxy.getCachedResult(M)) { - const TargetLibraryInfo *TLI = &AM.getResult(F); - AddressSanitizer Sanitizer(M, R, Options.CompileKernel, Options.Recover, - Options.UseAfterScope, Options.UseAfterReturn); - if (Sanitizer.instrumentFunction(F, TLI)) - return PreservedAnalyses::none(); - return PreservedAnalyses::all(); - } - - report_fatal_error( - "The ASanGlobalsMetadataAnalysis is required to run before " - "AddressSanitizer can run"); - return PreservedAnalyses::all(); -} - -void AddressSanitizerPass::printPipeline( - raw_ostream &OS, function_ref MapClassName2PassName) { - static_cast *>(this)->printPipeline( - OS, MapClassName2PassName); - OS << "<"; - if (Options.CompileKernel) - OS << "kernel"; - OS << ">"; -} - void ModuleAddressSanitizerPass::printPipeline( raw_ostream &OS, function_ref MapClassName2PassName) { static_cast *>(this)->printPipeline( @@ -1295,9 +1266,11 @@ ModuleAddressSanitizerPass::ModuleAddressSanitizerPass( bool CompileKernel, bool Recover, bool UseGlobalGC, bool UseOdrIndicator, - AsanDtorKind DestructorKind) + AsanDtorKind DestructorKind, bool UseAfterScope, + AsanDetectStackUseAfterReturnMode UseAfterReturn) : CompileKernel(CompileKernel), Recover(Recover), UseGlobalGC(UseGlobalGC), - UseOdrIndicator(UseOdrIndicator), DestructorKind(DestructorKind) {} + UseOdrIndicator(UseOdrIndicator), DestructorKind(DestructorKind), + UseAfterScope(UseAfterScope), UseAfterReturn(UseAfterReturn) {} PreservedAnalyses ModuleAddressSanitizerPass::run(Module &M, AnalysisManager &AM) { @@ -1305,7 +1278,14 @@ ModuleAddressSanitizer Sanitizer(M, &GlobalsMD, CompileKernel, Recover, UseGlobalGC, UseOdrIndicator, DestructorKind); - if (Sanitizer.instrumentModule(M)) + bool Modified = Sanitizer.instrumentModule(M); + const TargetLibraryInfo *TLI = &AM.getResult(M); + for (Function &F : M) { + AddressSanitizer Sanitizer(M, &GlobalsMD, CompileKernel, Recover, + UseAfterScope, UseAfterReturn); + Modified |= Sanitizer.instrumentFunction(F, TLI); + } + if (Modified) return PreservedAnalyses::none(); return PreservedAnalyses::all(); }