fuzzer_pass.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.h"
  15. #include "source/fuzz/instruction_descriptor.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. FuzzerPass::FuzzerPass(opt::IRContext* ir_context, FactManager* fact_manager,
  19. FuzzerContext* fuzzer_context,
  20. protobufs::TransformationSequence* transformations)
  21. : ir_context_(ir_context),
  22. fact_manager_(fact_manager),
  23. fuzzer_context_(fuzzer_context),
  24. transformations_(transformations) {}
  25. FuzzerPass::~FuzzerPass() = default;
  26. std::vector<opt::Instruction*> FuzzerPass::FindAvailableInstructions(
  27. const opt::Function& function, opt::BasicBlock* block,
  28. opt::BasicBlock::iterator inst_it,
  29. std::function<bool(opt::IRContext*, opt::Instruction*)>
  30. instruction_is_relevant) {
  31. // TODO(afd) The following is (relatively) simple, but may end up being
  32. // prohibitively inefficient, as it walks the whole dominator tree for
  33. // every instruction that is considered.
  34. std::vector<opt::Instruction*> result;
  35. // Consider all global declarations
  36. for (auto& global : GetIRContext()->module()->types_values()) {
  37. if (instruction_is_relevant(GetIRContext(), &global)) {
  38. result.push_back(&global);
  39. }
  40. }
  41. // Consider all previous instructions in this block
  42. for (auto prev_inst_it = block->begin(); prev_inst_it != inst_it;
  43. ++prev_inst_it) {
  44. if (instruction_is_relevant(GetIRContext(), &*prev_inst_it)) {
  45. result.push_back(&*prev_inst_it);
  46. }
  47. }
  48. // Walk the dominator tree to consider all instructions from dominating
  49. // blocks
  50. auto dominator_analysis = GetIRContext()->GetDominatorAnalysis(&function);
  51. for (auto next_dominator = dominator_analysis->ImmediateDominator(block);
  52. next_dominator != nullptr;
  53. next_dominator =
  54. dominator_analysis->ImmediateDominator(next_dominator)) {
  55. for (auto& dominating_inst : *next_dominator) {
  56. if (instruction_is_relevant(GetIRContext(), &dominating_inst)) {
  57. result.push_back(&dominating_inst);
  58. }
  59. }
  60. }
  61. return result;
  62. }
  63. void FuzzerPass::MaybeAddTransformationBeforeEachInstruction(
  64. std::function<
  65. void(const opt::Function& function, opt::BasicBlock* block,
  66. opt::BasicBlock::iterator inst_it,
  67. const protobufs::InstructionDescriptor& instruction_descriptor)>
  68. maybe_apply_transformation) {
  69. // Consider every block in every function.
  70. for (auto& function : *GetIRContext()->module()) {
  71. for (auto& block : function) {
  72. // We now consider every instruction in the block, randomly deciding
  73. // whether to apply a transformation before it.
  74. // In order for transformations to insert new instructions, they need to
  75. // be able to identify the instruction to insert before. We describe an
  76. // instruction via its opcode, 'opc', a base instruction 'base' that has a
  77. // result id, and the number of instructions with opcode 'opc' that we
  78. // should skip when searching from 'base' for the desired instruction.
  79. // (An instruction that has a result id is represented by its own opcode,
  80. // itself as 'base', and a skip-count of 0.)
  81. std::vector<std::tuple<uint32_t, SpvOp, uint32_t>>
  82. base_opcode_skip_triples;
  83. // The initial base instruction is the block label.
  84. uint32_t base = block.id();
  85. // Counts the number of times we have seen each opcode since we reset the
  86. // base instruction.
  87. std::map<SpvOp, uint32_t> skip_count;
  88. // Consider every instruction in the block. The label is excluded: it is
  89. // only necessary to consider it as a base in case the first instruction
  90. // in the block does not have a result id.
  91. for (auto inst_it = block.begin(); inst_it != block.end(); ++inst_it) {
  92. if (inst_it->HasResultId()) {
  93. // In the case that the instruction has a result id, we use the
  94. // instruction as its own base, and clear the skip counts we have
  95. // collected.
  96. base = inst_it->result_id();
  97. skip_count.clear();
  98. }
  99. const SpvOp opcode = inst_it->opcode();
  100. // Invoke the provided function, which might apply a transformation.
  101. maybe_apply_transformation(
  102. function, &block, inst_it,
  103. MakeInstructionDescriptor(
  104. base, opcode,
  105. skip_count.count(opcode) ? skip_count.at(opcode) : 0));
  106. if (!inst_it->HasResultId()) {
  107. skip_count[opcode] =
  108. skip_count.count(opcode) ? skip_count.at(opcode) + 1 : 1;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. } // namespace fuzz
  115. } // namespace spvtools