Skip to content

Commit b077d3f

Browse files
committedAug 30, 2016
[libfuzzer] simplified unit truncation; do not write trunc items to disc
Differential Revision: https://reviews.llvm.org/D24049 llvm-svn: 280153
1 parent 1c85295 commit b077d3f

File tree

2 files changed

+14
-34
lines changed

2 files changed

+14
-34
lines changed
 

Diff for: ‎llvm/lib/Fuzzer/FuzzerLoop.cpp

+11-29
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
namespace fuzzer {
3535
static const size_t kMaxUnitSizeToPrint = 256;
36-
static const size_t TruncateMaxRuns = 1000;
3736

3837
thread_local bool Fuzzer::IsMyThread;
3938

@@ -371,39 +370,22 @@ void Fuzzer::ShuffleCorpus(UnitVector *V) {
371370
}
372371

373372
// Tries random prefixes of corpus items.
374-
// Prefix length is chosen according to exponential distribution
375-
// to sample short lengths much more heavily.
376373
void Fuzzer::TruncateUnits(std::vector<Unit> *NewCorpus) {
377-
size_t MaxCorpusLen = 0;
378-
for (const auto &U : Corpus)
379-
MaxCorpusLen = std::max(MaxCorpusLen, U.size());
380-
381-
if (MaxCorpusLen <= 1)
382-
return;
374+
std::vector<double> Fractions = {0.25, 0.5, 0.75, 1.0};
383375

384-
// 50% of exponential distribution is Log[2]/lambda.
385-
// Choose lambda so that median is MaxCorpusLen / 2.
386-
double Lambda = 2.0 * log(2.0) / static_cast<double>(MaxCorpusLen);
387-
std::exponential_distribution<> Dist(Lambda);
388-
std::vector<double> Sizes;
389-
size_t TruncatePoints = std::max(1ul, TruncateMaxRuns / Corpus.size());
390-
Sizes.reserve(TruncatePoints);
391-
for (size_t I = 0; I < TruncatePoints; ++I) {
392-
Sizes.push_back(Dist(MD.GetRand().Get_mt19937()) + 1);
393-
}
394-
std::sort(Sizes.begin(), Sizes.end());
395-
396-
for (size_t S : Sizes) {
376+
size_t TruncInputs = 0;
377+
for (double Fraction : Fractions) {
397378
for (const auto &U : Corpus) {
398-
if (S < U.size() && RunOne(U.data(), S)) {
399-
Unit U1(U.begin(), U.begin() + S);
400-
NewCorpus->push_back(U1);
401-
WriteToOutputCorpus(U1);
402-
PrintStatusForNewUnit(U1);
403-
}
379+
uint64_t S = MD.GetRand()(U.size() * Fraction);
380+
if (!S || !RunOne(U.data(), S))
381+
continue;
382+
TruncInputs++;
383+
Unit U1(U.begin(), U.begin() + S);
384+
NewCorpus->push_back(U1);
404385
}
405386
}
406-
PrintStats("TRUNC ");
387+
if (TruncInputs)
388+
Printf("\tINFO TRUNC %zd units added to in-memory corpus\n", TruncInputs);
407389
}
408390

409391
void Fuzzer::ShuffleAndMinimize() {

Diff for: ‎llvm/lib/Fuzzer/test/fuzzer-trunc.test

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
RUN: rm -rf FuzzerTruncateTestCORPUS
33
RUN: mkdir FuzzerTruncateTestCORPUS
44
RUN: echo "01234567890123456789012345678901234567890" > FuzzerTruncateTestCORPUS/unit1
5-
# Simply running a fuzzer won't produce new results
6-
RUN: LLVMFuzzer-EmptyTest -seed=1 -runs=100 -truncate_units=0 ./FuzzerTruncateTestCORPUS
7-
# Truncating would create a new unit of length 1.
8-
RUN: LLVMFuzzer-EmptyTest -seed=1 -runs=0 -truncate_units=1 ./FuzzerTruncateTestCORPUS
9-
RUN: find FuzzerTruncateTestCORPUS/b6589fc6ab0dc82cf12099d1c2d40ab994e8410c
5+
RUN: LLVMFuzzer-EmptyTest -seed=1 -runs=0 -truncate_units=1 ./FuzzerTruncateTestCORPUS 2>&1 | FileCheck %s
106
RUN: rm -rf FuzzerTruncateTestCORPUS
7+
8+
CHECK: INFO TRUNC 1 units added

0 commit comments

Comments
 (0)
Please sign in to comment.