transformation_permute_function_parameters.cpp 5.9 KB

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