1fd4e5da5Sopenharmony_ci// Copyright (c) 2021 Google Inc.
2fd4e5da5Sopenharmony_ci//
3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License.
5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at
6fd4e5da5Sopenharmony_ci//
7fd4e5da5Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8fd4e5da5Sopenharmony_ci//
9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and
13fd4e5da5Sopenharmony_ci// limitations under the License.
14fd4e5da5Sopenharmony_ci
15fd4e5da5Sopenharmony_ci#ifndef TEST_FUZZERS_RANDOM_GENERATOR_H_
16fd4e5da5Sopenharmony_ci#define TEST_FUZZERS_RANDOM_GENERATOR_H_
17fd4e5da5Sopenharmony_ci
18fd4e5da5Sopenharmony_ci#include <cstdint>
19fd4e5da5Sopenharmony_ci#include <random>
20fd4e5da5Sopenharmony_ci
21fd4e5da5Sopenharmony_ci#include "source/spirv_target_env.h"
22fd4e5da5Sopenharmony_ci
23fd4e5da5Sopenharmony_cinamespace spvtools {
24fd4e5da5Sopenharmony_cinamespace fuzzers {
25fd4e5da5Sopenharmony_ci
26fd4e5da5Sopenharmony_ci/// Pseudo random generator utility class for fuzzing
27fd4e5da5Sopenharmony_ciclass RandomGenerator {
28fd4e5da5Sopenharmony_ci public:
29fd4e5da5Sopenharmony_ci  /// @brief Initializes the internal engine
30fd4e5da5Sopenharmony_ci  /// @param seed - seed value passed to engine
31fd4e5da5Sopenharmony_ci  explicit RandomGenerator(uint64_t seed);
32fd4e5da5Sopenharmony_ci
33fd4e5da5Sopenharmony_ci  /// @brief Initializes the internal engine
34fd4e5da5Sopenharmony_ci  /// @param data - data to calculate the seed from
35fd4e5da5Sopenharmony_ci  /// @param size - size of the data
36fd4e5da5Sopenharmony_ci  explicit RandomGenerator(const uint8_t* data, size_t size);
37fd4e5da5Sopenharmony_ci
38fd4e5da5Sopenharmony_ci  ~RandomGenerator() {}
39fd4e5da5Sopenharmony_ci
40fd4e5da5Sopenharmony_ci  /// Calculate a seed value based on a blob of data.
41fd4e5da5Sopenharmony_ci  /// Currently hashes bytes near the front of the buffer, after skipping N
42fd4e5da5Sopenharmony_ci  /// bytes.
43fd4e5da5Sopenharmony_ci  /// @param data - pointer to data to base calculation off of, must be !nullptr
44fd4e5da5Sopenharmony_ci  /// @param size - number of elements in |data|, must be > 0
45fd4e5da5Sopenharmony_ci  static uint64_t CalculateSeed(const uint8_t* data, size_t size);
46fd4e5da5Sopenharmony_ci
47fd4e5da5Sopenharmony_ci  /// Get random valid target env.
48fd4e5da5Sopenharmony_ci  spv_target_env GetTargetEnv();
49fd4e5da5Sopenharmony_ci
50fd4e5da5Sopenharmony_ci  /// Get uint32_t value from uniform distribution.
51fd4e5da5Sopenharmony_ci  /// @param lower - lower bound of integer generated
52fd4e5da5Sopenharmony_ci  /// @param upper - upper bound of integer generated
53fd4e5da5Sopenharmony_ci  /// @returns i, where lower <= i < upper
54fd4e5da5Sopenharmony_ci  uint32_t GetUInt32(uint32_t lower, uint32_t upper);
55fd4e5da5Sopenharmony_ci
56fd4e5da5Sopenharmony_ci  /// Get uint32_t value from uniform distribution.
57fd4e5da5Sopenharmony_ci  /// @param bound - Upper bound of integer generated
58fd4e5da5Sopenharmony_ci  /// @returns i, where 0 <= i < bound
59fd4e5da5Sopenharmony_ci  uint32_t GetUInt32(uint32_t bound);
60fd4e5da5Sopenharmony_ci
61fd4e5da5Sopenharmony_ci private:
62fd4e5da5Sopenharmony_ci  std::mt19937_64 engine_;
63fd4e5da5Sopenharmony_ci};  // class RandomGenerator
64fd4e5da5Sopenharmony_ci
65fd4e5da5Sopenharmony_ci}  // namespace fuzzers
66fd4e5da5Sopenharmony_ci}  // namespace spvtools
67fd4e5da5Sopenharmony_ci
68fd4e5da5Sopenharmony_ci#endif  // TEST_FUZZERS_RANDOM_GENERATOR_UTILS_H_
69