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 @@ -1058,6 +1058,70 @@ } } +static void addSanitizers(const Triple &TargetTriple, + const CodeGenOptions &CodeGenOpts, + const LangOptions &LangOpts, PassBuilder &PB) { + PB.registerOptimizerLastEPCallback([&](ModulePassManager &MPM, + PassBuilder::OptimizationLevel Level) { + if (CodeGenOpts.SanitizeCoverageType || + CodeGenOpts.SanitizeCoverageIndirectCalls || + CodeGenOpts.SanitizeCoverageTraceCmp) { + auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts); + MPM.addPass(ModuleSanitizerCoveragePass( + SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles, + CodeGenOpts.SanitizeCoverageBlocklistFiles)); + } + + auto MSanPass = [&](SanitizerMask Mask, bool CompileKernel) { + if (LangOpts.Sanitize.has(Mask)) { + int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins; + bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); + + MPM.addPass( + MemorySanitizerPass({TrackOrigins, Recover, CompileKernel})); + MPM.addPass(createModuleToFunctionPassAdaptor( + MemorySanitizerPass({TrackOrigins, Recover, CompileKernel}))); + } + }; + MSanPass(SanitizerKind::Memory, false); + MSanPass(SanitizerKind::KernelMemory, true); + + if (LangOpts.Sanitize.has(SanitizerKind::Thread)) { + MPM.addPass(ThreadSanitizerPass()); + MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass())); + } + + auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) { + if (LangOpts.Sanitize.has(Mask)) { + bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); + bool UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope; + bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts); + bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator; + MPM.addPass(RequireAnalysisPass()); + MPM.addPass(ModuleAddressSanitizerPass( + CompileKernel, Recover, ModuleUseAfterScope, UseOdrIndicator)); + MPM.addPass(createModuleToFunctionPassAdaptor( + AddressSanitizerPass(CompileKernel, Recover, UseAfterScope))); + } + }; + ASanPass(SanitizerKind::Address, false); + ASanPass(SanitizerKind::KernelAddress, true); + + auto HWASanPass = [&](SanitizerMask Mask, bool CompileKernel) { + if (LangOpts.Sanitize.has(Mask)) { + bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); + MPM.addPass(HWAddressSanitizerPass(CompileKernel, Recover)); + } + }; + HWASanPass(SanitizerKind::HWAddress, false); + HWASanPass(SanitizerKind::KernelHWAddress, true); + + if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) { + MPM.addPass(DataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles)); + } + }); +} + /// A clean version of `EmitAssembly` that uses the new pass manager. /// /// Not all features are currently supported in this system, but where @@ -1249,87 +1313,7 @@ FPM.addPass(BoundsCheckingPass()); }); - if (CodeGenOpts.SanitizeCoverageType || - CodeGenOpts.SanitizeCoverageIndirectCalls || - CodeGenOpts.SanitizeCoverageTraceCmp) { - PB.registerOptimizerLastEPCallback( - [this](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) { - auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts); - MPM.addPass(ModuleSanitizerCoveragePass( - SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles, - CodeGenOpts.SanitizeCoverageBlocklistFiles)); - }); - } - - auto MSanPass = [&](SanitizerMask Mask, bool CompileKernel) { - if (LangOpts.Sanitize.has(Mask)) { - int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins; - bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); - PB.registerOptimizerLastEPCallback( - [CompileKernel, TrackOrigins, Recover]( - ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) { - MPM.addPass( - MemorySanitizerPass({TrackOrigins, Recover, CompileKernel})); - MPM.addPass(createModuleToFunctionPassAdaptor( - MemorySanitizerPass({TrackOrigins, Recover, CompileKernel}))); - }); - } - }; - MSanPass(SanitizerKind::Memory, false); - MSanPass(SanitizerKind::KernelMemory, true); - - if (LangOpts.Sanitize.has(SanitizerKind::Thread)) { - PB.registerOptimizerLastEPCallback( - [](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) { - MPM.addPass(ThreadSanitizerPass()); - MPM.addPass( - createModuleToFunctionPassAdaptor(ThreadSanitizerPass())); - }); - } - - auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) { - if (LangOpts.Sanitize.has(Mask)) { - bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); - bool UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope; - bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts); - bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator; - PB.registerOptimizerLastEPCallback( - [CompileKernel, Recover, UseAfterScope, ModuleUseAfterScope, - UseOdrIndicator](ModulePassManager &MPM, - PassBuilder::OptimizationLevel Level) { - MPM.addPass( - RequireAnalysisPass()); - MPM.addPass(ModuleAddressSanitizerPass(CompileKernel, Recover, - ModuleUseAfterScope, - UseOdrIndicator)); - MPM.addPass(createModuleToFunctionPassAdaptor( - AddressSanitizerPass(CompileKernel, Recover, UseAfterScope))); - }); - } - }; - ASanPass(SanitizerKind::Address, false); - ASanPass(SanitizerKind::KernelAddress, true); - - auto HWASanPass = [&](SanitizerMask Mask, bool CompileKernel) { - if (LangOpts.Sanitize.has(Mask)) { - bool Recover = CodeGenOpts.SanitizeRecover.has(Mask); - PB.registerOptimizerLastEPCallback( - [CompileKernel, Recover](ModulePassManager &MPM, - PassBuilder::OptimizationLevel Level) { - MPM.addPass(HWAddressSanitizerPass(CompileKernel, Recover)); - }); - } - }; - HWASanPass(SanitizerKind::HWAddress, false); - HWASanPass(SanitizerKind::KernelHWAddress, true); - - if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) { - PB.registerOptimizerLastEPCallback( - [this](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) { - MPM.addPass( - DataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles)); - }); - } + addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB); if (Optional Options = getGCOVOptions(CodeGenOpts, LangOpts)) PB.registerPipelineStartEPCallback(