From 8fc4abbf015f4b7859c652dd10462fec26c5974b Mon Sep 17 00:00:00 2001 From: Moriz82 <72836413+Moriz82@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:20:48 -0600 Subject: [PATCH] d3d12: check field offsets within structured buffer SRV records (SHADER-BUFFER-BOUNDS) A structured SRV used the typed index-only bounds mode, allowing a load beyond the end of one record to read the next. Select the same field-offset bounds mode as a structured UAV, consistent with five-star-recomp 3d287ab. Leave raw and typed views unchanged. Main plus the test failed at the first structured descriptor. The child passed both descriptor checks and all 128 structured and typed GPU readbacks on Proton. The synthetic module passed SPIR-V validation. AI-assisted (Codex and Claude Code agents). Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 --- docs/buffer_bounds.md | 3 +++ runtime/shims/d3d12_vulkan_device.cpp | 3 ++- runtime/test/d3d12_compute_test.cpp | 20 ++++++++++++++------ runtime/test/glsl/cs_user_data.comp | 7 ++++--- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/docs/buffer_bounds.md b/docs/buffer_bounds.md index 9326fe5171..1f7afa5472 100644 --- a/docs/buffer_bounds.md +++ b/docs/buffer_bounds.md @@ -17,6 +17,9 @@ measured natively on AMD GPUs (SHADER-116). The translator reads this field and | 2 disabled | `N == 0` | same | same | | 3 raw | `offset + soffset + 4 > N`; `index * stride` moves the address but is not checked | `offset >= N`: differs | `offset + 4 > N`, without `soffset` | +Buffer SRVs use mode 3 for raw views, mode 0 for structured views and mode 1 for typed views. +A structured SRV checks the field offset against the record stride, as a structured UAV does. + - **RDNA2 follows Mesa's GFX11 column**, not the GFX10 one: of 732 cases, 729 match GFX11 and 696 match GFX10. Every GFX10 miss is an access that only partly fits, for example a raw buffer with `N = 3` read at offset 0. - **In raw mode `soffset` is part of the checked offset.** The 3 cases that match neither column are raw reads at diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 97ed334a11..6550a2476c 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -6471,7 +6471,8 @@ class Device final buffer.stride = std::uint32_t(stride); buffer.num_records = desc->Buffer.NumElements; buffer.format = leonida::gfx10::format_from_vulkan(VK_FORMAT_R32_UINT); - buffer.oob_select = leonida::gfx10::OobSelect::structured; + // Check the field offset against the record stride, as for structured UAVs (five-star-recomp 3d287ab). + buffer.oob_select = leonida::gfx10::OobSelect::structured_with_offset; } else { const Format fmt = vulkan_format(desc->Format, false); if (fmt.format == VK_FORMAT_UNDEFINED || fmt.typeless || !(fmt.aspects & VK_IMAGE_ASPECT_COLOR_BIT)) { diff --git a/runtime/test/d3d12_compute_test.cpp b/runtime/test/d3d12_compute_test.cpp index 5ba3ded525..ae39d0c03d 100644 --- a/runtime/test/d3d12_compute_test.cpp +++ b/runtime/test/d3d12_compute_test.cpp @@ -772,7 +772,7 @@ int main() { EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &deny_buf_desc, D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&srv_deny.p)))); - const auto srv_rb_desc = buffer_desc(6 * bytes, D3D12_RESOURCE_FLAG_NONE); + const auto srv_rb_desc = buffer_desc(8 * bytes, D3D12_RESOURCE_FLAG_NONE); EXPECT(SUCCEEDED(device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &srv_rb_desc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&srv_readback.p)))); @@ -848,10 +848,10 @@ int main() { EXPECT(decode_slot(3, d3)); EXPECT(d3.address == srv_va && d3.stride == 16 && d3.num_records == 32 && d3.format == leonida::gfx10::format_from_vulkan(VK_FORMAT_R32_UINT) && - d3.oob_select == leonida::gfx10::OobSelect::structured); + d3.oob_select == leonida::gfx10::OobSelect::structured_with_offset); EXPECT(decode_slot(4, d4)); EXPECT(d4.address == srv_va + 256 && d4.stride == 16 && d4.num_records == 32 && - d4.oob_select == leonida::gfx10::OobSelect::structured); + d4.oob_select == leonida::gfx10::OobSelect::structured_with_offset); EXPECT(decode_slot(5, d5)); const leonida::gfx10::Swizzle want_swz = {leonida::gfx10::select_z, leonida::gfx10::select_y, leonida::gfx10::select_x, leonida::gfx10::select_one}; @@ -887,7 +887,7 @@ int main() { EXPECT(expect_zero_srv(srv_default.p, &bad)); // out of bounds (70 * 16 > 1024) EXPECT(expect_zero_srv(nullptr, &raw_a)); // null resource -> null V# - // Upload buffer contents to arena and dispatch 6 GPU passes through the shader-visible descriptor table. + // Upload buffer contents to arena and dispatch 8 GPU passes through the shader-visible descriptor table. // out_a is already in D3D12_RESOURCE_STATE_UNORDERED_ACCESS at the end of the CBV pass. EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, pso.p))); list->CopyBufferRegion(srv_default.p, 0, srv_upload.p, 0, buf_bytes); @@ -901,6 +901,7 @@ int main() { const struct SrvDispatch { UINT slot_idx; UINT readback_offset; + UINT field_offset = 0; } srv_dispatches[] = { {1, 0 * bytes}, {2, 1 * bytes}, @@ -908,10 +909,12 @@ int main() { {4, 3 * bytes}, {5, 4 * bytes}, {7, 5 * bytes}, + {3, 6 * bytes, 16}, // first dword past the structured record + {5, 7 * bytes, 16}, // same offset with the typed index-only check }; - const std::uint32_t srv_mode[2] = {0u, 0xFFFFFFFFu}; // s1 == 0xFFFFFFFF selects buffer SRV V# mode - list->SetComputeRoot32BitConstants(0, 2, srv_mode, 0); for (const auto& cfg : srv_dispatches) { + const std::uint32_t args[2] = {cfg.field_offset, 0xFFFFFFFFu}; + list->SetComputeRoot32BitConstants(0, 2, args, 0); list->SetComputeRootDescriptorTable(2, slot_gpu(cfg.slot_idx)); list->Dispatch(1, 1, 1); const auto to_src = @@ -937,6 +940,11 @@ int main() { std::memcpy(srv_struct_b.data(), rb + 3 * bytes, bytes); std::memcpy(srv_typed.data(), rb + 4 * bytes, bytes); std::memcpy(srv_null.data(), rb + 5 * bytes, bytes); + const auto* cross_record = static_cast(srv_res) + 6 * elements; + for (UINT i = 0; i < elements; ++i) { + EXPECT(cross_record[i] == 0u); + EXPECT(cross_record[elements + i] == (i < 24 ? 0x50000u + (9u + i) * 4u : 0u)); + } } for (auto* r : {&out_a, &out_b, &out_c, &constants, &readback}) r->reset(); diff --git a/runtime/test/glsl/cs_user_data.comp b/runtime/test/glsl/cs_user_data.comp index b285c0f3f9..0e2b2a56b3 100644 --- a/runtime/test/glsl/cs_user_data.comp +++ b/runtime/test/glsl/cs_user_data.comp @@ -60,9 +60,10 @@ void main() { if (oob_select == 3u) { const uint byte_off = pc.s[4] + i * 4u; result.w[i] = (byte_off + 4u <= num_records) ? at(base).w[byte_off >> 2u] : 0u; - } else if (oob_select == 1u && stride >= 4u) { - const uint idx = pc.s[4] + i; - result.w[i] = (idx < num_records) ? at(base).w[(idx * stride) >> 2u] : 0u; + } else if (oob_select <= 1u && stride >= 4u) { + const uint field = pc.s[4]; + const bool in_bounds = i < num_records && (oob_select == 1u || field <= stride - 4u); + result.w[i] = in_bounds ? at(base).w[(i * stride + field) >> 2u] : 0u; } else { result.w[i] = 0u; }