@@ -118,6 +118,11 @@ static cl::opt<bool>
118
118
cl::desc (" increments 8-bit counter for every edge" ),
119
119
cl::Hidden);
120
120
121
+ static cl::opt<bool >
122
+ ClSancovDropCtors (" sanitizer-coverage-drop-ctors" ,
123
+ cl::desc (" do not emit module ctors for global counters" ),
124
+ cl::Hidden);
125
+
121
126
static cl::opt<bool >
122
127
ClInlineBoolFlag (" sanitizer-coverage-inline-bool-flag" ,
123
128
cl::desc (" sets a boolean flag for every edge" ),
@@ -298,11 +303,11 @@ class ModuleSanitizerCoverage {
298
303
LLVMContext *C;
299
304
const DataLayout *DL;
300
305
301
- GlobalVariable *FunctionGuardArray; // for trace-pc-guard.
302
- GlobalVariable *Function8bitCounterArray; // for inline-8bit-counters.
303
- GlobalVariable *FunctionBoolArray; // for inline-bool-flag.
304
- GlobalVariable *FunctionPCsArray; // for pc-table.
305
- GlobalVariable *FunctionCFsArray; // for control flow table
306
+ GlobalVariable *FunctionGuardArray; // for trace-pc-guard.
307
+ GlobalVariable *Function8bitCounterArray; // for inline-8bit-counters.
308
+ GlobalVariable *FunctionBoolArray; // for inline-bool-flag.
309
+ GlobalVariable *FunctionPCsArray; // for pc-table.
310
+ GlobalVariable *FunctionCFsArray; // for control flow table
306
311
SmallVector<GlobalValue *, 20 > GlobalsToAppendToUsed;
307
312
SmallVector<GlobalValue *, 20 > GlobalsToAppendToCompilerUsed;
308
313
@@ -346,13 +351,11 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
346
351
GlobalValue::LinkageTypes Linkage = TargetTriple.isOSBinFormatCOFF ()
347
352
? GlobalVariable::ExternalLinkage
348
353
: GlobalVariable::ExternalWeakLinkage;
349
- GlobalVariable *SecStart =
350
- new GlobalVariable (M, Ty, false , Linkage, nullptr ,
351
- getSectionStart (Section));
354
+ GlobalVariable *SecStart = new GlobalVariable (M, Ty, false , Linkage, nullptr ,
355
+ getSectionStart (Section));
352
356
SecStart->setVisibility (GlobalValue::HiddenVisibility);
353
- GlobalVariable *SecEnd =
354
- new GlobalVariable (M, Ty, false , Linkage, nullptr ,
355
- getSectionEnd (Section));
357
+ GlobalVariable *SecEnd = new GlobalVariable (M, Ty, false , Linkage, nullptr ,
358
+ getSectionEnd (Section));
356
359
SecEnd->setVisibility (GlobalValue::HiddenVisibility);
357
360
IRBuilder<> IRB (M.getContext ());
358
361
if (!TargetTriple.isOSBinFormatCOFF ())
@@ -368,6 +371,8 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
368
371
Function *ModuleSanitizerCoverage::CreateInitCallsForSections (
369
372
Module &M, const char *CtorName, const char *InitFunctionName, Type *Ty,
370
373
const char *Section) {
374
+ if (ClSancovDropCtors)
375
+ return nullptr ;
371
376
auto SecStartEnd = CreateSecStartEnd (M, Section, Ty);
372
377
auto SecStart = SecStartEnd.first ;
373
378
auto SecEnd = SecStartEnd.second ;
@@ -457,25 +462,16 @@ bool ModuleSanitizerCoverage::instrumentModule() {
457
462
458
463
// Loads.
459
464
SanCovLoadFunction[0 ] = M.getOrInsertFunction (SanCovLoad1, VoidTy, PtrTy);
460
- SanCovLoadFunction[1 ] =
461
- M.getOrInsertFunction (SanCovLoad2, VoidTy, PtrTy);
462
- SanCovLoadFunction[2 ] =
463
- M.getOrInsertFunction (SanCovLoad4, VoidTy, PtrTy);
464
- SanCovLoadFunction[3 ] =
465
- M.getOrInsertFunction (SanCovLoad8, VoidTy, PtrTy);
466
- SanCovLoadFunction[4 ] =
467
- M.getOrInsertFunction (SanCovLoad16, VoidTy, PtrTy);
465
+ SanCovLoadFunction[1 ] = M.getOrInsertFunction (SanCovLoad2, VoidTy, PtrTy);
466
+ SanCovLoadFunction[2 ] = M.getOrInsertFunction (SanCovLoad4, VoidTy, PtrTy);
467
+ SanCovLoadFunction[3 ] = M.getOrInsertFunction (SanCovLoad8, VoidTy, PtrTy);
468
+ SanCovLoadFunction[4 ] = M.getOrInsertFunction (SanCovLoad16, VoidTy, PtrTy);
468
469
// Stores.
469
- SanCovStoreFunction[0 ] =
470
- M.getOrInsertFunction (SanCovStore1, VoidTy, PtrTy);
471
- SanCovStoreFunction[1 ] =
472
- M.getOrInsertFunction (SanCovStore2, VoidTy, PtrTy);
473
- SanCovStoreFunction[2 ] =
474
- M.getOrInsertFunction (SanCovStore4, VoidTy, PtrTy);
475
- SanCovStoreFunction[3 ] =
476
- M.getOrInsertFunction (SanCovStore8, VoidTy, PtrTy);
477
- SanCovStoreFunction[4 ] =
478
- M.getOrInsertFunction (SanCovStore16, VoidTy, PtrTy);
470
+ SanCovStoreFunction[0 ] = M.getOrInsertFunction (SanCovStore1, VoidTy, PtrTy);
471
+ SanCovStoreFunction[1 ] = M.getOrInsertFunction (SanCovStore2, VoidTy, PtrTy);
472
+ SanCovStoreFunction[2 ] = M.getOrInsertFunction (SanCovStore4, VoidTy, PtrTy);
473
+ SanCovStoreFunction[3 ] = M.getOrInsertFunction (SanCovStore8, VoidTy, PtrTy);
474
+ SanCovStoreFunction[4 ] = M.getOrInsertFunction (SanCovStore16, VoidTy, PtrTy);
479
475
480
476
{
481
477
AttributeList AL;
@@ -547,16 +543,16 @@ bool ModuleSanitizerCoverage::instrumentModule() {
547
543
}
548
544
if (Ctor && Options.PCTable ) {
549
545
auto SecStartEnd = CreateSecStartEnd (M, SanCovPCsSectionName, IntptrTy);
550
- FunctionCallee InitFunction = declareSanitizerInitFunction (
551
- M, SanCovPCsInitName, {PtrTy, PtrTy});
546
+ FunctionCallee InitFunction =
547
+ declareSanitizerInitFunction ( M, SanCovPCsInitName, {PtrTy, PtrTy});
552
548
IRBuilder<> IRBCtor (Ctor->getEntryBlock ().getTerminator ());
553
549
IRBCtor.CreateCall (InitFunction, {SecStartEnd.first , SecStartEnd.second });
554
550
}
555
551
556
552
if (Ctor && Options.CollectControlFlow ) {
557
553
auto SecStartEnd = CreateSecStartEnd (M, SanCovCFsSectionName, IntptrTy);
558
- FunctionCallee InitFunction = declareSanitizerInitFunction (
559
- M, SanCovCFsInitName, {PtrTy, PtrTy});
554
+ FunctionCallee InitFunction =
555
+ declareSanitizerInitFunction ( M, SanCovCFsInitName, {PtrTy, PtrTy});
560
556
IRBuilder<> IRBCtor (Ctor->getEntryBlock ().getTerminator ());
561
557
IRBCtor.CreateCall (InitFunction, {SecStartEnd.first , SecStartEnd.second });
562
558
}
@@ -613,8 +609,8 @@ static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB,
613
609
614
610
// Do not instrument full dominators, or full post-dominators with multiple
615
611
// predecessors.
616
- return !isFullDominator (BB, DT)
617
- && !(isFullPostDominator (BB, PDT) && !BB->getSinglePredecessor ());
612
+ return !isFullDominator (BB, DT) &&
613
+ !(isFullPostDominator (BB, PDT) && !BB->getSinglePredecessor ());
618
614
}
619
615
620
616
// Returns true iff From->To is a backedge.
@@ -789,16 +785,16 @@ ModuleSanitizerCoverage::CreatePCArray(Function &F,
789
785
for (size_t i = 0 ; i < N; i++) {
790
786
if (&F.getEntryBlock () == AllBlocks[i]) {
791
787
PCs.push_back ((Constant *)IRB.CreatePointerCast (&F, PtrTy));
792
- PCs.push_back ((Constant *)IRB. CreateIntToPtr (
793
- ConstantInt::get (IntptrTy, 1 ), PtrTy));
788
+ PCs.push_back (
789
+ (Constant *)IRB. CreateIntToPtr ( ConstantInt::get (IntptrTy, 1 ), PtrTy));
794
790
} else {
795
791
PCs.push_back ((Constant *)IRB.CreatePointerCast (
796
792
BlockAddress::get (AllBlocks[i]), PtrTy));
797
793
PCs.push_back (Constant::getNullValue (PtrTy));
798
794
}
799
795
}
800
- auto *PCArray = CreateFunctionLocalArrayInSection (N * 2 , F, PtrTy,
801
- SanCovPCsSectionName);
796
+ auto *PCArray =
797
+ CreateFunctionLocalArrayInSection (N * 2 , F, PtrTy, SanCovPCsSectionName);
802
798
PCArray->setInitializer (
803
799
ConstantArray::get (ArrayType::get (PtrTy, N * 2 ), PCs));
804
800
PCArray->setConstant (true );
@@ -853,7 +849,8 @@ bool ModuleSanitizerCoverage::InjectCoverage(Function &F,
853
849
ArrayRef<BasicBlock *> AllBlocks,
854
850
Value *&FunctionGateCmp,
855
851
bool IsLeafFunc) {
856
- if (AllBlocks.empty ()) return false ;
852
+ if (AllBlocks.empty ())
853
+ return false ;
857
854
CreateFunctionLocalArrays (F, AllBlocks);
858
855
for (size_t i = 0 , N = AllBlocks.size (); i < N; i++)
859
856
InjectCoverageAtBlock (F, *AllBlocks[i], i, FunctionGateCmp, IsLeafFunc);
@@ -936,13 +933,14 @@ void ModuleSanitizerCoverage::InjectTraceForDiv(
936
933
for (auto *BO : DivTraceTargets) {
937
934
InstrumentationIRBuilder IRB (BO);
938
935
Value *A1 = BO->getOperand (1 );
939
- if (isa<ConstantInt>(A1)) continue ;
936
+ if (isa<ConstantInt>(A1))
937
+ continue ;
940
938
if (!A1->getType ()->isIntegerTy ())
941
939
continue ;
942
940
uint64_t TypeSize = DL->getTypeStoreSizeInBits (A1->getType ());
943
- int CallbackIdx = TypeSize == 32 ? 0 :
944
- TypeSize == 64 ? 1 : - 1 ;
945
- if (CallbackIdx < 0 ) continue ;
941
+ int CallbackIdx = TypeSize == 32 ? 0 : TypeSize == 64 ? 1 : - 1 ;
942
+ if (CallbackIdx < 0 )
943
+ continue ;
946
944
auto Ty = Type::getIntNTy (*C, TypeSize);
947
945
IRB.CreateCall (SanCovTraceDivFunction[CallbackIdx],
948
946
{IRB.CreateIntCast (A1, Ty, true )});
@@ -1000,17 +998,20 @@ void ModuleSanitizerCoverage::InjectTraceForCmp(
1000
998
if (!A0->getType ()->isIntegerTy ())
1001
999
continue ;
1002
1000
uint64_t TypeSize = DL->getTypeStoreSizeInBits (A0->getType ());
1003
- int CallbackIdx = TypeSize == 8 ? 0 :
1004
- TypeSize == 16 ? 1 :
1005
- TypeSize == 32 ? 2 :
1006
- TypeSize == 64 ? 3 : -1 ;
1007
- if (CallbackIdx < 0 ) continue ;
1001
+ int CallbackIdx = TypeSize == 8 ? 0
1002
+ : TypeSize == 16 ? 1
1003
+ : TypeSize == 32 ? 2
1004
+ : TypeSize == 64 ? 3
1005
+ : -1 ;
1006
+ if (CallbackIdx < 0 )
1007
+ continue ;
1008
1008
// __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1);
1009
1009
auto CallbackFunc = SanCovTraceCmpFunction[CallbackIdx];
1010
1010
bool FirstIsConst = isa<ConstantInt>(A0);
1011
1011
bool SecondIsConst = isa<ConstantInt>(A1);
1012
1012
// If both are const, then we don't need such a comparison.
1013
- if (FirstIsConst && SecondIsConst) continue ;
1013
+ if (FirstIsConst && SecondIsConst)
1014
+ continue ;
1014
1015
// If only one is const, then make it the first callback argument.
1015
1016
if (FirstIsConst || SecondIsConst) {
1016
1017
CallbackFunc = SanCovTraceConstCmpFunction[CallbackIdx];
@@ -1192,13 +1193,13 @@ void ModuleSanitizerCoverage::createFunctionControlFlow(Function &F) {
1192
1193
if (&BB == &F.getEntryBlock ())
1193
1194
CFs.push_back ((Constant *)IRB.CreatePointerCast (&F, PtrTy));
1194
1195
else
1195
- CFs.push_back ((Constant *)IRB. CreatePointerCast ( BlockAddress::get (&BB),
1196
- PtrTy));
1196
+ CFs.push_back (
1197
+ (Constant *)IRB. CreatePointerCast ( BlockAddress::get (&BB), PtrTy));
1197
1198
1198
1199
for (auto SuccBB : successors (&BB)) {
1199
1200
assert (SuccBB != &F.getEntryBlock ());
1200
- CFs.push_back ((Constant *)IRB. CreatePointerCast ( BlockAddress::get (SuccBB),
1201
- PtrTy));
1201
+ CFs.push_back (
1202
+ (Constant *)IRB. CreatePointerCast ( BlockAddress::get (SuccBB), PtrTy));
1202
1203
}
1203
1204
1204
1205
CFs.push_back ((Constant *)Constant::getNullValue (PtrTy));
@@ -1212,17 +1213,16 @@ void ModuleSanitizerCoverage::createFunctionControlFlow(Function &F) {
1212
1213
} else {
1213
1214
auto CalledF = CB->getCalledFunction ();
1214
1215
if (CalledF && !CalledF->isIntrinsic ())
1215
- CFs.push_back (
1216
- (Constant *)IRB.CreatePointerCast (CalledF, PtrTy));
1216
+ CFs.push_back ((Constant *)IRB.CreatePointerCast (CalledF, PtrTy));
1217
1217
}
1218
1218
}
1219
1219
}
1220
1220
1221
1221
CFs.push_back ((Constant *)Constant::getNullValue (PtrTy));
1222
1222
}
1223
1223
1224
- FunctionCFsArray = CreateFunctionLocalArrayInSection (
1225
- CFs. size (), F, PtrTy, SanCovCFsSectionName);
1224
+ FunctionCFsArray = CreateFunctionLocalArrayInSection (CFs. size (), F, PtrTy,
1225
+ SanCovCFsSectionName);
1226
1226
FunctionCFsArray->setInitializer (
1227
1227
ConstantArray::get (ArrayType::get (PtrTy, CFs.size ()), CFs));
1228
1228
FunctionCFsArray->setConstant (true );
0 commit comments