fuzzer_pass_copy_objects.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2019 Google LLC
  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_copy_objects.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/transformation_copy_object.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. FuzzerPassCopyObjects::FuzzerPassCopyObjects(
  20. opt::IRContext* ir_context, FactManager* fact_manager,
  21. FuzzerContext* fuzzer_context,
  22. protobufs::TransformationSequence* transformations)
  23. : FuzzerPass(ir_context, fact_manager, fuzzer_context, transformations) {}
  24. FuzzerPassCopyObjects::~FuzzerPassCopyObjects() = default;
  25. void FuzzerPassCopyObjects::Apply() {
  26. ForEachInstructionWithInstructionDescriptor(
  27. [this](opt::Function* function, opt::BasicBlock* block,
  28. opt::BasicBlock::iterator inst_it,
  29. const protobufs::InstructionDescriptor& instruction_descriptor)
  30. -> void {
  31. assert(inst_it->opcode() ==
  32. instruction_descriptor.target_instruction_opcode() &&
  33. "The opcode of the instruction we might insert before must be "
  34. "the same as the opcode in the descriptor for the instruction");
  35. // Check whether it is legitimate to insert a copy before this
  36. // instruction.
  37. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpCopyObject,
  38. inst_it)) {
  39. return;
  40. }
  41. // Randomly decide whether to try inserting an object copy here.
  42. if (!GetFuzzerContext()->ChoosePercentage(
  43. GetFuzzerContext()->GetChanceOfCopyingObject())) {
  44. return;
  45. }
  46. std::vector<opt::Instruction*> relevant_instructions =
  47. FindAvailableInstructions(function, block, inst_it,
  48. fuzzerutil::CanMakeSynonymOf);
  49. // At this point, |relevant_instructions| contains all the instructions
  50. // we might think of copying.
  51. if (relevant_instructions.empty()) {
  52. return;
  53. }
  54. // Choose a copyable instruction at random, and create and apply an
  55. // object copying transformation based on it.
  56. ApplyTransformation(TransformationCopyObject(
  57. relevant_instructions[GetFuzzerContext()->RandomIndex(
  58. relevant_instructions)]
  59. ->result_id(),
  60. instruction_descriptor, GetFuzzerContext()->GetFreshId()));
  61. });
  62. }
  63. } // namespace fuzz
  64. } // namespace spvtools