Skip to content

Commit bede46f

Browse files
authored
[Fuzzer] Optimize UpdateFeatureFrequency (#65288)
Instead of a linear scan, use a bitset to track rarity of features. This improves fuzzer throughput rather dramatically (close to 2x) in early exploratory phases; in steady state this seems to improve fuzzing throughput by ~15% according to perf. The benchmarks are done on an executable with ~100k features, so the results may change based on the executable that's being fuzzed. kFeatureSetSize is 2M so the bitset is adding 256 KB to sizeof(InputCorpus), but this should be fine since there's already three arrays indexed by feature index for a total of 200 MB.
1 parent c525b8e commit bede46f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

compiler-rt/lib/fuzzer/FuzzerCorpus.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "FuzzerSHA1.h"
1919
#include "FuzzerTracePC.h"
2020
#include <algorithm>
21+
#include <bitset>
2122
#include <chrono>
2223
#include <numeric>
2324
#include <random>
@@ -382,6 +383,7 @@ class InputCorpus {
382383
}
383384

384385
// Remove most abundant rare feature.
386+
IsRareFeature[Delete] = false;
385387
RareFeatures[Delete] = RareFeatures.back();
386388
RareFeatures.pop_back();
387389

@@ -397,6 +399,7 @@ class InputCorpus {
397399

398400
// Add rare feature, handle collisions, and update energy.
399401
RareFeatures.push_back(Idx);
402+
IsRareFeature[Idx] = true;
400403
GlobalFeatureFreqs[Idx] = 0;
401404
for (auto II : Inputs) {
402405
II->DeleteFeatureFreq(Idx);
@@ -450,9 +453,7 @@ class InputCorpus {
450453
uint16_t Freq = GlobalFeatureFreqs[Idx32]++;
451454

452455
// Skip if abundant.
453-
if (Freq > FreqOfMostAbundantRareFeature ||
454-
std::find(RareFeatures.begin(), RareFeatures.end(), Idx32) ==
455-
RareFeatures.end())
456+
if (Freq > FreqOfMostAbundantRareFeature || !IsRareFeature[Idx32])
456457
return;
457458

458459
// Update global frequencies.
@@ -581,6 +582,7 @@ class InputCorpus {
581582
uint16_t FreqOfMostAbundantRareFeature = 0;
582583
uint16_t GlobalFeatureFreqs[kFeatureSetSize] = {};
583584
std::vector<uint32_t> RareFeatures;
585+
std::bitset<kFeatureSetSize> IsRareFeature;
584586

585587
std::string OutputCorpus;
586588
};

0 commit comments

Comments
 (0)