Skip to content

Commit b80b89c

Browse files
author
kcc
committed
[libFuzzer] in autofocus mode, give more weight to functions with DFT
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk/lib/fuzzer@363473 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 1ee7f41 commit b80b89c

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

FuzzerDataFlowTrace.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ bool BlockCoverage::AppendCoverage(const std::string &S) {
4242
bool BlockCoverage::AppendCoverage(std::istream &IN) {
4343
std::string L;
4444
while (std::getline(IN, L, '\n')) {
45-
if (L.empty() || L[0] != 'C')
46-
continue; // Ignore non-coverage lines.
45+
if (L.empty())
46+
continue;
4747
std::stringstream SS(L.c_str() + 1);
4848
size_t FunctionId = 0;
4949
SS >> FunctionId;
50+
if (L[0] == 'F') {
51+
FunctionsWithDFT.insert(FunctionId);
52+
continue;
53+
}
54+
if (L[0] != 'C') continue;
5055
Vector<uint32_t> CoveredBlocks;
5156
while (true) {
5257
uint32_t BB = 0;
@@ -87,9 +92,12 @@ Vector<double> BlockCoverage::FunctionWeights(size_t NumFunctions) const {
8792
auto Counters = It.second;
8893
assert(FunctionID < NumFunctions);
8994
auto &Weight = Res[FunctionID];
90-
Weight = 1000.; // this function is covered.
95+
// Give higher weight if the function has a DFT.
96+
Weight = FunctionsWithDFT.count(FunctionID) ? 1000. : 1;
97+
// Give higher weight to functions with less frequently seen basic blocks.
9198
Weight /= SmallestNonZeroCounter(Counters);
92-
Weight *= NumberOfUncoveredBlocks(Counters) + 1; // make sure it's not 0.
99+
// Give higher weight to functions with the most uncovered basic blocks.
100+
Weight *= NumberOfUncoveredBlocks(Counters) + 1;
93101
}
94102
return Res;
95103
}

FuzzerDataFlowTrace.h

+2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ class BlockCoverage {
107107
// Function ID => vector of counters.
108108
// Each counter represents how many input files trigger the given basic block.
109109
std::unordered_map<size_t, CoverageVector> Functions;
110+
// Functions that have DFT entry.
111+
std::unordered_set<size_t> FunctionsWithDFT;
110112
};
111113

112114
class DataFlowTrace {

tests/FuzzerUnittest.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -840,11 +840,17 @@ TEST(DFT, FunctionWeights) {
840840
Weights = Cov.FunctionWeights(2);
841841
EXPECT_GT(Weights[0], Weights[1]);
842842

843-
// A function with more uncovered bclocks gets more weight.
843+
// A function with more uncovered blocks gets more weight.
844844
Cov.clear();
845845
EXPECT_TRUE(Cov.AppendCoverage("C0 1 2 3 5\nC1 2 4\n"));
846846
Weights = Cov.FunctionWeights(2);
847847
EXPECT_GT(Weights[1], Weights[0]);
848+
849+
// A function with DFT gets more weight than the function w/o DFT.
850+
Cov.clear();
851+
EXPECT_TRUE(Cov.AppendCoverage("F1 111\nC0 3\nC1 1 2 3\n"));
852+
Weights = Cov.FunctionWeights(2);
853+
EXPECT_GT(Weights[1], Weights[0]);
848854
}
849855

850856

0 commit comments

Comments
 (0)