fuzzer_pass_split_blocks.cpp 4.1 KB

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