fuzzer_pass_swap_conditional_branch_operands.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_swap_conditional_branch_operands.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_swap_conditional_branch_operands.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. FuzzerPassSwapBranchConditionalOperands::
  22. FuzzerPassSwapBranchConditionalOperands(
  23. opt::IRContext* ir_context,
  24. TransformationContext* transformation_context,
  25. FuzzerContext* fuzzer_context,
  26. protobufs::TransformationSequence* transformations,
  27. bool ignore_inapplicable_transformations)
  28. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  29. transformations, ignore_inapplicable_transformations) {}
  30. void FuzzerPassSwapBranchConditionalOperands::Apply() {
  31. ForEachInstructionWithInstructionDescriptor(
  32. [this](opt::Function* /*unused*/, opt::BasicBlock* /*unused*/,
  33. opt::BasicBlock::iterator inst_it,
  34. const protobufs::InstructionDescriptor& instruction_descriptor) {
  35. const auto& inst = *inst_it;
  36. if (inst.opcode() != spv::Op::OpBranchConditional) {
  37. return;
  38. }
  39. if (!GetFuzzerContext()->ChoosePercentage(
  40. GetFuzzerContext()
  41. ->GetChanceOfSwappingConditionalBranchOperands())) {
  42. return;
  43. }
  44. ApplyTransformation(TransformationSwapConditionalBranchOperands(
  45. instruction_descriptor, GetFuzzerContext()->GetFreshId()));
  46. });
  47. }
  48. } // namespace fuzz
  49. } // namespace spvtools