fuzzer_pass_copy_objects.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/protobufs/spirvfuzz_protobufs.h"
  17. #include "source/fuzz/transformation_add_synonym.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. FuzzerPassCopyObjects::FuzzerPassCopyObjects(
  21. opt::IRContext* ir_context, TransformationContext* transformation_context,
  22. FuzzerContext* fuzzer_context,
  23. protobufs::TransformationSequence* transformations,
  24. bool ignore_inapplicable_transformations)
  25. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  26. transformations, ignore_inapplicable_transformations) {}
  27. void FuzzerPassCopyObjects::Apply() {
  28. ForEachInstructionWithInstructionDescriptor(
  29. [this](opt::Function* function, opt::BasicBlock* block,
  30. opt::BasicBlock::iterator inst_it,
  31. const protobufs::InstructionDescriptor& instruction_descriptor)
  32. -> void {
  33. assert(
  34. inst_it->opcode() ==
  35. spv::Op(instruction_descriptor.target_instruction_opcode()) &&
  36. "The opcode of the instruction we might insert before must be "
  37. "the same as the opcode in the descriptor for the instruction");
  38. if (GetTransformationContext()->GetFactManager()->BlockIsDead(
  39. block->id())) {
  40. // Don't create synonyms in dead blocks.
  41. return;
  42. }
  43. // Check whether it is legitimate to insert a copy before this
  44. // instruction.
  45. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpCopyObject,
  46. inst_it)) {
  47. return;
  48. }
  49. // Randomly decide whether to try inserting an object copy here.
  50. if (!GetFuzzerContext()->ChoosePercentage(
  51. GetFuzzerContext()->GetChanceOfCopyingObject())) {
  52. return;
  53. }
  54. const auto relevant_instructions = FindAvailableInstructions(
  55. function, block, inst_it,
  56. [this](opt::IRContext* ir_context, opt::Instruction* inst) {
  57. return TransformationAddSynonym::IsInstructionValid(
  58. ir_context, *GetTransformationContext(), inst,
  59. protobufs::TransformationAddSynonym::COPY_OBJECT);
  60. });
  61. // At this point, |relevant_instructions| contains all the instructions
  62. // we might think of copying.
  63. if (relevant_instructions.empty()) {
  64. return;
  65. }
  66. // Choose a copyable instruction at random, and create and apply an
  67. // object copying transformation based on it.
  68. ApplyTransformation(TransformationAddSynonym(
  69. relevant_instructions[GetFuzzerContext()->RandomIndex(
  70. relevant_instructions)]
  71. ->result_id(),
  72. protobufs::TransformationAddSynonym::COPY_OBJECT,
  73. GetFuzzerContext()->GetFreshId(), instruction_descriptor));
  74. });
  75. }
  76. } // namespace fuzz
  77. } // namespace spvtools