| 1 |
// Copyright (c) 2018 The Khronos Group Inc. |
| 2 |
// Copyright (c) 2018 Valve Corporation |
| 3 |
// Copyright (c) 2018 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 |
|
| 17 |
#ifndef LIBSPIRV_OPT_INST_BINDLESS_CHECK_PASS_H_ |
| 18 |
#define LIBSPIRV_OPT_INST_BINDLESS_CHECK_PASS_H_ |
| 19 |
|
| 20 |
#include "instrument_pass.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace opt { |
| 24 |
|
| 25 |
// This class/pass is designed to support the bindless (descriptor indexing) |
| 26 |
// GPU-assisted validation layer of |
| 27 |
// https://github.com/KhronosGroup/Vulkan-ValidationLayers. Its internal and |
| 28 |
// external design may change as the layer evolves. |
| 29 |
class InstBindlessCheckPass : public InstrumentPass { |
| 30 |
public: |
| 31 |
// Old interface to support testing pre-buffer-overrun capability |
| 32 |
InstBindlessCheckPass(uint32_t desc_set, uint32_t shader_id, |
| 33 |
bool desc_idx_enable, bool desc_init_enable) |
| 34 |
: InstrumentPass(desc_set, shader_id, kInstValidationIdBindless, false), |
| 35 |
desc_idx_enabled_(desc_idx_enable), |
| 36 |
desc_init_enabled_(desc_init_enable), |
| 37 |
buffer_bounds_enabled_(false) {} |
| 38 |
|
| 39 |
// New interface supporting buffer overrun checking |
| 40 |
InstBindlessCheckPass(uint32_t desc_set, uint32_t shader_id, |
| 41 |
bool desc_idx_enable, bool desc_init_enable, |
| 42 |
bool buffer_bounds_enable) |
| 43 |
: InstrumentPass( |
| 44 |
desc_set, shader_id, kInstValidationIdBindless, |
| 45 |
desc_idx_enable || desc_init_enable || buffer_bounds_enable), |
| 46 |
desc_idx_enabled_(desc_idx_enable), |
| 47 |
desc_init_enabled_(desc_init_enable), |
| 48 |
buffer_bounds_enabled_(buffer_bounds_enable) {} |
| 49 |
|
| 50 |
~InstBindlessCheckPass() override = default; |
| 51 |
|
| 52 |
// See optimizer.hpp for pass user documentation. |
| 53 |
Status Process() override; |
| 54 |
|
| 55 |
const char* name() const override { return "inst-bindless-check-pass"; } |
| 56 |
|
| 57 |
private: |
| 58 |
// These functions do bindless checking instrumentation on a single |
| 59 |
// instruction which references through a descriptor (ie references into an |
| 60 |
// image or buffer). Refer to Vulkan API for further information on |
| 61 |
// descriptors. GenDescIdxCheckCode checks that an index into a descriptor |
| 62 |
// array (array of images or buffers) is in-bounds. GenDescInitCheckCode |
| 63 |
// checks that the referenced descriptor has been initialized, if the |
| 64 |
// SPV_EXT_descriptor_indexing extension is enabled, and initialized large |
| 65 |
// enough to handle the reference, if RobustBufferAccess is disabled. |
| 66 |
// |
| 67 |
// The functions are designed to be passed to |
| 68 |
// InstrumentPass::InstProcessEntryPointCallTree(), which applies the |
| 69 |
// function to each instruction in a module and replaces the instruction |
| 70 |
// if warranted. |
| 71 |
// |
| 72 |
// If |ref_inst_itr| is a bindless reference, return in |new_blocks| the |
| 73 |
// result of instrumenting it with validation code within its block at |
| 74 |
// |ref_block_itr|. The validation code first executes a check for the |
| 75 |
// specific condition called for. If the check passes, it executes |
| 76 |
// the remainder of the reference, otherwise writes a record to the debug |
| 77 |
// output buffer stream including |function_idx, instruction_idx, stage_idx| |
| 78 |
// and replaces the reference with the null value of the original type. The |
| 79 |
// block at |ref_block_itr| can just be replaced with the blocks in |
| 80 |
// |new_blocks|, which will contain at least two blocks. The last block will |
| 81 |
// comprise all instructions following |ref_inst_itr|, |
| 82 |
// preceded by a phi instruction. |
| 83 |
// |
| 84 |
// These instrumentation functions utilize GenDebugDirectRead() to read data |
| 85 |
// from the debug input buffer, specifically the lengths of variable length |
| 86 |
// descriptor arrays, and the initialization status of each descriptor. |
| 87 |
// The format of the debug input buffer is documented in instrument.hpp. |
| 88 |
// |
| 89 |
// These instrumentation functions utilize GenDebugStreamWrite() to write its |
| 90 |
// error records. The validation-specific part of the error record will |
| 91 |
// have the format: |
| 92 |
// |
| 93 |
// Validation Error Code (=kInstErrorBindlessBounds) |
| 94 |
// Descriptor Index |
| 95 |
// Descriptor Array Size |
| 96 |
// |
| 97 |
// The Descriptor Index is the index which has been determined to be |
| 98 |
// out-of-bounds. |
| 99 |
// |
| 100 |
// The Descriptor Array Size is the size of the descriptor array which was |
| 101 |
// indexed. |
| 102 |
void GenDescIdxCheckCode( |
| 103 |
BasicBlock::iterator ref_inst_itr, |
| 104 |
UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx, |
| 105 |
std::vector<std::unique_ptr<BasicBlock>>* new_blocks); |
| 106 |
|
| 107 |
void GenDescInitCheckCode( |
| 108 |
BasicBlock::iterator ref_inst_itr, |
| 109 |
UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx, |
| 110 |
std::vector<std::unique_ptr<BasicBlock>>* new_blocks); |
| 111 |
|
| 112 |
// Generate instructions into |builder| to read length of runtime descriptor |
| 113 |
// array |var_id| from debug input buffer and return id of value. |
| 114 |
uint32_t GenDebugReadLength(uint32_t var_id, InstructionBuilder* builder); |
| 115 |
|
| 116 |
// Generate instructions into |builder| to read initialization status of |
| 117 |
// descriptor array |image_id| at |index_id| from debug input buffer and |
| 118 |
// return id of value. |
| 119 |
uint32_t GenDebugReadInit(uint32_t image_id, uint32_t index_id, |
| 120 |
InstructionBuilder* builder); |
| 121 |
|
| 122 |
// Analysis data for descriptor reference components, generated by |
| 123 |
// AnalyzeDescriptorReference. It is necessary and sufficient for further |
| 124 |
// analysis and regeneration of the reference. |
| 125 |
typedef struct ref_analysis { |
| 126 |
uint32_t desc_load_id; |
| 127 |
uint32_t image_id; |
| 128 |
uint32_t load_id; |
| 129 |
uint32_t ptr_id; |
| 130 |
uint32_t var_id; |
| 131 |
uint32_t desc_idx_id; |
| 132 |
Instruction* ref_inst; |
| 133 |
} ref_analysis; |
| 134 |
|
| 135 |
// Return size of type |ty_id| in bytes. |
| 136 |
uint32_t ByteSize(uint32_t ty_id); |
| 137 |
|
| 138 |
// Return stride of type |ty_id| with decoration |stride_deco|. Return 0 |
| 139 |
// if not found |
| 140 |
uint32_t FindStride(uint32_t ty_id, uint32_t stride_deco); |
| 141 |
|
| 142 |
// Generate index of last byte referenced by buffer reference |ref| |
| 143 |
uint32_t GenLastByteIdx(ref_analysis* ref, InstructionBuilder* builder); |
| 144 |
|
| 145 |
// Clone original original reference encapsulated by |ref| into |builder|. |
| 146 |
// This may generate more than one instruction if neccessary. |
| 147 |
uint32_t CloneOriginalReference(ref_analysis* ref, |
| 148 |
InstructionBuilder* builder); |
| 149 |
|
| 150 |
// If |inst| references through an image, return the id of the image it |
| 151 |
// references through. Else return 0. |
| 152 |
uint32_t GetImageId(Instruction* inst); |
| 153 |
|
| 154 |
// Get pointee type inst of pointer value |ptr_inst|. |
| 155 |
Instruction* GetPointeeTypeInst(Instruction* ptr_inst); |
| 156 |
|
| 157 |
// Analyze descriptor reference |ref_inst| and save components into |ref|. |
| 158 |
// Return true if |ref_inst| is a descriptor reference, false otherwise. |
| 159 |
bool AnalyzeDescriptorReference(Instruction* ref_inst, ref_analysis* ref); |
| 160 |
|
| 161 |
// Generate instrumentation code for generic test result |check_id|, starting |
| 162 |
// with |builder| of block |new_blk_ptr|, adding new blocks to |new_blocks|. |
| 163 |
// Generate conditional branch to a valid or invalid branch. Generate valid |
| 164 |
// block which does original reference |ref|. Generate invalid block which |
| 165 |
// writes debug error output utilizing |ref|, |error_id|, |length_id| and |
| 166 |
// |stage_idx|. Generate merge block for valid and invalid branches. Kill |
| 167 |
// original reference. |
| 168 |
void GenCheckCode(uint32_t check_id, uint32_t error_id, uint32_t offset_id, |
| 169 |
uint32_t length_id, uint32_t stage_idx, ref_analysis* ref, |
| 170 |
std::vector<std::unique_ptr<BasicBlock>>* new_blocks); |
| 171 |
|
| 172 |
// Initialize state for instrumenting bindless checking |
| 173 |
void InitializeInstBindlessCheck(); |
| 174 |
|
| 175 |
// Apply GenDescIdxCheckCode to every instruction in module. Then apply |
| 176 |
// GenDescInitCheckCode to every instruction in module. |
| 177 |
Pass::Status ProcessImpl(); |
| 178 |
|
| 179 |
// Enable instrumentation of runtime array length checking |
| 180 |
bool desc_idx_enabled_; |
| 181 |
|
| 182 |
// Enable instrumentation of descriptor initialization checking |
| 183 |
bool desc_init_enabled_; |
| 184 |
|
| 185 |
// Enable instrumentation of buffer overrun checking |
| 186 |
bool buffer_bounds_enabled_; |
| 187 |
|
| 188 |
// Mapping from variable to descriptor set |
| 189 |
std::unordered_map<uint32_t, uint32_t> var2desc_set_; |
| 190 |
|
| 191 |
// Mapping from variable to binding |
| 192 |
std::unordered_map<uint32_t, uint32_t> var2binding_; |
| 193 |
}; |
| 194 |
|
| 195 |
} // namespace opt |
| 196 |
} // namespace spvtools |
| 197 |
|
| 198 |
#endif // LIBSPIRV_OPT_INST_BINDLESS_CHECK_PASS_H_ |