2021-07-13 12:01:25 +00:00
|
|
|
// Copyright 2021 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef FUZZERS_TINT_AST_FUZZER_PROBABILITY_CONTEXT_H_
|
|
|
|
#define FUZZERS_TINT_AST_FUZZER_PROBABILITY_CONTEXT_H_
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-09-03 00:59:35 +00:00
|
|
|
#include "fuzzers/random_generator.h"
|
2021-07-13 12:01:25 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace fuzzers {
|
|
|
|
namespace ast_fuzzer {
|
|
|
|
|
|
|
|
/// This class is intended to be used by the `MutationFinder`s to introduce some
|
|
|
|
/// variance to the mutation process.
|
|
|
|
class ProbabilityContext {
|
|
|
|
public:
|
|
|
|
/// Initializes this instance with a random number generator.
|
2021-09-03 00:59:35 +00:00
|
|
|
/// @param generator - must not be a `nullptr`. Must remain in scope as long
|
|
|
|
/// as this
|
2021-07-13 12:01:25 +00:00
|
|
|
/// instance exists.
|
2021-09-03 00:59:35 +00:00
|
|
|
explicit ProbabilityContext(RandomGenerator* generator);
|
2021-07-13 12:01:25 +00:00
|
|
|
|
2021-09-03 00:59:35 +00:00
|
|
|
/// Get random bool with even odds
|
|
|
|
/// @returns true 50% of the time and false %50 of time.
|
|
|
|
bool RandomBool() { return generator_->GetBool(); }
|
2021-07-13 12:01:25 +00:00
|
|
|
|
2021-09-03 00:59:35 +00:00
|
|
|
/// Get random bool with weighted odds
|
|
|
|
/// @param percentage - likelihood of true being returned
|
|
|
|
/// @returns true |percentage|% of the time, and false (100 - |percentage|)%
|
|
|
|
/// of the time.
|
2021-07-13 12:01:25 +00:00
|
|
|
bool ChoosePercentage(uint32_t percentage) {
|
2021-09-03 00:59:35 +00:00
|
|
|
return generator_->GetWeightedBool(percentage);
|
2021-07-13 12:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a random value in the range `[0; arr.size())`.
|
|
|
|
/// @tparam T - type of the elements in the vector.
|
|
|
|
/// @param arr - may not be empty.
|
|
|
|
/// @return the random index in the `arr`.
|
|
|
|
template <typename T>
|
|
|
|
size_t GetRandomIndex(const std::vector<T>& arr) {
|
2021-09-03 00:59:35 +00:00
|
|
|
return static_cast<size_t>(generator_->GetUInt64(arr.size()));
|
2021-07-13 12:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @return the probability of replacing some identifier with some other one.
|
|
|
|
uint32_t GetChanceOfReplacingIdentifiers() const {
|
|
|
|
return chance_of_replacing_identifiers_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// @param range - a pair of integers `a` and `b` s.t. `a <= b`.
|
|
|
|
/// @return an random number in the range `[a; b]`.
|
|
|
|
uint32_t RandomFromRange(std::pair<uint32_t, uint32_t> range);
|
|
|
|
|
2021-09-03 00:59:35 +00:00
|
|
|
RandomGenerator* generator_;
|
2021-07-13 12:01:25 +00:00
|
|
|
|
|
|
|
uint32_t chance_of_replacing_identifiers_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ast_fuzzer
|
|
|
|
} // namespace fuzzers
|
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // FUZZERS_TINT_AST_FUZZER_PROBABILITY_CONTEXT_H_
|