llvm.org GIT mirror llvm / 58a20d7
[asan] Fix coverage instrumentation with -asan-globals=0. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210103 91177308-0d34-0410-b5e6-96231b3b80d8 Evgeniy Stepanov 6 years ago
2 changed file(s) with 44 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
383383 private:
384384 void initializeCallbacks(Module &M);
385385
386 bool InstrumentGlobals(IRBuilder<> &IRB, Module &M);
386387 bool ShouldInstrumentGlobal(GlobalVariable *G);
387388 void poisonOneInitializer(Function &GlobalInit, GlobalValue *ModuleName);
388389 void createInitializerPoisonCalls(Module &M, GlobalValue *ModuleName);
982983 // This function replaces all global variables with new variables that have
983984 // trailing redzones. It also creates a function that poisons
984985 // redzones and inserts this function into llvm.global_ctors.
985 bool AddressSanitizerModule::runOnModule(Module &M) {
986 if (!ClGlobals) return false;
987
988 DataLayoutPass *DLP = getAnalysisIfAvailable();
989 if (!DLP)
990 return false;
991 DL = &DLP->getDataLayout();
992
993 BL.reset(SpecialCaseList::createOrDie(BlacklistFile));
994 if (BL->isIn(M)) return false;
995 C = &(M.getContext());
996 int LongSize = DL->getPointerSizeInBits();
997 IntptrTy = Type::getIntNTy(*C, LongSize);
998 Mapping = getShadowMapping(M, LongSize);
999 initializeCallbacks(M);
986 bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) {
1000987 DynamicallyInitializedGlobals.Init(M);
1001988
1002989 SmallVector GlobalsToChange;
1004991 for (auto &G : M.globals()) {
1005992 if (ShouldInstrumentGlobal(&G))
1006993 GlobalsToChange.push_back(&G);
1007 }
1008
1009 Function *CtorFunc = M.getFunction(kAsanModuleCtorName);
1010 assert(CtorFunc);
1011 IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator());
1012
1013 if (ClCoverage > 0) {
1014 Function *CovFunc = M.getFunction(kAsanCovName);
1015 int nCov = CovFunc ? CovFunc->getNumUses() : 0;
1016 IRB.CreateCall(AsanCovModuleInit, ConstantInt::get(IntptrTy, nCov));
1017994 }
1018995
1019996 size_t n = GlobalsToChange.size();
11311108
11321109 DEBUG(dbgs() << M);
11331110 return true;
1111 }
1112
1113 bool AddressSanitizerModule::runOnModule(Module &M) {
1114 DataLayoutPass *DLP = getAnalysisIfAvailable();
1115 if (!DLP)
1116 return false;
1117 DL = &DLP->getDataLayout();
1118 BL.reset(SpecialCaseList::createOrDie(BlacklistFile));
1119 C = &(M.getContext());
1120 int LongSize = DL->getPointerSizeInBits();
1121 IntptrTy = Type::getIntNTy(*C, LongSize);
1122 Mapping = getShadowMapping(M, LongSize);
1123 initializeCallbacks(M);
1124
1125 bool Changed = false;
1126
1127 Function *CtorFunc = M.getFunction(kAsanModuleCtorName);
1128 assert(CtorFunc);
1129 IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator());
1130
1131 if (ClCoverage > 0) {
1132 Function *CovFunc = M.getFunction(kAsanCovName);
1133 int nCov = CovFunc ? CovFunc->getNumUses() : 0;
1134 IRB.CreateCall(AsanCovModuleInit, ConstantInt::get(IntptrTy, nCov));
1135 Changed = true;
1136 }
1137
1138 if (ClGlobals && !BL->isIn(M)) Changed |= InstrumentGlobals(IRB, M);
1139
1140 return Changed;
11341141 }
11351142
11361143 void AddressSanitizer::initializeCallbacks(Module &M) {
22 ; RUN: opt < %s -asan -asan-module -asan-coverage=2 -S | FileCheck %s --check-prefix=CHECK2
33 ; RUN: opt < %s -asan -asan-module -asan-coverage=2 -asan-coverage-block-threshold=10 -S | FileCheck %s --check-prefix=CHECK2
44 ; RUN: opt < %s -asan -asan-module -asan-coverage=2 -asan-coverage-block-threshold=1 -S | FileCheck %s --check-prefix=CHECK1
5
6 ; RUN: opt < %s -asan -asan-module -asan-coverage=0 -asan-globals=0 -S | \
7 ; RUN: FileCheck %s --check-prefix=CHECK0
8 ; RUN: opt < %s -asan -asan-module -asan-coverage=1 -asan-globals=0 -S | \
9 ; RUN: FileCheck %s --check-prefix=CHECK1
10 ; RUN: opt < %s -asan -asan-module -asan-coverage=2 -asan-globals=0 -S | \
11 ; RUN: FileCheck %s --check-prefix=CHECK2
12 ; RUN: opt < %s -asan -asan-module -asan-coverage=2 -asan-coverage-block-threshold=10 \
13 ; RUN: -asan-globals=0 -S | FileCheck %s --check-prefix=CHECK2
14 ; RUN: opt < %s -asan -asan-module -asan-coverage=2 -asan-coverage-block-threshold=1 \
15 ; RUN: -asan-globals=0 -S | FileCheck %s --check-prefix=CHECK1
16
517 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
618 target triple = "x86_64-unknown-linux-gnu"
719 define void @foo(i32* %a) sanitize_address {