Unify fuzzer random number generation into a single class

BUG=tint:1098

Change-Id: I84931804515487d931bbbb5f0d5239d03ca76dfc
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/63300
Auto-Submit: Ryan Harrison <rharrison@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Alastair Donaldson <afdx@google.com>
This commit is contained in:
Ryan Harrison
2021-09-03 00:59:35 +00:00
committed by Tint LUCI CQ
parent 6a1eb45961
commit 5dc0ea7cce
26 changed files with 265 additions and 309 deletions

View File

@@ -13,6 +13,7 @@
# limitations under the License.
set(FUZZER_SOURCES
../random_generator.cc
cli.cc
fuzzer.cc
mutator.cc
@@ -23,6 +24,7 @@ set(FUZZER_SOURCES
util.cc)
set(FUZZER_SOURCES ${FUZZER_SOURCES}
../random_generator.h
cli.h
mutator.h
mutator_cache.h
@@ -32,8 +34,8 @@ set(FUZZER_SOURCES ${FUZZER_SOURCES}
util.h)
set(FUZZER_SOURCES ${FUZZER_SOURCES}
../tint_common_fuzzer.h
../tint_common_fuzzer.cc)
../tint_common_fuzzer.cc
../tint_common_fuzzer.h)
function(configure_spirv_tools_fuzzer_target NAME SOURCES)
add_executable(${NAME} ${SOURCES})
@@ -61,6 +63,7 @@ target_compile_definitions(tint_spirv_tools_fuzzer PRIVATE TARGET_FUZZER)
target_link_libraries(tint_spirv_tools_fuzzer libtint-fuzz)
set(DEBUGGER_SOURCES
../random_generator.cc
cli.cc
mutator.cc
mutator_debugger.cc
@@ -70,6 +73,7 @@ set(DEBUGGER_SOURCES
util.cc)
set(DEBUGGER_SOURCES ${DEBUGGER_SOURCES}
../random_generator.h
cli.h
mutator.h
spirv_fuzz_mutator.h

View File

@@ -14,10 +14,10 @@
#include <cassert>
#include <memory>
#include <random>
#include <string>
#include <vector>
#include "fuzzers/random_generator.h"
#include "fuzzers/tint_common_fuzzer.h"
#include "fuzzers/tint_spirv_tools_fuzzer/cli.h"
#include "fuzzers/tint_spirv_tools_fuzzer/mutator_cache.h"
@@ -67,9 +67,8 @@ std::unique_ptr<Mutator> CreateMutator(const std::vector<uint32_t>& binary,
}
assert(!types.empty() && "At least one mutator type must be specified");
std::mt19937 rng(seed);
auto mutator_type =
types[std::uniform_int_distribution<size_t>(0, types.size() - 1)(rng)];
RandomGenerator generator(seed);
auto mutator_type = types[generator.GetUInt64(types.size())];
const auto& mutator_params = context->params.mutator_params;
switch (mutator_type) {

View File

@@ -66,7 +66,7 @@ SpirvOptMutator::SpirvOptMutator(spv_target_env target_env,
optimized_binary_(),
validate_after_each_opt_(validate_after_each_opt),
opt_batch_size_(opt_batch_size),
rng_(seed) {
generator_(seed) {
assert(spvtools::SpirvTools(target_env).Validate(original_binary_) &&
"Initial binary is invalid");
assert(!opt_passes_.empty() && "Must be at least one pass");
@@ -105,8 +105,7 @@ SpirvOptMutator::Result SpirvOptMutator::Mutate() {
std::vector<std::string> passes;
while (passes.size() < num_of_passes) {
auto idx = std::uniform_int_distribution<size_t>(
0, opt_passes_.size() - 1)(rng_);
auto idx = generator_.GetUInt64(opt_passes_.size());
passes.push_back(opt_passes_[idx]);
}

View File

@@ -15,11 +15,11 @@
#ifndef FUZZERS_TINT_SPIRV_TOOLS_FUZZER_SPIRV_OPT_MUTATOR_H_
#define FUZZERS_TINT_SPIRV_TOOLS_FUZZER_SPIRV_OPT_MUTATOR_H_
#include <random>
#include <sstream>
#include <string>
#include <vector>
#include "fuzzers/random_generator.h"
#include "fuzzers/tint_spirv_tools_fuzzer/mutator.h"
#include "spirv-tools/libspirv.h"
@@ -86,7 +86,7 @@ class SpirvOptMutator : public Mutator {
std::stringstream errors_;
// The random number generator initialized with `seed_`.
std::mt19937 rng_;
RandomGenerator generator_;
};
} // namespace spvtools_fuzzer

View File

@@ -44,7 +44,7 @@ SpirvReduceMutator::SpirvReduceMutator(spv_target_env target_env,
bool validate_after_each_reduction)
: ir_context_(nullptr),
finders_(),
rng_(seed),
generator_(seed),
errors_(),
is_valid_(true),
reductions_batch_size_(reductions_batch_size),

View File

@@ -16,12 +16,12 @@
#define FUZZERS_TINT_SPIRV_TOOLS_FUZZER_SPIRV_REDUCE_MUTATOR_H_
#include <memory>
#include <random>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include "fuzzers/random_generator.h"
#include "fuzzers/tint_spirv_tools_fuzzer/mutator.h"
#include "source/reduce/reduction_opportunity_finder.h"
@@ -65,7 +65,7 @@ class SpirvReduceMutator : public Mutator {
private:
template <typename T, typename... Args>
void MaybeAddFinder(Args&&... args) {
if (enable_all_reductions_ || std::uniform_int_distribution<>(0, 1)(rng_)) {
if (enable_all_reductions_ || generator_.GetBool()) {
finders_.push_back(std::make_unique<T>(std::forward<Args>(args)...));
}
}
@@ -73,16 +73,14 @@ class SpirvReduceMutator : public Mutator {
template <typename T>
T* GetRandomElement(std::vector<T>* arr) {
assert(!arr->empty() && "Can't get random element from an empty vector");
auto index =
std::uniform_int_distribution<size_t>(0, arr->size() - 1)(rng_);
auto index = generator_.GetUInt64(arr->size());
return &(*arr)[index];
}
template <typename T>
T* GetRandomElement(std::vector<std::unique_ptr<T>>* arr) {
assert(!arr->empty() && "Can't get random element from an empty vector");
auto index =
std::uniform_int_distribution<size_t>(0, arr->size() - 1)(rng_);
auto index = generator_.GetUInt64(arr->size());
return (*arr)[index].get();
}
@@ -97,7 +95,7 @@ class SpirvReduceMutator : public Mutator {
finders_;
// Random number generator initialized with `seed_`.
std::mt19937 rng_;
RandomGenerator generator_;
// All the errors produced by the reducer.
std::stringstream errors_;