fuzzer_util.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. std::vector<uint32_t> GetSuccessors(opt::BasicBlock* block) {
  193. std::vector<uint32_t> result;
  194. switch (block->terminator()->opcode()) {
  195. case SpvOpBranch:
  196. result.push_back(block->terminator()->GetSingleWordInOperand(0));
  197. break;
  198. case SpvOpBranchConditional:
  199. result.push_back(block->terminator()->GetSingleWordInOperand(1));
  200. result.push_back(block->terminator()->GetSingleWordInOperand(2));
  201. break;
  202. case SpvOpSwitch:
  203. for (uint32_t i = 1; i < block->terminator()->NumInOperands(); i += 2) {
  204. result.push_back(block->terminator()->GetSingleWordInOperand(i));
  205. }
  206. break;
  207. default:
  208. break;
  209. }
  210. return result;
  211. }
  212. void FindBypassedBlocks(opt::IRContext* context, opt::BasicBlock* bb_from,
  213. opt::BasicBlock* bb_to,
  214. std::set<opt::BasicBlock*>* bypassed_blocks) {
  215. // This algorithm finds all blocks different from |bb_from| that:
  216. // - are in the innermost structured control flow construct containing
  217. // |bb_from|
  218. // - can be reached from |bb_from| without traversing a back-edge or going
  219. // through |bb_to|
  220. //
  221. // This is achieved by doing a depth-first search of the function's CFG,
  222. // exploring merge blocks before successors, and grabbing all blocks that are
  223. // visited in the sub-search rooted at |bb_from|. (As an optimization, the
  224. // search terminates as soon as exploration of |bb_from| has completed.)
  225. // This represents a basic block in a partial state of exploration. As we
  226. // wish to visit merge blocks in advance of regular successors, we track them
  227. // separately.
  228. struct StackNode {
  229. opt::BasicBlock* block;
  230. bool handled_merge;
  231. std::vector<uint32_t> successors;
  232. uint32_t next_successor;
  233. };
  234. auto enclosing_function = bb_from->GetParent();
  235. // The set of block ids already visited during search. We put |bb_to| in
  236. // there initially so that search automatically backtracks when this block is
  237. // reached.
  238. std::set<uint32_t> visited;
  239. visited.insert(bb_to->id());
  240. // Tracks when we are in the region of blocks that are to be grabbed; we flip
  241. // this to 'true' once we reach |bb_from| and have finished searching its
  242. // merge block (in the case that it happens to be a header.
  243. bool interested = false;
  244. std::vector<StackNode> dfs_stack;
  245. opt::BasicBlock* entry_block = enclosing_function->entry().get();
  246. dfs_stack.push_back({entry_block, false, GetSuccessors(entry_block), 0});
  247. while (!dfs_stack.empty()) {
  248. StackNode* node = &dfs_stack.back();
  249. // First make sure we search the merge block associated ith this block, if
  250. // there is one.
  251. if (!node->handled_merge) {
  252. node->handled_merge = true;
  253. if (node->block->MergeBlockIdIfAny()) {
  254. opt::BasicBlock* merge_block =
  255. context->cfg()->block(node->block->MergeBlockIdIfAny());
  256. // A block can only be the merge block for one header, so this block
  257. // should only be in |visited| if it is |bb_to|, which we put into
  258. // |visited| in advance.
  259. assert(visited.count(merge_block->id()) == 0 || merge_block == bb_to);
  260. if (visited.count(merge_block->id()) == 0) {
  261. visited.insert(merge_block->id());
  262. dfs_stack.push_back(
  263. {merge_block, false, GetSuccessors(merge_block), 0});
  264. }
  265. }
  266. continue;
  267. }
  268. // If we find |bb_from|, we are interested in grabbing previously unseen
  269. // successor blocks (by this point we will have already searched the merge
  270. // block associated with |bb_from|, if there is one.
  271. if (node->block == bb_from) {
  272. interested = true;
  273. }
  274. // Consider the next unexplored successor.
  275. if (node->next_successor < node->successors.size()) {
  276. uint32_t successor_id = node->successors[node->next_successor];
  277. if (visited.count(successor_id) == 0) {
  278. visited.insert(successor_id);
  279. opt::BasicBlock* successor_block = context->cfg()->block(successor_id);
  280. if (interested) {
  281. // If we're in the region of interest, grab this block.
  282. bypassed_blocks->insert(successor_block);
  283. }
  284. dfs_stack.push_back(
  285. {successor_block, false, GetSuccessors(successor_block), 0});
  286. }
  287. node->next_successor++;
  288. } else {
  289. // We have finished exploring |node|. If it is |bb_from|, we can
  290. // terminate search -- we have grabbed all the relevant blocks.
  291. if (node->block == bb_from) {
  292. break;
  293. }
  294. dfs_stack.pop_back();
  295. }
  296. }
  297. }
  298. bool NewEdgeLeavingConstructBodyRespectsUseDefDominance(
  299. opt::IRContext* context, opt::BasicBlock* bb_from, opt::BasicBlock* bb_to) {
  300. // Find those blocks that the edge from |bb_from| to |bb_to| might bypass.
  301. std::set<opt::BasicBlock*> bypassed_blocks;
  302. FindBypassedBlocks(context, bb_from, bb_to, &bypassed_blocks);
  303. // For each bypassed block, check whether it contains a definition that is
  304. // used by some non-bypassed block - that would be problematic.
  305. for (auto defining_block : bypassed_blocks) {
  306. for (auto& inst : *defining_block) {
  307. if (!context->get_def_use_mgr()->WhileEachUse(
  308. &inst,
  309. [context, &bypassed_blocks](opt::Instruction* user,
  310. uint32_t operand_index) -> bool {
  311. // If this use is in an OpPhi, we need to check that dominance
  312. // of the relevant *parent* block is not spoiled. Otherwise we
  313. // need to check that dominance of the block containing the use
  314. // is not spoiled.
  315. opt::BasicBlock* use_block_or_phi_parent =
  316. user->opcode() == SpvOpPhi
  317. ? context->cfg()->block(
  318. user->GetSingleWordOperand(operand_index + 1))
  319. : context->get_instr_block(user);
  320. // There might not be any relevant block, e.g. if the use is in
  321. // a decoration; in this case the new edge is unproblematic.
  322. if (use_block_or_phi_parent == nullptr) {
  323. return true;
  324. }
  325. // If the use-block is not in |bypassed_blocks| then we have
  326. // found a block in the construct that is reachable from
  327. // |from_block|, and which defines an id that is used outside of
  328. // the construct. Adding an edge from |from_block| to
  329. // |to_block| would prevent this use being dominated.
  330. return bypassed_blocks.find(use_block_or_phi_parent) !=
  331. bypassed_blocks.end();
  332. })) {
  333. return false;
  334. }
  335. }
  336. }
  337. return true;
  338. }
  339. } // namespace fuzzerutil
  340. } // namespace fuzz
  341. } // namespace spvtools