Skip to content

Commit

Permalink
[ASan] Process functions in Asan module pass
Browse files Browse the repository at this point in the history
This came up as recommendation while reviewing D112098.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D112732
  • Loading branch information
kstoimenov committed Nov 3, 2021
1 parent 0a3d755 commit a55c4ec
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 0 additions & 2 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,8 +1188,6 @@ static void addSanitizers(const Triple &TargetTriple,
MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
MPM.addPass(ModuleAddressSanitizerPass(
Opts, UseGlobalGC, UseOdrIndicator, DestructorKind));
MPM.addPass(
createModuleToFunctionPassAdaptor(AddressSanitizerPass(Opts)));
}
};
ASanPass(SanitizerKind::Address, false);
Expand Down
26 changes: 18 additions & 8 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1300,14 +1300,22 @@ ModuleAddressSanitizerPass::ModuleAddressSanitizerPass(
UseOdrIndicator(UseOdrIndicator), DestructorKind(DestructorKind) {}

PreservedAnalyses ModuleAddressSanitizerPass::run(Module &M,
AnalysisManager<Module> &AM) {
GlobalsMetadata &GlobalsMD = AM.getResult<ASanGlobalsMetadataAnalysis>(M);
ModuleAddressSanitizer Sanitizer(M, &GlobalsMD, Options.CompileKernel,
Options.Recover, UseGlobalGC,
UseOdrIndicator, DestructorKind);
if (Sanitizer.instrumentModule(M))
return PreservedAnalyses::none();
return PreservedAnalyses::all();
ModuleAnalysisManager &MAM) {
GlobalsMetadata &GlobalsMD = MAM.getResult<ASanGlobalsMetadataAnalysis>(M);
ModuleAddressSanitizer ModuleSanitizer(M, &GlobalsMD, Options.CompileKernel,
Options.Recover, UseGlobalGC,
UseOdrIndicator, DestructorKind);
bool Modified = false;
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
for (Function &F : M) {
AddressSanitizer FunctionSanitizer(M, &GlobalsMD, Options.CompileKernel,
Options.Recover, Options.UseAfterScope,
Options.UseAfterReturn);
const TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
Modified |= FunctionSanitizer.instrumentFunction(F, &TLI);
}
Modified |= ModuleSanitizer.instrumentModule(M);
return Modified ? PreservedAnalyses::none() : PreservedAnalyses::all();
}

INITIALIZE_PASS(ASanGlobalsMetadataWrapperPass, "asan-globals-md",
Expand Down Expand Up @@ -2841,6 +2849,8 @@ bool AddressSanitizer::suppressInstrumentationSiteForDebug(int &Instrumented) {

bool AddressSanitizer::instrumentFunction(Function &F,
const TargetLibraryInfo *TLI) {
if (F.empty())
return false;
if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false;
if (!ClDebugFunc.empty() && ClDebugFunc == F.getName()) return false;
if (F.getName().startswith("__asan_")) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
; Make sure asan does not instrument __sancov_gen_

; RUN: opt < %s -passes='module(sancov-module)' -sanitizer-coverage-level=3 -sanitizer-coverage-trace-pc-guard -S | FileCheck %s
; RUN: opt < %s -passes='module(require<asan-globals-md>,sancov-module,asan-module),function(asan)' -sanitizer-coverage-level=3 -sanitizer-coverage-trace-pc-guard -S | FileCheck %s
; RUN: opt < %s -passes='module(require<asan-globals-md>,sancov-module,asan-module)' -sanitizer-coverage-level=3 -sanitizer-coverage-trace-pc-guard -S | FileCheck %s
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
$Foo = comdat any
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/opt/NewPMDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,6 @@ bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
if (Name == "asan-pipeline") {
MPM.addPass(
RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
MPM.addPass(
createModuleToFunctionPassAdaptor(AddressSanitizerPass(Opts)));
MPM.addPass(ModuleAddressSanitizerPass(Opts));
return true;
} else if (Name == "asan-function-pipeline") {
Expand Down

0 comments on commit a55c4ec

Please sign in to comment.