fuzzer_pass_permute_function_parameters.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // Apply our transformation
  53. ApplyTransformation(TransformationPermuteFunctionParameters(
  54. function_id, GetFuzzerContext()->GetFreshId(), permutation));
  55. }
  56. }
  57. } // namespace fuzz
  58. } // namespace spvtools