File: vertex_attribute_fetch_oob.cpp

package info (click to toggle)
vulkan-validationlayers 1.4.341.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 54,356 kB
  • sloc: cpp: 675,478; python: 12,311; sh: 24; makefile: 24; xml: 14
file content (372 lines) | stat: -rw-r--r-- 21,266 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* Copyright (c) 2024-2026 LunarG, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "containers/container_utils.h"
#include "gpuav/core/gpuav.h"
#include "gpuav/resources/gpuav_state_trackers.h"
#include "gpuav/resources/gpuav_vulkan_objects.h"
#include "gpuav/shaders/gpuav_error_codes.h"
#include "gpuav/shaders/gpuav_error_header.h"
#include "gpuav/shaders/gpuav_shaders_constants.h"
#include "gpuav/spirv/vertex_attribute_fetch_oob_pass.h"
#include "state_tracker/shader_module.h"
#include "utils/action_command_utils.h"
#include "utils/image_utils.h"

namespace gpuav {
struct VertexAttributeFetchLimit {
    // Default value indicates that no vertex buffer attribute fetching will be OOB
    VkDeviceSize max_vertex_attributes_count = std::numeric_limits<VkDeviceSize>::max();
    vvl::VertexBufferBinding binding_info{};
    VkVertexInputAttributeDescription attribute{};
    uint32_t instance_rate_divisor = vvl::kNoIndex32;
};

// Computes vertex attributes fetching limits based on the set of bound vertex buffers.
// Used to detect out of bounds indices in index buffers.
static std::pair<std::optional<VertexAttributeFetchLimit>, std::optional<VertexAttributeFetchLimit>> GetVertexAttributeFetchLimits(
    const vvl::CommandBuffer &cb_state) {
    const LastBound &last_bound = cb_state.GetLastBoundGraphics();
    const vvl::Pipeline *pipeline_state = last_bound.pipeline_state;

    const bool dynamic_vertex_input = last_bound.IsDynamic(CB_DYNAMIC_STATE_VERTEX_INPUT_EXT);

    const auto &vertex_binding_descriptions =
        dynamic_vertex_input ? cb_state.dynamic_state_value.vertex_bindings : pipeline_state->vertex_input_state->bindings;

    std::optional<VertexAttributeFetchLimit> vertex_attribute_fetch_limit_vertex_input_rate;
    std::optional<VertexAttributeFetchLimit> vertex_attribute_fetch_limit_instance_input_rate;

    small_vector<uint32_t, 32> vertex_shader_used_locations;
    {
        const ::spirv::EntryPoint *vertex_entry_point = last_bound.GetVertexEntryPoint();
        if (!vertex_entry_point) {
            return {std::optional<VertexAttributeFetchLimit>{}, std::optional<VertexAttributeFetchLimit>{}};
        }
        for (const ::spirv::StageInterfaceVariable &interface_var : vertex_entry_point->stage_interface_variables) {
            for (const ::spirv::InterfaceSlot &interface_slot : interface_var.interface_slots) {
                const uint32_t location = interface_slot.Location();
                if (std::find(vertex_shader_used_locations.begin(), vertex_shader_used_locations.end(), location) ==
                    vertex_shader_used_locations.end()) {
                    vertex_shader_used_locations.emplace_back(location);
                }
            }
        }
    }

    for (const auto &[binding, vertex_binding_desc] : vertex_binding_descriptions) {
        const vvl::VertexBufferBinding *vbb = vvl::Find(cb_state.current_vertex_buffer_binding_info, binding);
        if (!vbb) {
            // Validation error
            continue;
        }

        for (const auto &[location, attrib] : vertex_binding_desc.locations) {
            if (std::find(vertex_shader_used_locations.begin(), vertex_shader_used_locations.end(), location) ==
                vertex_shader_used_locations.end()) {
                continue;
            }
            const VkDeviceSize attribute_size = GetVertexInputFormatSize(attrib.desc.format);

            VkDeviceSize vertex_buffer_remaining_size =
                vbb->effective_size > attrib.desc.offset ? vbb->effective_size - attrib.desc.offset : 0;

            VkDeviceSize vertex_attributes_count = 0;
            if (vbb->stride > 0) {
                vertex_attributes_count = vertex_buffer_remaining_size / vbb->stride;

                if (vertex_buffer_remaining_size > vertex_attributes_count * vbb->stride) {
                    vertex_buffer_remaining_size -= vertex_attributes_count * vbb->stride;
                } else {
                    vertex_buffer_remaining_size = 0;
                }

                // maybe room for one more attribute but not full stride - not having stride space does not matter for last element
                if (vertex_buffer_remaining_size >= attribute_size) {
                    vertex_attributes_count += 1;
                }
            } else {
                // For the current attribute type, if stride is 0, the same vertex data chunk will be accessed by all vertex shader
                // instances See https://docs.vulkan.org/spec/latest/chapters/fxvertex.html#fxvertex-input-address-calculation
                if (vertex_buffer_remaining_size >= attribute_size) {
                    // attribute won't be limiting
                    continue;
                } else {
                    // Vertex attribute does not fit in vertex buffer
                    vertex_attributes_count = 0;
                }
            }

            if (vertex_binding_desc.desc.inputRate == VK_VERTEX_INPUT_RATE_VERTEX) {
                if (!vertex_attribute_fetch_limit_vertex_input_rate.has_value()) {
                    vertex_attribute_fetch_limit_vertex_input_rate = VertexAttributeFetchLimit{};
                }

                vertex_attribute_fetch_limit_vertex_input_rate->max_vertex_attributes_count =
                    std::min(vertex_attribute_fetch_limit_vertex_input_rate->max_vertex_attributes_count, vertex_attributes_count);
                if (vertex_attribute_fetch_limit_vertex_input_rate->max_vertex_attributes_count == vertex_attributes_count) {
                    vertex_attribute_fetch_limit_vertex_input_rate->binding_info = *vbb;
                    vertex_attribute_fetch_limit_vertex_input_rate->attribute.location = attrib.desc.location;
                    vertex_attribute_fetch_limit_vertex_input_rate->attribute.binding = attrib.desc.binding;
                    vertex_attribute_fetch_limit_vertex_input_rate->attribute.format = attrib.desc.format;
                    vertex_attribute_fetch_limit_vertex_input_rate->attribute.offset = attrib.desc.offset;
                }
            } else if (vertex_binding_desc.desc.inputRate == VK_VERTEX_INPUT_RATE_INSTANCE) {
                if (!vertex_attribute_fetch_limit_instance_input_rate.has_value()) {
                    vertex_attribute_fetch_limit_instance_input_rate = VertexAttributeFetchLimit{};
                }

                vertex_attribute_fetch_limit_instance_input_rate->max_vertex_attributes_count =
                    std::min(vertex_attribute_fetch_limit_instance_input_rate->max_vertex_attributes_count,
                             vertex_attributes_count * vertex_binding_desc.desc.divisor);
                if (vertex_attribute_fetch_limit_instance_input_rate->max_vertex_attributes_count ==
                    (vertex_attributes_count * vertex_binding_desc.desc.divisor)) {
                    vertex_attribute_fetch_limit_instance_input_rate->binding_info = *vbb;
                    vertex_attribute_fetch_limit_instance_input_rate->attribute.location = attrib.desc.location;
                    vertex_attribute_fetch_limit_instance_input_rate->attribute.binding = attrib.desc.binding;
                    vertex_attribute_fetch_limit_instance_input_rate->attribute.format = attrib.desc.format;
                    vertex_attribute_fetch_limit_instance_input_rate->attribute.offset = attrib.desc.offset;
                    vertex_attribute_fetch_limit_instance_input_rate->instance_rate_divisor = vertex_binding_desc.desc.divisor;
                }
            }
        }
    }
    return {vertex_attribute_fetch_limit_vertex_input_rate, vertex_attribute_fetch_limit_instance_input_rate};
}

void RegisterVertexAttributeFetchOobValidation(Validator &gpuav, CommandBufferSubState &cb) {
    if (!gpuav.gpuav_settings.shader_instrumentation.vertex_attribute_fetch_oob) {
        return;
    }

    struct ErrorInfo {
        std::optional<VertexAttributeFetchLimit> vertex_attribute_fetch_limit_vertex_input_rate{};
        std::optional<VertexAttributeFetchLimit> vertex_attribute_fetch_limit_instance_input_rate{};
        std::optional<vvl::IndexBufferBinding> index_buffer_binding{};
    };

    // Used to communicate error info between lambdas
    auto error_info = std::make_shared<ErrorInfo>();

    cb.on_instrumentation_error_logger_register_functions.emplace_back([error_info](Validator &gpuav, CommandBufferSubState &cb,
                                                                                    const LastBound &last_bound) {
        auto local_error_info = std::make_shared<ErrorInfo>();
        *local_error_info = *error_info;
        CommandBufferSubState::InstrumentationErrorLogger inst_error_logger = [local_error_info = std::move(local_error_info)](
                                                                                  Validator& gpuav, const Location& loc,
                                                                                  const uint32_t* error_record,
                                                                                  std::string& out_error_msg,
                                                                                  std::string& out_vuid_msg) {
            if (GetErrorGroup(error_record) != glsl::kErrorGroup_InstIndexedDraw) {
                return false;
            }

            const uint32_t error_sub_code = GetSubError(error_record);
            if (error_sub_code != glsl::kErrorSubCode_IndexedDraw_OOBVertexIndex &&
                error_sub_code != glsl::kErrorSubCode_IndexedDraw_OOBInstanceIndex) {
                return false;
            }

            switch (loc.function) {
                case vvl::Func::vkCmdDrawIndexed:
                    out_vuid_msg = "VUID-vkCmdDrawIndexed-None-02721";
                    break;
                case vvl::Func::vkCmdDrawIndexedIndirectCount:
                case vvl::Func::vkCmdDrawIndexedIndirectCountKHR:
                    out_vuid_msg = "VUID-vkCmdDrawIndexedIndirectCount-None-02721";
                    break;
                case vvl::Func::vkCmdDrawIndexedIndirect:
                    out_vuid_msg = "VUID-vkCmdDrawIndexedIndirect-None-02721";
                    break;
                case vvl::Func::vkCmdDrawMultiIndexedEXT:
                    out_vuid_msg = "VUID-vkCmdDrawMultiIndexedEXT-None-02721";
                    break;
                default:
                    return false;
            }

            assert(local_error_info->vertex_attribute_fetch_limit_vertex_input_rate.has_value() ||
                   local_error_info->vertex_attribute_fetch_limit_instance_input_rate.has_value());
            assert(local_error_info->index_buffer_binding.has_value());

            auto add_vertex_buffer_binding_info =
                [&gpuav, error_sub_code](const VertexAttributeFetchLimit &vertex_attribute_fetch_limit, std::string &out) {
                    out += "Vertex Buffer (";
                    out += gpuav.FormatHandle(vertex_attribute_fetch_limit.binding_info.buffer);
                    out += ") binding info:\n";
                    out += "  - Binding: ";
                    out += std::to_string(vertex_attribute_fetch_limit.attribute.binding);
                    out += '\n';
                    out += "  - Offset: ";
                    out += std::to_string(vertex_attribute_fetch_limit.binding_info.offset);
                    out += " bytes\n";
                    out += "  - Effective Size: ";
                    out += std::to_string(vertex_attribute_fetch_limit.binding_info.effective_size);
                    out += " bytes\n";
                    out += "  - Vertices Count: ";
                    out += std::to_string(vertex_attribute_fetch_limit.max_vertex_attributes_count);
                    out += '\n';
                    out += "  - Stride: ";
                    out += std::to_string(vertex_attribute_fetch_limit.binding_info.stride);
                    out += " bytes\n";
                    if (error_sub_code == glsl::kErrorSubCode_IndexedDraw_OOBInstanceIndex) {
                        if (vertex_attribute_fetch_limit.instance_rate_divisor != vvl::kNoIndex32) {
                            out += "  - Instance rate divisor: ";
                            out += std::to_string(vertex_attribute_fetch_limit.instance_rate_divisor);
                            out += '\n';
                        }
                    }
                };

            auto add_vertex_attribute_info = [](const VertexAttributeFetchLimit &vertex_attribute_fetch_limit, std::string &out) {
                out += "The following VkVertexInputAttributeDescription caused OOB access:\n";
                out += "  - Location: ";
                out += std::to_string(vertex_attribute_fetch_limit.attribute.location);
                out += '\n';
                out += "  - Binding: ";
                out += std::to_string(vertex_attribute_fetch_limit.attribute.binding);
                out += '\n';
                out += "  - Format: ";
                out += string_VkFormat(vertex_attribute_fetch_limit.attribute.format);
                out += '\n';
                out += "  - Offset: ";
                out += std::to_string(vertex_attribute_fetch_limit.attribute.offset);
                out += " bytes\n";
            };

            if (error_sub_code == glsl::kErrorSubCode_IndexedDraw_OOBVertexIndex) {
                out_error_msg += "Vertex index ";
                const uint32_t oob_vertex_index = error_record[glsl::kHeader_StageInfoOffset_0];
                out_error_msg += std::to_string(oob_vertex_index);
            } else if (error_sub_code == glsl::kErrorSubCode_IndexedDraw_OOBInstanceIndex) {
                out_error_msg += "Instance index ";
                const uint32_t oob_instance_index = error_record[glsl::kHeader_StageInfoOffset_1];
                out_error_msg += std::to_string(oob_instance_index);
                const uint32_t instance_rate_divisor =
                    local_error_info->vertex_attribute_fetch_limit_instance_input_rate->instance_rate_divisor;
                if (instance_rate_divisor > 1 && instance_rate_divisor != vvl::kNoIndex32) {
                    out_error_msg += " (or ";
                    out_error_msg += std::to_string(oob_instance_index / instance_rate_divisor);
                    out_error_msg += " if divided by instance rate divisor of ";
                    out_error_msg += std::to_string(instance_rate_divisor);
                    out_error_msg += ")";
                }
            }

            if (error_sub_code == glsl::kErrorSubCode_IndexedDraw_OOBVertexIndex) {
                out_error_msg += ", using VK_VERTEX_INPUT_RATE_VERTEX, has caused an OOB access within a bound vertex buffer.\n";

            } else if (error_sub_code == glsl::kErrorSubCode_IndexedDraw_OOBInstanceIndex) {
                out_error_msg += ", using VK_VERTEX_INPUT_RATE_INSTANCE, has caused an OOB access within a bound vertex buffer.\n";
            }

            if (error_sub_code == glsl::kErrorSubCode_IndexedDraw_OOBVertexIndex) {
                add_vertex_buffer_binding_info(*local_error_info->vertex_attribute_fetch_limit_vertex_input_rate, out_error_msg);
                add_vertex_attribute_info(*local_error_info->vertex_attribute_fetch_limit_vertex_input_rate, out_error_msg);

            } else if (error_sub_code == glsl::kErrorSubCode_IndexedDraw_OOBInstanceIndex) {
                add_vertex_buffer_binding_info(*local_error_info->vertex_attribute_fetch_limit_instance_input_rate, out_error_msg);
                add_vertex_attribute_info(*local_error_info->vertex_attribute_fetch_limit_instance_input_rate, out_error_msg);
            }

            if (error_sub_code == glsl::kErrorSubCode_IndexedDraw_OOBVertexIndex) {
                const uint32_t index_bits_size = GetIndexBitsSize(local_error_info->index_buffer_binding->index_type);
                const uint32_t max_indices_in_buffer =
                    static_cast<uint32_t>(local_error_info->index_buffer_binding->size / (index_bits_size / 8u));
                out_error_msg += "Index Buffer (";
                out_error_msg += gpuav.FormatHandle(local_error_info->index_buffer_binding->buffer);
                out_error_msg += ") binding info:\n";
                out_error_msg += "  - Type: ";
                out_error_msg += string_VkIndexType(local_error_info->index_buffer_binding->index_type);
                out_error_msg += '\n';
                out_error_msg += "  - Offset: ";
                out_error_msg += std::to_string(local_error_info->index_buffer_binding->offset);
                out_error_msg += " bytes\n";
                out_error_msg += "  - Size: ";
                out_error_msg += std::to_string(local_error_info->index_buffer_binding->size);
                out_error_msg += " bytes (sizeof(";
                out_error_msg += string_VkIndexType(local_error_info->index_buffer_binding->index_type);
                out_error_msg += ") * ";
                out_error_msg += std::to_string(max_indices_in_buffer);
                out_error_msg += ")\n";
            }
            out_error_msg +=
                "Note: Vertex buffer binding size is the effective, valid one, based on how the VkBuffer was created and "
                "the vkCmdBindVertexBuffers parameters. So it can be clamped up to 0 if binding was invalid.";

            return true;
        };

        return inst_error_logger;
    });

    cb.on_instrumentation_desc_set_update_functions.emplace_back(
        [&gpuav, error_info](CommandBufferSubState &cb, VkPipelineBindPoint bind_point, const Location &loc,
                             VkDescriptorBufferInfo &out_buffer_info, uint32_t &out_dst_binding) {
            if (!vvl::IsCommandDrawVertex(loc.function)) {
                return;
            }

            if (vvl::IsCommandDrawVertexIndexed(loc.function)) {
                vko::BufferRange vertex_attribute_fetch_limits_buffer_range =
                    cb.gpu_resources_manager.GetHostCoherentBufferRange(4 * sizeof(uint32_t));
                if (vertex_attribute_fetch_limits_buffer_range.buffer == VK_NULL_HANDLE) {
                    return;
                }

                auto vertex_attribute_fetch_limits_buffer_ptr =
                    (uint32_t *)vertex_attribute_fetch_limits_buffer_range.offset_mapped_ptr;

                const auto [vertex_attribute_fetch_limit_vertex_input_rate, vertex_attribute_fetch_limit_instance_input_rate] =
                    GetVertexAttributeFetchLimits(cb.base);
                if (vertex_attribute_fetch_limit_vertex_input_rate.has_value()) {
                    vertex_attribute_fetch_limits_buffer_ptr[0] = 1u;
                    vertex_attribute_fetch_limits_buffer_ptr[1] =
                        (uint32_t)vertex_attribute_fetch_limit_vertex_input_rate->max_vertex_attributes_count;
                } else {
                    vertex_attribute_fetch_limits_buffer_ptr[0] = 0u;
                }

                if (vertex_attribute_fetch_limit_instance_input_rate.has_value()) {
                    vertex_attribute_fetch_limits_buffer_ptr[2] = 1u;
                    vertex_attribute_fetch_limits_buffer_ptr[3] =
                        (uint32_t)vertex_attribute_fetch_limit_instance_input_rate->max_vertex_attributes_count;
                } else {
                    vertex_attribute_fetch_limits_buffer_ptr[2] = 0u;
                }

                error_info->vertex_attribute_fetch_limit_vertex_input_rate = vertex_attribute_fetch_limit_vertex_input_rate;
                error_info->vertex_attribute_fetch_limit_instance_input_rate = vertex_attribute_fetch_limit_instance_input_rate;
                error_info->index_buffer_binding = cb.base.index_buffer_binding;

                out_buffer_info.buffer = vertex_attribute_fetch_limits_buffer_range.buffer;
                out_buffer_info.offset = vertex_attribute_fetch_limits_buffer_range.offset;
                out_buffer_info.range = vertex_attribute_fetch_limits_buffer_range.size;
            } else {
                // Point all non-indexed draws to our global buffer that will bypass the check in shader
                VertexAttributeFetchOff &resource = gpuav.shared_resources_cache.GetOrCreate<VertexAttributeFetchOff>(gpuav);
                if (!resource.valid) {
                    return;
                }
                out_buffer_info.buffer = resource.buffer.VkHandle();
                out_buffer_info.offset = 0;
                out_buffer_info.range = VK_WHOLE_SIZE;
            }

            out_dst_binding = glsl::kBindingInstVertexAttributeFetchLimits;
        });
}

}  // namespace gpuav