fuzzer_pass_add_loads.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) 2020 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_add_loads.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/transformation_load.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. FuzzerPassAddLoads::FuzzerPassAddLoads(
  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. FuzzerPassAddLoads::~FuzzerPassAddLoads() = default;
  26. void FuzzerPassAddLoads::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 load before this
  37. // instruction.
  38. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpLoad, inst_it)) {
  39. return;
  40. }
  41. // Randomly decide whether to try inserting a load here.
  42. if (!GetFuzzerContext()->ChoosePercentage(
  43. GetFuzzerContext()->GetChanceOfAddingLoad())) {
  44. return;
  45. }
  46. std::vector<opt::Instruction*> relevant_instructions =
  47. FindAvailableInstructions(
  48. function, block, inst_it,
  49. [](opt::IRContext* context,
  50. opt::Instruction* instruction) -> bool {
  51. if (!instruction->result_id() || !instruction->type_id()) {
  52. return false;
  53. }
  54. switch (instruction->opcode()) {
  55. case SpvOpConstantNull:
  56. case SpvOpUndef:
  57. // Do not allow loading from a null or undefined pointer;
  58. // this might be OK if the block is dead, but for now we
  59. // conservatively avoid it.
  60. return false;
  61. default:
  62. break;
  63. }
  64. return context->get_def_use_mgr()
  65. ->GetDef(instruction->type_id())
  66. ->opcode() == SpvOpTypePointer;
  67. });
  68. // At this point, |relevant_instructions| contains all the pointers
  69. // we might think of loading from.
  70. if (relevant_instructions.empty()) {
  71. return;
  72. }
  73. // Choose a pointer at random, and create and apply a loading
  74. // transformation based on it.
  75. ApplyTransformation(TransformationLoad(
  76. GetFuzzerContext()->GetFreshId(),
  77. relevant_instructions[GetFuzzerContext()->RandomIndex(
  78. relevant_instructions)]
  79. ->result_id(),
  80. instruction_descriptor));
  81. });
  82. }
  83. } // namespace fuzz
  84. } // namespace spvtools