transformation_permute_function_parameters.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <vector>
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/transformation_permute_function_parameters.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 new_type_id,
  27. const std::vector<uint32_t>& permutation) {
  28. message_.set_function_id(function_id);
  29. message_.set_new_type_id(new_type_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. // Check that new function's type is valid:
  67. // - Has the same number of operands
  68. // - Has the same result type as the old one
  69. // - Order of arguments is permuted
  70. auto new_type_id = message_.new_type_id();
  71. const auto* new_type = ir_context->get_def_use_mgr()->GetDef(new_type_id);
  72. if (!new_type || new_type->opcode() != SpvOpTypeFunction ||
  73. new_type->NumInOperands() != function_type->NumInOperands()) {
  74. return false;
  75. }
  76. // Check that both instructions have the same result type
  77. if (new_type->GetSingleWordInOperand(0) !=
  78. function_type->GetSingleWordInOperand(0)) {
  79. return false;
  80. }
  81. // Check that new function type has its arguments permuted
  82. for (int i = 0, n = static_cast<int>(permutation.size()); i < n; ++i) {
  83. // +1 to take return type into account
  84. if (new_type->GetSingleWordInOperand(i + 1) !=
  85. function_type->GetSingleWordInOperand(permutation[i] + 1)) {
  86. return false;
  87. }
  88. }
  89. return true;
  90. }
  91. void TransformationPermuteFunctionParameters::Apply(
  92. opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
  93. // Retrieve all data from the message
  94. uint32_t function_id = message_.function_id();
  95. uint32_t new_type_id = message_.new_type_id();
  96. const auto& permutation = message_.permutation();
  97. // Find the function that will be transformed
  98. auto* function = fuzzerutil::FindFunction(ir_context, function_id);
  99. assert(function && "Can't find the function");
  100. // Change function's type
  101. function->DefInst().SetInOperand(1, {new_type_id});
  102. // Adjust OpFunctionParameter instructions
  103. // Collect ids and types from OpFunctionParameter instructions
  104. std::vector<uint32_t> param_id, param_type;
  105. function->ForEachParam(
  106. [&param_id, &param_type](const opt::Instruction* param) {
  107. param_id.push_back(param->result_id());
  108. param_type.push_back(param->type_id());
  109. });
  110. // Permute parameters' ids and types
  111. std::vector<uint32_t> permuted_param_id, permuted_param_type;
  112. for (auto index : permutation) {
  113. permuted_param_id.push_back(param_id[index]);
  114. permuted_param_type.push_back(param_type[index]);
  115. }
  116. // Set OpFunctionParameter instructions to point to new parameters
  117. size_t i = 0;
  118. function->ForEachParam(
  119. [&i, &permuted_param_id, &permuted_param_type](opt::Instruction* param) {
  120. param->SetResultType(permuted_param_type[i]);
  121. param->SetResultId(permuted_param_id[i]);
  122. ++i;
  123. });
  124. // Fix all OpFunctionCall instructions
  125. ir_context->get_def_use_mgr()->ForEachUser(
  126. &function->DefInst(),
  127. [function_id, &permutation](opt::Instruction* call) {
  128. if (call->opcode() != SpvOpFunctionCall ||
  129. call->GetSingleWordInOperand(0) != function_id) {
  130. return;
  131. }
  132. opt::Instruction::OperandList call_operands = {
  133. call->GetInOperand(0) // Function id
  134. };
  135. for (auto index : permutation) {
  136. // Take function id into account
  137. call_operands.push_back(call->GetInOperand(index + 1));
  138. }
  139. call->SetInOperands(std::move(call_operands));
  140. });
  141. // Make sure our changes are analyzed
  142. ir_context->InvalidateAnalysesExceptFor(
  143. opt::IRContext::Analysis::kAnalysisNone);
  144. }
  145. protobufs::Transformation TransformationPermuteFunctionParameters::ToMessage()
  146. const {
  147. protobufs::Transformation result;
  148. *result.mutable_permute_function_parameters() = message_;
  149. return result;
  150. }
  151. } // namespace fuzz
  152. } // namespace spvtools