| 1 |
// Copyright (c) 2020 Vasyl Teliman |
| 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 "source/fuzz/fuzzer_pass_permute_function_parameters.h" |
| 16 |
|
| 17 |
#include <numeric> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include "source/fuzz/fuzzer_context.h" |
| 21 |
#include "source/fuzz/fuzzer_util.h" |
| 22 |
#include "source/fuzz/instruction_descriptor.h" |
| 23 |
#include "source/fuzz/transformation_permute_function_parameters.h" |
| 24 |
|
| 25 |
namespace spvtools { |
| 26 |
namespace fuzz { |
| 27 |
|
| 28 |
FuzzerPassPermuteFunctionParameters::FuzzerPassPermuteFunctionParameters( |
| 29 |
opt::IRContext* ir_context, TransformationContext* transformation_context, |
| 30 |
FuzzerContext* fuzzer_context, |
| 31 |
protobufs::TransformationSequence* transformations) |
| 32 |
: FuzzerPass(ir_context, transformation_context, fuzzer_context, |
| 33 |
transformations) {} |
| 34 |
|
| 35 |
FuzzerPassPermuteFunctionParameters::~FuzzerPassPermuteFunctionParameters() = |
| 36 |
default; |
| 37 |
|
| 38 |
void FuzzerPassPermuteFunctionParameters::Apply() { |
| 39 |
for (const auto& function : *GetIRContext()->module()) { |
| 40 |
uint32_t function_id = function.result_id(); |
| 41 |
|
| 42 |
// Skip the function if it is an entry point |
| 43 |
if (fuzzerutil::FunctionIsEntryPoint(GetIRContext(), function_id)) { |
| 44 |
continue; |
| 45 |
} |
| 46 |
|
| 47 |
if (!GetFuzzerContext()->ChoosePercentage( |
| 48 |
GetFuzzerContext()->GetChanceOfPermutingParameters())) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
// Compute permutation for parameters |
| 53 |
auto* function_type = |
| 54 |
fuzzerutil::GetFunctionType(GetIRContext(), &function); |
| 55 |
assert(function_type && "Function type is null"); |
| 56 |
|
| 57 |
// Don't take return type into account |
| 58 |
uint32_t arg_size = function_type->NumInOperands() - 1; |
| 59 |
|
| 60 |
// Create a vector, fill it with [0, n-1] values and shuffle it |
| 61 |
std::vector<uint32_t> permutation(arg_size); |
| 62 |
std::iota(permutation.begin(), permutation.end(), 0); |
| 63 |
GetFuzzerContext()->Shuffle(&permutation); |
| 64 |
|
| 65 |
// Apply our transformation |
| 66 |
ApplyTransformation(TransformationPermuteFunctionParameters( |
| 67 |
function_id, GetFuzzerContext()->GetFreshId(), permutation)); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
} // namespace fuzz |
| 72 |
} // namespace spvtools |