transformation_permute_function_parameters.cpp 6.5 KB

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