12 #ifndef LLVM_FUZZER_CORPUS
13 #define LLVM_FUZZER_CORPUS
23 #include <unordered_set>
42 InputCorpus(
const std::string &OutputCorpus) : OutputCorpus(OutputCorpus) {
43 memset(InputSizesPerFeature, 0,
sizeof(InputSizesPerFeature));
44 memset(SmallestElementPerFeature, 0,
sizeof(SmallestElementPerFeature));
47 for (
auto II : Inputs)
50 size_t size()
const {
return Inputs.size(); }
53 for (
auto II : Inputs)
59 for (
auto II : Inputs)
60 Res += !II->U.empty();
65 for (
auto II : Inputs)
66 Res = std::max(Res, II->U.size());
69 bool empty()
const {
return Inputs.empty(); }
84 UpdateCorpusDistribution();
89 bool HasUnit(
const std::string &
H) {
return Hashes.count(H); }
100 size_t Idx =
static_cast<size_t>(CorpusDistribution(Rand.
Get_mt19937()));
101 assert(Idx < Inputs.size());
106 for (
size_t i = 0;
i < Inputs.size();
i++) {
107 const auto &II = *Inputs[
i];
108 Printf(
" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\n",
i,
110 II.NumExecutedMutations, II.NumSuccessfullMutations);
116 if(
size_t Sz = GetFeature(
i))
117 Printf(
"[%zd: id %zd sz%zd] ",
i, SmallestElementPerFeature[
i], Sz);
120 for (
size_t i = 0;
i < Inputs.size();
i++)
132 Printf(
"EVICTED %zd\n", Idx);
139 if (OldSize == 0 || (Shrink && OldSize > NewSize)) {
141 size_t OldIdx = SmallestElementPerFeature[Idx];
149 Printf(
"ADD FEATURE %zd sz %d\n", Idx, NewSize);
150 SmallestElementPerFeature[Idx] = Inputs.size();
151 InputSizesPerFeature[Idx] = NewSize;
152 CountingFeatures =
true;
161 Res += GetFeature(
i) != 0;
167 memset(InputSizesPerFeature, 0,
sizeof(InputSizesPerFeature));
168 memset(SmallestElementPerFeature, 0,
sizeof(SmallestElementPerFeature));
173 static const bool FeatureDebug =
false;
175 size_t GetFeature(
size_t Idx)
const {
return InputSizesPerFeature[Idx]; }
177 void ValidateFeatureSet() {
178 if (!CountingFeatures)
return;
183 Inputs[SmallestElementPerFeature[Idx]]->Tmp++;
184 for (
auto II: Inputs) {
185 if (II->Tmp != II->NumFeatures)
186 Printf(
"ZZZ %zd %zd\n", II->Tmp, II->NumFeatures);
187 assert(II->Tmp == II->NumFeatures);
194 void UpdateCorpusDistribution() {
195 size_t N = Inputs.size();
196 Intervals.resize(N + 1);
198 std::iota(Intervals.begin(), Intervals.end(), 0);
199 if (CountingFeatures)
200 for (
size_t i = 0;
i <
N;
i++)
203 std::iota(Weights.begin(), Weights.end(), 1);
204 CorpusDistribution = std::piecewise_constant_distribution<double>(
205 Intervals.begin(), Intervals.end(), Weights.begin());
207 std::piecewise_constant_distribution<double> CorpusDistribution;
209 std::vector<double> Intervals;
210 std::vector<double> Weights;
212 std::unordered_set<std::string> Hashes;
213 std::vector<InputInfo*> Inputs;
215 bool CountingFeatures =
false;
219 std::string OutputCorpus;
224 #endif // LLVM_FUZZER_CORPUS
void Printf(const char *Fmt,...)
void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out)
std::string DirPlusFile(const std::string &DirPath, const std::string &FileName)
std::mt19937 & Get_mt19937()
void RemoveFile(const std::string &Path)
std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes])
std::vector< uint8_t > Unit
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::string Hash(const Unit &U)
static const int kSHA1NumBytes