2016-09-21 03:50:50 +02:00
|
|
|
//===- FuzzerCorpus.h - Internal header for the Fuzzer ----------*- C++ -* ===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// fuzzer::InputCorpus
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_FUZZER_CORPUS
|
|
|
|
#define LLVM_FUZZER_CORPUS
|
|
|
|
|
|
|
|
#include "FuzzerDefs.h"
|
2016-11-30 20:06:14 +01:00
|
|
|
#include "FuzzerIO.h"
|
2016-09-21 23:41:48 +02:00
|
|
|
#include "FuzzerRandom.h"
|
2016-11-30 20:06:14 +01:00
|
|
|
#include "FuzzerSHA1.h"
|
2016-09-30 03:19:56 +02:00
|
|
|
#include "FuzzerTracePC.h"
|
2016-12-30 01:15:40 +01:00
|
|
|
#include <algorithm>
|
2016-12-13 18:46:11 +01:00
|
|
|
#include <numeric>
|
|
|
|
#include <random>
|
|
|
|
#include <unordered_set>
|
2016-09-21 03:50:50 +02:00
|
|
|
|
|
|
|
namespace fuzzer {
|
|
|
|
|
|
|
|
struct InputInfo {
|
|
|
|
Unit U; // The actual input data.
|
2016-09-21 23:41:48 +02:00
|
|
|
uint8_t Sha1[kSHA1NumBytes]; // Checksum.
|
2016-09-30 03:19:56 +02:00
|
|
|
// Number of features that this input has and no smaller input has.
|
|
|
|
size_t NumFeatures = 0;
|
|
|
|
size_t Tmp = 0; // Used by ValidateFeatureSet.
|
2016-09-22 00:42:17 +02:00
|
|
|
// Stats.
|
2016-09-30 03:19:56 +02:00
|
|
|
size_t NumExecutedMutations = 0;
|
|
|
|
size_t NumSuccessfullMutations = 0;
|
2016-10-09 01:24:45 +02:00
|
|
|
bool MayDeleteFile = false;
|
2017-07-18 20:47:36 +02:00
|
|
|
bool Reduced = false;
|
2017-07-18 03:36:50 +02:00
|
|
|
std::vector<uint32_t> UniqFeatureSet;
|
2016-09-21 03:50:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class InputCorpus {
|
2017-03-24 01:45:15 +01:00
|
|
|
static const size_t kFeatureSetSize = 1 << 21;
|
2016-09-21 03:50:50 +02:00
|
|
|
public:
|
2016-10-09 01:24:45 +02:00
|
|
|
InputCorpus(const std::string &OutputCorpus) : OutputCorpus(OutputCorpus) {
|
2016-10-06 00:56:21 +02:00
|
|
|
memset(InputSizesPerFeature, 0, sizeof(InputSizesPerFeature));
|
|
|
|
memset(SmallestElementPerFeature, 0, sizeof(SmallestElementPerFeature));
|
2016-09-21 03:50:50 +02:00
|
|
|
}
|
2016-10-08 23:57:48 +02:00
|
|
|
~InputCorpus() {
|
|
|
|
for (auto II : Inputs)
|
|
|
|
delete II;
|
|
|
|
}
|
2016-09-21 23:41:48 +02:00
|
|
|
size_t size() const { return Inputs.size(); }
|
2016-10-05 02:25:17 +02:00
|
|
|
size_t SizeInBytes() const {
|
|
|
|
size_t Res = 0;
|
2016-10-08 23:57:48 +02:00
|
|
|
for (auto II : Inputs)
|
|
|
|
Res += II->U.size();
|
2016-10-05 02:25:17 +02:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
size_t NumActiveUnits() const {
|
|
|
|
size_t Res = 0;
|
2016-10-08 23:57:48 +02:00
|
|
|
for (auto II : Inputs)
|
|
|
|
Res += !II->U.empty();
|
2016-10-05 02:25:17 +02:00
|
|
|
return Res;
|
|
|
|
}
|
2016-12-28 00:24:55 +01:00
|
|
|
size_t MaxInputSize() const {
|
|
|
|
size_t Res = 0;
|
|
|
|
for (auto II : Inputs)
|
|
|
|
Res = std::max(Res, II->U.size());
|
|
|
|
return Res;
|
|
|
|
}
|
2016-09-21 23:41:48 +02:00
|
|
|
bool empty() const { return Inputs.empty(); }
|
2016-10-08 23:57:48 +02:00
|
|
|
const Unit &operator[] (size_t Idx) const { return Inputs[Idx]->U; }
|
2017-07-13 03:08:53 +02:00
|
|
|
void AddToCorpus(const Unit &U, size_t NumFeatures, bool MayDeleteFile,
|
|
|
|
const std::vector<uint32_t> &FeatureSet) {
|
2016-10-05 02:25:17 +02:00
|
|
|
assert(!U.empty());
|
2016-10-06 00:56:21 +02:00
|
|
|
if (FeatureDebug)
|
|
|
|
Printf("ADD_TO_CORPUS %zd NF %zd\n", Inputs.size(), NumFeatures);
|
2016-10-08 23:57:48 +02:00
|
|
|
Inputs.push_back(new InputInfo());
|
|
|
|
InputInfo &II = *Inputs.back();
|
2016-09-21 03:50:50 +02:00
|
|
|
II.U = U;
|
2016-10-06 00:56:21 +02:00
|
|
|
II.NumFeatures = NumFeatures;
|
2016-10-09 01:24:45 +02:00
|
|
|
II.MayDeleteFile = MayDeleteFile;
|
2017-07-18 03:36:50 +02:00
|
|
|
II.UniqFeatureSet = FeatureSet;
|
|
|
|
std::sort(II.UniqFeatureSet.begin(), II.UniqFeatureSet.end());
|
2017-07-13 03:08:53 +02:00
|
|
|
ComputeSHA1(U.data(), U.size(), II.Sha1);
|
|
|
|
Hashes.insert(Sha1ToString(II.Sha1));
|
2016-09-21 23:41:48 +02:00
|
|
|
UpdateCorpusDistribution();
|
2017-07-13 03:08:53 +02:00
|
|
|
PrintCorpus();
|
2017-03-24 01:45:15 +01:00
|
|
|
// ValidateFeatureSet();
|
2016-09-21 03:50:50 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 03:08:53 +02:00
|
|
|
// Debug-only
|
|
|
|
void PrintUnit(const Unit &U) {
|
|
|
|
if (!FeatureDebug) return;
|
|
|
|
for (uint8_t C : U) {
|
|
|
|
if (C != 'F' && C != 'U' && C != 'Z')
|
|
|
|
C = '.';
|
|
|
|
Printf("%c", C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug-only
|
|
|
|
void PrintFeatureSet(const std::vector<uint32_t> &FeatureSet) {
|
|
|
|
if (!FeatureDebug) return;
|
|
|
|
Printf("{");
|
|
|
|
for (uint32_t Feature: FeatureSet)
|
|
|
|
Printf("%u,", Feature);
|
|
|
|
Printf("}");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug-only
|
|
|
|
void PrintCorpus() {
|
|
|
|
if (!FeatureDebug) return;
|
|
|
|
Printf("======= CORPUS:\n");
|
|
|
|
int i = 0;
|
|
|
|
for (auto II : Inputs) {
|
|
|
|
if (std::find(II->U.begin(), II->U.end(), 'F') != II->U.end()) {
|
|
|
|
Printf("[%2d] ", i);
|
|
|
|
Printf("%s sz=%zd ", Sha1ToString(II->Sha1).c_str(), II->U.size());
|
|
|
|
PrintUnit(II->U);
|
|
|
|
Printf(" ");
|
2017-07-18 03:36:50 +02:00
|
|
|
PrintFeatureSet(II->UniqFeatureSet);
|
2017-07-13 03:08:53 +02:00
|
|
|
Printf("\n");
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Replace(InputInfo *II, const Unit &U) {
|
2017-07-18 20:47:36 +02:00
|
|
|
assert(II->U.size() > U.size());
|
2017-07-13 03:56:37 +02:00
|
|
|
Hashes.erase(Sha1ToString(II->Sha1));
|
|
|
|
DeleteFile(*II);
|
2017-07-13 03:08:53 +02:00
|
|
|
ComputeSHA1(U.data(), U.size(), II->Sha1);
|
|
|
|
Hashes.insert(Sha1ToString(II->Sha1));
|
|
|
|
II->U = U;
|
2017-07-18 20:47:36 +02:00
|
|
|
II->Reduced = true;
|
2017-07-13 03:08:53 +02:00
|
|
|
}
|
|
|
|
|
2016-09-21 03:50:50 +02:00
|
|
|
bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); }
|
2016-10-01 03:04:29 +02:00
|
|
|
bool HasUnit(const std::string &H) { return Hashes.count(H); }
|
2016-09-22 00:42:17 +02:00
|
|
|
InputInfo &ChooseUnitToMutate(Random &Rand) {
|
2016-10-08 23:57:48 +02:00
|
|
|
InputInfo &II = *Inputs[ChooseUnitIdxToMutate(Rand)];
|
2016-10-05 02:25:17 +02:00
|
|
|
assert(!II.U.empty());
|
|
|
|
return II;
|
2016-09-21 23:41:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Returns an index of random unit from the corpus to mutate.
|
|
|
|
// Hypothesis: units added to the corpus last are more likely to be
|
|
|
|
// interesting. This function gives more weight to the more recent units.
|
|
|
|
size_t ChooseUnitIdxToMutate(Random &Rand) {
|
2017-02-07 23:37:34 +01:00
|
|
|
size_t Idx = static_cast<size_t>(CorpusDistribution(Rand));
|
2016-09-21 23:41:48 +02:00
|
|
|
assert(Idx < Inputs.size());
|
|
|
|
return Idx;
|
|
|
|
}
|
|
|
|
|
2016-09-22 00:42:17 +02:00
|
|
|
void PrintStats() {
|
|
|
|
for (size_t i = 0; i < Inputs.size(); i++) {
|
2016-10-08 23:57:48 +02:00
|
|
|
const auto &II = *Inputs[i];
|
2016-09-23 03:58:51 +02:00
|
|
|
Printf(" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\n", i,
|
2016-09-22 00:42:17 +02:00
|
|
|
Sha1ToString(II.Sha1).c_str(), II.U.size(),
|
2016-09-23 03:58:51 +02:00
|
|
|
II.NumExecutedMutations, II.NumSuccessfullMutations);
|
2016-09-22 00:42:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 03:19:56 +02:00
|
|
|
void PrintFeatureSet() {
|
|
|
|
for (size_t i = 0; i < kFeatureSetSize; i++) {
|
2016-10-06 00:56:21 +02:00
|
|
|
if(size_t Sz = GetFeature(i))
|
|
|
|
Printf("[%zd: id %zd sz%zd] ", i, SmallestElementPerFeature[i], Sz);
|
2016-09-30 03:19:56 +02:00
|
|
|
}
|
|
|
|
Printf("\n\t");
|
|
|
|
for (size_t i = 0; i < Inputs.size(); i++)
|
2016-10-08 23:57:48 +02:00
|
|
|
if (size_t N = Inputs[i]->NumFeatures)
|
2016-09-30 03:19:56 +02:00
|
|
|
Printf(" %zd=>%zd ", i, N);
|
|
|
|
Printf("\n");
|
|
|
|
}
|
|
|
|
|
2017-07-13 03:56:37 +02:00
|
|
|
void DeleteFile(const InputInfo &II) {
|
2016-10-09 01:24:45 +02:00
|
|
|
if (!OutputCorpus.empty() && II.MayDeleteFile)
|
2016-12-13 18:46:40 +01:00
|
|
|
RemoveFile(DirPlusFile(OutputCorpus, Sha1ToString(II.Sha1)));
|
2017-07-13 03:56:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteInput(size_t Idx) {
|
|
|
|
InputInfo &II = *Inputs[Idx];
|
|
|
|
DeleteFile(II);
|
2016-10-09 01:24:45 +02:00
|
|
|
Unit().swap(II.U);
|
|
|
|
if (FeatureDebug)
|
|
|
|
Printf("EVICTED %zd\n", Idx);
|
|
|
|
}
|
|
|
|
|
2017-07-18 03:36:50 +02:00
|
|
|
bool AddFeature(size_t Idx, uint32_t NewSize, bool Shrink) {
|
2016-10-06 00:56:21 +02:00
|
|
|
assert(NewSize);
|
|
|
|
Idx = Idx % kFeatureSetSize;
|
|
|
|
uint32_t OldSize = GetFeature(Idx);
|
|
|
|
if (OldSize == 0 || (Shrink && OldSize > NewSize)) {
|
|
|
|
if (OldSize > 0) {
|
2016-10-09 01:24:45 +02:00
|
|
|
size_t OldIdx = SmallestElementPerFeature[Idx];
|
|
|
|
InputInfo &II = *Inputs[OldIdx];
|
2016-10-06 00:56:21 +02:00
|
|
|
assert(II.NumFeatures > 0);
|
|
|
|
II.NumFeatures--;
|
2016-10-09 01:24:45 +02:00
|
|
|
if (II.NumFeatures == 0)
|
|
|
|
DeleteInput(OldIdx);
|
2017-03-24 01:45:15 +01:00
|
|
|
} else {
|
|
|
|
NumAddedFeatures++;
|
2016-10-06 00:56:21 +02:00
|
|
|
}
|
2017-03-31 06:17:45 +02:00
|
|
|
NumUpdatedFeatures++;
|
2016-10-06 00:56:21 +02:00
|
|
|
if (FeatureDebug)
|
|
|
|
Printf("ADD FEATURE %zd sz %d\n", Idx, NewSize);
|
|
|
|
SmallestElementPerFeature[Idx] = Inputs.size();
|
|
|
|
InputSizesPerFeature[Idx] = NewSize;
|
2017-07-18 03:36:50 +02:00
|
|
|
return true;
|
2016-10-06 00:56:21 +02:00
|
|
|
}
|
2017-07-18 03:36:50 +02:00
|
|
|
return false;
|
2016-10-06 00:56:21 +02:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:45:15 +01:00
|
|
|
size_t NumFeatures() const { return NumAddedFeatures; }
|
2017-03-31 06:17:45 +02:00
|
|
|
size_t NumFeatureUpdates() const { return NumUpdatedFeatures; }
|
2016-10-06 00:56:21 +02:00
|
|
|
|
2016-10-14 22:20:33 +02:00
|
|
|
void ResetFeatureSet() {
|
|
|
|
assert(Inputs.empty());
|
|
|
|
memset(InputSizesPerFeature, 0, sizeof(InputSizesPerFeature));
|
|
|
|
memset(SmallestElementPerFeature, 0, sizeof(SmallestElementPerFeature));
|
|
|
|
}
|
|
|
|
|
2016-09-21 23:41:48 +02:00
|
|
|
private:
|
|
|
|
|
2016-09-30 03:19:56 +02:00
|
|
|
static const bool FeatureDebug = false;
|
2016-10-06 00:56:21 +02:00
|
|
|
|
|
|
|
size_t GetFeature(size_t Idx) const { return InputSizesPerFeature[Idx]; }
|
2016-09-30 03:19:56 +02:00
|
|
|
|
|
|
|
void ValidateFeatureSet() {
|
2016-10-06 00:56:21 +02:00
|
|
|
if (FeatureDebug)
|
|
|
|
PrintFeatureSet();
|
|
|
|
for (size_t Idx = 0; Idx < kFeatureSetSize; Idx++)
|
|
|
|
if (GetFeature(Idx))
|
2016-10-08 23:57:48 +02:00
|
|
|
Inputs[SmallestElementPerFeature[Idx]]->Tmp++;
|
|
|
|
for (auto II: Inputs) {
|
|
|
|
if (II->Tmp != II->NumFeatures)
|
|
|
|
Printf("ZZZ %zd %zd\n", II->Tmp, II->NumFeatures);
|
|
|
|
assert(II->Tmp == II->NumFeatures);
|
|
|
|
II->Tmp = 0;
|
2016-09-30 03:19:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 23:41:48 +02:00
|
|
|
// Updates the probability distribution for the units in the corpus.
|
|
|
|
// Must be called whenever the corpus or unit weights are changed.
|
|
|
|
void UpdateCorpusDistribution() {
|
|
|
|
size_t N = Inputs.size();
|
2017-07-15 03:31:40 +02:00
|
|
|
assert(N);
|
2016-10-04 03:51:44 +02:00
|
|
|
Intervals.resize(N + 1);
|
|
|
|
Weights.resize(N);
|
2016-09-21 23:41:48 +02:00
|
|
|
std::iota(Intervals.begin(), Intervals.end(), 0);
|
2017-07-15 03:31:40 +02:00
|
|
|
for (size_t i = 0; i < N; i++)
|
|
|
|
Weights[i] = Inputs[i]->NumFeatures * (i + 1);
|
2016-09-21 23:41:48 +02:00
|
|
|
CorpusDistribution = std::piecewise_constant_distribution<double>(
|
|
|
|
Intervals.begin(), Intervals.end(), Weights.begin());
|
|
|
|
}
|
|
|
|
std::piecewise_constant_distribution<double> CorpusDistribution;
|
2016-09-21 03:50:50 +02:00
|
|
|
|
2016-10-04 03:51:44 +02:00
|
|
|
std::vector<double> Intervals;
|
|
|
|
std::vector<double> Weights;
|
|
|
|
|
2016-09-21 03:50:50 +02:00
|
|
|
std::unordered_set<std::string> Hashes;
|
2016-10-08 23:57:48 +02:00
|
|
|
std::vector<InputInfo*> Inputs;
|
2016-09-30 03:19:56 +02:00
|
|
|
|
2017-03-24 01:45:15 +01:00
|
|
|
size_t NumAddedFeatures = 0;
|
2017-03-31 06:17:45 +02:00
|
|
|
size_t NumUpdatedFeatures = 0;
|
2016-10-06 00:56:21 +02:00
|
|
|
uint32_t InputSizesPerFeature[kFeatureSetSize];
|
|
|
|
uint32_t SmallestElementPerFeature[kFeatureSetSize];
|
2016-10-09 01:24:45 +02:00
|
|
|
|
|
|
|
std::string OutputCorpus;
|
2016-09-21 03:50:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace fuzzer
|
|
|
|
|
|
|
|
#endif // LLVM_FUZZER_CORPUS
|