| 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/transformation_permute_function_parameters.h" |
| 16 |
|
| 17 |
#include <vector> |
| 18 |
|
| 19 |
#include "source/fuzz/fuzzer_util.h" |
| 20 |
|
| 21 |
namespace spvtools { |
| 22 |
namespace fuzz { |
| 23 |
|
| 24 |
TransformationPermuteFunctionParameters:: |
| 25 |
TransformationPermuteFunctionParameters( |
| 26 |
const spvtools::fuzz::protobufs:: |
| 27 |
TransformationPermuteFunctionParameters& message) |
| 28 |
: message_(message) {} |
| 29 |
|
| 30 |
TransformationPermuteFunctionParameters:: |
| 31 |
TransformationPermuteFunctionParameters( |
| 32 |
uint32_t function_id, uint32_t function_type_fresh_id, |
| 33 |
const std::vector<uint32_t>& permutation) { |
| 34 |
message_.set_function_id(function_id); |
| 35 |
message_.set_function_type_fresh_id(function_type_fresh_id); |
| 36 |
|
| 37 |
for (auto index : permutation) { |
| 38 |
message_.add_permutation(index); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
bool TransformationPermuteFunctionParameters::IsApplicable( |
| 43 |
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const { |
| 44 |
// Check that function exists |
| 45 |
const auto* function = |
| 46 |
fuzzerutil::FindFunction(ir_context, message_.function_id()); |
| 47 |
if (!function || function->DefInst().opcode() != SpvOpFunction || |
| 48 |
fuzzerutil::FunctionIsEntryPoint(ir_context, function->result_id())) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
// Check that permutation has valid indices |
| 53 |
const auto* function_type = fuzzerutil::GetFunctionType(ir_context, function); |
| 54 |
assert(function_type && "Function type is null"); |
| 55 |
|
| 56 |
std::vector<uint32_t> permutation(message_.permutation().begin(), |
| 57 |
message_.permutation().end()); |
| 58 |
|
| 59 |
// Don't take return type into account |
| 60 |
auto arg_size = function_type->NumInOperands() - 1; |
| 61 |
|
| 62 |
// |permutation| vector should be equal to the number of arguments |
| 63 |
if (static_cast<uint32_t>(permutation.size()) != arg_size) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
// Check that permutation doesn't have duplicated values. |
| 68 |
assert(!fuzzerutil::HasDuplicates(permutation) && |
| 69 |
"Permutation has duplicates"); |
| 70 |
|
| 71 |
// Check that elements in permutation are in range [0, arg_size - 1]. |
| 72 |
// |
| 73 |
// We must check whether the permutation is empty first because in that case |
| 74 |
// |arg_size - 1| will produce |std::numeric_limits<uint32_t>::max()| since |
| 75 |
// it's an unsigned integer. |
| 76 |
if (!permutation.empty() && |
| 77 |
!fuzzerutil::IsPermutationOfRange(permutation, 0, arg_size - 1)) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
return fuzzerutil::IsFreshId(ir_context, message_.function_type_fresh_id()); |
| 82 |
} |
| 83 |
|
| 84 |
void TransformationPermuteFunctionParameters::Apply( |
| 85 |
opt::IRContext* ir_context, TransformationContext* /*unused*/) const { |
| 86 |
// Find the function that will be transformed |
| 87 |
auto* function = fuzzerutil::FindFunction(ir_context, message_.function_id()); |
| 88 |
assert(function && "Can't find the function"); |
| 89 |
|
| 90 |
// Adjust OpFunctionParameter instructions |
| 91 |
|
| 92 |
// Collect ids and types from OpFunctionParameter instructions |
| 93 |
std::vector<uint32_t> param_id, param_type; |
| 94 |
function->ForEachParam( |
| 95 |
[¶m_id, ¶m_type](const opt::Instruction* param) { |
| 96 |
param_id.push_back(param->result_id()); |
| 97 |
param_type.push_back(param->type_id()); |
| 98 |
}); |
| 99 |
|
| 100 |
// Permute parameters' ids and types |
| 101 |
std::vector<uint32_t> permuted_param_id, permuted_param_type; |
| 102 |
for (auto index : message_.permutation()) { |
| 103 |
permuted_param_id.push_back(param_id[index]); |
| 104 |
permuted_param_type.push_back(param_type[index]); |
| 105 |
} |
| 106 |
|
| 107 |
// Set OpFunctionParameter instructions to point to new parameters |
| 108 |
size_t i = 0; |
| 109 |
function->ForEachParam( |
| 110 |
[&i, &permuted_param_id, &permuted_param_type](opt::Instruction* param) { |
| 111 |
param->SetResultType(permuted_param_type[i]); |
| 112 |
param->SetResultId(permuted_param_id[i]); |
| 113 |
++i; |
| 114 |
}); |
| 115 |
|
| 116 |
// Fix all OpFunctionCall instructions |
| 117 |
for (auto* call : fuzzerutil::GetCallers(ir_context, function->result_id())) { |
| 118 |
opt::Instruction::OperandList call_operands = { |
| 119 |
call->GetInOperand(0) // Function id |
| 120 |
}; |
| 121 |
|
| 122 |
for (auto index : message_.permutation()) { |
| 123 |
// Take function id into account |
| 124 |
call_operands.push_back(call->GetInOperand(index + 1)); |
| 125 |
} |
| 126 |
|
| 127 |
call->SetInOperands(std::move(call_operands)); |
| 128 |
} |
| 129 |
|
| 130 |
// Update function type. |
| 131 |
{ |
| 132 |
// We use a separate scope here since |old_function_type_inst| might become |
| 133 |
// a dangling pointer after the call to the fuzzerutil::UpdateFunctionType. |
| 134 |
|
| 135 |
auto* old_function_type_inst = |
| 136 |
fuzzerutil::GetFunctionType(ir_context, function); |
| 137 |
assert(old_function_type_inst && "Function must have a valid type"); |
| 138 |
|
| 139 |
std::vector<uint32_t> parameter_type_ids; |
| 140 |
for (auto index : message_.permutation()) { |
| 141 |
// +1 since the first operand to OpTypeFunction is a return type. |
| 142 |
parameter_type_ids.push_back( |
| 143 |
old_function_type_inst->GetSingleWordInOperand(index + 1)); |
| 144 |
} |
| 145 |
|
| 146 |
// Change function's type. |
| 147 |
fuzzerutil::UpdateFunctionType( |
| 148 |
ir_context, function->result_id(), message_.function_type_fresh_id(), |
| 149 |
old_function_type_inst->GetSingleWordInOperand(0), parameter_type_ids); |
| 150 |
} |
| 151 |
|
| 152 |
// Make sure our changes are analyzed |
| 153 |
ir_context->InvalidateAnalysesExceptFor( |
| 154 |
opt::IRContext::Analysis::kAnalysisNone); |
| 155 |
} |
| 156 |
|
| 157 |
protobufs::Transformation TransformationPermuteFunctionParameters::ToMessage() |
| 158 |
const { |
| 159 |
protobufs::Transformation result; |
| 160 |
*result.mutable_permute_function_parameters() = message_; |
| 161 |
return result; |
| 162 |
} |
| 163 |
|
| 164 |
std::unordered_set<uint32_t> |
| 165 |
TransformationPermuteFunctionParameters::GetFreshIds() const { |
| 166 |
return {message_.function_type_fresh_id()}; |
| 167 |
} |
| 168 |
|
| 169 |
} // namespace fuzz |
| 170 |
} // namespace spvtools |