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 <rharrison@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ryan Harrison 2021-09-22 17:43:06 +00:00 committed by Tint LUCI CQ
parent a617d0f0fc
commit 5e6d4577fd
1 changed files with 17 additions and 3 deletions

View File

@ -38,6 +38,22 @@ I RandomUInt(std::mt19937* engine, I lower, I upper) {
return std::uniform_int_distribution<I>(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<size_t>(hash_begin_i64);
size_t hash_size = static_cast<size_t>(hash_end_i64) - hash_begin;
std::vector<uint8_t> 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