Skip to content

Commit 9e7b730

Browse files
pzreadmorehouse
authored andcommitted
[libFuzzer] Update InputInfo.TimeOfUnit when replacing it in the corpus.
Previously, when the fuzzing loop replaced an input in the corpus, it didn't update the execution time of the input. Therefore, some schedulers (e.g. Entropic) would adjust weights based on the incorrect execution time. This patch updates the execution time of the input when replacing it. Reviewed By: morehouse Differential Revision: https://reviews.llvm.org/D111479
1 parent cbe3b6b commit 9e7b730

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

compiler-rt/lib/fuzzer/FuzzerCorpus.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,16 @@ class InputCorpus {
284284
}
285285
}
286286

287-
void Replace(InputInfo *II, const Unit &U) {
287+
void Replace(InputInfo *II, const Unit &U,
288+
std::chrono::microseconds TimeOfUnit) {
288289
assert(II->U.size() > U.size());
289290
Hashes.erase(Sha1ToString(II->Sha1));
290291
DeleteFile(*II);
291292
ComputeSHA1(U.data(), U.size(), II->Sha1);
292293
Hashes.insert(Sha1ToString(II->Sha1));
293294
II->U = U;
294295
II->Reduced = true;
296+
II->TimeOfUnit = TimeOfUnit;
295297
DistributionNeedsUpdate = true;
296298
}
297299

compiler-rt/lib/fuzzer/FuzzerLoop.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ bool Fuzzer::RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile,
548548
FoundUniqFeaturesOfII == II->UniqFeatureSet.size() &&
549549
II->U.size() > Size) {
550550
auto OldFeaturesFile = Sha1ToString(II->Sha1);
551-
Corpus.Replace(II, {Data, Data + Size});
551+
Corpus.Replace(II, {Data, Data + Size}, TimeOfUnit);
552552
RenameFeatureSetFile(Options.FeaturesDir, OldFeaturesFile,
553553
Sha1ToString(II->Sha1));
554554
return true;

compiler-rt/lib/fuzzer/tests/FuzzerUnittest.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,39 @@ TEST(Corpus, Distribution) {
652652
}
653653
}
654654

655+
TEST(Corpus, Replace) {
656+
DataFlowTrace DFT;
657+
struct EntropicOptions Entropic = {false, 0xFF, 100, false};
658+
std::unique_ptr<InputCorpus> C(
659+
new InputCorpus(/*OutputCorpus*/ "", Entropic));
660+
InputInfo *FirstII =
661+
C->AddToCorpus(Unit{0x01, 0x00}, /*NumFeatures*/ 1,
662+
/*MayDeleteFile*/ false, /*HasFocusFunction*/ false,
663+
/*ForceAddToCorpus*/ false,
664+
/*TimeOfUnit*/ std::chrono::microseconds(1234),
665+
/*FeatureSet*/ {}, DFT,
666+
/*BaseII*/ nullptr);
667+
InputInfo *SecondII =
668+
C->AddToCorpus(Unit{0x02}, /*NumFeatures*/ 1,
669+
/*MayDeleteFile*/ false, /*HasFocusFunction*/ false,
670+
/*ForceAddToCorpus*/ false,
671+
/*TimeOfUnit*/ std::chrono::microseconds(5678),
672+
/*FeatureSet*/ {}, DFT,
673+
/*BaseII*/ nullptr);
674+
Unit ReplacedU = Unit{0x03};
675+
676+
C->Replace(FirstII, ReplacedU,
677+
/*TimeOfUnit*/ std::chrono::microseconds(321));
678+
679+
EXPECT_EQ(FirstII->U, Unit{0x03});
680+
EXPECT_EQ(FirstII->Reduced, true);
681+
EXPECT_EQ(FirstII->TimeOfUnit, std::chrono::microseconds(321));
682+
std::vector<uint8_t> ExpectedSha1(kSHA1NumBytes);
683+
ComputeSHA1(ReplacedU.data(), ReplacedU.size(), ExpectedSha1.data());
684+
std::vector<uint8_t> IISha1(FirstII->Sha1, FirstII->Sha1 + kSHA1NumBytes);
685+
EXPECT_EQ(IISha1, ExpectedSha1);
686+
}
687+
655688
template <typename T>
656689
void EQ(const std::vector<T> &A, const std::vector<T> &B) {
657690
EXPECT_EQ(A, B);

0 commit comments

Comments
 (0)