From 612f1edebdcf5add10d90c259a1a457c86313a93 Mon Sep 17 00:00:00 2001 From: SuarezPM Date: Sat, 21 Nov 2026 17:04:28 -0300 Subject: Root signature packer + reconciler + hard-fail (fix the least verifiable part of the layer) AI-assisted (Muse Spark + explorer fleet). Implements docs/d3d12_vulkan.md section 3.5 as code: a portable D3D12 root-signature model (1.0/1.1, no d3d12.h) plus a packer that lays root parameters into the 32 user-SGPR dwords in root-parameter order (constants take N, views/tables take 2, static samplers take none). Anything that does not fit - more than 32 user SGPRs (spill layout unknown) or more than the 64-dword D3D12 cost limit - is refused loudly instead of truncated. tools/root_sig_contract.py reconciles a candidate signature against every shader's user_sgpr_reads from spirv/index.json (const32 vs constant dwords, 64-bit VAs vs view/table pairs, unpaired-hi detection) and exits nonzero on the first contradiction; --cluster shows which read-set shapes the game actually needs. The RTS0 chunk reader is marked hypothesis until the new Windows oracle test (synthetic descs serialized by the real d3d12.dll) confirms the layout; Tier A (model/packer/canonical codec/RTS0 self-consistency/reconciler) runs everywhere with no game data and no GPU. Tested: python -m pytest tests/test_root_sig_contract.py (12 passed); full suite 651 passed, 20 skipped, 1 xfailed; compileall + make_game --help; portable C++ test 17/17 PASS under g++ -Wall -Wextra -Wpedantic -Werror with ASan+UBSan; turing atlas on RTX 2060 SUPER (subgroup 32, mesh/bary/m8/heap true): compute32/ballot64/f16_input/maintenance8 valid, broken_negative correctly invalid. Windows-only Tier B (oracle + MSVC build) runs in CI. --- runtime/test/root_signature_test.cpp | 172 +++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 runtime/test/root_signature_test.cpp (limited to 'runtime/test/root_signature_test.cpp') diff --git a/runtime/test/root_signature_test.cpp b/runtime/test/root_signature_test.cpp new file mode 100644 index 000000000..aff0d782a --- /dev/null +++ b/runtime/test/root_signature_test.cpp @@ -0,0 +1,172 @@ +// Portable tests for runtime/src/root_signature.{h,cpp} (no windows.h). +// +// Own main + PASS/FAIL lines like the other runtime tests; returns nonzero +// on any failure so CTest gates on it. Everything here is hand-built: no +// game data, no DXBC, no serializer needed. +#include +#include +#include + +#include "root_signature.h" + +namespace { + +int g_failures = 0; + +void check(bool condition, const char* what) { + std::printf("%s %s\n", condition ? "PASS" : "FAIL", what); + if (!condition) ++g_failures; +} + +rt::rootsig::RootParameter consts(uint32_t n) { + rt::rootsig::RootParameter p; + p.type = rt::rootsig::ParamType::Constants; + p.num_values = n; + return p; +} + +rt::rootsig::RootParameter view() { + rt::rootsig::RootParameter p; + p.type = rt::rootsig::ParamType::Descriptor; + return p; +} + +rt::rootsig::RootParameter table() { + rt::rootsig::RootParameter p; + p.type = rt::rootsig::ParamType::Table; + p.ranges.push_back({0, 8}); + return p; +} + +void put_u32(std::vector& out, uint32_t v) { + out.push_back(static_cast(v)); + out.push_back(static_cast(v >> 8)); + out.push_back(static_cast(v >> 16)); + out.push_back(static_cast(v >> 24)); +} + +} // namespace + +int main() { + using namespace rt::rootsig; + + // Mixed signature: 4 constants + CBV + table + 1 static sampler. + { + RootSignature sig; + sig.params = {consts(4), view(), table()}; + StaticSampler s; + s.shader_register = 3; + sig.static_samplers = {s}; + const PackResult r = pack(sig); + check(r.ok, "mixed signature packs"); + check(r.dword_count == 8, "mixed signature uses 8 dwords"); + check(r.d3d12_cost == 4 + 2 + 1, "mixed signature costs 7"); + check(r.provenance.size() == 8 && r.provenance[0].param_index == 0 && + r.provenance[0].sub_index == 0 && r.provenance[4].param_index == 1 && + r.provenance[4].sub_index == 0 && r.provenance[5].sub_index == 1 && + r.provenance[6].param_index == 2, + "provenance tracks param and sub-dword"); + } + + // Exactly 32 dwords still fits. + { + RootSignature sig; + for (int i = 0; i < 16; ++i) sig.params.push_back(view()); + const PackResult r = pack(sig); + check(r.ok && r.dword_count == 32, "16 views fill 32 dwords"); + } + + // 34 dwords: spill is refused loudly, never truncated. + { + RootSignature sig; + for (int i = 0; i < 17; ++i) sig.params.push_back(view()); + const PackResult r = pack(sig); + check(!r.ok && r.error.find("spill") != std::string::npos, + "17 views refused as spill"); + } + + // 65 constants exceed the 64-dword D3D12 cost limit. + { + RootSignature sig; + sig.params = {consts(65)}; + const PackResult r = pack(sig); + check(!r.ok && r.error.find("64-dword") != std::string::npos, + "65 constants refused by cost limit"); + } + + // Canonical codec round-trips byte-exactly, sampler included. + { + RootSignature sig; + sig.version = Version::V1_0; + sig.params = {consts(2), table()}; + sig.flags = 0x03000000u; // Xbox high byte survives the model + StaticSampler s; + s.filter = 21; + s.mip_lod_bias = 0.5f; + sig.static_samplers = {s}; + const std::vector bytes = encode_canonical(sig); + const DecodeResult d = decode_canonical(bytes.data(), bytes.size()); + check(d.ok, "canonical round-trip decodes"); + check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 2 && + d.sig.params[1].ranges.size() == 1 && + d.sig.static_samplers.size() == 1 && d.sig.flags == 0x03000000u && + d.sig.static_samplers[0].filter == 21, + "canonical round-trip preserves model"); + } + + // Canonical decoder controls: bad magic, truncation, trailing bytes. + { + const std::vector bad = {0, 1, 2, 3}; + check(!decode_canonical(bad.data(), bad.size()).ok, "bad magic rejected"); + RootSignature sig; + sig.params = {consts(1)}; + const std::vector bytes = encode_canonical(sig); + check(!decode_canonical(bytes.data(), bytes.size() - 1).ok, + "truncated canonical rejected"); + std::vector trailed = bytes; + trailed.push_back(0); + check(!decode_canonical(trailed.data(), trailed.size()).ok, + "trailing bytes rejected"); + } + + // RTS0 hypothesis: a hand-built blob per the documented layout parses. + { + std::vector blob; + put_u32(blob, 2); // version 1.1 + put_u32(blob, 2); // 2 params + put_u32(blob, 0); // 0 samplers + put_u32(blob, 0x03000000u); // Xbox flags + put_u32(blob, 0); // Constants + put_u32(blob, 0); // visibility All + put_u32(blob, 7); // register (ignored by packer) + put_u32(blob, 0); // space + put_u32(blob, 3); // 3 values + put_u32(blob, 1); // Descriptor + put_u32(blob, 4); // visibility Pixel + put_u32(blob, 0); // register + put_u32(blob, 0); // space + const DecodeResult d = parse_rts0(blob.data(), blob.size()); + check(d.ok, "hand-built RTS0 parses"); + check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 3 && + d.sig.flags == 0x03000000u, + "RTS0 model matches hand-built blob"); + const PackResult r = pack(d.sig); + check(r.ok && r.dword_count == 3 + 2, "parsed RTS0 packs to 5 dwords"); + } + + // RTS0 controls: unknown version and truncation fail loudly. + { + std::vector blob; + put_u32(blob, 9); + put_u32(blob, 0); + put_u32(blob, 0); + put_u32(blob, 0); + const DecodeResult d = parse_rts0(blob.data(), blob.size()); + check(!d.ok, "unknown RTS0 version rejected"); + std::vector cut = {1, 0}; + check(!parse_rts0(cut.data(), cut.size()).ok, "truncated RTS0 rejected"); + } + + if (g_failures != 0) std::printf("%d FAILURES\n", g_failures); + return g_failures == 0 ? 0 : 1; +} -- cgit v1.2.3 From 87989cea5f1080dac729d5407aa3fa19cb3f8fd3 Mon Sep 17 00:00:00 2001 From: SuarezPM Date: Sat, 21 Nov 2026 17:31:08 -0300 Subject: Parse RTS0 via DXBC chunk walk; fix OOB wrap and FourCC typo The MS serializer returns a DXBC container, not a bare payload (proven by the CI blob dump starting with DXBC). parse_rts0 now walks the chunk table like input_signature does and decodes the RTS0 payload. Also fixed: 'O' (0x4F) vs '0' (0x30) in the FourCC check, which silently missed every chunk, and u32 arithmetic wrap in the bounds checks (a hostile offset could wrap off+8 past the guard into an OOB read). Adds a hand-built DXBC container test covering the walk path. --- runtime/src/root_signature.cpp | 64 +++++++++++++++++++++++++++++++++++- runtime/test/root_signature_test.cpp | 34 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) (limited to 'runtime/test/root_signature_test.cpp') diff --git a/runtime/src/root_signature.cpp b/runtime/src/root_signature.cpp index 8b43136be..99446c155 100644 --- a/runtime/src/root_signature.cpp +++ b/runtime/src/root_signature.cpp @@ -225,7 +225,8 @@ DecodeResult decode_canonical(const uint8_t* data, size_t size) { // u32 num_static_samplers, u32 flags, then per-parameter fields in d3d12.h // order. Static samplers follow the same field order as D3D12_STATIC_SAMPLER_DESC. // Anything else (v1.2 blobs, extra dwords, truncation) is a hard error. -DecodeResult parse_rts0(const uint8_t* data, size_t size) { +// Payload decoder: the RTS0 chunk contents (see header for the hypothesis). +static DecodeResult parse_rts0_payload(const uint8_t* data, size_t size) { DecodeResult out; Reader r(data == nullptr ? reinterpret_cast("") : data, size); uint32_t ver = 0, nparams = 0, nsamplers = 0, flags = 0; @@ -324,4 +325,65 @@ DecodeResult parse_rts0(const uint8_t* data, size_t size) { return out; } +DecodeResult parse_rts0(const uint8_t* data, size_t size) { + DecodeResult out; + if ((data == nullptr && size != 0) || (data != nullptr && size < 4)) { + out.error = "blob too small for a DXBC container"; + return out; + } + if (data == nullptr) { + out.error = "null blob"; + return out; + } + // A bare payload (unit-test path) has no DXBC magic: decode directly. + if (!(data[0] == 'D' && data[1] == 'X' && data[2] == 'B' && data[3] == 'C')) { + return parse_rts0_payload(data, size); + } + // DXBC container walk (mirrors input_signature in tools/rdna2spirv.py): + // +28: chunk count, +32: chunk offsets, each chunk is FourCC + u32 size. + auto at = [&](size_t off, uint32_t& v) { + if (off + 4 > size) return false; + v = static_cast(data[off]) | (static_cast(data[off + 1]) << 8) | + (static_cast(data[off + 2]) << 16) | (static_cast(data[off + 3]) << 24); + return true; + }; + uint32_t nchunks = 0; + if (!at(28, nchunks)) { + out.error = "truncated DXBC header"; + return out; + } + if (nchunks > 1024) { + out.error = "absurd DXBC chunk count"; + return out; + } + for (uint32_t k = 0; k < nchunks; ++k) { + uint32_t off = 0; + // NOTE: off and payload_size are u32; promote to size_t BEFORE adding + // or a hostile container wraps the check and turns it into an OOB read. + if (!at(32 + 4 * static_cast(k), off) || + static_cast(off) + 8 > size) { + out.error = "truncated DXBC chunk table"; + return out; + } + if (data[off] == 'R' && data[off + 1] == 'T' && data[off + 2] == 'S' && data[off + 3] == '0') { + uint32_t payload_size = 0; + if (static_cast(off) + 8 > size) { + out.error = "truncated RTS0 chunk header"; + return out; + } + payload_size = static_cast(data[off + 4]) | + (static_cast(data[off + 5]) << 8) | + (static_cast(data[off + 6]) << 16) | + (static_cast(data[off + 7]) << 24); + if (static_cast(off) + 8 + payload_size > size) { + out.error = "RTS0 chunk overruns its container"; + return out; + } + return parse_rts0_payload(data + off + 8, payload_size); + } + } + out.error = "no RTS0 chunk in DXBC container"; + return out; +} + } // namespace rt::rootsig diff --git a/runtime/test/root_signature_test.cpp b/runtime/test/root_signature_test.cpp index aff0d782a..531371e10 100644 --- a/runtime/test/root_signature_test.cpp +++ b/runtime/test/root_signature_test.cpp @@ -167,6 +167,40 @@ int main() { check(!parse_rts0(cut.data(), cut.size()).ok, "truncated RTS0 rejected"); } + // DXBC container walk: a hand-built container with one RTS0 chunk parses + // through the chunk table (same path the MS serializer blob takes). + { + std::vector payload; + put_u32(payload, 2); // version 1.1 + put_u32(payload, 1); // 1 param + put_u32(payload, 0); // 0 samplers + put_u32(payload, 0); // flags + put_u32(payload, 0); // Constants + put_u32(payload, 0); // visibility All + put_u32(payload, 0); // register + put_u32(payload, 0); // space + put_u32(payload, 2); // 2 values + std::vector blob; + blob.insert(blob.end(), {'D', 'X', 'B', 'C'}); + blob.insert(blob.end(), 16, 0); // hash + put_u32(blob, 1); // version + put_u32(blob, 0); // file size (patched below) + put_u32(blob, 1); // 1 chunk + put_u32(blob, 36); // chunk offset: 4 magic + 16 hash + 4 ver + 4 size + 4 count + 4 offset + blob.insert(blob.end(), {'R', 'T', 'S', '0'}); + put_u32(blob, static_cast(payload.size())); + blob.insert(blob.end(), payload.begin(), payload.end()); + const uint32_t total = static_cast(blob.size()); + blob[24] = static_cast(total); + blob[25] = static_cast(total >> 8); + blob[26] = static_cast(total >> 16); + blob[27] = static_cast(total >> 24); + const DecodeResult d = parse_rts0(blob.data(), blob.size()); + check(d.ok, "DXBC container walk finds RTS0"); + check(d.sig.params.size() == 1 && d.sig.params[0].num_values == 2, + "walked RTS0 model matches payload"); + } + if (g_failures != 0) std::printf("%d FAILURES\n", g_failures); return g_failures == 0 ? 0 : 1; } -- cgit v1.2.3 From 71d39cefb9b513364bdbbfce75210d8f895596a2 Mon Sep 17 00:00:00 2001 From: SuarezPM Date: Sat, 21 Nov 2026 17:40:40 -0300 Subject: Rework RTS0 parser to offset-table layout; harden oracle test Per maintainer feedback: the real blob is a DXBC container whose RTS0 chunk starts version, numParameters, parametersOffset, numStaticSamplers, staticSamplersOffset, flags, with parameters as a {type, visibility, offset} table and payloads at their offsets (d3d types follow D3D12_ROOT_PARAMETER_TYPE numbering, not the model's enum order). Also fixes the oracle segfault mechanism: return early when parsing fails instead of indexing an empty model, and bounds every offset addition in size_t so hostile containers cannot wrap the guard into an OOB read. Adds a wild-offset control plus a DXBC-walk test (19 portable checks green under ASan+UBSan). --- runtime/src/root_signature.cpp | 133 +++++++++++++++++++++------- runtime/src/root_signature.h | 24 ++--- runtime/test/root_signature_oracle_test.cpp | 5 ++ runtime/test/root_signature_test.cpp | 71 +++++++++------ 4 files changed, 162 insertions(+), 71 deletions(-) (limited to 'runtime/test/root_signature_test.cpp') diff --git a/runtime/src/root_signature.cpp b/runtime/src/root_signature.cpp index 99446c155..79a633b61 100644 --- a/runtime/src/root_signature.cpp +++ b/runtime/src/root_signature.cpp @@ -228,13 +228,39 @@ DecodeResult decode_canonical(const uint8_t* data, size_t size) { // Payload decoder: the RTS0 chunk contents (see header for the hypothesis). static DecodeResult parse_rts0_payload(const uint8_t* data, size_t size) { DecodeResult out; - Reader r(data == nullptr ? reinterpret_cast("") : data, size); - uint32_t ver = 0, nparams = 0, nsamplers = 0, flags = 0; if (data == nullptr && size != 0) { out.error = "null data with nonzero size"; return out; } - if (!r.u32(ver) || !r.u32(nparams) || !r.u32(nsamplers) || !r.u32(flags)) { + if (data == nullptr) { + out.error = "null RTS0 payload"; + return out; + } + // Random access into the payload: every (offset, byte-count) pair is + // validated before any byte is touched. Offsets count from payload start. + auto at = [&](size_t off, size_t bytes) -> const uint8_t* { + if (bytes > size || off > size - bytes) return nullptr; + return data + off; + }; + auto u32_at = [&](size_t off, uint32_t& v) { + const uint8_t* p = at(off, 4); + if (p == nullptr) return false; + v = static_cast(p[0]) | (static_cast(p[1]) << 8) | + (static_cast(p[2]) << 16) | (static_cast(p[3]) << 24); + return true; + }; + auto f32_at = [&](size_t off, float& v) { + uint32_t bits = 0; + if (!u32_at(off, bits)) return false; + static_assert(sizeof(bits) == sizeof(v)); + std::memcpy(&v, &bits, sizeof(v)); + return true; + }; + // Header: version, num_params, params_offset, num_samplers, + // samplers_offset, flags. + uint32_t ver = 0, nparams = 0, params_off = 0, nsamplers = 0, samplers_off = 0, flags = 0; + if (!u32_at(0, ver) || !u32_at(4, nparams) || !u32_at(8, params_off) || + !u32_at(12, nsamplers) || !u32_at(16, samplers_off) || !u32_at(20, flags)) { out.error = "truncated RTS0 header"; return out; } @@ -252,75 +278,116 @@ static DecodeResult parse_rts0_payload(const uint8_t* data, size_t size) { } out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1; out.sig.flags = flags; + // Parameter table at params_off: num_params x {u32 d3d_type, u32 + // visibility, u32 offset}. d3d_type follows D3D12_ROOT_PARAMETER_TYPE + // (0 table, 1 constants, 2/3/4 CBV/SRV/UAV root descriptors). + constexpr size_t kEntryBytes = 12; + if (nparams > 0 && + (params_off > size || nparams > (size - params_off) / kEntryBytes)) { + out.error = "RTS0 parameter table overruns its payload"; + return out; + } out.sig.params.reserve(nparams); for (uint32_t i = 0; i < nparams; ++i) { - RootParameter p; - uint32_t type = 0, vis = 0; - if (!r.u32(type) || !r.u32(vis)) { - out.error = "truncated RTS0 parameter"; - return out; - } - if (type > 2) { - out.error = "unknown RTS0 parameter type"; + const size_t eoff = static_cast(params_off) + i * kEntryBytes; + uint32_t dtype = 0, vis = 0, poff = 0; + if (!u32_at(eoff, dtype) || !u32_at(eoff + 4, vis) || !u32_at(eoff + 8, poff)) { + out.error = "truncated RTS0 parameter entry"; return out; } - p.type = static_cast(type); + RootParameter p; p.visibility = static_cast(vis); - if (p.type == ParamType::Constants) { - if (!r.u32(p.shader_register) || !r.u32(p.register_space) || !r.u32(p.num_values)) { - out.error = "truncated RTS0 constants"; + if (dtype == 1) { + p.type = ParamType::Constants; + if (!u32_at(poff, p.shader_register) || !u32_at(poff + 4, p.register_space) || + !u32_at(poff + 8, p.num_values)) { + out.error = "truncated RTS0 constants payload"; return out; } - } else if (p.type == ParamType::Descriptor) { - if (!r.u32(p.descriptor_register) || !r.u32(p.descriptor_space)) { - out.error = "truncated RTS0 descriptor"; + } else if (dtype == 2 || dtype == 3 || dtype == 4) { + p.type = ParamType::Descriptor; + uint32_t dflags = 0; + const size_t need = ver == 2 ? 12 : 8; + if (poff > size || need > size - static_cast(poff)) { + out.error = "truncated RTS0 descriptor payload"; return out; } - } else { + if (!u32_at(poff, p.descriptor_register) || !u32_at(poff + 4, p.descriptor_space)) { + out.error = "truncated RTS0 descriptor payload"; + return out; + } + if (ver == 2 && !u32_at(poff + 8, dflags)) { + out.error = "truncated RTS0 descriptor flags"; + return out; + } + (void)dflags; // descriptor flags do not affect the user-SGPR map + } else if (dtype == 0) { + p.type = ParamType::Table; uint32_t nranges = 0; - if (!r.u32(nranges)) { - out.error = "truncated RTS0 table"; + if (!u32_at(poff, nranges)) { + out.error = "truncated RTS0 table payload"; return out; } if (nranges > 1024) { out.error = "absurd RTS0 range count"; return out; } + // Range record: {type, num, base_reg, space, offset} (+ flags on 1.1). + const size_t stride = ver == 2 ? 24 : 20; + if (nranges > 0 && + (poff + 4 > size || nranges > (size - (poff + 4)) / stride)) { + out.error = "RTS0 ranges overrun their payload"; + return out; + } p.ranges.reserve(nranges); for (uint32_t j = 0; j < nranges; ++j) { + const size_t roff = static_cast(poff) + 4 + j * stride; DescriptorRange range; - uint32_t base_reg = 0, space = 0, offset = 0; - if (!r.u32(range.range_type) || !r.u32(range.num_descriptors) || !r.u32(base_reg) || - !r.u32(space) || !r.u32(offset)) { + uint32_t base_reg = 0, space = 0, disp_off = 0, rflags = 0; + if (!u32_at(roff, range.range_type) || !u32_at(roff + 4, range.num_descriptors) || + !u32_at(roff + 8, base_reg) || !u32_at(roff + 12, space) || + !u32_at(roff + 16, disp_off) || + (ver == 2 && !u32_at(roff + 20, rflags))) { out.error = "truncated RTS0 range"; return out; } (void)base_reg; (void)space; - (void)offset; + (void)disp_off; + (void)rflags; p.ranges.push_back(range); } + } else { + out.error = "unknown RTS0 parameter type"; + return out; } out.sig.params.push_back(std::move(p)); } + // Sampler records mirror D3D12_STATIC_SAMPLER_DESC field order. + constexpr size_t kSamplerBytes = 13 * 4; + if (nsamplers > 0 && + (samplers_off > size || nsamplers > (size - samplers_off) / kSamplerBytes)) { + out.error = "RTS0 samplers overrun their payload"; + return out; + } out.sig.static_samplers.reserve(nsamplers); for (uint32_t i = 0; i < nsamplers; ++i) { + const size_t soff = static_cast(samplers_off) + i * kSamplerBytes; StaticSampler s; uint32_t vis = 0; - if (!r.u32(s.filter) || !r.u32(s.address_u) || !r.u32(s.address_v) || !r.u32(s.address_w) || - !r.f32(s.mip_lod_bias) || !r.u32(s.max_anisotropy) || !r.u32(s.comparison_func) || - !r.u32(s.border_color) || !r.f32(s.min_lod) || !r.f32(s.max_lod) || - !r.u32(s.shader_register) || !r.u32(s.register_space) || !r.u32(vis)) { + if (!u32_at(soff, s.filter) || !u32_at(soff + 4, s.address_u) || + !u32_at(soff + 8, s.address_v) || !u32_at(soff + 12, s.address_w) || + !f32_at(soff + 16, s.mip_lod_bias) || !u32_at(soff + 20, s.max_anisotropy) || + !u32_at(soff + 24, s.comparison_func) || !u32_at(soff + 28, s.border_color) || + !f32_at(soff + 32, s.min_lod) || !f32_at(soff + 36, s.max_lod) || + !u32_at(soff + 40, s.shader_register) || !u32_at(soff + 44, s.register_space) || + !u32_at(soff + 48, vis)) { out.error = "truncated RTS0 sampler"; return out; } s.visibility = static_cast(vis); out.sig.static_samplers.push_back(s); } - if (!r.at_end()) { - out.error = "trailing bytes after RTS0 root signature"; - return out; - } out.ok = true; return out; } diff --git a/runtime/src/root_signature.h b/runtime/src/root_signature.h index c17bf4276..4c19cf72d 100644 --- a/runtime/src/root_signature.h +++ b/runtime/src/root_signature.h @@ -27,15 +27,15 @@ enum class ParamType : uint32_t { Table = 2, // DESCRIPTOR_TABLE: 2 dwords (table GPU VA) }; -// D3D12_SHADER_VISIBILITY (D3D12 order: ALL 0, VERTEX 1, HULL 2, DOMAIN 3, -// GEOMETRY 4, PIXEL 5). +// D3D12_SHADER_VISIBILITY (ALL 0, VERTEX 1, HULL 2, DOMAIN 3, +// GEOMETRY 4, PIXEL 5, AMPLIFICATION 6, MESH 7). enum class Visibility : uint32_t { All = 0, Vertex = 1, Geometry = 4, Pixel = 5, - Compute = 6, // no D3D12 value; Xbox extension bucket, opaque for packing - Mesh = 7, // Xbox extension bucket; treated as opaque for packing + Amplification = 6, + Mesh = 7, }; // One descriptor range inside a table (SRV/CBV/UAV/sampler + count). @@ -133,13 +133,17 @@ struct DecodeResult { [[nodiscard]] DecodeResult decode_canonical(const uint8_t* data, size_t size); -// RTS0 chunk parser (HYPOTHESIS until the Windows oracle test passes). +// RTS0 chunk parser (payload layout per the maintainer: offset table). // -// The DXBC "RTS0" chunk is the console serializer's output for one root -// signature. Its exact byte layout is undocumented, so this decodes it as -// the straight-line flattening of the public descriptor structs (version, -// counts, then per-parameter fields in d3d12.h order). Every deviation the -// oracle test finds is a hard failure to investigate, not a quirk to absorb. +// The input is a whole DXBC container (what D3D12SerializeVersionedRootSignature +// returns: "DXBC" magic, hash, size, chunk table). The parser walks the chunk +// table exactly like tools/rdna2spirv.py:input_signature does, finds the +// "RTS0" chunk and decodes its payload: version, counts and table offsets, +// then a parameter table of {d3d_type, visibility, offset} entries with +// payloads at their offsets (d3d types follow D3D12_ROOT_PARAMETER_TYPE +// numbering). A bare payload without the DXBC header is also accepted +// (unit-test path). Every deviation the oracle test finds is a hard failure +// to investigate, not a quirk to absorb. // Returns ok == false with a reason for unknown versions or truncation. [[nodiscard]] DecodeResult parse_rts0(const uint8_t* data, size_t size); diff --git a/runtime/test/root_signature_oracle_test.cpp b/runtime/test/root_signature_oracle_test.cpp index 33d0bfb42..d60dfae3a 100644 --- a/runtime/test/root_signature_oracle_test.cpp +++ b/runtime/test/root_signature_oracle_test.cpp @@ -100,6 +100,11 @@ int main() { RootSignature model; check(parse_blob(blobs.blob, model), "oracle blob parses"); + if (g_failures != 0) { + // Never index into a model that failed to parse (that segfaulted CI). + std::printf("%d FAILURES\n", g_failures); + return 1; + } check(model.params.size() == 2, "oracle model has 2 params"); check(model.params[0].type == ParamType::Constants && model.params[0].num_values == 4 && diff --git a/runtime/test/root_signature_test.cpp b/runtime/test/root_signature_test.cpp index 531371e10..92ef8347f 100644 --- a/runtime/test/root_signature_test.cpp +++ b/runtime/test/root_signature_test.cpp @@ -45,6 +45,32 @@ void put_u32(std::vector& out, uint32_t v) { out.push_back(static_cast(v >> 24)); } +// Hand-built RTS0 payload in the offset-table layout the maintainer +// described: header, parameter table, then payloads at their offsets. +std::vector build_payload() { + std::vector p; + put_u32(p, 2); // version 1.1 + put_u32(p, 2); // 2 params + put_u32(p, 24); // params table offset + put_u32(p, 0); // 0 samplers + put_u32(p, 0); // samplers offset + put_u32(p, 0x03000000u); // Xbox flags + // entry 0: constants at 48; entry 1: CBV descriptor at 60. + put_u32(p, 1); + put_u32(p, 0); + put_u32(p, 48); + put_u32(p, 2); + put_u32(p, 4); + put_u32(p, 60); + put_u32(p, 7); // register (ignored by packer) + put_u32(p, 0); // space + put_u32(p, 3); // 3 values + put_u32(p, 0); // register + put_u32(p, 0); // space + put_u32(p, 0); // 1.1 descriptor flags + return p; +} + } // namespace int main() { @@ -130,22 +156,11 @@ int main() { } // RTS0 hypothesis: a hand-built blob per the documented layout parses. + // Hand-built RTS0 payload in the offset-table layout the maintainer + // described: header, parameter table, then payloads at their offsets. { - std::vector blob; - put_u32(blob, 2); // version 1.1 - put_u32(blob, 2); // 2 params - put_u32(blob, 0); // 0 samplers - put_u32(blob, 0x03000000u); // Xbox flags - put_u32(blob, 0); // Constants - put_u32(blob, 0); // visibility All - put_u32(blob, 7); // register (ignored by packer) - put_u32(blob, 0); // space - put_u32(blob, 3); // 3 values - put_u32(blob, 1); // Descriptor - put_u32(blob, 4); // visibility Pixel - put_u32(blob, 0); // register - put_u32(blob, 0); // space - const DecodeResult d = parse_rts0(blob.data(), blob.size()); + const std::vector payload = build_payload(); + const DecodeResult d = parse_rts0(payload.data(), payload.size()); check(d.ok, "hand-built RTS0 parses"); check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 3 && d.sig.flags == 0x03000000u, @@ -154,10 +169,12 @@ int main() { check(r.ok && r.dword_count == 3 + 2, "parsed RTS0 packs to 5 dwords"); } - // RTS0 controls: unknown version and truncation fail loudly. + // RTS0 controls: unknown version, wild offsets and truncation fail loudly. { std::vector blob; - put_u32(blob, 9); + put_u32(blob, 9); // unknown version + put_u32(blob, 0); + put_u32(blob, 0); put_u32(blob, 0); put_u32(blob, 0); put_u32(blob, 0); @@ -165,21 +182,19 @@ int main() { check(!d.ok, "unknown RTS0 version rejected"); std::vector cut = {1, 0}; check(!parse_rts0(cut.data(), cut.size()).ok, "truncated RTS0 rejected"); + std::vector wild = build_payload(); + // Corrupt the params offset to point past the end. + wild[8] = 0xFF; + wild[9] = 0xFF; + wild[10] = 0xFF; + wild[11] = 0xFF; + check(!parse_rts0(wild.data(), wild.size()).ok, "wild offset rejected"); } // DXBC container walk: a hand-built container with one RTS0 chunk parses // through the chunk table (same path the MS serializer blob takes). { - std::vector payload; - put_u32(payload, 2); // version 1.1 - put_u32(payload, 1); // 1 param - put_u32(payload, 0); // 0 samplers - put_u32(payload, 0); // flags - put_u32(payload, 0); // Constants - put_u32(payload, 0); // visibility All - put_u32(payload, 0); // register - put_u32(payload, 0); // space - put_u32(payload, 2); // 2 values + const std::vector payload = build_payload(); std::vector blob; blob.insert(blob.end(), {'D', 'X', 'B', 'C'}); blob.insert(blob.end(), 16, 0); // hash @@ -197,7 +212,7 @@ int main() { blob[27] = static_cast(total >> 24); const DecodeResult d = parse_rts0(blob.data(), blob.size()); check(d.ok, "DXBC container walk finds RTS0"); - check(d.sig.params.size() == 1 && d.sig.params[0].num_values == 2, + check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 3, "walked RTS0 model matches payload"); } -- cgit v1.2.3