mirror of
https://github.com/RPCSX/llvm.git
synced 2025-02-03 19:15:30 +00:00
[SanitizerCoverage] Introduce SanitizerCoverageOptions struct.
Summary: This gives frontend more precise control over collected coverage information. User can still override these options by passing -mllvm flags. No functionality change. Test Plan: regression test suite. Reviewers: kcc Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D9539 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@236687 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
0518652fc4
commit
cddf82ae35
@ -98,8 +98,28 @@ ModulePass *createDataFlowSanitizerPass(
|
|||||||
const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
|
const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
|
||||||
void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
|
void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
|
||||||
|
|
||||||
|
// Options for sanitizer coverage instrumentation.
|
||||||
|
struct SanitizerCoverageOptions {
|
||||||
|
SanitizerCoverageOptions()
|
||||||
|
: CoverageType(SCK_None), IndirectCalls(false), TraceBB(false),
|
||||||
|
TraceCmp(false), Use8bitCounters(false) {}
|
||||||
|
|
||||||
|
enum Type {
|
||||||
|
SCK_None = 0,
|
||||||
|
SCK_Function,
|
||||||
|
SCK_BB,
|
||||||
|
SCK_Edge
|
||||||
|
} CoverageType;
|
||||||
|
bool IndirectCalls;
|
||||||
|
bool TraceBB;
|
||||||
|
bool TraceCmp;
|
||||||
|
bool Use8bitCounters;
|
||||||
|
};
|
||||||
|
|
||||||
// Insert SanitizerCoverage instrumentation.
|
// Insert SanitizerCoverage instrumentation.
|
||||||
ModulePass *createSanitizerCoverageModulePass(int CoverageLevel);
|
ModulePass *createSanitizerCoverageModulePass(int CoverageLevel);
|
||||||
|
ModulePass *createSanitizerCoverageModulePass(
|
||||||
|
const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());
|
||||||
|
|
||||||
#if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID)
|
#if defined(__GNUC__) && defined(__linux__) && !defined(ANDROID)
|
||||||
inline ModulePass *createDataFlowSanitizerPassForJIT(
|
inline ModulePass *createDataFlowSanitizerPassForJIT(
|
||||||
|
@ -11,19 +11,17 @@
|
|||||||
// and potentially with other Sanitizers.
|
// and potentially with other Sanitizers.
|
||||||
//
|
//
|
||||||
// We create a Guard variable with the same linkage
|
// We create a Guard variable with the same linkage
|
||||||
// as the function and inject this code into the entry block (CoverageLevel=1)
|
// as the function and inject this code into the entry block (SCK_Function)
|
||||||
// or all blocks (CoverageLevel>=2):
|
// or all blocks (SCK_BB):
|
||||||
// if (Guard < 0) {
|
// if (Guard < 0) {
|
||||||
// __sanitizer_cov(&Guard);
|
// __sanitizer_cov(&Guard);
|
||||||
// }
|
// }
|
||||||
// The accesses to Guard are atomic. The rest of the logic is
|
// The accesses to Guard are atomic. The rest of the logic is
|
||||||
// in __sanitizer_cov (it's fine to call it more than once).
|
// in __sanitizer_cov (it's fine to call it more than once).
|
||||||
//
|
//
|
||||||
// With CoverageLevel>=3 we also split critical edges this effectively
|
// With SCK_Edge we also split critical edges this effectively
|
||||||
// instrumenting all edges.
|
// instrumenting all edges.
|
||||||
//
|
//
|
||||||
// CoverageLevel>=4 add indirect call profiling implented as a function call.
|
|
||||||
//
|
|
||||||
// This coverage implementation provides very limited data:
|
// This coverage implementation provides very limited data:
|
||||||
// it only tells if a given function (block) was ever executed. No counters.
|
// it only tells if a given function (block) was ever executed. No counters.
|
||||||
// But for many use cases this is what we need and the added slowdown small.
|
// But for many use cases this is what we need and the added slowdown small.
|
||||||
@ -99,11 +97,45 @@ static cl::opt<bool> ClUse8bitCounters("sanitizer-coverage-8bit-counters",
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) {
|
||||||
|
SanitizerCoverageOptions Res;
|
||||||
|
switch (LegacyCoverageLevel) {
|
||||||
|
case 0:
|
||||||
|
Res.CoverageType = SanitizerCoverageOptions::SCK_None;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
Res.CoverageType = SanitizerCoverageOptions::SCK_Function;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
Res.CoverageType = SanitizerCoverageOptions::SCK_BB;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
|
||||||
|
Res.IndirectCalls = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return Res;
|
||||||
|
}
|
||||||
|
|
||||||
|
SanitizerCoverageOptions OverrideFromCL(SanitizerCoverageOptions Options) {
|
||||||
|
// Sets CoverageType and IndirectCalls.
|
||||||
|
SanitizerCoverageOptions CLOpts = getOptions(ClCoverageLevel);
|
||||||
|
Options.CoverageType = std::max(Options.CoverageType, CLOpts.CoverageType);
|
||||||
|
Options.IndirectCalls |= CLOpts.IndirectCalls;
|
||||||
|
Options.TraceBB |= ClExperimentalTracing;
|
||||||
|
Options.TraceCmp |= ClExperimentalCMPTracing;
|
||||||
|
Options.Use8bitCounters |= ClUse8bitCounters;
|
||||||
|
return Options;
|
||||||
|
}
|
||||||
|
|
||||||
class SanitizerCoverageModule : public ModulePass {
|
class SanitizerCoverageModule : public ModulePass {
|
||||||
public:
|
public:
|
||||||
SanitizerCoverageModule(int CoverageLevel = 0)
|
SanitizerCoverageModule(
|
||||||
: ModulePass(ID),
|
const SanitizerCoverageOptions &Options = SanitizerCoverageOptions())
|
||||||
CoverageLevel(std::max(CoverageLevel, (int)ClCoverageLevel)) {}
|
: ModulePass(ID), Options(OverrideFromCL(Options)) {}
|
||||||
bool runOnModule(Module &M) override;
|
bool runOnModule(Module &M) override;
|
||||||
bool runOnFunction(Function &F);
|
bool runOnFunction(Function &F);
|
||||||
static char ID; // Pass identification, replacement for typeid
|
static char ID; // Pass identification, replacement for typeid
|
||||||
@ -135,13 +167,14 @@ class SanitizerCoverageModule : public ModulePass {
|
|||||||
GlobalVariable *GuardArray;
|
GlobalVariable *GuardArray;
|
||||||
GlobalVariable *EightBitCounterArray;
|
GlobalVariable *EightBitCounterArray;
|
||||||
|
|
||||||
int CoverageLevel;
|
SanitizerCoverageOptions Options;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
bool SanitizerCoverageModule::runOnModule(Module &M) {
|
bool SanitizerCoverageModule::runOnModule(Module &M) {
|
||||||
if (!CoverageLevel) return false;
|
if (Options.CoverageType == SanitizerCoverageOptions::SCK_None)
|
||||||
|
return false;
|
||||||
C = &(M.getContext());
|
C = &(M.getContext());
|
||||||
DL = &M.getDataLayout();
|
DL = &M.getDataLayout();
|
||||||
IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
|
IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
|
||||||
@ -177,7 +210,7 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
|
|||||||
StringRef(""), StringRef(""),
|
StringRef(""), StringRef(""),
|
||||||
/*hasSideEffects=*/true);
|
/*hasSideEffects=*/true);
|
||||||
|
|
||||||
if (ClExperimentalTracing) {
|
if (Options.TraceBB) {
|
||||||
SanCovTraceEnter = checkSanitizerInterfaceFunction(
|
SanCovTraceEnter = checkSanitizerInterfaceFunction(
|
||||||
M.getOrInsertFunction(kSanCovTraceEnter, VoidTy, Int32PtrTy, nullptr));
|
M.getOrInsertFunction(kSanCovTraceEnter, VoidTy, Int32PtrTy, nullptr));
|
||||||
SanCovTraceBB = checkSanitizerInterfaceFunction(
|
SanCovTraceBB = checkSanitizerInterfaceFunction(
|
||||||
@ -192,7 +225,7 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
|
|||||||
GuardArray =
|
GuardArray =
|
||||||
new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage,
|
new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage,
|
||||||
nullptr, "__sancov_gen_cov_tmp");
|
nullptr, "__sancov_gen_cov_tmp");
|
||||||
if (ClUse8bitCounters)
|
if (Options.Use8bitCounters)
|
||||||
EightBitCounterArray =
|
EightBitCounterArray =
|
||||||
new GlobalVariable(M, Int8Ty, false, GlobalVariable::ExternalLinkage,
|
new GlobalVariable(M, Int8Ty, false, GlobalVariable::ExternalLinkage,
|
||||||
nullptr, "__sancov_gen_cov_tmp");
|
nullptr, "__sancov_gen_cov_tmp");
|
||||||
@ -216,7 +249,7 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
|
|||||||
GuardArray->eraseFromParent();
|
GuardArray->eraseFromParent();
|
||||||
|
|
||||||
GlobalVariable *RealEightBitCounterArray;
|
GlobalVariable *RealEightBitCounterArray;
|
||||||
if (ClUse8bitCounters) {
|
if (Options.Use8bitCounters) {
|
||||||
// Make sure the array is 16-aligned.
|
// Make sure the array is 16-aligned.
|
||||||
static const int kCounterAlignment = 16;
|
static const int kCounterAlignment = 16;
|
||||||
Type *Int8ArrayNTy =
|
Type *Int8ArrayNTy =
|
||||||
@ -242,7 +275,7 @@ bool SanitizerCoverageModule::runOnModule(Module &M) {
|
|||||||
IRB.CreateCall4(
|
IRB.CreateCall4(
|
||||||
SanCovModuleInit, IRB.CreatePointerCast(RealGuardArray, Int32PtrTy),
|
SanCovModuleInit, IRB.CreatePointerCast(RealGuardArray, Int32PtrTy),
|
||||||
ConstantInt::get(IntptrTy, N),
|
ConstantInt::get(IntptrTy, N),
|
||||||
ClUse8bitCounters
|
Options.Use8bitCounters
|
||||||
? IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy)
|
? IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy)
|
||||||
: Constant::getNullValue(Int8PtrTy),
|
: Constant::getNullValue(Int8PtrTy),
|
||||||
IRB.CreatePointerCast(ModuleName, Int8PtrTy));
|
IRB.CreatePointerCast(ModuleName, Int8PtrTy));
|
||||||
@ -253,7 +286,7 @@ bool SanitizerCoverageModule::runOnFunction(Function &F) {
|
|||||||
if (F.empty()) return false;
|
if (F.empty()) return false;
|
||||||
if (F.getName().find(".module_ctor") != std::string::npos)
|
if (F.getName().find(".module_ctor") != std::string::npos)
|
||||||
return false; // Should not instrument sanitizer init functions.
|
return false; // Should not instrument sanitizer init functions.
|
||||||
if (CoverageLevel >= 3)
|
if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge)
|
||||||
SplitAllCriticalEdges(F);
|
SplitAllCriticalEdges(F);
|
||||||
SmallVector<Instruction*, 8> IndirCalls;
|
SmallVector<Instruction*, 8> IndirCalls;
|
||||||
SmallVector<BasicBlock*, 16> AllBlocks;
|
SmallVector<BasicBlock*, 16> AllBlocks;
|
||||||
@ -261,14 +294,13 @@ bool SanitizerCoverageModule::runOnFunction(Function &F) {
|
|||||||
for (auto &BB : F) {
|
for (auto &BB : F) {
|
||||||
AllBlocks.push_back(&BB);
|
AllBlocks.push_back(&BB);
|
||||||
for (auto &Inst : BB) {
|
for (auto &Inst : BB) {
|
||||||
if (CoverageLevel >= 4) {
|
if (Options.IndirectCalls) {
|
||||||
CallSite CS(&Inst);
|
CallSite CS(&Inst);
|
||||||
if (CS && !CS.getCalledFunction())
|
if (CS && !CS.getCalledFunction())
|
||||||
IndirCalls.push_back(&Inst);
|
IndirCalls.push_back(&Inst);
|
||||||
}
|
}
|
||||||
if (ClExperimentalCMPTracing)
|
if (Options.TraceCmp && isa<ICmpInst>(&Inst))
|
||||||
if (isa<ICmpInst>(&Inst))
|
CmpTraceTargets.push_back(&Inst);
|
||||||
CmpTraceTargets.push_back(&Inst);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
InjectCoverage(F, AllBlocks);
|
InjectCoverage(F, AllBlocks);
|
||||||
@ -279,16 +311,19 @@ bool SanitizerCoverageModule::runOnFunction(Function &F) {
|
|||||||
|
|
||||||
bool SanitizerCoverageModule::InjectCoverage(Function &F,
|
bool SanitizerCoverageModule::InjectCoverage(Function &F,
|
||||||
ArrayRef<BasicBlock *> AllBlocks) {
|
ArrayRef<BasicBlock *> AllBlocks) {
|
||||||
if (!CoverageLevel) return false;
|
switch (Options.CoverageType) {
|
||||||
|
case SanitizerCoverageOptions::SCK_None:
|
||||||
if (CoverageLevel == 1) {
|
return false;
|
||||||
|
case SanitizerCoverageOptions::SCK_Function:
|
||||||
InjectCoverageAtBlock(F, F.getEntryBlock(), false);
|
InjectCoverageAtBlock(F, F.getEntryBlock(), false);
|
||||||
} else {
|
return true;
|
||||||
|
default: {
|
||||||
|
bool UseCalls = ClCoverageBlockThreshold < AllBlocks.size();
|
||||||
for (auto BB : AllBlocks)
|
for (auto BB : AllBlocks)
|
||||||
InjectCoverageAtBlock(F, *BB,
|
InjectCoverageAtBlock(F, *BB, UseCalls);
|
||||||
ClCoverageBlockThreshold < AllBlocks.size());
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// On every indirect call we call a run-time function
|
// On every indirect call we call a run-time function
|
||||||
@ -321,7 +356,6 @@ void SanitizerCoverageModule::InjectCoverageForIndirectCalls(
|
|||||||
|
|
||||||
void SanitizerCoverageModule::InjectTraceForCmp(
|
void SanitizerCoverageModule::InjectTraceForCmp(
|
||||||
Function &F, ArrayRef<Instruction *> CmpTraceTargets) {
|
Function &F, ArrayRef<Instruction *> CmpTraceTargets) {
|
||||||
if (!ClExperimentalCMPTracing) return;
|
|
||||||
for (auto I : CmpTraceTargets) {
|
for (auto I : CmpTraceTargets) {
|
||||||
if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
|
if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
|
||||||
IRBuilder<> IRB(ICMP);
|
IRBuilder<> IRB(ICMP);
|
||||||
@ -386,7 +420,7 @@ void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
|
|||||||
IRB.CreateCall(EmptyAsm); // Avoids callback merge.
|
IRB.CreateCall(EmptyAsm); // Avoids callback merge.
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ClUse8bitCounters) {
|
if (Options.Use8bitCounters) {
|
||||||
IRB.SetInsertPoint(IP);
|
IRB.SetInsertPoint(IP);
|
||||||
Value *P = IRB.CreateAdd(
|
Value *P = IRB.CreateAdd(
|
||||||
IRB.CreatePointerCast(EightBitCounterArray, IntptrTy),
|
IRB.CreatePointerCast(EightBitCounterArray, IntptrTy),
|
||||||
@ -399,7 +433,7 @@ void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
|
|||||||
SetNoSanitizeMetadata(SI);
|
SetNoSanitizeMetadata(SI);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ClExperimentalTracing) {
|
if (Options.TraceBB) {
|
||||||
// Experimental support for tracing.
|
// Experimental support for tracing.
|
||||||
// Insert a callback with the same guard variable as used for coverage.
|
// Insert a callback with the same guard variable as used for coverage.
|
||||||
IRB.SetInsertPoint(IP);
|
IRB.SetInsertPoint(IP);
|
||||||
@ -412,5 +446,9 @@ INITIALIZE_PASS(SanitizerCoverageModule, "sancov",
|
|||||||
"SanitizerCoverage: TODO."
|
"SanitizerCoverage: TODO."
|
||||||
"ModulePass", false, false)
|
"ModulePass", false, false)
|
||||||
ModulePass *llvm::createSanitizerCoverageModulePass(int CoverageLevel) {
|
ModulePass *llvm::createSanitizerCoverageModulePass(int CoverageLevel) {
|
||||||
return new SanitizerCoverageModule(CoverageLevel);
|
return createSanitizerCoverageModulePass(getOptions(CoverageLevel));
|
||||||
|
}
|
||||||
|
ModulePass *llvm::createSanitizerCoverageModulePass(
|
||||||
|
const SanitizerCoverageOptions &Options) {
|
||||||
|
return new SanitizerCoverageModule(Options);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user