fuzzer_pass_copy_objects.cpp 3.0 KB

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