fuzzer_pass_permute_function_parameters.cpp 2.8 KB

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