fuzzer_pass_permute_instructions.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 "source/fuzz/fuzzer_pass_permute_instructions.h"
  15. #include "source/fuzz/fuzzer_context.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_descriptor.h"
  18. #include "source/fuzz/transformation_move_instruction_down.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. FuzzerPassPermuteInstructions::FuzzerPassPermuteInstructions(
  22. opt::IRContext* ir_context, TransformationContext* transformation_context,
  23. FuzzerContext* fuzzer_context,
  24. protobufs::TransformationSequence* transformations,
  25. bool ignore_inapplicable_transformations)
  26. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  27. transformations, ignore_inapplicable_transformations) {}
  28. void FuzzerPassPermuteInstructions::Apply() {
  29. // We are iterating over all instructions in all basic blocks.
  30. for (auto& function : *GetIRContext()->module()) {
  31. for (auto& block : function) {
  32. // We need to collect all instructions in the block into a separate vector
  33. // since application of the transformation below might invalidate
  34. // iterators.
  35. std::vector<opt::Instruction*> instructions;
  36. for (auto& instruction : block) {
  37. instructions.push_back(&instruction);
  38. }
  39. // We consider all instructions in reverse to increase the possible number
  40. // of applied transformations.
  41. for (auto it = instructions.rbegin(); it != instructions.rend(); ++it) {
  42. if (!GetFuzzerContext()->ChoosePercentage(
  43. GetFuzzerContext()->GetChanceOfPermutingInstructions())) {
  44. continue;
  45. }
  46. while (MaybeApplyTransformation(TransformationMoveInstructionDown(
  47. MakeInstructionDescriptor(GetIRContext(), *it)))) {
  48. // Apply the transformation as many times as possible.
  49. }
  50. }
  51. }
  52. }
  53. }
  54. } // namespace fuzz
  55. } // namespace spvtools