transformation_permute_function_parameters.cpp 6.1 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 <unordered_set>
  15. #include <vector>
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/transformation_permute_function_parameters.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. TransformationPermuteFunctionParameters::
  21. TransformationPermuteFunctionParameters(
  22. const spvtools::fuzz::protobufs::
  23. TransformationPermuteFunctionParameters& message)
  24. : message_(message) {}
  25. TransformationPermuteFunctionParameters::
  26. TransformationPermuteFunctionParameters(
  27. uint32_t function_id, uint32_t new_type_id,
  28. const std::vector<uint32_t>& permutation) {
  29. message_.set_function_id(function_id);
  30. message_.set_new_type_id(new_type_id);
  31. for (auto index : permutation) {
  32. message_.add_permutation(index);
  33. }
  34. }
  35. bool TransformationPermuteFunctionParameters::IsApplicable(
  36. opt::IRContext* context, const FactManager& /*unused*/) const {
  37. // Check that function exists
  38. const auto* function =
  39. fuzzerutil::FindFunction(context, message_.function_id());
  40. if (!function || function->DefInst().opcode() != SpvOpFunction ||
  41. fuzzerutil::FunctionIsEntryPoint(context, function->result_id())) {
  42. return false;
  43. }
  44. // Check that permutation has valid indices
  45. const auto* function_type = fuzzerutil::GetFunctionType(context, function);
  46. assert(function_type && "Function type is null");
  47. const auto& permutation = message_.permutation();
  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 all indices are valid
  55. // and unique integers from the [0, n-1] set
  56. std::unordered_set<uint32_t> unique_indices;
  57. for (auto index : permutation) {
  58. // We don't compare |index| with 0 since it's an unsigned integer
  59. if (index >= arg_size) {
  60. return false;
  61. }
  62. unique_indices.insert(index);
  63. }
  64. // Check that permutation doesn't have duplicated values
  65. assert(unique_indices.size() == arg_size && "Permutation has duplicates");
  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 = 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* context, FactManager* /*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(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. 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. context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
  143. }
  144. protobufs::Transformation TransformationPermuteFunctionParameters::ToMessage()
  145. const {
  146. protobufs::Transformation result;
  147. *result.mutable_permute_function_parameters() = message_;
  148. return result;
  149. }
  150. } // namespace fuzz
  151. } // namespace spvtools