From 10f7fc21c76368414ae14115f6ae04effeb12dc1 Mon Sep 17 00:00:00 2001 From: natterman12 <92529018+natterman12@users.noreply.github.com> Date: Mon, 23 Nov 2026 22:15:38 -0500 Subject: [PATCH] d3d12: ExecuteIndirect accepts the argument ranges newer D3D12 runtimes accept PC D3D12 runtimes disagree on five ExecuteIndirect cases. Older Windows 11 builds bound max_count * stride and check the argument offset at a zero count; newer ones (D3D12Core 10.0.26100.9818 on Windows 11 26300, WARP and NVIDIA alike) bound (max_count - 1) * stride + record_size and check nothing at a zero count. e1ec785b2 moved the layer to the older rule, so d3d12_execute_indirect_validation failed on every newer host, as it had failed on older ones before. The layer takes the wider rule again, in recording (d3d12_com.cpp) and in replay (vulkan_replay.h): it is the rule vkCmdDraw*Indirect states, no byte outside the buffer is read, and a refusal drops the whole command list for a call a shipping runtime runs. The test marks the five cases host_varies: the layer has to accept them and a live host may answer either way, so the test stops failing on one group of machines or the other. Two cases that neither rule accepts are added, and the positive GPU path replays both disputed shapes with validation on. Co-Authored-By: Claude Fable 5.2 --- runtime/shims/d3d12_com.cpp | 19 +++++-- runtime/src/vulkan_replay.h | 14 ++--- ...d3d12_execute_indirect_validation_test.cpp | 56 ++++++++++++++----- 3 files changed, 63 insertions(+), 26 deletions(-) diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4f4904e965..bb9331b348 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -835,18 +835,25 @@ class GraphicsCommandList final return check(false); } // The record size is checked for every call: D3D12 rejects a signature whose stride cannot hold a record - // even when no command runs. The argument span is what the signature can walk, max_command_count * stride: - // the host refuses a range that ends past the buffer (`240` with stride `20` in a `256`-byte buffer), and - // it checks the offset itself when max_command_count is 0, where the required bytes are none. + // even when no command runs. const UINT64 record_size = kind == leonida::IndirectKind::draw ? sizeof(D3D12_DRAW_ARGUMENTS) : kind == leonida::IndirectKind::draw_indexed ? sizeof(D3D12_DRAW_INDEXED_ARGUMENTS) : kind == leonida::IndirectKind::dispatch ? sizeof(D3D12_DISPATCH_ARGUMENTS) : 0; if (!record_size || stride < record_size || stride % 4 != 0) return check(false); - if (argument_buffer_offset > arg_desc.Width || - UINT64(max_command_count) > (arg_desc.Width - argument_buffer_offset) / stride) - return check(false); + // The argument range is the bytes the commands read: (max_command_count - 1) * stride + record_size, and + // nothing when max_command_count is 0. PC D3D12 runtimes do not agree here. Some bound max_command_count * + // stride and check the offset at a zero count; others (D3D12Core 10.0.26100.9818, WARP and NVIDIA alike) + // accept a last record that fits while its stride runs past the buffer, and any aligned offset at a zero + // count. The layer takes the wider rule: it is the one Vulkan's own indirect draws use, no byte outside the + // buffer is read, and refusing here drops the whole command list for a call a real runtime runs. + // d3d12_execute_indirect_validation_test lists the five cases the runtimes split on. + if (max_command_count) { + const UINT64 required = UINT64(max_command_count - 1) * stride + record_size; + if (argument_buffer_offset > arg_desc.Width || arg_desc.Width - argument_buffer_offset < required) + return check(false); + } leonida::ExecuteIndirect cmd{}; cmd.kind = kind; cmd.stride = stride; diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index d5a73b909d..e780f491ed 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -3071,13 +3071,13 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun const VkDeviceSize record_size = detail::indirect_record_size(ind->kind); if (!record_size || ind->stride < record_size || ind->stride % sizeof(std::uint32_t)) return ReplayResult::invalid_indirect_arguments; - // The span is max_count * stride, the bytes the signature can walk, as D3D12 measures it: a record that - // fits at the end of the buffer while its stride runs past it is refused (`240` with stride `20` in a - // `256`-byte buffer). A zero max_count still has to name an offset inside the buffer. - if (ind->argument_offset > p.binding.size || - ind->max_count > (p.binding.size - ind->argument_offset) / ind->stride || - !detail::indirect_range(p.binding, ind->argument_offset, - VkDeviceSize(ind->max_count) * ind->stride)) + // The range is the bytes the commands read, (max_count - 1) * stride + record_size: the rule recording + // applies (d3d12_com.cpp, ExecuteIndirect) and the one vkCmdDraw*Indirect states. A zero max_count is a + // no-op that recording never passes on; if one arrives it reads nothing and needs no range. + if (ind->max_count + ? !detail::indirect_range(p.binding, ind->argument_offset, + VkDeviceSize(ind->max_count - 1) * ind->stride + record_size) + : (p.binding.buffer_offset | ind->argument_offset) % sizeof(std::uint32_t) != 0) return ReplayResult::invalid_indirect_arguments; if (ind->count_buffer) { const ResourceBinding* cnt = resolve(ind->count_buffer); diff --git a/runtime/test/d3d12_execute_indirect_validation_test.cpp b/runtime/test/d3d12_execute_indirect_validation_test.cpp index a2fb98a412..39a4961f82 100644 --- a/runtime/test/d3d12_execute_indirect_validation_test.cpp +++ b/runtime/test/d3d12_execute_indirect_validation_test.cpp @@ -125,8 +125,12 @@ struct Env { struct Case { const char* name; - bool expected_ok; // Measured on Windows 11 WARP and RTX 5090 HW D3D12 + bool expected_ok; // What the layer answers. Outside host_varies it is also what every measured host answers. std::function record; + // PC D3D12 runtimes disagree on this case, so a live host may answer either way and only the layer is judged. + // Measured: WARP and an RTX 5060 refuse all five such cases (e1ec785b2); WARP and an RTX 3080 Ti on D3D12Core + // 10.0.26100.9818 accept all five. The layer accepts them (d3d12_com.cpp, ExecuteIndirect, says why). + bool host_varies = false; }; bool run_case(Env& env, const Case& c, HRESULT& out_hr) { @@ -212,12 +216,12 @@ std::vector parity_cases() { [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 260, nullptr, 0); }}, {"arg_offset=256 ==width (max=0 no-op)", true, [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 256, nullptr, 0); }}, - {"arg_offset=260 >width (max=0 no-op)", false, - [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 260, nullptr, 0); }}, + {"arg_offset=260 >width (max=0 no-op)", true, + [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 260, nullptr, 0); }, true}, {"arg_offset=U64MAX-3 wrap (max=1)", false, [=](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, kU64Max - 3, nullptr, 0); }}, - {"arg_offset=U64MAX-3 aligned (max=0 no-op)", false, - [=](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, kU64Max - 3, nullptr, 0); }}, + {"arg_offset=U64MAX-3 aligned (max=0 no-op)", true, + [=](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, kU64Max - 3, nullptr, 0); }, true}, {"arg_offset=U64MAX unaligned+wrap (max=1)", false, [=](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, kU64Max, nullptr, 0); }}, {"arg_offset=240 max=2 cnt=null (OOB)", false, @@ -233,17 +237,23 @@ std::vector parity_cases() { {"arg_buf width=3 (max=1 OOB)", false, [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf3.p, 0, nullptr, 0); }}, - // Padded stride: the host bounds max_count * stride, not (max_count - 1) * stride + record_size, so a - // record that fits at the end of the buffer while its stride runs past it is refused - {"draw stride=20 off=240 max=1 (240+20>256)", false, - [](Env& e) { e.list->ExecuteIndirect(e.draw20.p, 1, e.buf256.p, 240, nullptr, 0); }}, - {"draw stride=20 off=220 max=2 (220+40>256)", false, - [](Env& e) { e.list->ExecuteIndirect(e.draw20.p, 2, e.buf256.p, 220, nullptr, 0); }}, - {"dispatch stride=20 off=244 max=1 (244+20>256)", false, + // Padded stride: the last record fits at the end of the buffer while its stride runs past it. A runtime + // that bounds max_count * stride refuses these; one that bounds (max_count - 1) * stride + record_size + // accepts them, and so does the layer. + {"draw stride=20 off=240 max=1 (240+16==256)", true, + [](Env& e) { e.list->ExecuteIndirect(e.draw20.p, 1, e.buf256.p, 240, nullptr, 0); }, true}, + {"draw stride=20 off=220 max=2 (220+20+16==256)", true, + [](Env& e) { e.list->ExecuteIndirect(e.draw20.p, 2, e.buf256.p, 220, nullptr, 0); }, true}, + {"dispatch stride=20 off=244 max=1 (244+12==256)", true, [](Env& e) { e.bind_compute_if_available(); e.list->ExecuteIndirect(e.dispatch20.p, 1, e.buf256.p, 244, nullptr, 0); - }}, + }, true}, + // One byte group further and the record itself no longer fits: refused under either rule. + {"draw stride=20 off=244 max=1 (244+16>256)", false, + [](Env& e) { e.list->ExecuteIndirect(e.draw20.p, 1, e.buf256.p, 244, nullptr, 0); }}, + {"draw stride=20 off=224 max=2 (224+20+16>256)", false, + [](Env& e) { e.list->ExecuteIndirect(e.draw20.p, 2, e.buf256.p, 224, nullptr, 0); }}, {"draw_indexed stride=24 off=240 max=1 (OOB)", false, [](Env& e) { e.list->ExecuteIndirect(e.draw_indexed24.p, 1, e.buf256.p, 240, nullptr, 0); }}, @@ -304,6 +314,11 @@ bool run_positive_gpu_dispatch(Env& env) { // Exact-fit indirect dispatch (244 + 12 == 256) plus max_command_count == 0 no-op at the buffer's end. list->ExecuteIndirect(env.dispatch12.p, 1, args.p, 244, nullptr, 0); list->ExecuteIndirect(env.dispatch12.p, 0, args.p, 256, nullptr, 0); + // The two shapes PC runtimes split on, which the layer accepts: the same record read through a padded stride + // that runs past the buffer, and a zero count at an offset outside it. Replayed with validation on, so a rule + // that let Vulkan read past the buffer would show up as a validation error below. + list->ExecuteIndirect(env.dispatch20.p, 1, args.p, 244, nullptr, 0); + list->ExecuteIndirect(env.dispatch12.p, 0, args.p, 260, nullptr, 0); CHECK(SUCCEEDED(list->Close())); ID3D12CommandList* lists[] = {list.p}; @@ -430,6 +445,7 @@ int main() { std::filesystem::remove_all(fixture, ec); unsigned failures = 0; + unsigned varied_accepted = 0, varied_refused = 0; // live host answers on the host_varies cases std::printf("%-48s | %-10s | %-10s | %-10s\n", "case", "layer", "WARP", hw_ran ? hw_name.c_str() : "HW"); for (std::size_t i = 0; i < table.size(); ++i) { const bool layer_ok = SUCCEEDED(layer_hr[i]); @@ -440,12 +456,26 @@ int main() { static_cast(layer_hr[i]), warp_ran ? (SUCCEEDED(warp_hr[i]) ? "S_OK" : "FAIL") : (warp_ok ? "(S_OK)" : "(FAIL)"), hw_ran ? (SUCCEEDED(hw_hr[i]) ? "S_OK" : "FAIL") : (hw_ok ? "(S_OK)" : "(FAIL)")); + if (table[i].host_varies) { + // Runtimes split on this case: the hosts are reported, the layer alone has to answer as expected. + if (warp_ran) ++(warp_ok ? varied_accepted : varied_refused); + if (hw_ran) ++(hw_ok ? varied_accepted : varied_refused); + if (layer_ok != table[i].expected_ok) { + std::fprintf(stderr, "FAIL %s: layer=%d expected=%d (hosts differ by D3D12 runtime: warp=%d hw=%d)\n", + table[i].name, int(layer_ok), int(table[i].expected_ok), int(warp_ok), int(hw_ok)); + ++failures; + } + continue; + } if (layer_ok != warp_ok || layer_ok != hw_ok || layer_ok != table[i].expected_ok) { std::fprintf(stderr, "FAIL %s: layer=%d warp=%d hw=%d expected=%d\n", table[i].name, int(layer_ok), int(warp_ok), int(hw_ok), int(table[i].expected_ok)); ++failures; } } + if (varied_accepted || varied_refused) + std::printf("runtime-dependent cases: the live hosts accepted %u and refused %u answers; the layer accepts " + "all of them\n", varied_accepted, varied_refused); const unsigned validation_errors = vk.validation_errors() - errors_before; if (validation_errors) {