2021-07-28 11:11:26 +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_REGEX_FUZZER_WGSL_MUTATOR_H_
|
|
|
|
#define FUZZERS_TINT_REGEX_FUZZER_WGSL_MUTATOR_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-09-03 00:59:35 +00:00
|
|
|
#include "fuzzers/random_generator.h"
|
|
|
|
|
2021-07-28 11:11:26 +00:00
|
|
|
namespace tint {
|
|
|
|
namespace fuzzers {
|
|
|
|
namespace regex_fuzzer {
|
|
|
|
|
|
|
|
/// A function that given a delimiter, returns a vector that contains
|
|
|
|
/// all the positions of the delimiter in the WGSL code.
|
|
|
|
/// @param delimiter - the delimiter of the enclosed region.
|
|
|
|
/// @param wgsl_code - the initial string (WGSL code) that will be mutated.
|
|
|
|
/// @return a vector with the positions of the delimiter in the WGSL code.
|
|
|
|
std::vector<size_t> FindDelimiterIndices(const std::string& delimiter,
|
|
|
|
const std::string& wgsl_code);
|
|
|
|
|
2021-08-04 07:12:20 +00:00
|
|
|
/// A function that finds all the identifiers in a WGSL-like string.
|
|
|
|
/// @param wgsl_code - the WGSL-like string where the identifiers will be found.
|
|
|
|
/// @return a vector with the positions and the length of all the
|
|
|
|
/// identifiers in wgsl_code.
|
|
|
|
std::vector<std::pair<size_t, size_t>> GetIdentifiers(
|
|
|
|
const std::string& wgsl_code);
|
|
|
|
|
2021-08-05 22:24:20 +00:00
|
|
|
/// A function that returns returns the starting position
|
|
|
|
/// and the length of all the integer literals in a WGSL-like string.
|
|
|
|
/// @param wgsl_code - the WGSL-like string where the int literals
|
|
|
|
/// will be found.
|
|
|
|
/// @return a vector with the starting positions and the length
|
|
|
|
/// of all the integer literals.
|
|
|
|
std::vector<std::pair<size_t, size_t>> GetIntLiterals(
|
|
|
|
const std::string& wgsl_code);
|
|
|
|
|
2021-07-28 11:11:26 +00:00
|
|
|
/// Given 4 indices, idx1, idx2, idx3 and idx4 it swaps the regions
|
2021-08-02 20:56:39 +00:00
|
|
|
/// in the interval (idx1, idx2] with the region in the interval (idx3, idx4]
|
2021-07-28 11:11:26 +00:00
|
|
|
/// in wgsl_text.
|
|
|
|
/// @param idx1 - starting index of the first region.
|
2021-08-04 07:12:20 +00:00
|
|
|
/// @param reg1_len - length of the first region.
|
|
|
|
/// @param idx2 - starting index of the second region.
|
|
|
|
/// @param reg2_len - length of the second region.
|
2021-07-28 11:11:26 +00:00
|
|
|
/// @param wgsl_code - the string where the swap will occur.
|
|
|
|
void SwapIntervals(size_t idx1,
|
2021-08-04 07:12:20 +00:00
|
|
|
size_t reg1_len,
|
2021-07-28 11:11:26 +00:00
|
|
|
size_t idx2,
|
2021-08-04 07:12:20 +00:00
|
|
|
size_t reg2_len,
|
2021-07-29 15:40:57 +00:00
|
|
|
std::string& wgsl_code);
|
2021-07-28 11:11:26 +00:00
|
|
|
|
2021-08-04 07:12:20 +00:00
|
|
|
/// Given index idx1 it delets the region of length interval_len
|
|
|
|
/// starting at index idx1;
|
2021-07-29 15:40:57 +00:00
|
|
|
/// @param idx1 - starting index of the first region.
|
2021-08-04 07:12:20 +00:00
|
|
|
/// @param reg_len - terminating index of the second region.
|
2021-07-29 15:40:57 +00:00
|
|
|
/// @param wgsl_code - the string where the swap will occur.
|
2021-08-04 07:12:20 +00:00
|
|
|
void DeleteInterval(size_t idx1, size_t reg_len, std::string& wgsl_code);
|
2021-07-29 15:40:57 +00:00
|
|
|
|
2021-08-04 07:12:20 +00:00
|
|
|
/// Given 2 indices, idx1, idx2, it inserts the region of length
|
|
|
|
/// reg1_len starting at idx1 after idx2.
|
2021-07-29 15:40:57 +00:00
|
|
|
/// @param idx1 - starting index of region.
|
2021-08-04 07:12:20 +00:00
|
|
|
/// @param reg1_len - length of the region.
|
|
|
|
/// @param idx2 - the position where the region will be inserted.
|
2021-07-29 15:40:57 +00:00
|
|
|
/// @param wgsl_code - the string where the swap will occur.
|
|
|
|
void DuplicateInterval(size_t idx1,
|
2021-08-04 07:12:20 +00:00
|
|
|
size_t reg1_len,
|
2021-07-29 15:40:57 +00:00
|
|
|
size_t idx2,
|
|
|
|
std::string& wgsl_code);
|
|
|
|
|
2021-08-04 07:12:20 +00:00
|
|
|
/// Replaces a region of a WGSL-like string of length id2_len starting
|
|
|
|
/// at position idx2 with a region of length id1_len starting at
|
|
|
|
/// position idx1.
|
2021-08-05 22:24:20 +00:00
|
|
|
/// @param idx1 - starting position of the first region.
|
|
|
|
/// @param id1_len - length of the first region.
|
|
|
|
/// @param idx2 - starting position of the second region.
|
|
|
|
/// @param id2_len - length of the second region.
|
|
|
|
/// @param wgsl_code - the string where the replacement will occur.
|
2021-08-04 07:12:20 +00:00
|
|
|
void ReplaceRegion(size_t idx1,
|
|
|
|
size_t id1_len,
|
|
|
|
size_t idx2,
|
|
|
|
size_t id2_len,
|
|
|
|
std::string& wgsl_code);
|
|
|
|
|
2021-08-11 14:10:05 +00:00
|
|
|
/// Replaces an interval of length `length` starting at start_index
|
|
|
|
/// with the `replacement_text`.
|
2021-08-05 22:24:20 +00:00
|
|
|
/// @param start_index - starting position of the interval to be replaced.
|
2021-08-11 13:02:45 +00:00
|
|
|
/// @param length - length of the interval to be replaced.
|
2021-08-05 22:24:20 +00:00
|
|
|
/// @param replacement_text - the interval that will be used as a replacement.
|
|
|
|
/// @param wgsl_code - the WGSL-like string where the replacement will occur.
|
|
|
|
void ReplaceInterval(size_t start_index,
|
|
|
|
size_t length,
|
|
|
|
std::string replacement_text,
|
|
|
|
std::string& wgsl_code);
|
|
|
|
|
2021-07-29 15:40:57 +00:00
|
|
|
/// A function that, given WGSL-like string and a delimiter,
|
|
|
|
/// generates another WGSL-like string by picking two random regions
|
|
|
|
/// enclosed by the delimiter and swapping them.
|
|
|
|
/// @param delimiter - the delimiter that will be used to find enclosed regions.
|
2021-07-28 11:11:26 +00:00
|
|
|
/// @param wgsl_code - the initial string (WGSL code) that will be mutated.
|
2021-07-29 15:40:57 +00:00
|
|
|
/// @param generator - the random number generator.
|
|
|
|
/// @return true if a swap happened or false otherwise.
|
|
|
|
bool SwapRandomIntervals(const std::string& delimiter,
|
|
|
|
std::string& wgsl_code,
|
2021-09-03 00:59:35 +00:00
|
|
|
RandomGenerator& generator);
|
2021-07-29 15:40:57 +00:00
|
|
|
|
|
|
|
/// A function that, given a WGSL-like string and a delimiter,
|
|
|
|
/// generates another WGSL-like string by deleting a random
|
|
|
|
/// region enclosed by the delimiter.
|
2021-07-28 11:11:26 +00:00
|
|
|
/// @param delimiter - the delimiter that will be used to find enclosed regions.
|
2021-07-29 15:40:57 +00:00
|
|
|
/// @param wgsl_code - the initial string (WGSL code) that will be mutated.
|
|
|
|
/// @param generator - the random number generator.
|
|
|
|
/// @return true if a deletion happened or false otherwise.
|
|
|
|
bool DeleteRandomInterval(const std::string& delimiter,
|
|
|
|
std::string& wgsl_code,
|
2021-09-03 00:59:35 +00:00
|
|
|
RandomGenerator& generator);
|
2021-07-29 15:40:57 +00:00
|
|
|
|
|
|
|
/// A function that, given a WGSL-like string and a delimiter,
|
|
|
|
/// generates another WGSL-like string by duplicating a random
|
|
|
|
/// region enclosed by the delimiter.
|
|
|
|
/// @param delimiter - the delimiter that will be used to find enclosed regions.
|
|
|
|
/// @param wgsl_code - the initial string (WGSL code) that will be mutated.
|
2021-07-28 11:11:26 +00:00
|
|
|
/// @param generator - the random number generator.
|
2021-07-29 15:40:57 +00:00
|
|
|
/// @return true if a duplication happened or false otherwise.
|
|
|
|
bool DuplicateRandomInterval(const std::string& delimiter,
|
|
|
|
std::string& wgsl_code,
|
2021-09-03 00:59:35 +00:00
|
|
|
RandomGenerator& generator);
|
2021-07-28 11:11:26 +00:00
|
|
|
|
2021-08-05 22:24:20 +00:00
|
|
|
/// Replaces a randomly-chosen identifier in wgsl_code.
|
2021-08-04 07:12:20 +00:00
|
|
|
/// @param wgsl_code - WGSL-like string where the replacement will occur.
|
|
|
|
/// @param generator - the random number generator.
|
|
|
|
/// @return true if a replacement happened or false otherwise.
|
2021-09-03 00:59:35 +00:00
|
|
|
bool ReplaceRandomIdentifier(std::string& wgsl_code,
|
|
|
|
RandomGenerator& generator);
|
2021-08-04 07:12:20 +00:00
|
|
|
|
2021-08-05 22:24:20 +00:00
|
|
|
/// Replaces the value of a randomly-chosen integer with one of
|
|
|
|
/// the values in the set {INT_MAX, INT_MIN, 0, -1}.
|
|
|
|
/// @param wgsl_code - WGSL-like string where the replacement will occur.
|
|
|
|
/// @param generator - the random number generator.
|
|
|
|
/// @return true if a replacement happened or false otherwise.
|
2021-09-03 00:59:35 +00:00
|
|
|
bool ReplaceRandomIntLiteral(std::string& wgsl_code,
|
|
|
|
RandomGenerator& generator);
|
2021-08-05 22:24:20 +00:00
|
|
|
|
2021-07-28 11:11:26 +00:00
|
|
|
} // namespace regex_fuzzer
|
|
|
|
} // namespace fuzzers
|
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // FUZZERS_TINT_REGEX_FUZZER_WGSL_MUTATOR_H_
|