Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -3016,13 +3016,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);
Expand Down
56 changes: 43 additions & 13 deletions runtime/test/d3d12_execute_indirect_validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(Env&)> 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) {
Expand Down Expand Up @@ -212,12 +216,12 @@ std::vector<Case> 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,
Expand All @@ -233,17 +237,23 @@ std::vector<Case> 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); }},

Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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]);
Expand All @@ -440,12 +456,26 @@ int main() {
static_cast<unsigned long>(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) {
Expand Down
Loading