1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "fuzzers/random_generator.h"
16
17 #include <algorithm>
18 #include <cassert>
19 #include <utility>
20
21 #include "fuzzers/mersenne_twister_engine.h"
22 #include "fuzzers/random_generator_engine.h"
23 #include "src/utils/hash.h"
24
25 namespace tint {
26 namespace fuzzers {
27
28 namespace {
29
30 /// Calculate the hash for the contents of a c-style data buffer
31 /// This is intentionally not implemented as a generic override of HashCombine
32 /// in "src/utils/hash.h", because it conflicts with the vardiac override for
33 /// the case where a pointer and an integer are being hashed.
34 /// @param data - pointer to buffer to be hashed
35 /// @param size - number of elements in buffer
36 /// @returns hash of the data in the buffer
HashBuffer(const uint8_t * data,const size_t size)37 size_t HashBuffer(const uint8_t* data, const size_t size) {
38 size_t hash = 102931;
39 utils::HashCombine(&hash, size);
40 for (size_t i = 0; i < size; i++) {
41 utils::HashCombine(&hash, data[i]);
42 }
43 return hash;
44 }
45
46 } // namespace
47
RandomGenerator(std::unique_ptr<RandomGeneratorEngine> engine)48 RandomGenerator::RandomGenerator(std::unique_ptr<RandomGeneratorEngine> engine)
49 : engine_(std::move(engine)) {}
50
RandomGenerator(uint64_t seed)51 RandomGenerator::RandomGenerator(uint64_t seed)
52 : RandomGenerator(std::make_unique<MersenneTwisterEngine>(seed)) {}
53
GetUInt32(uint32_t lower,uint32_t upper)54 uint32_t RandomGenerator::GetUInt32(uint32_t lower, uint32_t upper) {
55 assert(lower < upper && "|lower| must be strictly less than |upper|");
56 return engine_->RandomUInt32(lower, upper);
57 }
58
GetUInt32(uint32_t bound)59 uint32_t RandomGenerator::GetUInt32(uint32_t bound) {
60 assert(bound > 0 && "|bound| must be greater than 0");
61 return engine_->RandomUInt32(0u, bound);
62 }
63
GetUInt64(uint64_t lower,uint64_t upper)64 uint64_t RandomGenerator::GetUInt64(uint64_t lower, uint64_t upper) {
65 assert(lower < upper && "|lower| must be strictly less than |upper|");
66 return engine_->RandomUInt64(lower, upper);
67 }
68
GetUInt64(uint64_t bound)69 uint64_t RandomGenerator::GetUInt64(uint64_t bound) {
70 assert(bound > 0 && "|bound| must be greater than 0");
71 return engine_->RandomUInt64(static_cast<uint64_t>(0), bound);
72 }
73
GetByte()74 uint8_t RandomGenerator::GetByte() {
75 uint8_t result;
76 engine_->RandomNBytes(&result, 1);
77 return result;
78 }
79
Get4Bytes()80 uint32_t RandomGenerator::Get4Bytes() {
81 uint32_t result;
82 engine_->RandomNBytes(reinterpret_cast<uint8_t*>(&result), 4);
83 return result;
84 }
85
GetNBytes(uint8_t * dest,size_t n)86 void RandomGenerator::GetNBytes(uint8_t* dest, size_t n) {
87 assert(dest && "|dest| must not be nullptr");
88 engine_->RandomNBytes(dest, n);
89 }
90
GetBool()91 bool RandomGenerator::GetBool() {
92 return engine_->RandomUInt32(0u, 2u);
93 }
94
GetWeightedBool(uint32_t percentage)95 bool RandomGenerator::GetWeightedBool(uint32_t percentage) {
96 static const uint32_t kMaxPercentage = 100;
97 assert(percentage <= kMaxPercentage &&
98 "|percentage| needs to be within [0, 100]");
99 return engine_->RandomUInt32(0u, kMaxPercentage) < percentage;
100 }
101
CalculateSeed(const uint8_t * data,size_t size)102 uint64_t RandomGenerator::CalculateSeed(const uint8_t* data, size_t size) {
103 assert(data != nullptr && "|data| must be !nullptr");
104
105 // Number of bytes we want to skip at the start of data for the hash.
106 // Fewer bytes may be skipped when `size` is small.
107 // Has lower precedence than kHashDesiredMinBytes.
108 static const int64_t kHashDesiredLeadingSkipBytes = 5;
109 // Minimum number of bytes we want to use in the hash.
110 // Used for short buffers.
111 static const int64_t kHashDesiredMinBytes = 4;
112 // Maximum number of bytes we want to use in the hash.
113 static const int64_t kHashDesiredMaxBytes = 32;
114 auto size_i64 = static_cast<int64_t>(size);
115 auto hash_begin_i64 =
116 std::min(kHashDesiredLeadingSkipBytes,
117 std::max<int64_t>(size_i64 - kHashDesiredMinBytes, 0));
118 auto hash_end_i64 = std::min(hash_begin_i64 + kHashDesiredMaxBytes, size_i64);
119 auto hash_begin = static_cast<size_t>(hash_begin_i64);
120 auto hash_size = static_cast<size_t>(hash_end_i64) - hash_begin;
121 return HashBuffer(data + hash_begin, hash_size);
122 }
123 } // namespace fuzzers
124 } // namespace tint
125