fuzzer_pass_split_blocks.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_split_blocks.h"
  15. #include <vector>
  16. #include "source/fuzz/instruction_descriptor.h"
  17. #include "source/fuzz/transformation_split_block.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. FuzzerPassSplitBlocks::FuzzerPassSplitBlocks(
  21. opt::IRContext* ir_context, FactManager* fact_manager,
  22. FuzzerContext* fuzzer_context,
  23. protobufs::TransformationSequence* transformations)
  24. : FuzzerPass(ir_context, fact_manager, fuzzer_context, transformations) {}
  25. FuzzerPassSplitBlocks::~FuzzerPassSplitBlocks() = default;
  26. void FuzzerPassSplitBlocks::Apply() {
  27. // Gather up pointers to all the blocks in the module. We are then able to
  28. // iterate over these pointers and split the blocks to which they point;
  29. // we cannot safely split blocks while we iterate through the module.
  30. std::vector<opt::BasicBlock*> blocks;
  31. for (auto& function : *GetIRContext()->module()) {
  32. for (auto& block : function) {
  33. blocks.push_back(&block);
  34. }
  35. }
  36. // Now go through all the block pointers that were gathered.
  37. for (auto& block : blocks) {
  38. // Probabilistically decide whether to try to split this block.
  39. if (!GetFuzzerContext()->ChoosePercentage(
  40. GetFuzzerContext()->GetChanceOfSplittingBlock())) {
  41. // We are not going to try to split this block.
  42. continue;
  43. }
  44. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/2964): consider
  45. // taking a simpler approach to identifying the instruction before which
  46. // to split a block.
  47. // We are going to try to split this block. We now need to choose where
  48. // to split it. We describe the instruction before which we would like to
  49. // split a block via an InstructionDescriptor, details of which are
  50. // commented in the protobufs definition file.
  51. std::vector<protobufs::InstructionDescriptor> instruction_descriptors;
  52. // The initial base instruction is the block label.
  53. uint32_t base = block->id();
  54. // Counts the number of times we have seen each opcode since we reset the
  55. // base instruction.
  56. std::map<SpvOp, uint32_t> skip_count;
  57. // Consider every instruction in the block. The label is excluded: it is
  58. // only necessary to consider it as a base in case the first instruction
  59. // in the block does not have a result id.
  60. for (auto& inst : *block) {
  61. if (inst.HasResultId()) {
  62. // In the case that the instruction has a result id, we use the
  63. // instruction as its own base, and clear the skip counts we have
  64. // collected.
  65. base = inst.result_id();
  66. skip_count.clear();
  67. }
  68. const SpvOp opcode = inst.opcode();
  69. instruction_descriptors.emplace_back(MakeInstructionDescriptor(
  70. base, opcode, skip_count.count(opcode) ? skip_count.at(opcode) : 0));
  71. if (!inst.HasResultId()) {
  72. skip_count[opcode] =
  73. skip_count.count(opcode) ? skip_count.at(opcode) + 1 : 1;
  74. }
  75. }
  76. // Having identified all the places we might be able to split the block,
  77. // we choose one of them.
  78. auto transformation = TransformationSplitBlock(
  79. instruction_descriptors[GetFuzzerContext()->RandomIndex(
  80. instruction_descriptors)],
  81. GetFuzzerContext()->GetFreshId());
  82. // If the position we have chosen turns out to be a valid place to split
  83. // the block, we apply the split. Otherwise the block just doesn't get
  84. // split.
  85. if (transformation.IsApplicable(GetIRContext(), *GetFactManager())) {
  86. transformation.Apply(GetIRContext(), GetFactManager());
  87. *GetTransformations()->add_transformation() = transformation.ToMessage();
  88. }
  89. }
  90. }
  91. } // namespace fuzz
  92. } // namespace spvtools