Skip to content

Commit

Permalink
[libFuzzer] Use std::discrete_distribution for input selection.
Browse files Browse the repository at this point in the history
Summary:
Since we're casting from double to size_t during input selection, we
really want a discrete distribution over size_t rather than a piecewise
distribution over doubles.

Reviewers: kcc

Reviewed By: kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D50356

llvm-svn: 339973
  • Loading branch information
morehouse committed Aug 17, 2018
1 parent a93e726 commit 0094d31
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions compiler-rt/lib/fuzzer/FuzzerCorpus.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class InputCorpus {

// Returns an index of random unit from the corpus to mutate.
size_t ChooseUnitIdxToMutate(Random &Rand) {
size_t Idx = static_cast<size_t>(CorpusDistribution(Rand));
size_t Idx = CorpusDistribution(Rand);
assert(Idx < Inputs.size());
return Idx;
}
Expand Down Expand Up @@ -276,9 +276,7 @@ class InputCorpus {
void UpdateCorpusDistribution() {
size_t N = Inputs.size();
assert(N);
Intervals.resize(N + 1);
Weights.resize(N);
std::iota(Intervals.begin(), Intervals.end(), 0);
for (size_t i = 0; i < N; i++)
Weights[i] = Inputs[i]->NumFeatures
? (i + 1) * (Inputs[i]->HasFocusFunction ? 1000 : 1)
Expand All @@ -291,12 +289,11 @@ class InputCorpus {
Printf("%f ", Weights[i]);
Printf("Weights\n");
}
CorpusDistribution = std::piecewise_constant_distribution<double>(
Intervals.begin(), Intervals.end(), Weights.begin());
CorpusDistribution =
std::discrete_distribution<size_t>(Weights.begin(), Weights.end());
}
std::piecewise_constant_distribution<double> CorpusDistribution;
std::discrete_distribution<size_t> CorpusDistribution;

Vector<double> Intervals;
Vector<double> Weights;

std::unordered_set<std::string> Hashes;
Expand Down

0 comments on commit 0094d31

Please sign in to comment.