fuzzer_pass_outline_functions.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_outline_functions.h"
  15. #include <vector>
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_descriptor.h"
  18. #include "source/fuzz/transformation_outline_function.h"
  19. #include "source/fuzz/transformation_split_block.h"
  20. namespace spvtools {
  21. namespace fuzz {
  22. FuzzerPassOutlineFunctions::FuzzerPassOutlineFunctions(
  23. opt::IRContext* ir_context, TransformationContext* transformation_context,
  24. FuzzerContext* fuzzer_context,
  25. protobufs::TransformationSequence* transformations,
  26. bool ignore_inapplicable_transformations)
  27. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  28. transformations, ignore_inapplicable_transformations) {}
  29. void FuzzerPassOutlineFunctions::Apply() {
  30. std::vector<opt::Function*> original_functions;
  31. for (auto& function : *GetIRContext()->module()) {
  32. original_functions.push_back(&function);
  33. }
  34. for (auto& function : original_functions) {
  35. if (!GetFuzzerContext()->ChoosePercentage(
  36. GetFuzzerContext()->GetChanceOfOutliningFunction())) {
  37. continue;
  38. }
  39. std::vector<opt::BasicBlock*> blocks;
  40. for (auto& block : *function) {
  41. blocks.push_back(&block);
  42. }
  43. auto entry_block = MaybeGetEntryBlockSuitableForOutlining(
  44. blocks[GetFuzzerContext()->RandomIndex(blocks)]);
  45. if (!entry_block) {
  46. // The chosen block is not suitable to be the entry block of a region that
  47. // will be outlined.
  48. continue;
  49. }
  50. auto dominator_analysis = GetIRContext()->GetDominatorAnalysis(function);
  51. auto postdominator_analysis =
  52. GetIRContext()->GetPostDominatorAnalysis(function);
  53. std::vector<opt::BasicBlock*> candidate_exit_blocks;
  54. for (auto postdominates_entry_block = entry_block;
  55. postdominates_entry_block != nullptr;
  56. postdominates_entry_block = postdominator_analysis->ImmediateDominator(
  57. postdominates_entry_block)) {
  58. // Consider the block if it is dominated by the entry block, ignore it if
  59. // it is a continue target.
  60. if (dominator_analysis->Dominates(entry_block,
  61. postdominates_entry_block) &&
  62. !GetIRContext()->GetStructuredCFGAnalysis()->IsContinueBlock(
  63. postdominates_entry_block->id())) {
  64. candidate_exit_blocks.push_back(postdominates_entry_block);
  65. }
  66. }
  67. if (candidate_exit_blocks.empty()) {
  68. continue;
  69. }
  70. auto exit_block = MaybeGetExitBlockSuitableForOutlining(
  71. candidate_exit_blocks[GetFuzzerContext()->RandomIndex(
  72. candidate_exit_blocks)]);
  73. if (!exit_block) {
  74. // The block chosen is not suitable
  75. continue;
  76. }
  77. auto region_blocks = TransformationOutlineFunction::GetRegionBlocks(
  78. GetIRContext(), entry_block, exit_block);
  79. std::map<uint32_t, uint32_t> input_id_to_fresh_id;
  80. for (auto id : TransformationOutlineFunction::GetRegionInputIds(
  81. GetIRContext(), region_blocks, exit_block)) {
  82. input_id_to_fresh_id[id] = GetFuzzerContext()->GetFreshId();
  83. }
  84. std::map<uint32_t, uint32_t> output_id_to_fresh_id;
  85. for (auto id : TransformationOutlineFunction::GetRegionOutputIds(
  86. GetIRContext(), region_blocks, exit_block)) {
  87. output_id_to_fresh_id[id] = GetFuzzerContext()->GetFreshId();
  88. }
  89. TransformationOutlineFunction transformation(
  90. entry_block->id(), exit_block->id(),
  91. /*new_function_struct_return_type_id*/
  92. GetFuzzerContext()->GetFreshId(),
  93. /*new_function_type_id*/ GetFuzzerContext()->GetFreshId(),
  94. /*new_function_id*/ GetFuzzerContext()->GetFreshId(),
  95. /*new_function_region_entry_block*/
  96. GetFuzzerContext()->GetFreshId(),
  97. /*new_caller_result_id*/ GetFuzzerContext()->GetFreshId(),
  98. /*new_callee_result_id*/ GetFuzzerContext()->GetFreshId(),
  99. /*input_id_to_fresh_id*/ input_id_to_fresh_id,
  100. /*output_id_to_fresh_id*/ output_id_to_fresh_id);
  101. MaybeApplyTransformation(transformation);
  102. }
  103. }
  104. opt::BasicBlock*
  105. FuzzerPassOutlineFunctions::MaybeGetEntryBlockSuitableForOutlining(
  106. opt::BasicBlock* entry_block) {
  107. // If the entry block is a loop header, we need to get or create its
  108. // preheader and make it the entry block, if possible.
  109. if (entry_block->IsLoopHeader()) {
  110. auto predecessors =
  111. GetIRContext()->cfg()->preds(entry_block->GetLabel()->result_id());
  112. if (predecessors.size() < 2) {
  113. // The header only has one predecessor (the back-edge block) and thus
  114. // it is unreachable. The block cannot be adjusted to be suitable for
  115. // outlining.
  116. return nullptr;
  117. }
  118. // Get or create a suitable preheader and make it become the entry block.
  119. entry_block =
  120. GetOrCreateSimpleLoopPreheader(entry_block->GetLabel()->result_id());
  121. }
  122. assert(!entry_block->IsLoopHeader() &&
  123. "The entry block cannot be a loop header at this point.");
  124. // If the entry block starts with OpPhi or OpVariable, try to split it.
  125. if (entry_block->begin()->opcode() == spv::Op::OpPhi ||
  126. entry_block->begin()->opcode() == spv::Op::OpVariable) {
  127. // Find the first non-OpPhi and non-OpVariable instruction.
  128. auto non_phi_or_var_inst = &*entry_block->begin();
  129. while (non_phi_or_var_inst->opcode() == spv::Op::OpPhi ||
  130. non_phi_or_var_inst->opcode() == spv::Op::OpVariable) {
  131. non_phi_or_var_inst = non_phi_or_var_inst->NextNode();
  132. }
  133. // Split the block.
  134. uint32_t new_block_id = GetFuzzerContext()->GetFreshId();
  135. ApplyTransformation(TransformationSplitBlock(
  136. MakeInstructionDescriptor(GetIRContext(), non_phi_or_var_inst),
  137. new_block_id));
  138. // The new entry block is the newly-created block.
  139. entry_block = &*entry_block->GetParent()->FindBlock(new_block_id);
  140. }
  141. return entry_block;
  142. }
  143. opt::BasicBlock*
  144. FuzzerPassOutlineFunctions::MaybeGetExitBlockSuitableForOutlining(
  145. opt::BasicBlock* exit_block) {
  146. // The exit block must not be a continue target.
  147. assert(!GetIRContext()->GetStructuredCFGAnalysis()->IsContinueBlock(
  148. exit_block->id()) &&
  149. "A candidate exit block cannot be a continue target.");
  150. // If the exit block is a merge block, try to split it and return the second
  151. // block in the pair as the exit block.
  152. if (GetIRContext()->GetStructuredCFGAnalysis()->IsMergeBlock(
  153. exit_block->id())) {
  154. uint32_t new_block_id = GetFuzzerContext()->GetFreshId();
  155. // Find the first non-OpPhi instruction, after which to split.
  156. auto split_before = &*exit_block->begin();
  157. while (split_before->opcode() == spv::Op::OpPhi) {
  158. split_before = split_before->NextNode();
  159. }
  160. if (!MaybeApplyTransformation(TransformationSplitBlock(
  161. MakeInstructionDescriptor(GetIRContext(), split_before),
  162. new_block_id))) {
  163. return nullptr;
  164. }
  165. return &*exit_block->GetParent()->FindBlock(new_block_id);
  166. }
  167. return exit_block;
  168. }
  169. } // namespace fuzz
  170. } // namespace spvtools