[libFuzzer] in autofocus mode, give more weight to functions with DFT

llvm-svn: 363473
GitOrigin-RevId: 0feed5d585f75cc6ae5fa5802c096cbbf3cf164c
diff --git a/FuzzerDataFlowTrace.cpp b/FuzzerDataFlowTrace.cpp
index c7200a3..99ff918 100644
--- a/FuzzerDataFlowTrace.cpp
+++ b/FuzzerDataFlowTrace.cpp
@@ -42,11 +42,16 @@
 bool BlockCoverage::AppendCoverage(std::istream &IN) {
   std::string L;
   while (std::getline(IN, L, '\n')) {
-    if (L.empty() || L[0] != 'C')
-      continue; // Ignore non-coverage lines.
+    if (L.empty())
+      continue;
     std::stringstream SS(L.c_str() + 1);
     size_t FunctionId  = 0;
     SS >> FunctionId;
+    if (L[0] == 'F') {
+      FunctionsWithDFT.insert(FunctionId);
+      continue;
+    }
+    if (L[0] != 'C') continue;
     Vector<uint32_t> CoveredBlocks;
     while (true) {
       uint32_t BB = 0;
@@ -87,9 +92,12 @@
     auto Counters = It.second;
     assert(FunctionID < NumFunctions);
     auto &Weight = Res[FunctionID];
-    Weight = 1000.;  // this function is covered.
+    // Give higher weight if the function has a DFT.
+    Weight = FunctionsWithDFT.count(FunctionID) ? 1000. : 1;
+    // Give higher weight to functions with less frequently seen basic blocks.
     Weight /= SmallestNonZeroCounter(Counters);
-    Weight *= NumberOfUncoveredBlocks(Counters) + 1;  // make sure it's not 0.
+    // Give higher weight to functions with the most uncovered basic blocks.
+    Weight *= NumberOfUncoveredBlocks(Counters) + 1;
   }
   return Res;
 }
diff --git a/FuzzerDataFlowTrace.h b/FuzzerDataFlowTrace.h
index 022e854..d6e3de3 100644
--- a/FuzzerDataFlowTrace.h
+++ b/FuzzerDataFlowTrace.h
@@ -107,6 +107,8 @@
   // Function ID => vector of counters.
   // Each counter represents how many input files trigger the given basic block.
   std::unordered_map<size_t, CoverageVector> Functions;
+  // Functions that have DFT entry.
+  std::unordered_set<size_t> FunctionsWithDFT;
 };
 
 class DataFlowTrace {
diff --git a/tests/FuzzerUnittest.cpp b/tests/FuzzerUnittest.cpp
index 93f70b5..abb203d 100644
--- a/tests/FuzzerUnittest.cpp
+++ b/tests/FuzzerUnittest.cpp
@@ -840,11 +840,17 @@
   Weights = Cov.FunctionWeights(2);
   EXPECT_GT(Weights[0], Weights[1]);
 
-  // A function with more uncovered bclocks gets more weight.
+  // A function with more uncovered blocks gets more weight.
   Cov.clear();
   EXPECT_TRUE(Cov.AppendCoverage("C0 1 2 3 5\nC1 2 4\n"));
   Weights = Cov.FunctionWeights(2);
   EXPECT_GT(Weights[1], Weights[0]);
+
+  // A function with DFT gets more weight than the function w/o DFT.
+  Cov.clear();
+  EXPECT_TRUE(Cov.AppendCoverage("F1 111\nC0 3\nC1 1 2 3\n"));
+  Weights = Cov.FunctionWeights(2);
+  EXPECT_GT(Weights[1], Weights[0]);
 }