2
0

fuzzer_pass_split_blocks.cpp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <utility>
  16. #include <vector>
  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. // We are going to try to split this block. We now need to choose where
  45. // to split it. We do this by finding a base instruction that has a
  46. // result id, and an offset from that base instruction. We would like
  47. // offsets to be as small as possible and ideally 0 - we only need offsets
  48. // because not all instructions can be identified by a result id (e.g.
  49. // OpStore instructions cannot).
  50. std::vector<std::pair<uint32_t, uint32_t>> base_offset_pairs;
  51. // The initial base instruction is the block label.
  52. uint32_t base = block->id();
  53. uint32_t offset = 0;
  54. // Consider every instruction in the block. The label is excluded: it is
  55. // only necessary to consider it as a base in case the first instruction
  56. // in the block does not have a result id.
  57. for (auto& inst : *block) {
  58. if (inst.HasResultId()) {
  59. // In the case that the instruction has a result id, we use the
  60. // instruction as its own base, with zero offset.
  61. base = inst.result_id();
  62. offset = 0;
  63. } else {
  64. // The instruction does not have a result id, so we need to identify
  65. // it via the latest instruction that did have a result id (base), and
  66. // an incremented offset.
  67. offset++;
  68. }
  69. base_offset_pairs.emplace_back(base, offset);
  70. }
  71. // Having identified all the places we might be able to split the block,
  72. // we choose one of them.
  73. auto base_offset =
  74. base_offset_pairs[GetFuzzerContext()->RandomIndex(base_offset_pairs)];
  75. auto transformation =
  76. TransformationSplitBlock(base_offset.first, base_offset.second,
  77. GetFuzzerContext()->GetFreshId());
  78. // If the position we have chosen turns out to be a valid place to split
  79. // the block, we apply the split. Otherwise the block just doesn't get
  80. // split.
  81. if (transformation.IsApplicable(GetIRContext(), *GetFactManager())) {
  82. transformation.Apply(GetIRContext(), GetFactManager());
  83. *GetTransformations()->add_transformation() = transformation.ToMessage();
  84. }
  85. }
  86. }
  87. } // namespace fuzz
  88. } // namespace spvtools