fuzzer_util.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_util.h"
  15. namespace spvtools {
  16. namespace fuzz {
  17. namespace fuzzerutil {
  18. bool IsFreshId(opt::IRContext* context, uint32_t id) {
  19. return !context->get_def_use_mgr()->GetDef(id);
  20. }
  21. void UpdateModuleIdBound(opt::IRContext* context, uint32_t id) {
  22. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/2541) consider the
  23. // case where the maximum id bound is reached.
  24. context->module()->SetIdBound(
  25. std::max(context->module()->id_bound(), id + 1));
  26. }
  27. opt::BasicBlock* MaybeFindBlock(opt::IRContext* context,
  28. uint32_t maybe_block_id) {
  29. auto inst = context->get_def_use_mgr()->GetDef(maybe_block_id);
  30. if (inst == nullptr) {
  31. // No instruction defining this id was found.
  32. return nullptr;
  33. }
  34. if (inst->opcode() != SpvOpLabel) {
  35. // The instruction defining the id is not a label, so it cannot be a block
  36. // id.
  37. return nullptr;
  38. }
  39. return context->cfg()->block(maybe_block_id);
  40. }
  41. bool PhiIdsOkForNewEdge(
  42. opt::IRContext* context, opt::BasicBlock* bb_from, opt::BasicBlock* bb_to,
  43. const google::protobuf::RepeatedField<google::protobuf::uint32>& phi_ids) {
  44. if (bb_from->IsSuccessor(bb_to)) {
  45. // There is already an edge from |from_block| to |to_block|, so there is
  46. // no need to extend OpPhi instructions. Do not allow phi ids to be
  47. // present. This might turn out to be too strict; perhaps it would be OK
  48. // just to ignore the ids in this case.
  49. return phi_ids.empty();
  50. }
  51. // The edge would add a previously non-existent edge from |from_block| to
  52. // |to_block|, so we go through the given phi ids and check that they exactly
  53. // match the OpPhi instructions in |to_block|.
  54. uint32_t phi_index = 0;
  55. // An explicit loop, rather than applying a lambda to each OpPhi in |bb_to|,
  56. // makes sense here because we need to increment |phi_index| for each OpPhi
  57. // instruction.
  58. for (auto& inst : *bb_to) {
  59. if (inst.opcode() != SpvOpPhi) {
  60. // The OpPhi instructions all occur at the start of the block; if we find
  61. // a non-OpPhi then we have seen them all.
  62. break;
  63. }
  64. if (phi_index == static_cast<uint32_t>(phi_ids.size())) {
  65. // Not enough phi ids have been provided to account for the OpPhi
  66. // instructions.
  67. return false;
  68. }
  69. // Look for an instruction defining the next phi id.
  70. opt::Instruction* phi_extension =
  71. context->get_def_use_mgr()->GetDef(phi_ids[phi_index]);
  72. if (!phi_extension) {
  73. // The id given to extend this OpPhi does not exist.
  74. return false;
  75. }
  76. if (phi_extension->type_id() != inst.type_id()) {
  77. // The instruction given to extend this OpPhi either does not have a type
  78. // or its type does not match that of the OpPhi.
  79. return false;
  80. }
  81. if (context->get_instr_block(phi_extension)) {
  82. // The instruction defining the phi id has an associated block (i.e., it
  83. // is not a global value). Check whether its definition dominates the
  84. // exit of |from_block|.
  85. auto dominator_analysis =
  86. context->GetDominatorAnalysis(bb_from->GetParent());
  87. if (!dominator_analysis->Dominates(phi_extension,
  88. bb_from->terminator())) {
  89. // The given id is no good as its definition does not dominate the exit
  90. // of |from_block|
  91. return false;
  92. }
  93. }
  94. phi_index++;
  95. }
  96. // Return false if not all of the ids for extending OpPhi instructions are
  97. // needed. This might turn out to be stricter than necessary; perhaps it would
  98. // be OK just to not use the ids in this case.
  99. return phi_index == static_cast<uint32_t>(phi_ids.size());
  100. }
  101. void AddUnreachableEdgeAndUpdateOpPhis(
  102. opt::IRContext* context, opt::BasicBlock* bb_from, opt::BasicBlock* bb_to,
  103. bool condition_value,
  104. const google::protobuf::RepeatedField<google::protobuf::uint32>& phi_ids) {
  105. assert(PhiIdsOkForNewEdge(context, bb_from, bb_to, phi_ids) &&
  106. "Precondition on phi_ids is not satisfied");
  107. assert(bb_from->terminator()->opcode() == SpvOpBranch &&
  108. "Precondition on terminator of bb_from is not satisfied");
  109. // Get the id of the boolean constant to be used as the condition.
  110. opt::analysis::Bool bool_type;
  111. opt::analysis::BoolConstant bool_constant(
  112. context->get_type_mgr()->GetRegisteredType(&bool_type)->AsBool(),
  113. condition_value);
  114. uint32_t bool_id = context->get_constant_mgr()->FindDeclaredConstant(
  115. &bool_constant, context->get_type_mgr()->GetId(&bool_type));
  116. const bool from_to_edge_already_exists = bb_from->IsSuccessor(bb_to);
  117. auto successor = bb_from->terminator()->GetSingleWordInOperand(0);
  118. // Add the dead branch, by turning OpBranch into OpBranchConditional, and
  119. // ordering the targets depending on whether the given boolean corresponds to
  120. // true or false.
  121. bb_from->terminator()->SetOpcode(SpvOpBranchConditional);
  122. bb_from->terminator()->SetInOperands(
  123. {{SPV_OPERAND_TYPE_ID, {bool_id}},
  124. {SPV_OPERAND_TYPE_ID, {condition_value ? successor : bb_to->id()}},
  125. {SPV_OPERAND_TYPE_ID, {condition_value ? bb_to->id() : successor}}});
  126. // Update OpPhi instructions in the target block if this branch adds a
  127. // previously non-existent edge from source to target.
  128. if (!from_to_edge_already_exists) {
  129. uint32_t phi_index = 0;
  130. for (auto& inst : *bb_to) {
  131. if (inst.opcode() != SpvOpPhi) {
  132. break;
  133. }
  134. assert(phi_index < static_cast<uint32_t>(phi_ids.size()) &&
  135. "There should be exactly one phi id per OpPhi instruction.");
  136. inst.AddOperand({SPV_OPERAND_TYPE_ID, {phi_ids[phi_index]}});
  137. inst.AddOperand({SPV_OPERAND_TYPE_ID, {bb_from->id()}});
  138. phi_index++;
  139. }
  140. assert(phi_index == static_cast<uint32_t>(phi_ids.size()) &&
  141. "There should be exactly one phi id per OpPhi instruction.");
  142. }
  143. }
  144. bool BlockIsInLoopContinueConstruct(opt::IRContext* context, uint32_t block_id,
  145. uint32_t maybe_loop_header_id) {
  146. // We deem a block to be part of a loop's continue construct if the loop's
  147. // continue target dominates the block.
  148. auto containing_construct_block = context->cfg()->block(maybe_loop_header_id);
  149. if (containing_construct_block->IsLoopHeader()) {
  150. auto continue_target = containing_construct_block->ContinueBlockId();
  151. if (context->GetDominatorAnalysis(containing_construct_block->GetParent())
  152. ->Dominates(continue_target, block_id)) {
  153. return true;
  154. }
  155. }
  156. return false;
  157. }
  158. opt::BasicBlock::iterator GetIteratorForBaseInstructionAndOffset(
  159. opt::BasicBlock* block, const opt::Instruction* base_inst,
  160. uint32_t offset) {
  161. // The cases where |base_inst| is the block's label, vs. inside the block,
  162. // are dealt with separately.
  163. if (base_inst == block->GetLabelInst()) {
  164. // |base_inst| is the block's label.
  165. if (offset == 0) {
  166. // We cannot return an iterator to the block's label.
  167. return block->end();
  168. }
  169. // Conceptually, the first instruction in the block is [label + 1].
  170. // We thus start from 1 when applying the offset.
  171. auto inst_it = block->begin();
  172. for (uint32_t i = 1; i < offset && inst_it != block->end(); i++) {
  173. ++inst_it;
  174. }
  175. // This is either the desired instruction, or the end of the block.
  176. return inst_it;
  177. }
  178. // |base_inst| is inside the block.
  179. for (auto inst_it = block->begin(); inst_it != block->end(); ++inst_it) {
  180. if (base_inst == &*inst_it) {
  181. // We have found the base instruction; we now apply the offset.
  182. for (uint32_t i = 0; i < offset && inst_it != block->end(); i++) {
  183. ++inst_it;
  184. }
  185. // This is either the desired instruction, or the end of the block.
  186. return inst_it;
  187. }
  188. }
  189. assert(false && "The base instruction was not found.");
  190. return nullptr;
  191. }
  192. } // namespace fuzzerutil
  193. } // namespace fuzz
  194. } // namespace spvtools