Skip to content

Commit a55c4ec

Browse files
committed
[ASan] Process functions in Asan module pass
This came up as recommendation while reviewing D112098. Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D112732
1 parent 0a3d755 commit a55c4ec

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,8 +1188,6 @@ static void addSanitizers(const Triple &TargetTriple,
11881188
MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
11891189
MPM.addPass(ModuleAddressSanitizerPass(
11901190
Opts, UseGlobalGC, UseOdrIndicator, DestructorKind));
1191-
MPM.addPass(
1192-
createModuleToFunctionPassAdaptor(AddressSanitizerPass(Opts)));
11931191
}
11941192
};
11951193
ASanPass(SanitizerKind::Address, false);

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,14 +1300,22 @@ ModuleAddressSanitizerPass::ModuleAddressSanitizerPass(
13001300
UseOdrIndicator(UseOdrIndicator), DestructorKind(DestructorKind) {}
13011301

13021302
PreservedAnalyses ModuleAddressSanitizerPass::run(Module &M,
1303-
AnalysisManager<Module> &AM) {
1304-
GlobalsMetadata &GlobalsMD = AM.getResult<ASanGlobalsMetadataAnalysis>(M);
1305-
ModuleAddressSanitizer Sanitizer(M, &GlobalsMD, Options.CompileKernel,
1306-
Options.Recover, UseGlobalGC,
1307-
UseOdrIndicator, DestructorKind);
1308-
if (Sanitizer.instrumentModule(M))
1309-
return PreservedAnalyses::none();
1310-
return PreservedAnalyses::all();
1303+
ModuleAnalysisManager &MAM) {
1304+
GlobalsMetadata &GlobalsMD = MAM.getResult<ASanGlobalsMetadataAnalysis>(M);
1305+
ModuleAddressSanitizer ModuleSanitizer(M, &GlobalsMD, Options.CompileKernel,
1306+
Options.Recover, UseGlobalGC,
1307+
UseOdrIndicator, DestructorKind);
1308+
bool Modified = false;
1309+
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
1310+
for (Function &F : M) {
1311+
AddressSanitizer FunctionSanitizer(M, &GlobalsMD, Options.CompileKernel,
1312+
Options.Recover, Options.UseAfterScope,
1313+
Options.UseAfterReturn);
1314+
const TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
1315+
Modified |= FunctionSanitizer.instrumentFunction(F, &TLI);
1316+
}
1317+
Modified |= ModuleSanitizer.instrumentModule(M);
1318+
return Modified ? PreservedAnalyses::none() : PreservedAnalyses::all();
13111319
}
13121320

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

28422850
bool AddressSanitizer::instrumentFunction(Function &F,
28432851
const TargetLibraryInfo *TLI) {
2852+
if (F.empty())
2853+
return false;
28442854
if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false;
28452855
if (!ClDebugFunc.empty() && ClDebugFunc == F.getName()) return false;
28462856
if (F.getName().startswith("__asan_")) return false;

llvm/test/Instrumentation/SanitizerCoverage/tracing-comdat.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; Make sure asan does not instrument __sancov_gen_
33

44
; RUN: opt < %s -passes='module(sancov-module)' -sanitizer-coverage-level=3 -sanitizer-coverage-trace-pc-guard -S | FileCheck %s
5-
; 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
5+
; 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
66
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
77
target triple = "x86_64-unknown-linux-gnu"
88
$Foo = comdat any

llvm/tools/opt/NewPMDriver.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,6 @@ bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
344344
if (Name == "asan-pipeline") {
345345
MPM.addPass(
346346
RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
347-
MPM.addPass(
348-
createModuleToFunctionPassAdaptor(AddressSanitizerPass(Opts)));
349347
MPM.addPass(ModuleAddressSanitizerPass(Opts));
350348
return true;
351349
} else if (Name == "asan-function-pipeline") {

0 commit comments

Comments
 (0)