fuzzer_pass_permute_function_variables.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (c) 2021 Mostafa Ashraf
  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 "source/fuzz/fuzzer_pass_permute_function_variables.h"
  15. #include <algorithm>
  16. #include <numeric>
  17. #include <vector>
  18. #include "source/fuzz/fuzzer_context.h"
  19. #include "source/fuzz/fuzzer_util.h"
  20. #include "source/fuzz/instruction_descriptor.h"
  21. #include "source/fuzz/transformation_swap_function_variables.h"
  22. namespace spvtools {
  23. namespace fuzz {
  24. FuzzerPassPermuteFunctionVariables::FuzzerPassPermuteFunctionVariables(
  25. opt::IRContext* ir_context, TransformationContext* transformation_context,
  26. FuzzerContext* fuzzer_context,
  27. protobufs::TransformationSequence* transformations,
  28. bool ignore_inapplicable_transformations)
  29. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  30. transformations, ignore_inapplicable_transformations) {
  31. } // Here we call parent constructor.
  32. void FuzzerPassPermuteFunctionVariables::Apply() {
  33. // Permuting OpVariable instructions in each function.
  34. for (auto& function : *GetIRContext()->module()) {
  35. if (!GetFuzzerContext()->ChoosePercentage(
  36. GetFuzzerContext()->GetChanceOfPermutingFunctionVariables())) {
  37. continue;
  38. }
  39. auto first_block = function.entry().get();
  40. std::vector<opt::Instruction*> variables;
  41. for (auto& instruction : *first_block) {
  42. if (instruction.opcode() == spv::Op::OpVariable) {
  43. variables.push_back(&instruction);
  44. }
  45. }
  46. if (variables.size() <= 1) {
  47. continue;
  48. }
  49. do {
  50. uint32_t instruction_1_index = GetFuzzerContext()->RandomIndex(variables);
  51. uint32_t instruction_2_index = GetFuzzerContext()->RandomIndex(variables);
  52. if (instruction_1_index != instruction_2_index) {
  53. ApplyTransformation(TransformationSwapFunctionVariables(
  54. variables[instruction_1_index]->result_id(),
  55. variables[instruction_2_index]->result_id()));
  56. }
  57. } while (GetFuzzerContext()->ChoosePercentage(
  58. GetFuzzerContext()
  59. ->GetChanceOfSwappingAnotherPairOfFunctionVariables()) &&
  60. variables.size() > 2);
  61. }
  62. }
  63. } // namespace fuzz
  64. } // namespace spvtools