Skip to content

Commit ab20b31

Browse files
xinhaoyuancopybara-github
authored andcommitted
Pass RNG to functions that require randomness for cleaner interface.
PiperOrigin-RevId: 817646190
1 parent a213424 commit ab20b31

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed

centipede/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ cc_library(
603603
":feature_set",
604604
":runner_result",
605605
":util",
606+
"@abseil-cpp//absl/random",
607+
"@abseil-cpp//absl/random:bit_gen_ref",
606608
"@abseil-cpp//absl/strings",
607609
"@com_google_fuzztest//common:defs",
608610
"@com_google_fuzztest//common:logging",

centipede/centipede.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,9 @@ void Centipede::FuzzingLoop() {
840840
std::vector<MutationInputRef> mutation_inputs;
841841
mutation_inputs.reserve(env_.mutate_batch_size);
842842
for (size_t i = 0; i < env_.mutate_batch_size; i++) {
843-
const auto &corpus_record = env_.use_corpus_weights
844-
? corpus_.WeightedRandom(rng_())
845-
: corpus_.UniformRandom(rng_());
843+
const auto& corpus_record = env_.use_corpus_weights
844+
? corpus_.WeightedRandom(rng_)
845+
: corpus_.UniformRandom(rng_);
846846
mutation_inputs.push_back(
847847
MutationInputRef{corpus_record.data, &corpus_record.metadata});
848848
}

centipede/corpus.cc

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <utility>
2323
#include <vector>
2424

25+
#include "absl/random/bit_gen_ref.h"
26+
#include "absl/random/random.h"
2527
#include "absl/strings/str_cat.h"
2628
#include "absl/strings/str_join.h"
2729
#include "absl/strings/substitute.h"
@@ -126,12 +128,12 @@ void Corpus::Add(const ByteArray& data, const FeatureVec& fv,
126128
weighted_distribution_.AddWeight(ComputeWeight(fv, fs, coverage_frontier));
127129
}
128130

129-
const CorpusRecord &Corpus::WeightedRandom(size_t random) const {
130-
return records_[weighted_distribution_.RandomIndex(random)];
131+
const CorpusRecord& Corpus::WeightedRandom(absl::BitGenRef rng) const {
132+
return records_[weighted_distribution_.RandomIndex(rng)];
131133
}
132134

133-
const CorpusRecord &Corpus::UniformRandom(size_t random) const {
134-
return records_[random % records_.size()];
135+
const CorpusRecord& Corpus::UniformRandom(absl::BitGenRef rng) const {
136+
return records_[absl::Uniform<size_t>(rng, 0, records_.size())];
135137
}
136138

137139
void Corpus::DumpStatsToFile(const FeatureSet &fs, std::string_view filepath,
@@ -210,18 +212,21 @@ void WeightedDistribution::RecomputeInternalState() {
210212
}
211213

212214
__attribute__((noinline)) // to see it in profile.
213-
size_t
214-
WeightedDistribution::RandomIndex(size_t random) const {
215+
size_t WeightedDistribution::RandomIndex(absl::BitGenRef rng) const {
215216
FUZZTEST_CHECK(!weights_.empty());
216217
FUZZTEST_CHECK(cumulative_weights_valid_);
217-
uint64_t sum_of_all_weights = cumulative_weights_.back();
218-
if (sum_of_all_weights == 0)
219-
return random % size(); // can't do much else here.
220-
random = random % sum_of_all_weights;
221-
auto it = std::upper_bound(cumulative_weights_.begin(),
222-
cumulative_weights_.end(), random);
218+
const uint64_t sum_of_all_weights = cumulative_weights_.back();
219+
if (sum_of_all_weights == 0) {
220+
// Can't do much else here.
221+
return absl::Uniform<size_t>(rng, 0, size());
222+
}
223+
auto it =
224+
std::upper_bound(cumulative_weights_.begin(), cumulative_weights_.end(),
225+
absl::Uniform<uint64_t>(rng, 0, sum_of_all_weights));
223226
FUZZTEST_CHECK(it != cumulative_weights_.end());
224-
return it - cumulative_weights_.begin();
227+
const size_t index = it - cumulative_weights_.begin();
228+
FUZZTEST_CHECK(weights_[index] != 0);
229+
return index;
225230
}
226231

227232
uint64_t WeightedDistribution::PopBack() {

centipede/corpus.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <utility>
2424
#include <vector>
2525

26+
#include "absl/random/bit_gen_ref.h"
2627
#include "./centipede/binary_info.h"
2728
#include "./centipede/execution_metadata.h"
2829
#include "./centipede/feature.h"
@@ -49,7 +50,7 @@ class WeightedDistribution {
4950
// For proper randomness, `random` should come from a 64-bit RNG.
5051
// RandomIndex() must not be called after ChangeWeight() without first
5152
// calling RecomputeInternalState().
52-
size_t RandomIndex(size_t random) const;
53+
size_t RandomIndex(absl::BitGenRef rng) const;
5354
// Returns the number of weights.
5455
size_t size() const { return weights_.size(); }
5556
// Removes all weights.
@@ -131,9 +132,9 @@ class Corpus {
131132
std::pair<size_t, size_t> MaxAndAvgSize() const;
132133
// Returns a random active corpus record using weighted distribution.
133134
// See WeightedDistribution.
134-
const CorpusRecord &WeightedRandom(size_t random) const;
135+
const CorpusRecord& WeightedRandom(absl::BitGenRef rng) const;
135136
// Returns a random active corpus record using uniform distribution.
136-
const CorpusRecord &UniformRandom(size_t random) const;
137+
const CorpusRecord& UniformRandom(absl::BitGenRef rng) const;
137138
// Returns the element with index 'idx', where `idx` < NumActive().
138139
const ByteArray &Get(size_t idx) const { return records_[idx].data; }
139140
// Returns the execution metadata for the element `idx`, `idx` < NumActive().

centipede/corpus_test.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,19 @@ TEST(WeightedDistribution, WeightedDistribution) {
175175
}
176176
};
177177

178+
Rng rng(12345);
178179
auto compute_freq = [&]() {
179180
freq.clear();
180181
freq.resize(wd.size());
181-
// We use numbers in [0, kNumIter) instead of random numbers
182-
// for simplicity.
183182
for (int i = 0; i < kNumIter; i++) {
184-
freq[wd.RandomIndex(i)]++;
183+
freq[wd.RandomIndex(rng)]++;
185184
}
186185
};
187186

188187
set_weights({1, 1});
189188
compute_freq();
190-
EXPECT_EQ(freq[0], kNumIter / 2);
191-
EXPECT_EQ(freq[1], kNumIter / 2);
189+
EXPECT_NEAR(freq[0], kNumIter / 2, 100);
190+
EXPECT_NEAR(freq[1], kNumIter / 2, 100);
192191

193192
set_weights({1, 2});
194193
compute_freq();

0 commit comments

Comments
 (0)