fuzzer_pass_permute_function_parameters.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <numeric>
  15. #include <vector>
  16. #include "source/fuzz/fuzzer_context.h"
  17. #include "source/fuzz/fuzzer_pass_permute_function_parameters.h"
  18. #include "source/fuzz/fuzzer_util.h"
  19. #include "source/fuzz/instruction_descriptor.h"
  20. #include "source/fuzz/transformation_permute_function_parameters.h"
  21. namespace spvtools {
  22. namespace fuzz {
  23. FuzzerPassPermuteFunctionParameters::FuzzerPassPermuteFunctionParameters(
  24. opt::IRContext* ir_context, TransformationContext* transformation_context,
  25. FuzzerContext* fuzzer_context,
  26. protobufs::TransformationSequence* transformations)
  27. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  28. transformations) {}
  29. FuzzerPassPermuteFunctionParameters::~FuzzerPassPermuteFunctionParameters() =
  30. default;
  31. void FuzzerPassPermuteFunctionParameters::Apply() {
  32. for (const auto& function : *GetIRContext()->module()) {
  33. uint32_t function_id = function.result_id();
  34. // Skip the function if it is an entry point
  35. if (fuzzerutil::FunctionIsEntryPoint(GetIRContext(), function_id)) {
  36. continue;
  37. }
  38. if (!GetFuzzerContext()->ChoosePercentage(
  39. GetFuzzerContext()->GetChanceOfPermutingParameters())) {
  40. continue;
  41. }
  42. // Compute permutation for parameters
  43. auto* function_type =
  44. fuzzerutil::GetFunctionType(GetIRContext(), &function);
  45. assert(function_type && "Function type is null");
  46. // Don't take return type into account
  47. uint32_t arg_size = function_type->NumInOperands() - 1;
  48. // Create a vector, fill it with [0, n-1] values and shuffle it
  49. std::vector<uint32_t> permutation(arg_size);
  50. std::iota(permutation.begin(), permutation.end(), 0);
  51. GetFuzzerContext()->Shuffle(&permutation);
  52. // Create a new OpFunctionType instruction with permuted arguments
  53. // if needed
  54. auto result_type_id = function_type->GetSingleWordInOperand(0);
  55. std::vector<uint32_t> argument_ids;
  56. for (auto index : permutation) {
  57. // +1 to take function's return type into account
  58. argument_ids.push_back(function_type->GetSingleWordInOperand(index + 1));
  59. }
  60. // Apply our transformation
  61. ApplyTransformation(TransformationPermuteFunctionParameters(
  62. function_id, FindOrCreateFunctionType(result_type_id, argument_ids),
  63. permutation));
  64. }
  65. }
  66. } // namespace fuzz
  67. } // namespace spvtools