From 5e6d4577fdc644bb2b695fa6eb768b0d5f73d962 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Wed, 22 Sep 2021 17:43:06 +0000 Subject: [PATCH] Remove excess copy from fuzzer random number generation code Adds limited ability to hash C-style buffers, so that the seed can be directly calculated on the provided input, instead of converting it to a vector. BUG=tint:1161 Change-Id: I1b9b0805665436a3242d5918fb563242b91b0f09 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/63420 Auto-Submit: Ryan Harrison Kokoro: Kokoro Commit-Queue: Ryan Harrison Reviewed-by: Ben Clayton --- fuzzers/random_generator.cc | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/fuzzers/random_generator.cc b/fuzzers/random_generator.cc index 73c594c5b2..93c7f4ce4b 100644 --- a/fuzzers/random_generator.cc +++ b/fuzzers/random_generator.cc @@ -38,6 +38,22 @@ I RandomUInt(std::mt19937* engine, I lower, I upper) { return std::uniform_int_distribution(lower, upper - 1)(*engine); } +/// Calculate the hash for the contents of a c-style data buffer +/// This is intentionally not implemented as a generic override of HashCombine +/// in "src/utils/hash.h", because it conflicts with the vardiac override for +/// the case where a pointer and an integer are being hashed. +/// @param data - pointer to buffer to be hashed +/// @param size - number of elements in buffer +/// @returns hash of the data in the buffer +size_t HashBuffer(const uint8_t* data, const size_t size) { + size_t hash = 102931; + utils::HashCombine(&hash, size); + for (size_t i = 0; i < size; i++) { + utils::HashCombine(&hash, data[i]); + } + return hash; +} + } // namespace RandomGenerator::RandomGenerator(uint64_t seed) : engine_(seed) {} @@ -107,9 +123,7 @@ uint64_t RandomGenerator::CalculateSeed(const uint8_t* data, size_t size) { std::max(hash_begin_i64 + kHashDesiredMaxBytes, size_i64); size_t hash_begin = static_cast(hash_begin_i64); size_t hash_size = static_cast(hash_end_i64) - hash_begin; - std::vector hash_portion(data + hash_begin, - data + hash_begin + hash_size + 1); - return tint::utils::Hash(hash_portion); + return HashBuffer(data + hash_begin, hash_size); } } // namespace fuzzers