inst_buff_addr_check_pass.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // Copyright (c) 2019 The Khronos Group Inc.
  2. // Copyright (c) 2019 Valve Corporation
  3. // Copyright (c) 2019 LunarG Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #include "inst_buff_addr_check_pass.h"
  17. namespace spvtools {
  18. namespace opt {
  19. uint32_t InstBuffAddrCheckPass::CloneOriginalReference(
  20. Instruction* ref_inst, InstructionBuilder* builder) {
  21. // Clone original ref with new result id (if load)
  22. assert(
  23. (ref_inst->opcode() == SpvOpLoad || ref_inst->opcode() == SpvOpStore) &&
  24. "unexpected ref");
  25. std::unique_ptr<Instruction> new_ref_inst(ref_inst->Clone(context()));
  26. uint32_t ref_result_id = ref_inst->result_id();
  27. uint32_t new_ref_id = 0;
  28. if (ref_result_id != 0) {
  29. new_ref_id = TakeNextId();
  30. new_ref_inst->SetResultId(new_ref_id);
  31. }
  32. // Register new reference and add to new block
  33. Instruction* added_inst = builder->AddInstruction(std::move(new_ref_inst));
  34. uid2offset_[added_inst->unique_id()] = uid2offset_[ref_inst->unique_id()];
  35. if (new_ref_id != 0)
  36. get_decoration_mgr()->CloneDecorations(ref_result_id, new_ref_id);
  37. return new_ref_id;
  38. }
  39. bool InstBuffAddrCheckPass::IsPhysicalBuffAddrReference(Instruction* ref_inst) {
  40. if (ref_inst->opcode() != SpvOpLoad && ref_inst->opcode() != SpvOpStore)
  41. return false;
  42. uint32_t ptr_id = ref_inst->GetSingleWordInOperand(0);
  43. analysis::DefUseManager* du_mgr = get_def_use_mgr();
  44. Instruction* ptr_inst = du_mgr->GetDef(ptr_id);
  45. if (ptr_inst->opcode() != SpvOpAccessChain) return false;
  46. uint32_t ptr_ty_id = ptr_inst->type_id();
  47. Instruction* ptr_ty_inst = du_mgr->GetDef(ptr_ty_id);
  48. if (ptr_ty_inst->GetSingleWordInOperand(0) !=
  49. SpvStorageClassPhysicalStorageBufferEXT)
  50. return false;
  51. return true;
  52. }
  53. // TODO(greg-lunarg): Refactor with InstBindlessCheckPass::GenCheckCode() ??
  54. void InstBuffAddrCheckPass::GenCheckCode(
  55. uint32_t check_id, uint32_t error_id, uint32_t ref_uptr_id,
  56. uint32_t stage_idx, Instruction* ref_inst,
  57. std::vector<std::unique_ptr<BasicBlock>>* new_blocks) {
  58. BasicBlock* back_blk_ptr = &*new_blocks->back();
  59. InstructionBuilder builder(
  60. context(), back_blk_ptr,
  61. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  62. // Gen conditional branch on check_id. Valid branch generates original
  63. // reference. Invalid generates debug output and zero result (if needed).
  64. uint32_t merge_blk_id = TakeNextId();
  65. uint32_t valid_blk_id = TakeNextId();
  66. uint32_t invalid_blk_id = TakeNextId();
  67. std::unique_ptr<Instruction> merge_label(NewLabel(merge_blk_id));
  68. std::unique_ptr<Instruction> valid_label(NewLabel(valid_blk_id));
  69. std::unique_ptr<Instruction> invalid_label(NewLabel(invalid_blk_id));
  70. (void)builder.AddConditionalBranch(check_id, valid_blk_id, invalid_blk_id,
  71. merge_blk_id, SpvSelectionControlMaskNone);
  72. // Gen valid branch
  73. std::unique_ptr<BasicBlock> new_blk_ptr(
  74. new BasicBlock(std::move(valid_label)));
  75. builder.SetInsertPoint(&*new_blk_ptr);
  76. uint32_t new_ref_id = CloneOriginalReference(ref_inst, &builder);
  77. (void)builder.AddBranch(merge_blk_id);
  78. new_blocks->push_back(std::move(new_blk_ptr));
  79. // Gen invalid block
  80. new_blk_ptr.reset(new BasicBlock(std::move(invalid_label)));
  81. builder.SetInsertPoint(&*new_blk_ptr);
  82. // Convert uptr from uint64 to 2 uint32
  83. Instruction* lo_uptr_inst =
  84. builder.AddUnaryOp(GetUintId(), SpvOpUConvert, ref_uptr_id);
  85. Instruction* rshift_uptr_inst =
  86. builder.AddBinaryOp(GetUint64Id(), SpvOpShiftRightLogical, ref_uptr_id,
  87. builder.GetUintConstantId(32));
  88. Instruction* hi_uptr_inst = builder.AddUnaryOp(GetUintId(), SpvOpUConvert,
  89. rshift_uptr_inst->result_id());
  90. GenDebugStreamWrite(
  91. uid2offset_[ref_inst->unique_id()], stage_idx,
  92. {error_id, lo_uptr_inst->result_id(), hi_uptr_inst->result_id()},
  93. &builder);
  94. // Gen zero for invalid load. If pointer type, need to convert uint64
  95. // zero to pointer; cannot create ConstantNull of pointer type.
  96. uint32_t null_id = 0;
  97. if (new_ref_id != 0) {
  98. uint32_t ref_type_id = ref_inst->type_id();
  99. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  100. analysis::Type* ref_type = type_mgr->GetType(ref_type_id);
  101. if (ref_type->AsPointer() != nullptr) {
  102. uint32_t null_u64_id = GetNullId(GetUint64Id());
  103. Instruction* null_ptr_inst =
  104. builder.AddUnaryOp(ref_type_id, SpvOpConvertUToPtr, null_u64_id);
  105. null_id = null_ptr_inst->result_id();
  106. } else {
  107. null_id = GetNullId(ref_type_id);
  108. }
  109. }
  110. (void)builder.AddBranch(merge_blk_id);
  111. new_blocks->push_back(std::move(new_blk_ptr));
  112. // Gen merge block
  113. new_blk_ptr.reset(new BasicBlock(std::move(merge_label)));
  114. builder.SetInsertPoint(&*new_blk_ptr);
  115. // Gen phi of new reference and zero, if necessary, and replace the
  116. // result id of the original reference with that of the Phi. Kill original
  117. // reference.
  118. if (new_ref_id != 0) {
  119. Instruction* phi_inst =
  120. builder.AddPhi(ref_inst->type_id(),
  121. {new_ref_id, valid_blk_id, null_id, invalid_blk_id});
  122. context()->ReplaceAllUsesWith(ref_inst->result_id(), phi_inst->result_id());
  123. }
  124. new_blocks->push_back(std::move(new_blk_ptr));
  125. context()->KillInst(ref_inst);
  126. }
  127. uint32_t InstBuffAddrCheckPass::GetTypeLength(uint32_t type_id) {
  128. Instruction* type_inst = get_def_use_mgr()->GetDef(type_id);
  129. switch (type_inst->opcode()) {
  130. case SpvOpTypeFloat:
  131. case SpvOpTypeInt:
  132. return type_inst->GetSingleWordInOperand(0) / 8u;
  133. case SpvOpTypeVector:
  134. case SpvOpTypeMatrix:
  135. return type_inst->GetSingleWordInOperand(1) *
  136. GetTypeLength(type_inst->GetSingleWordInOperand(0));
  137. case SpvOpTypePointer:
  138. assert(type_inst->GetSingleWordInOperand(0) ==
  139. SpvStorageClassPhysicalStorageBufferEXT &&
  140. "unexpected pointer type");
  141. return 8u;
  142. default:
  143. assert(false && "unexpected buffer reference type");
  144. return 0;
  145. }
  146. }
  147. void InstBuffAddrCheckPass::AddParam(uint32_t type_id,
  148. std::vector<uint32_t>* param_vec,
  149. std::unique_ptr<Function>* input_func) {
  150. uint32_t pid = TakeNextId();
  151. param_vec->push_back(pid);
  152. std::unique_ptr<Instruction> param_inst(new Instruction(
  153. get_module()->context(), SpvOpFunctionParameter, type_id, pid, {}));
  154. get_def_use_mgr()->AnalyzeInstDefUse(&*param_inst);
  155. (*input_func)->AddParameter(std::move(param_inst));
  156. }
  157. uint32_t InstBuffAddrCheckPass::GetSearchAndTestFuncId() {
  158. if (search_test_func_id_ == 0) {
  159. // Generate function "bool search_and_test(uint64_t ref_ptr, uint32_t len)"
  160. // which searches input buffer for buffer which most likely contains the
  161. // pointer value |ref_ptr| and verifies that the entire reference of
  162. // length |len| bytes is contained in the buffer.
  163. search_test_func_id_ = TakeNextId();
  164. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  165. std::vector<const analysis::Type*> param_types = {
  166. type_mgr->GetType(GetUint64Id()), type_mgr->GetType(GetUintId())};
  167. analysis::Function func_ty(type_mgr->GetType(GetBoolId()), param_types);
  168. analysis::Type* reg_func_ty = type_mgr->GetRegisteredType(&func_ty);
  169. std::unique_ptr<Instruction> func_inst(
  170. new Instruction(get_module()->context(), SpvOpFunction, GetBoolId(),
  171. search_test_func_id_,
  172. {{spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER,
  173. {SpvFunctionControlMaskNone}},
  174. {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  175. {type_mgr->GetTypeInstruction(reg_func_ty)}}}));
  176. get_def_use_mgr()->AnalyzeInstDefUse(&*func_inst);
  177. std::unique_ptr<Function> input_func =
  178. MakeUnique<Function>(std::move(func_inst));
  179. std::vector<uint32_t> param_vec;
  180. // Add ref_ptr and length parameters
  181. AddParam(GetUint64Id(), &param_vec, &input_func);
  182. AddParam(GetUintId(), &param_vec, &input_func);
  183. // Empty first block.
  184. uint32_t first_blk_id = TakeNextId();
  185. std::unique_ptr<Instruction> first_blk_label(NewLabel(first_blk_id));
  186. std::unique_ptr<BasicBlock> first_blk_ptr =
  187. MakeUnique<BasicBlock>(std::move(first_blk_label));
  188. InstructionBuilder builder(
  189. context(), &*first_blk_ptr,
  190. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  191. uint32_t hdr_blk_id = TakeNextId();
  192. // Branch to search loop header
  193. std::unique_ptr<Instruction> hdr_blk_label(NewLabel(hdr_blk_id));
  194. (void)builder.AddInstruction(MakeUnique<Instruction>(
  195. context(), SpvOpBranch, 0, 0,
  196. std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {hdr_blk_id}}}));
  197. first_blk_ptr->SetParent(&*input_func);
  198. input_func->AddBasicBlock(std::move(first_blk_ptr));
  199. // Linear search loop header block
  200. // TODO(greg-lunarg): Implement binary search
  201. std::unique_ptr<BasicBlock> hdr_blk_ptr =
  202. MakeUnique<BasicBlock>(std::move(hdr_blk_label));
  203. builder.SetInsertPoint(&*hdr_blk_ptr);
  204. // Phi for search index. Starts with 1.
  205. uint32_t cont_blk_id = TakeNextId();
  206. std::unique_ptr<Instruction> cont_blk_label(NewLabel(cont_blk_id));
  207. // Deal with def-use cycle caused by search loop index computation.
  208. // Create Add and Phi instructions first, then do Def analysis on Add.
  209. // Add Phi and Add instructions and do Use analysis later.
  210. uint32_t idx_phi_id = TakeNextId();
  211. uint32_t idx_inc_id = TakeNextId();
  212. std::unique_ptr<Instruction> idx_inc_inst(new Instruction(
  213. context(), SpvOpIAdd, GetUintId(), idx_inc_id,
  214. {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {idx_phi_id}},
  215. {spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  216. {builder.GetUintConstantId(1u)}}}));
  217. std::unique_ptr<Instruction> idx_phi_inst(new Instruction(
  218. context(), SpvOpPhi, GetUintId(), idx_phi_id,
  219. {{spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  220. {builder.GetUintConstantId(1u)}},
  221. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {first_blk_id}},
  222. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {idx_inc_id}},
  223. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {cont_blk_id}}}));
  224. get_def_use_mgr()->AnalyzeInstDef(&*idx_inc_inst);
  225. // Add (previously created) search index phi
  226. (void)builder.AddInstruction(std::move(idx_phi_inst));
  227. // LoopMerge
  228. uint32_t bound_test_blk_id = TakeNextId();
  229. std::unique_ptr<Instruction> bound_test_blk_label(
  230. NewLabel(bound_test_blk_id));
  231. (void)builder.AddInstruction(MakeUnique<Instruction>(
  232. context(), SpvOpLoopMerge, 0, 0,
  233. std::initializer_list<Operand>{
  234. {SPV_OPERAND_TYPE_ID, {bound_test_blk_id}},
  235. {SPV_OPERAND_TYPE_ID, {cont_blk_id}},
  236. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {SpvLoopControlMaskNone}}}));
  237. // Branch to continue/work block
  238. (void)builder.AddInstruction(MakeUnique<Instruction>(
  239. context(), SpvOpBranch, 0, 0,
  240. std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {cont_blk_id}}}));
  241. hdr_blk_ptr->SetParent(&*input_func);
  242. input_func->AddBasicBlock(std::move(hdr_blk_ptr));
  243. // Continue/Work Block. Read next buffer pointer and break if greater
  244. // than ref_ptr arg.
  245. std::unique_ptr<BasicBlock> cont_blk_ptr =
  246. MakeUnique<BasicBlock>(std::move(cont_blk_label));
  247. builder.SetInsertPoint(&*cont_blk_ptr);
  248. // Add (previously created) search index increment now.
  249. (void)builder.AddInstruction(std::move(idx_inc_inst));
  250. // Load next buffer address from debug input buffer
  251. uint32_t ibuf_id = GetInputBufferId();
  252. uint32_t ibuf_ptr_id = GetInputBufferPtrId();
  253. Instruction* uptr_ac_inst = builder.AddTernaryOp(
  254. ibuf_ptr_id, SpvOpAccessChain, ibuf_id,
  255. builder.GetUintConstantId(kDebugInputDataOffset), idx_inc_id);
  256. uint32_t ibuf_type_id = GetInputBufferTypeId();
  257. Instruction* uptr_load_inst =
  258. builder.AddUnaryOp(ibuf_type_id, SpvOpLoad, uptr_ac_inst->result_id());
  259. // If loaded address greater than ref_ptr arg, break, else branch back to
  260. // loop header
  261. Instruction* uptr_test_inst =
  262. builder.AddBinaryOp(GetBoolId(), SpvOpUGreaterThan,
  263. uptr_load_inst->result_id(), param_vec[0]);
  264. (void)builder.AddConditionalBranch(uptr_test_inst->result_id(),
  265. bound_test_blk_id, hdr_blk_id,
  266. kInvalidId, SpvSelectionControlMaskNone);
  267. cont_blk_ptr->SetParent(&*input_func);
  268. input_func->AddBasicBlock(std::move(cont_blk_ptr));
  269. // Bounds test block. Read length of selected buffer and test that
  270. // all len arg bytes are in buffer.
  271. std::unique_ptr<BasicBlock> bound_test_blk_ptr =
  272. MakeUnique<BasicBlock>(std::move(bound_test_blk_label));
  273. builder.SetInsertPoint(&*bound_test_blk_ptr);
  274. // Decrement index to point to previous/candidate buffer address
  275. Instruction* cand_idx_inst = builder.AddBinaryOp(
  276. GetUintId(), SpvOpISub, idx_inc_id, builder.GetUintConstantId(1u));
  277. // Load candidate buffer address
  278. Instruction* cand_ac_inst =
  279. builder.AddTernaryOp(ibuf_ptr_id, SpvOpAccessChain, ibuf_id,
  280. builder.GetUintConstantId(kDebugInputDataOffset),
  281. cand_idx_inst->result_id());
  282. Instruction* cand_load_inst =
  283. builder.AddUnaryOp(ibuf_type_id, SpvOpLoad, cand_ac_inst->result_id());
  284. // Compute offset of ref_ptr from candidate buffer address
  285. Instruction* offset_inst = builder.AddBinaryOp(
  286. ibuf_type_id, SpvOpISub, param_vec[0], cand_load_inst->result_id());
  287. // Convert ref length to uint64
  288. Instruction* ref_len_64_inst =
  289. builder.AddUnaryOp(ibuf_type_id, SpvOpUConvert, param_vec[1]);
  290. // Add ref length to ref offset to compute end of reference
  291. Instruction* ref_end_inst =
  292. builder.AddBinaryOp(ibuf_type_id, SpvOpIAdd, offset_inst->result_id(),
  293. ref_len_64_inst->result_id());
  294. // Load starting index of lengths in input buffer and convert to uint32
  295. Instruction* len_start_ac_inst =
  296. builder.AddTernaryOp(ibuf_ptr_id, SpvOpAccessChain, ibuf_id,
  297. builder.GetUintConstantId(kDebugInputDataOffset),
  298. builder.GetUintConstantId(0u));
  299. Instruction* len_start_load_inst = builder.AddUnaryOp(
  300. ibuf_type_id, SpvOpLoad, len_start_ac_inst->result_id());
  301. Instruction* len_start_32_inst = builder.AddUnaryOp(
  302. GetUintId(), SpvOpUConvert, len_start_load_inst->result_id());
  303. // Decrement search index to get candidate buffer length index
  304. Instruction* cand_len_idx_inst =
  305. builder.AddBinaryOp(GetUintId(), SpvOpISub, cand_idx_inst->result_id(),
  306. builder.GetUintConstantId(1u));
  307. // Add candidate length index to start index
  308. Instruction* len_idx_inst = builder.AddBinaryOp(
  309. GetUintId(), SpvOpIAdd, cand_len_idx_inst->result_id(),
  310. len_start_32_inst->result_id());
  311. // Load candidate buffer length
  312. Instruction* len_ac_inst =
  313. builder.AddTernaryOp(ibuf_ptr_id, SpvOpAccessChain, ibuf_id,
  314. builder.GetUintConstantId(kDebugInputDataOffset),
  315. len_idx_inst->result_id());
  316. Instruction* len_load_inst =
  317. builder.AddUnaryOp(ibuf_type_id, SpvOpLoad, len_ac_inst->result_id());
  318. // Test if reference end within candidate buffer length
  319. Instruction* len_test_inst = builder.AddBinaryOp(
  320. GetBoolId(), SpvOpULessThanEqual, ref_end_inst->result_id(),
  321. len_load_inst->result_id());
  322. // Return test result
  323. (void)builder.AddInstruction(MakeUnique<Instruction>(
  324. context(), SpvOpReturnValue, 0, 0,
  325. std::initializer_list<Operand>{
  326. {SPV_OPERAND_TYPE_ID, {len_test_inst->result_id()}}}));
  327. // Close block
  328. bound_test_blk_ptr->SetParent(&*input_func);
  329. input_func->AddBasicBlock(std::move(bound_test_blk_ptr));
  330. // Close function and add function to module
  331. std::unique_ptr<Instruction> func_end_inst(
  332. new Instruction(get_module()->context(), SpvOpFunctionEnd, 0, 0, {}));
  333. get_def_use_mgr()->AnalyzeInstDefUse(&*func_end_inst);
  334. input_func->SetFunctionEnd(std::move(func_end_inst));
  335. context()->AddFunction(std::move(input_func));
  336. }
  337. return search_test_func_id_;
  338. }
  339. uint32_t InstBuffAddrCheckPass::GenSearchAndTest(Instruction* ref_inst,
  340. InstructionBuilder* builder,
  341. uint32_t* ref_uptr_id) {
  342. // Enable Int64 if necessary
  343. if (!get_feature_mgr()->HasCapability(SpvCapabilityInt64)) {
  344. std::unique_ptr<Instruction> cap_int64_inst(new Instruction(
  345. context(), SpvOpCapability, 0, 0,
  346. std::initializer_list<Operand>{
  347. {SPV_OPERAND_TYPE_CAPABILITY, {SpvCapabilityInt64}}}));
  348. get_def_use_mgr()->AnalyzeInstDefUse(&*cap_int64_inst);
  349. context()->AddCapability(std::move(cap_int64_inst));
  350. }
  351. // Convert reference pointer to uint64
  352. uint32_t ref_ptr_id = ref_inst->GetSingleWordInOperand(0);
  353. Instruction* ref_uptr_inst =
  354. builder->AddUnaryOp(GetUint64Id(), SpvOpConvertPtrToU, ref_ptr_id);
  355. *ref_uptr_id = ref_uptr_inst->result_id();
  356. // Compute reference length in bytes
  357. analysis::DefUseManager* du_mgr = get_def_use_mgr();
  358. Instruction* ref_ptr_inst = du_mgr->GetDef(ref_ptr_id);
  359. uint32_t ref_ptr_ty_id = ref_ptr_inst->type_id();
  360. Instruction* ref_ptr_ty_inst = du_mgr->GetDef(ref_ptr_ty_id);
  361. uint32_t ref_len = GetTypeLength(ref_ptr_ty_inst->GetSingleWordInOperand(1));
  362. uint32_t ref_len_id = builder->GetUintConstantId(ref_len);
  363. // Gen call to search and test function
  364. const std::vector<uint32_t> args = {GetSearchAndTestFuncId(), *ref_uptr_id,
  365. ref_len_id};
  366. Instruction* call_inst =
  367. builder->AddNaryOp(GetBoolId(), SpvOpFunctionCall, args);
  368. uint32_t retval = call_inst->result_id();
  369. return retval;
  370. }
  371. void InstBuffAddrCheckPass::GenBuffAddrCheckCode(
  372. BasicBlock::iterator ref_inst_itr,
  373. UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx,
  374. std::vector<std::unique_ptr<BasicBlock>>* new_blocks) {
  375. // Look for reference through indexed descriptor. If found, analyze and
  376. // save components. If not, return.
  377. Instruction* ref_inst = &*ref_inst_itr;
  378. if (!IsPhysicalBuffAddrReference(ref_inst)) return;
  379. // Move original block's preceding instructions into first new block
  380. std::unique_ptr<BasicBlock> new_blk_ptr;
  381. MovePreludeCode(ref_inst_itr, ref_block_itr, &new_blk_ptr);
  382. InstructionBuilder builder(
  383. context(), &*new_blk_ptr,
  384. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  385. new_blocks->push_back(std::move(new_blk_ptr));
  386. uint32_t error_id = builder.GetUintConstantId(kInstErrorBuffAddrUnallocRef);
  387. // Generate code to do search and test if all bytes of reference
  388. // are within a listed buffer. Return reference pointer converted to uint64.
  389. uint32_t ref_uptr_id;
  390. uint32_t valid_id = GenSearchAndTest(ref_inst, &builder, &ref_uptr_id);
  391. // Generate test of search results with true branch
  392. // being full reference and false branch being debug output and zero
  393. // for the referenced value.
  394. GenCheckCode(valid_id, error_id, ref_uptr_id, stage_idx, ref_inst,
  395. new_blocks);
  396. // Move original block's remaining code into remainder/merge block and add
  397. // to new blocks
  398. BasicBlock* back_blk_ptr = &*new_blocks->back();
  399. MovePostludeCode(ref_block_itr, back_blk_ptr);
  400. }
  401. void InstBuffAddrCheckPass::InitInstBuffAddrCheck() {
  402. // Initialize base class
  403. InitializeInstrument();
  404. // Initialize class
  405. search_test_func_id_ = 0;
  406. }
  407. Pass::Status InstBuffAddrCheckPass::ProcessImpl() {
  408. // Perform bindless bounds check on each entry point function in module
  409. InstProcessFunction pfn =
  410. [this](BasicBlock::iterator ref_inst_itr,
  411. UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx,
  412. std::vector<std::unique_ptr<BasicBlock>>* new_blocks) {
  413. return GenBuffAddrCheckCode(ref_inst_itr, ref_block_itr, stage_idx,
  414. new_blocks);
  415. };
  416. bool modified = InstProcessEntryPointCallTree(pfn);
  417. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  418. }
  419. Pass::Status InstBuffAddrCheckPass::Process() {
  420. if (!get_feature_mgr()->HasCapability(
  421. SpvCapabilityPhysicalStorageBufferAddressesEXT))
  422. return Status::SuccessWithoutChange;
  423. InitInstBuffAddrCheck();
  424. return ProcessImpl();
  425. }
  426. } // namespace opt
  427. } // namespace spvtools