FuzzerCorpus.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. //===- FuzzerCorpus.h - Internal header for the Fuzzer ----------*- C++ -* ===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // fuzzer::InputCorpus
  9. //===----------------------------------------------------------------------===//
  10. #ifndef LLVM_FUZZER_CORPUS
  11. #define LLVM_FUZZER_CORPUS
  12. #include "FuzzerDataFlowTrace.h"
  13. #include "FuzzerDefs.h"
  14. #include "FuzzerIO.h"
  15. #include "FuzzerRandom.h"
  16. #include "FuzzerSHA1.h"
  17. #include "FuzzerTracePC.h"
  18. #include <algorithm>
  19. #include <bitset>
  20. #include <chrono>
  21. #include <numeric>
  22. #include <random>
  23. #include <unordered_set>
  24. namespace fuzzer {
  25. struct InputInfo {
  26. Unit U; // The actual input data.
  27. std::chrono::microseconds TimeOfUnit;
  28. uint8_t Sha1[kSHA1NumBytes]; // Checksum.
  29. // Number of features that this input has and no smaller input has.
  30. size_t NumFeatures = 0;
  31. size_t Tmp = 0; // Used by ValidateFeatureSet.
  32. // Stats.
  33. size_t NumExecutedMutations = 0;
  34. size_t NumSuccessfullMutations = 0;
  35. bool NeverReduce = false;
  36. bool MayDeleteFile = false;
  37. bool Reduced = false;
  38. bool HasFocusFunction = false;
  39. std::vector<uint32_t> UniqFeatureSet;
  40. std::vector<uint8_t> DataFlowTraceForFocusFunction;
  41. // Power schedule.
  42. bool NeedsEnergyUpdate = false;
  43. double Energy = 0.0;
  44. double SumIncidence = 0.0;
  45. std::vector<std::pair<uint32_t, uint16_t>> FeatureFreqs;
  46. // Delete feature Idx and its frequency from FeatureFreqs.
  47. bool DeleteFeatureFreq(uint32_t Idx) {
  48. if (FeatureFreqs.empty())
  49. return false;
  50. // Binary search over local feature frequencies sorted by index.
  51. auto Lower = std::lower_bound(FeatureFreqs.begin(), FeatureFreqs.end(),
  52. std::pair<uint32_t, uint16_t>(Idx, 0));
  53. if (Lower != FeatureFreqs.end() && Lower->first == Idx) {
  54. FeatureFreqs.erase(Lower);
  55. return true;
  56. }
  57. return false;
  58. }
  59. // Assign more energy to a high-entropy seed, i.e., that reveals more
  60. // information about the globally rare features in the neighborhood of the
  61. // seed. Since we do not know the entropy of a seed that has never been
  62. // executed we assign fresh seeds maximum entropy and let II->Energy approach
  63. // the true entropy from above. If ScalePerExecTime is true, the computed
  64. // entropy is scaled based on how fast this input executes compared to the
  65. // average execution time of inputs. The faster an input executes, the more
  66. // energy gets assigned to the input.
  67. void UpdateEnergy(size_t GlobalNumberOfFeatures, bool ScalePerExecTime,
  68. std::chrono::microseconds AverageUnitExecutionTime) {
  69. Energy = 0.0;
  70. SumIncidence = 0.0;
  71. // Apply add-one smoothing to locally discovered features.
  72. for (const auto &F : FeatureFreqs) {
  73. double LocalIncidence = F.second + 1;
  74. Energy -= LocalIncidence * log(LocalIncidence);
  75. SumIncidence += LocalIncidence;
  76. }
  77. // Apply add-one smoothing to locally undiscovered features.
  78. // PreciseEnergy -= 0; // since log(1.0) == 0)
  79. SumIncidence +=
  80. static_cast<double>(GlobalNumberOfFeatures - FeatureFreqs.size());
  81. // Add a single locally abundant feature apply add-one smoothing.
  82. double AbdIncidence = static_cast<double>(NumExecutedMutations + 1);
  83. Energy -= AbdIncidence * log(AbdIncidence);
  84. SumIncidence += AbdIncidence;
  85. // Normalize.
  86. if (SumIncidence != 0)
  87. Energy = Energy / SumIncidence + log(SumIncidence);
  88. if (ScalePerExecTime) {
  89. // Scaling to favor inputs with lower execution time.
  90. uint32_t PerfScore = 100;
  91. if (TimeOfUnit.count() > AverageUnitExecutionTime.count() * 10)
  92. PerfScore = 10;
  93. else if (TimeOfUnit.count() > AverageUnitExecutionTime.count() * 4)
  94. PerfScore = 25;
  95. else if (TimeOfUnit.count() > AverageUnitExecutionTime.count() * 2)
  96. PerfScore = 50;
  97. else if (TimeOfUnit.count() * 3 > AverageUnitExecutionTime.count() * 4)
  98. PerfScore = 75;
  99. else if (TimeOfUnit.count() * 4 < AverageUnitExecutionTime.count())
  100. PerfScore = 300;
  101. else if (TimeOfUnit.count() * 3 < AverageUnitExecutionTime.count())
  102. PerfScore = 200;
  103. else if (TimeOfUnit.count() * 2 < AverageUnitExecutionTime.count())
  104. PerfScore = 150;
  105. Energy *= PerfScore;
  106. }
  107. }
  108. // Increment the frequency of the feature Idx.
  109. void UpdateFeatureFrequency(uint32_t Idx) {
  110. NeedsEnergyUpdate = true;
  111. // The local feature frequencies is an ordered vector of pairs.
  112. // If there are no local feature frequencies, push_back preserves order.
  113. // Set the feature frequency for feature Idx32 to 1.
  114. if (FeatureFreqs.empty()) {
  115. FeatureFreqs.push_back(std::pair<uint32_t, uint16_t>(Idx, 1));
  116. return;
  117. }
  118. // Binary search over local feature frequencies sorted by index.
  119. auto Lower = std::lower_bound(FeatureFreqs.begin(), FeatureFreqs.end(),
  120. std::pair<uint32_t, uint16_t>(Idx, 0));
  121. // If feature Idx32 already exists, increment its frequency.
  122. // Otherwise, insert a new pair right after the next lower index.
  123. if (Lower != FeatureFreqs.end() && Lower->first == Idx) {
  124. Lower->second++;
  125. } else {
  126. FeatureFreqs.insert(Lower, std::pair<uint32_t, uint16_t>(Idx, 1));
  127. }
  128. }
  129. };
  130. struct EntropicOptions {
  131. bool Enabled;
  132. size_t NumberOfRarestFeatures;
  133. size_t FeatureFrequencyThreshold;
  134. bool ScalePerExecTime;
  135. };
  136. class InputCorpus {
  137. static const uint32_t kFeatureSetSize = 1 << 21;
  138. static const uint8_t kMaxMutationFactor = 20;
  139. static const size_t kSparseEnergyUpdates = 100;
  140. size_t NumExecutedMutations = 0;
  141. EntropicOptions Entropic;
  142. public:
  143. InputCorpus(const std::string &OutputCorpus, EntropicOptions Entropic)
  144. : Entropic(Entropic), OutputCorpus(OutputCorpus) {
  145. memset(InputSizesPerFeature, 0, sizeof(InputSizesPerFeature));
  146. memset(SmallestElementPerFeature, 0, sizeof(SmallestElementPerFeature));
  147. }
  148. ~InputCorpus() {
  149. for (auto II : Inputs)
  150. delete II;
  151. }
  152. size_t size() const { return Inputs.size(); }
  153. size_t SizeInBytes() const {
  154. size_t Res = 0;
  155. for (auto II : Inputs)
  156. Res += II->U.size();
  157. return Res;
  158. }
  159. size_t NumActiveUnits() const {
  160. size_t Res = 0;
  161. for (auto II : Inputs)
  162. Res += !II->U.empty();
  163. return Res;
  164. }
  165. size_t MaxInputSize() const {
  166. size_t Res = 0;
  167. for (auto II : Inputs)
  168. Res = std::max(Res, II->U.size());
  169. return Res;
  170. }
  171. void IncrementNumExecutedMutations() { NumExecutedMutations++; }
  172. size_t NumInputsThatTouchFocusFunction() {
  173. return std::count_if(Inputs.begin(), Inputs.end(), [](const InputInfo *II) {
  174. return II->HasFocusFunction;
  175. });
  176. }
  177. size_t NumInputsWithDataFlowTrace() {
  178. return std::count_if(Inputs.begin(), Inputs.end(), [](const InputInfo *II) {
  179. return !II->DataFlowTraceForFocusFunction.empty();
  180. });
  181. }
  182. bool empty() const { return Inputs.empty(); }
  183. const Unit &operator[] (size_t Idx) const { return Inputs[Idx]->U; }
  184. InputInfo *AddToCorpus(const Unit &U, size_t NumFeatures, bool MayDeleteFile,
  185. bool HasFocusFunction, bool NeverReduce,
  186. std::chrono::microseconds TimeOfUnit,
  187. const std::vector<uint32_t> &FeatureSet,
  188. const DataFlowTrace &DFT, const InputInfo *BaseII) {
  189. assert(!U.empty());
  190. if (FeatureDebug)
  191. Printf("ADD_TO_CORPUS %zd NF %zd\n", Inputs.size(), NumFeatures);
  192. // Inputs.size() is cast to uint32_t below.
  193. assert(Inputs.size() < std::numeric_limits<uint32_t>::max());
  194. Inputs.push_back(new InputInfo());
  195. InputInfo &II = *Inputs.back();
  196. II.U = U;
  197. II.NumFeatures = NumFeatures;
  198. II.NeverReduce = NeverReduce;
  199. II.TimeOfUnit = TimeOfUnit;
  200. II.MayDeleteFile = MayDeleteFile;
  201. II.UniqFeatureSet = FeatureSet;
  202. II.HasFocusFunction = HasFocusFunction;
  203. // Assign maximal energy to the new seed.
  204. II.Energy = RareFeatures.empty() ? 1.0 : log(RareFeatures.size());
  205. II.SumIncidence = static_cast<double>(RareFeatures.size());
  206. II.NeedsEnergyUpdate = false;
  207. std::sort(II.UniqFeatureSet.begin(), II.UniqFeatureSet.end());
  208. ComputeSHA1(U.data(), U.size(), II.Sha1);
  209. auto Sha1Str = Sha1ToString(II.Sha1);
  210. Hashes.insert(Sha1Str);
  211. if (HasFocusFunction)
  212. if (auto V = DFT.Get(Sha1Str))
  213. II.DataFlowTraceForFocusFunction = *V;
  214. // This is a gross heuristic.
  215. // Ideally, when we add an element to a corpus we need to know its DFT.
  216. // But if we don't, we'll use the DFT of its base input.
  217. if (II.DataFlowTraceForFocusFunction.empty() && BaseII)
  218. II.DataFlowTraceForFocusFunction = BaseII->DataFlowTraceForFocusFunction;
  219. DistributionNeedsUpdate = true;
  220. PrintCorpus();
  221. // ValidateFeatureSet();
  222. return &II;
  223. }
  224. // Debug-only
  225. void PrintUnit(const Unit &U) {
  226. if (!FeatureDebug) return;
  227. for (uint8_t C : U) {
  228. if (C != 'F' && C != 'U' && C != 'Z')
  229. C = '.';
  230. Printf("%c", C);
  231. }
  232. }
  233. // Debug-only
  234. void PrintFeatureSet(const std::vector<uint32_t> &FeatureSet) {
  235. if (!FeatureDebug) return;
  236. Printf("{");
  237. for (uint32_t Feature: FeatureSet)
  238. Printf("%u,", Feature);
  239. Printf("}");
  240. }
  241. // Debug-only
  242. void PrintCorpus() {
  243. if (!FeatureDebug) return;
  244. Printf("======= CORPUS:\n");
  245. int i = 0;
  246. for (auto II : Inputs) {
  247. if (std::find(II->U.begin(), II->U.end(), 'F') != II->U.end()) {
  248. Printf("[%2d] ", i);
  249. Printf("%s sz=%zd ", Sha1ToString(II->Sha1).c_str(), II->U.size());
  250. PrintUnit(II->U);
  251. Printf(" ");
  252. PrintFeatureSet(II->UniqFeatureSet);
  253. Printf("\n");
  254. }
  255. i++;
  256. }
  257. }
  258. void Replace(InputInfo *II, const Unit &U,
  259. std::chrono::microseconds TimeOfUnit) {
  260. assert(II->U.size() > U.size());
  261. Hashes.erase(Sha1ToString(II->Sha1));
  262. DeleteFile(*II);
  263. ComputeSHA1(U.data(), U.size(), II->Sha1);
  264. Hashes.insert(Sha1ToString(II->Sha1));
  265. II->U = U;
  266. II->Reduced = true;
  267. II->TimeOfUnit = TimeOfUnit;
  268. DistributionNeedsUpdate = true;
  269. }
  270. bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); }
  271. bool HasUnit(const std::string &H) { return Hashes.count(H); }
  272. InputInfo &ChooseUnitToMutate(Random &Rand) {
  273. InputInfo &II = *Inputs[ChooseUnitIdxToMutate(Rand)];
  274. assert(!II.U.empty());
  275. return II;
  276. }
  277. InputInfo &ChooseUnitToCrossOverWith(Random &Rand, bool UniformDist) {
  278. if (!UniformDist) {
  279. return ChooseUnitToMutate(Rand);
  280. }
  281. InputInfo &II = *Inputs[Rand(Inputs.size())];
  282. assert(!II.U.empty());
  283. return II;
  284. }
  285. // Returns an index of random unit from the corpus to mutate.
  286. size_t ChooseUnitIdxToMutate(Random &Rand) {
  287. UpdateCorpusDistribution(Rand);
  288. size_t Idx = static_cast<size_t>(CorpusDistribution(Rand));
  289. assert(Idx < Inputs.size());
  290. return Idx;
  291. }
  292. void PrintStats() {
  293. for (size_t i = 0; i < Inputs.size(); i++) {
  294. const auto &II = *Inputs[i];
  295. Printf(" [% 3zd %s] sz: % 5zd runs: % 5zd succ: % 5zd focus: %d\n", i,
  296. Sha1ToString(II.Sha1).c_str(), II.U.size(),
  297. II.NumExecutedMutations, II.NumSuccessfullMutations,
  298. II.HasFocusFunction);
  299. }
  300. }
  301. void PrintFeatureSet() {
  302. for (size_t i = 0; i < kFeatureSetSize; i++) {
  303. if(size_t Sz = GetFeature(i))
  304. Printf("[%zd: id %zd sz%zd] ", i, SmallestElementPerFeature[i], Sz);
  305. }
  306. Printf("\n\t");
  307. for (size_t i = 0; i < Inputs.size(); i++)
  308. if (size_t N = Inputs[i]->NumFeatures)
  309. Printf(" %zd=>%zd ", i, N);
  310. Printf("\n");
  311. }
  312. void DeleteFile(const InputInfo &II) {
  313. if (!OutputCorpus.empty() && II.MayDeleteFile)
  314. RemoveFile(DirPlusFile(OutputCorpus, Sha1ToString(II.Sha1)));
  315. }
  316. void DeleteInput(size_t Idx) {
  317. InputInfo &II = *Inputs[Idx];
  318. DeleteFile(II);
  319. Unit().swap(II.U);
  320. II.Energy = 0.0;
  321. II.NeedsEnergyUpdate = false;
  322. DistributionNeedsUpdate = true;
  323. if (FeatureDebug)
  324. Printf("EVICTED %zd\n", Idx);
  325. }
  326. void AddRareFeature(uint32_t Idx) {
  327. // Maintain *at least* TopXRarestFeatures many rare features
  328. // and all features with a frequency below ConsideredRare.
  329. // Remove all other features.
  330. while (RareFeatures.size() > Entropic.NumberOfRarestFeatures &&
  331. FreqOfMostAbundantRareFeature > Entropic.FeatureFrequencyThreshold) {
  332. // Find most and second most abbundant feature.
  333. uint32_t MostAbundantRareFeatureIndices[2] = {RareFeatures[0],
  334. RareFeatures[0]};
  335. size_t Delete = 0;
  336. for (size_t i = 0; i < RareFeatures.size(); i++) {
  337. uint32_t Idx2 = RareFeatures[i];
  338. if (GlobalFeatureFreqs[Idx2] >=
  339. GlobalFeatureFreqs[MostAbundantRareFeatureIndices[0]]) {
  340. MostAbundantRareFeatureIndices[1] = MostAbundantRareFeatureIndices[0];
  341. MostAbundantRareFeatureIndices[0] = Idx2;
  342. Delete = i;
  343. }
  344. }
  345. // Remove most abundant rare feature.
  346. IsRareFeature[Delete] = false;
  347. RareFeatures[Delete] = RareFeatures.back();
  348. RareFeatures.pop_back();
  349. for (auto II : Inputs) {
  350. if (II->DeleteFeatureFreq(MostAbundantRareFeatureIndices[0]))
  351. II->NeedsEnergyUpdate = true;
  352. }
  353. // Set 2nd most abundant as the new most abundant feature count.
  354. FreqOfMostAbundantRareFeature =
  355. GlobalFeatureFreqs[MostAbundantRareFeatureIndices[1]];
  356. }
  357. // Add rare feature, handle collisions, and update energy.
  358. RareFeatures.push_back(Idx);
  359. IsRareFeature[Idx] = true;
  360. GlobalFeatureFreqs[Idx] = 0;
  361. for (auto II : Inputs) {
  362. II->DeleteFeatureFreq(Idx);
  363. // Apply add-one smoothing to this locally undiscovered feature.
  364. // Zero energy seeds will never be fuzzed and remain zero energy.
  365. if (II->Energy > 0.0) {
  366. II->SumIncidence += 1;
  367. II->Energy += log(II->SumIncidence) / II->SumIncidence;
  368. }
  369. }
  370. DistributionNeedsUpdate = true;
  371. }
  372. bool AddFeature(size_t Idx, uint32_t NewSize, bool Shrink) {
  373. assert(NewSize);
  374. Idx = Idx % kFeatureSetSize;
  375. uint32_t OldSize = GetFeature(Idx);
  376. if (OldSize == 0 || (Shrink && OldSize > NewSize)) {
  377. if (OldSize > 0) {
  378. size_t OldIdx = SmallestElementPerFeature[Idx];
  379. InputInfo &II = *Inputs[OldIdx];
  380. assert(II.NumFeatures > 0);
  381. II.NumFeatures--;
  382. if (II.NumFeatures == 0)
  383. DeleteInput(OldIdx);
  384. } else {
  385. NumAddedFeatures++;
  386. if (Entropic.Enabled)
  387. AddRareFeature((uint32_t)Idx);
  388. }
  389. NumUpdatedFeatures++;
  390. if (FeatureDebug)
  391. Printf("ADD FEATURE %zd sz %d\n", Idx, NewSize);
  392. // Inputs.size() is guaranteed to be less than UINT32_MAX by AddToCorpus.
  393. SmallestElementPerFeature[Idx] = static_cast<uint32_t>(Inputs.size());
  394. InputSizesPerFeature[Idx] = NewSize;
  395. return true;
  396. }
  397. return false;
  398. }
  399. // Increment frequency of feature Idx globally and locally.
  400. void UpdateFeatureFrequency(InputInfo *II, size_t Idx) {
  401. uint32_t Idx32 = Idx % kFeatureSetSize;
  402. // Saturated increment.
  403. if (GlobalFeatureFreqs[Idx32] == 0xFFFF)
  404. return;
  405. uint16_t Freq = GlobalFeatureFreqs[Idx32]++;
  406. // Skip if abundant.
  407. if (Freq > FreqOfMostAbundantRareFeature || !IsRareFeature[Idx32])
  408. return;
  409. // Update global frequencies.
  410. if (Freq == FreqOfMostAbundantRareFeature)
  411. FreqOfMostAbundantRareFeature++;
  412. // Update local frequencies.
  413. if (II)
  414. II->UpdateFeatureFrequency(Idx32);
  415. }
  416. size_t NumFeatures() const { return NumAddedFeatures; }
  417. size_t NumFeatureUpdates() const { return NumUpdatedFeatures; }
  418. private:
  419. static const bool FeatureDebug = false;
  420. uint32_t GetFeature(size_t Idx) const { return InputSizesPerFeature[Idx]; }
  421. void ValidateFeatureSet() {
  422. if (FeatureDebug)
  423. PrintFeatureSet();
  424. for (size_t Idx = 0; Idx < kFeatureSetSize; Idx++)
  425. if (GetFeature(Idx))
  426. Inputs[SmallestElementPerFeature[Idx]]->Tmp++;
  427. for (auto II: Inputs) {
  428. if (II->Tmp != II->NumFeatures)
  429. Printf("ZZZ %zd %zd\n", II->Tmp, II->NumFeatures);
  430. assert(II->Tmp == II->NumFeatures);
  431. II->Tmp = 0;
  432. }
  433. }
  434. // Updates the probability distribution for the units in the corpus.
  435. // Must be called whenever the corpus or unit weights are changed.
  436. //
  437. // Hypothesis: inputs that maximize information about globally rare features
  438. // are interesting.
  439. void UpdateCorpusDistribution(Random &Rand) {
  440. // Skip update if no seeds or rare features were added/deleted.
  441. // Sparse updates for local change of feature frequencies,
  442. // i.e., randomly do not skip.
  443. if (!DistributionNeedsUpdate &&
  444. (!Entropic.Enabled || Rand(kSparseEnergyUpdates)))
  445. return;
  446. DistributionNeedsUpdate = false;
  447. size_t N = Inputs.size();
  448. assert(N);
  449. Intervals.resize(N + 1);
  450. Weights.resize(N);
  451. std::iota(Intervals.begin(), Intervals.end(), 0);
  452. std::chrono::microseconds AverageUnitExecutionTime(0);
  453. for (auto II : Inputs) {
  454. AverageUnitExecutionTime += II->TimeOfUnit;
  455. }
  456. AverageUnitExecutionTime /= N;
  457. bool VanillaSchedule = true;
  458. if (Entropic.Enabled) {
  459. for (auto II : Inputs) {
  460. if (II->NeedsEnergyUpdate && II->Energy != 0.0) {
  461. II->NeedsEnergyUpdate = false;
  462. II->UpdateEnergy(RareFeatures.size(), Entropic.ScalePerExecTime,
  463. AverageUnitExecutionTime);
  464. }
  465. }
  466. for (size_t i = 0; i < N; i++) {
  467. if (Inputs[i]->NumFeatures == 0) {
  468. // If the seed doesn't represent any features, assign zero energy.
  469. Weights[i] = 0.;
  470. } else if (Inputs[i]->NumExecutedMutations / kMaxMutationFactor >
  471. NumExecutedMutations / Inputs.size()) {
  472. // If the seed was fuzzed a lot more than average, assign zero energy.
  473. Weights[i] = 0.;
  474. } else {
  475. // Otherwise, simply assign the computed energy.
  476. Weights[i] = Inputs[i]->Energy;
  477. }
  478. // If energy for all seeds is zero, fall back to vanilla schedule.
  479. if (Weights[i] > 0.0)
  480. VanillaSchedule = false;
  481. }
  482. }
  483. if (VanillaSchedule) {
  484. for (size_t i = 0; i < N; i++)
  485. Weights[i] =
  486. Inputs[i]->NumFeatures
  487. ? static_cast<double>((i + 1) *
  488. (Inputs[i]->HasFocusFunction ? 1000 : 1))
  489. : 0.;
  490. }
  491. if (FeatureDebug) {
  492. for (size_t i = 0; i < N; i++)
  493. Printf("%zd ", Inputs[i]->NumFeatures);
  494. Printf("SCORE\n");
  495. for (size_t i = 0; i < N; i++)
  496. Printf("%f ", Weights[i]);
  497. Printf("Weights\n");
  498. }
  499. CorpusDistribution = std::piecewise_constant_distribution<double>(
  500. Intervals.begin(), Intervals.end(), Weights.begin());
  501. }
  502. std::piecewise_constant_distribution<double> CorpusDistribution;
  503. std::vector<double> Intervals;
  504. std::vector<double> Weights;
  505. std::unordered_set<std::string> Hashes;
  506. std::vector<InputInfo *> Inputs;
  507. size_t NumAddedFeatures = 0;
  508. size_t NumUpdatedFeatures = 0;
  509. uint32_t InputSizesPerFeature[kFeatureSetSize];
  510. uint32_t SmallestElementPerFeature[kFeatureSetSize];
  511. bool DistributionNeedsUpdate = true;
  512. uint16_t FreqOfMostAbundantRareFeature = 0;
  513. uint16_t GlobalFeatureFreqs[kFeatureSetSize] = {};
  514. std::vector<uint32_t> RareFeatures;
  515. std::bitset<kFeatureSetSize> IsRareFeature;
  516. std::string OutputCorpus;
  517. };
  518. } // namespace fuzzer
  519. #endif // LLVM_FUZZER_CORPUS