Fully support initializing fuzzer utilities using a seed value

Adds a constructor to TransformBuilder that takes a seed value.
Removes accessing internal details of the fuzzing utilities.
Also a bunch of little clean ups through out the code.

BUG=tint:1261

Change-Id: Iac7ace25b91fa96959e6c07b1df963900a1f7100
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/67700
Auto-Submit: Ryan Harrison <rharrison@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ryan Harrison 2021-11-01 18:14:42 +00:00 committed by Tint LUCI CQ
parent 98dc04329f
commit d3f628b303
7 changed files with 35 additions and 23 deletions

View File

@ -25,18 +25,20 @@
namespace tint { namespace tint {
namespace fuzzers { namespace fuzzers {
/// Builder for generic pseudo-random data using a data buffer as seed /// Builder for generic pseudo-random data
class DataBuilder { class DataBuilder {
public: public:
/// @brief Initialize internal random number generation using data as seed /// @brief Initializes the internal engine using a seed value
/// @param data - pointer to a data buffer to use as a seed /// @param seed - seed value passed to engine
explicit DataBuilder(uint64_t seed) : generator_(seed) {}
/// @brief Initializes the internal engine using seed data
/// @param data - data fuzzer to calculate seed from
/// @param size - size of data buffer /// @param size - size of data buffer
explicit DataBuilder(const uint8_t* data, size_t size) explicit DataBuilder(const uint8_t* data, size_t size)
: generator_(data, size) {} : generator_(data, size) {
assert(data != nullptr && "|data| must be !nullptr");
/// @brief Initialize internal random number generation using seed value }
/// @param seed - value to use as seed
explicit DataBuilder(uint64_t seed) : generator_(seed) {}
~DataBuilder() = default; ~DataBuilder() = default;
DataBuilder(DataBuilder&&) = default; DataBuilder(DataBuilder&&) = default;
@ -131,7 +133,7 @@ class DataBuilder {
} }
std::vector<uint8_t> source(count); std::vector<uint8_t> source(count);
b->build(source.data(), count); b->build(source.data(), count);
return std::string(source.begin(), source.end()); return {source.begin(), source.end()};
} }
}; };

View File

@ -59,7 +59,9 @@ size_t HashBuffer(const uint8_t* data, const size_t size) {
RandomGenerator::RandomGenerator(uint64_t seed) : engine_(seed) {} RandomGenerator::RandomGenerator(uint64_t seed) : engine_(seed) {}
RandomGenerator::RandomGenerator(const uint8_t* data, size_t size) RandomGenerator::RandomGenerator(const uint8_t* data, size_t size)
: engine_(RandomGenerator::CalculateSeed(data, size)) {} : engine_(RandomGenerator::CalculateSeed(data, size)) {
assert(data != nullptr && "|data| must be !nullptr");
}
uint32_t RandomGenerator::GetUInt32(uint32_t lower, uint32_t upper) { uint32_t RandomGenerator::GetUInt32(uint32_t lower, uint32_t upper) {
return RandomUInt(&engine_, lower, upper); return RandomUInt(&engine_, lower, upper);

View File

@ -21,9 +21,9 @@ namespace tint {
namespace fuzzers { namespace fuzzers {
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
DataBuilder b(data, size); DataBuilder db(data, size);
writer::msl::Options options; writer::msl::Options options;
GenerateMslOptions(&b, &options); GenerateMslOptions(&db, &options);
tint::fuzzers::ReaderWriterFuzzer fuzzer(InputFormat::kSpv, tint::fuzzers::ReaderWriterFuzzer fuzzer(InputFormat::kSpv,
OutputFormat::kMSL); OutputFormat::kMSL);
fuzzer.SetOptionsMsl(options); fuzzer.SetOptionsMsl(options);

View File

@ -21,9 +21,9 @@ namespace tint {
namespace fuzzers { namespace fuzzers {
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
DataBuilder b(data, size); DataBuilder db(data, size);
writer::spirv::Options options; writer::spirv::Options options;
GenerateSpirvOptions(&b, &options); GenerateSpirvOptions(&db, &options);
tint::fuzzers::ReaderWriterFuzzer fuzzer(InputFormat::kSpv, tint::fuzzers::ReaderWriterFuzzer fuzzer(InputFormat::kSpv,
OutputFormat::kSpv); OutputFormat::kSpv);
fuzzer.SetOptionsSpirv(options); fuzzer.SetOptionsSpirv(options);

View File

@ -21,9 +21,9 @@ namespace tint {
namespace fuzzers { namespace fuzzers {
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
DataBuilder b(data, size); DataBuilder db(data, size);
writer::msl::Options options; writer::msl::Options options;
GenerateMslOptions(&b, &options); GenerateMslOptions(&db, &options);
tint::fuzzers::ReaderWriterFuzzer fuzzer(InputFormat::kWGSL, tint::fuzzers::ReaderWriterFuzzer fuzzer(InputFormat::kWGSL,
OutputFormat::kMSL); OutputFormat::kMSL);
fuzzer.SetOptionsMsl(options); fuzzer.SetOptionsMsl(options);

View File

@ -21,9 +21,9 @@ namespace tint {
namespace fuzzers { namespace fuzzers {
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
DataBuilder b(data, size); DataBuilder db(data, size);
writer::spirv::Options options; writer::spirv::Options options;
GenerateSpirvOptions(&b, &options); GenerateSpirvOptions(&db, &options);
tint::fuzzers::ReaderWriterFuzzer fuzzer(InputFormat::kWGSL, tint::fuzzers::ReaderWriterFuzzer fuzzer(InputFormat::kWGSL,
OutputFormat::kSpv); OutputFormat::kSpv);
fuzzer.SetOptionsSpirv(options); fuzzer.SetOptionsSpirv(options);

View File

@ -29,16 +29,22 @@ namespace fuzzers {
/// manager. /// manager.
class TransformBuilder { class TransformBuilder {
public: public:
/// @brief Initialize the data source using a data buffer as a seed /// @brief Initializes the internal builder using a seed value
/// @param data - pointer to a data buffer to use as a seed /// @param seed - seed value passed to engine
explicit TransformBuilder(uint64_t seed) : builder_(seed) {}
/// @brief Initializes the internal builder using seed data
/// @param data - data fuzzer to calculate seed from
/// @param size - size of data buffer /// @param size - size of data buffer
explicit TransformBuilder(const uint8_t* data, size_t size) explicit TransformBuilder(const uint8_t* data, size_t size)
: builder_(data, size) {} : builder_(data, size) {
~TransformBuilder() {} assert(data != nullptr && "|data| must be !nullptr");
}
~TransformBuilder() = default;
transform::Manager* manager() { return &manager_; } transform::Manager* manager() { return &manager_; }
transform::DataMap* data_map() { return &data_map_; } transform::DataMap* data_map() { return &data_map_; }
DataBuilder* builder() { return &builder_; }
/// Adds a transform and needed data to |manager_| and |data_map_|. /// Adds a transform and needed data to |manager_| and |data_map_|.
/// @tparam T - A class that inherits from transform::Transform and has an /// @tparam T - A class that inherits from transform::Transform and has an
@ -66,6 +72,8 @@ class TransformBuilder {
transform::Manager manager_; transform::Manager manager_;
transform::DataMap data_map_; transform::DataMap data_map_;
DataBuilder* builder() { return &builder_; }
/// Implementation of AddTransform, specialized for each transform that is /// Implementation of AddTransform, specialized for each transform that is
/// implemented. Default implementation intentionally deleted to cause compile /// implemented. Default implementation intentionally deleted to cause compile
/// error if unimplemented type passed in. /// error if unimplemented type passed in.