12 #ifndef LLVM_FUZZER_CORPUS
13 #define LLVM_FUZZER_CORPUS
23 #include <unordered_set>
42 static const size_t kFeatureSetSize = 1 << 21;
44 InputCorpus(
const std::string &OutputCorpus) : OutputCorpus(OutputCorpus) {
45 memset(InputSizesPerFeature, 0,
sizeof(InputSizesPerFeature));
46 memset(SmallestElementPerFeature, 0,
sizeof(SmallestElementPerFeature));
49 for (
auto II : Inputs)
52 size_t size()
const {
return Inputs.size(); }
55 for (
auto II : Inputs)
61 for (
auto II : Inputs)
62 Res += !II->U.empty();
67 for (
auto II : Inputs)
71 bool empty()
const {
return Inputs.empty(); }
74 const std::vector<uint32_t> &FeatureSet) {
87 UpdateCorpusDistribution();
94 if (!FeatureDebug)
return;
96 if (
C !=
'F' &&
C !=
'U' &&
C !=
'Z')
104 if (!FeatureDebug)
return;
113 if (!FeatureDebug)
return;
114 Printf(
"======= CORPUS:\n");
116 for (
auto II : Inputs) {
117 if (
std::find(II->U.begin(), II->U.end(),
'F') != II->U.end()) {
130 assert(II->
U.size() > U.size());
140 bool HasUnit(
const std::string &
H) {
return Hashes.count(H); }
151 size_t Idx =
static_cast<size_t>(CorpusDistribution(Rand));
152 assert(Idx < Inputs.size());
157 for (
size_t i = 0; i < Inputs.size(); i++) {
158 const auto &II = *Inputs[i];
159 Printf(
" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\n", i,
161 II.NumExecutedMutations, II.NumSuccessfullMutations);
166 for (
size_t i = 0; i < kFeatureSetSize; i++) {
167 if(
size_t Sz = GetFeature(i))
168 Printf(
"[%zd: id %zd sz%zd] ", i, SmallestElementPerFeature[i], Sz);
171 for (
size_t i = 0; i < Inputs.size(); i++)
187 Printf(
"EVICTED %zd\n", Idx);
192 Idx = Idx % kFeatureSetSize;
194 if (OldSize == 0 || (Shrink && OldSize > NewSize)) {
196 size_t OldIdx = SmallestElementPerFeature[Idx];
205 NumUpdatedFeatures++;
207 Printf(
"ADD FEATURE %zd sz %d\n", Idx, NewSize);
208 SmallestElementPerFeature[Idx] = Inputs.size();
209 InputSizesPerFeature[Idx] = NewSize;
220 memset(InputSizesPerFeature, 0,
sizeof(InputSizesPerFeature));
221 memset(SmallestElementPerFeature, 0,
sizeof(SmallestElementPerFeature));
226 static const bool FeatureDebug =
false;
228 size_t GetFeature(
size_t Idx)
const {
return InputSizesPerFeature[Idx]; }
230 void ValidateFeatureSet() {
233 for (
size_t Idx = 0; Idx < kFeatureSetSize; Idx++)
235 Inputs[SmallestElementPerFeature[Idx]]->Tmp++;
236 for (
auto II: Inputs) {
237 if (II->Tmp != II->NumFeatures)
238 Printf(
"ZZZ %zd %zd\n", II->Tmp, II->NumFeatures);
239 assert(II->Tmp == II->NumFeatures);
246 void UpdateCorpusDistribution() {
247 size_t N = Inputs.size();
249 Intervals.resize(N + 1);
251 std::iota(Intervals.begin(), Intervals.end(), 0);
252 for (
size_t i = 0; i <
N; i++)
254 CorpusDistribution = std::piecewise_constant_distribution<double>(
255 Intervals.begin(), Intervals.end(), Weights.begin());
257 std::piecewise_constant_distribution<double> CorpusDistribution;
259 std::vector<double> Intervals;
260 std::vector<double> Weights;
262 std::unordered_set<std::string> Hashes;
263 std::vector<InputInfo*> Inputs;
265 size_t NumAddedFeatures = 0;
266 size_t NumUpdatedFeatures = 0;
267 uint32_t InputSizesPerFeature[kFeatureSetSize];
268 uint32_t SmallestElementPerFeature[kFeatureSetSize];
270 std::string OutputCorpus;
275 #endif // LLVM_FUZZER_CORPUS
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out)
std::string DirPlusFile(const std::string &DirPath, const std::string &FileName)
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
ATTRIBUTE_NO_SANITIZE_ALL __attribute__((noinline)) void TracePC size_t N
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)
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
Printf(" # Score: %d, Used: %d\n", Scores[i], Usages[i])
static const int kSHA1NumBytes