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/src/root_signature.cpp | 317 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 317 insertions(+) create mode 100644 runtime/src/root_signature.cpp (limited to 'runtime/src/root_signature.cpp') diff --git a/runtime/src/root_signature.cpp b/runtime/src/root_signature.cpp new file mode 100644 index 000000000..eda380198 --- /dev/null +++ b/runtime/src/root_signature.cpp @@ -0,0 +1,317 @@ +// Portable root-signature packer, see root_signature.h. +#include "root_signature.h" + +namespace rt::rootsig { + +PackResult pack(const RootSignature& sig) { + PackResult out; + if (sig.params.size() > 64) { + out.error = "root signature has more than 64 root parameters"; + return out; + } + uint32_t cost = 0; + for (const RootParameter& p : sig.params) { + if (p.type == ParamType::Constants && p.num_values == 0) { + out.error = "root constant parameter needs at least 1 dword"; + return out; + } + const uint32_t dwords = p.type == ParamType::Table ? 2 + : p.type == ParamType::Descriptor ? 2 + : p.num_values; + cost += param_cost(p.type, p.num_values); + if (cost > kMaxD3d12Cost) { + out.error = "root signature exceeds the 64-dword D3D12 cost limit"; + return out; + } + if (out.dword_count + dwords > kMaxUserSgprs) { + // Spill layout is unknown (see __XB_GetRootSignatureSpillPointer): + // refuse loudly instead of silently truncating every draw. + out.error = "root signature needs more than 32 user SGPRs (spill unsupported)"; + return out; + } + const uint32_t param_index = + static_cast(&p - sig.params.data()); + for (uint32_t i = 0; i < dwords; ++i) { + out.dwords[out.dword_count] = 0; // filled at bind time + out.provenance.push_back({param_index, i}); + ++out.dword_count; + } + } + out.d3d12_cost = cost; + out.ok = true; + return out; +} + +namespace { + +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)); +} + +void put_f32(std::vector& out, float v) { + uint32_t bits = 0; + static_assert(sizeof(bits) == sizeof(v)); + __builtin_memcpy(&bits, &v, sizeof(bits)); + put_u32(out, bits); +} + +class Reader { +public: + Reader(const uint8_t* data, size_t size) : data_(data), size_(size) {} + + [[nodiscard]] bool u32(uint32_t& v) { + if (pos_ + 4 > size_) return false; + v = static_cast(data_[pos_]) | + (static_cast(data_[pos_ + 1]) << 8) | + (static_cast(data_[pos_ + 2]) << 16) | + (static_cast(data_[pos_ + 3]) << 24); + pos_ += 4; + return true; + } + + [[nodiscard]] bool f32(float& v) { + uint32_t bits = 0; + if (!u32(bits)) return false; + __builtin_memcpy(&v, &bits, sizeof(v)); + return true; + } + + [[nodiscard]] bool at_end() const { return pos_ == size_; } + +private: + const uint8_t* data_ = nullptr; + size_t size_ = 0; + size_t pos_ = 0; +}; + +constexpr uint32_t kCanonicalMagic = 0x3153524Cu; // "LRS1" little-endian +constexpr uint32_t kCanonicalVersion = 1; + +} // namespace + +std::vector encode_canonical(const RootSignature& sig) { + std::vector out; + put_u32(out, kCanonicalMagic); + put_u32(out, kCanonicalVersion); + put_u32(out, static_cast(sig.version)); + put_u32(out, static_cast(sig.params.size())); + put_u32(out, static_cast(sig.static_samplers.size())); + put_u32(out, sig.flags); + for (const RootParameter& p : sig.params) { + put_u32(out, static_cast(p.type)); + put_u32(out, static_cast(p.visibility)); + put_u32(out, p.num_values); + put_u32(out, p.shader_register); + put_u32(out, p.register_space); + put_u32(out, p.descriptor_register); + put_u32(out, p.descriptor_space); + put_u32(out, static_cast(p.ranges.size())); + for (const DescriptorRange& r : p.ranges) { + put_u32(out, r.range_type); + put_u32(out, r.num_descriptors); + } + } + for (const StaticSampler& s : sig.static_samplers) { + put_u32(out, s.filter); + put_u32(out, s.address_u); + put_u32(out, s.address_v); + put_u32(out, s.address_w); + put_f32(out, s.mip_lod_bias); + put_u32(out, s.max_anisotropy); + put_u32(out, s.comparison_func); + put_u32(out, s.border_color); + put_f32(out, s.min_lod); + put_f32(out, s.max_lod); + put_u32(out, s.shader_register); + put_u32(out, s.register_space); + put_u32(out, static_cast(s.visibility)); + } + return out; +} + +DecodeResult decode_canonical(const uint8_t* data, size_t size) { + DecodeResult out; + if (data == nullptr && size != 0) { + out.error = "null data with nonzero size"; + return out; + } + Reader r(data == nullptr ? reinterpret_cast("") : data, size); + uint32_t magic = 0, version = 0, nparams = 0, nsamplers = 0; + if (!r.u32(magic) || magic != kCanonicalMagic) { + out.error = "bad canonical magic"; + return out; + } + if (!r.u32(version) || version != kCanonicalVersion) { + out.error = "unsupported canonical version"; + return out; + } + uint32_t ver = 0; + if (!r.u32(ver) || !r.u32(nparams) || !r.u32(nsamplers) || !r.u32(out.sig.flags)) { + out.error = "truncated canonical header"; + return out; + } + if (ver != 1 && ver != 2) { + out.error = "unknown root-signature version"; + return out; + } + if (nparams > 64) { + out.error = "more than 64 root parameters"; + return out; + } + out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1; + out.sig.params.reserve(nparams); + for (uint32_t i = 0; i < nparams; ++i) { + RootParameter p; + uint32_t type = 0, vis = 0, nranges = 0; + if (!r.u32(type) || !r.u32(vis) || !r.u32(p.num_values) || !r.u32(p.shader_register) || + !r.u32(p.register_space) || !r.u32(p.descriptor_register) || + !r.u32(p.descriptor_space) || !r.u32(nranges)) { + out.error = "truncated canonical parameter"; + return out; + } + if (type > 2) { + out.error = "unknown parameter type"; + return out; + } + p.type = static_cast(type); + p.visibility = static_cast(vis); + if (nranges > 1024) { + out.error = "absurd range count"; + return out; + } + p.ranges.reserve(nranges); + for (uint32_t j = 0; j < nranges; ++j) { + DescriptorRange range; + if (!r.u32(range.range_type) || !r.u32(range.num_descriptors)) { + out.error = "truncated canonical range"; + return out; + } + p.ranges.push_back(range); + } + out.sig.params.push_back(std::move(p)); + } + out.sig.static_samplers.reserve(nsamplers); + for (uint32_t i = 0; i < nsamplers; ++i) { + 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)) { + out.error = "truncated canonical sampler"; + return out; + } + s.visibility = static_cast(vis); + out.sig.static_samplers.push_back(s); + } + if (!r.at_end()) { + out.error = "trailing bytes after canonical root signature"; + return out; + } + out.ok = true; + return out; +} + +// RTS0 payload hypothesis: u32 version (1 = 1.0, 2 = 1.1), u32 num_params, +// 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) { + 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)) { + out.error = "truncated RTS0 header"; + return out; + } + if (ver != 1 && ver != 2) { + out.error = "unknown RTS0 version (not 1.0/1.1)"; + return out; + } + if (nparams > 64) { + out.error = "RTS0 with more than 64 root parameters"; + return out; + } + out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1; + out.sig.flags = flags; + 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"; + return out; + } + p.type = static_cast(type); + 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"; + return out; + } + } else if (p.type == ParamType::Descriptor) { + if (!r.u32(p.descriptor_register) || !r.u32(p.descriptor_space)) { + out.error = "truncated RTS0 descriptor"; + return out; + } + } else { + uint32_t nranges = 0; + if (!r.u32(nranges)) { + out.error = "truncated RTS0 table"; + return out; + } + if (nranges > 1024) { + out.error = "absurd RTS0 range count"; + return out; + } + p.ranges.reserve(nranges); + for (uint32_t j = 0; j < nranges; ++j) { + 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)) { + out.error = "truncated RTS0 range"; + return out; + } + (void)base_reg; + (void)space; + (void)offset; + p.ranges.push_back(range); + } + } + out.sig.params.push_back(std::move(p)); + } + out.sig.static_samplers.reserve(nsamplers); + for (uint32_t i = 0; i < nsamplers; ++i) { + 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)) { + 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; +} + +} // namespace rt::rootsig -- cgit v1.2.3 From d2debe87af9fab6c1039ecdbc6dbb5bbb4022653 Mon Sep 17 00:00:00 2001 From: SuarezPM Date: Sat, 21 Nov 2026 17:15:29 -0300 Subject: Fix MSVC build: std::memcpy instead of __builtin_memcpy The 'build' and 'check' jobs failed with C3861 on MSVC 14.52: __builtin_memcpy is GCC/Clang-only. std::memcpy () works everywhere the portable model compiles. --- runtime/src/root_signature.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'runtime/src/root_signature.cpp') diff --git a/runtime/src/root_signature.cpp b/runtime/src/root_signature.cpp index eda380198..4892ecb8c 100644 --- a/runtime/src/root_signature.cpp +++ b/runtime/src/root_signature.cpp @@ -1,6 +1,8 @@ // Portable root-signature packer, see root_signature.h. #include "root_signature.h" +#include + namespace rt::rootsig { PackResult pack(const RootSignature& sig) { @@ -54,7 +56,7 @@ void put_u32(std::vector& out, uint32_t v) { void put_f32(std::vector& out, float v) { uint32_t bits = 0; static_assert(sizeof(bits) == sizeof(v)); - __builtin_memcpy(&bits, &v, sizeof(bits)); + std::memcpy(&bits, &v, sizeof(bits)); put_u32(out, bits); } @@ -75,7 +77,7 @@ public: [[nodiscard]] bool f32(float& v) { uint32_t bits = 0; if (!u32(bits)) return false; - __builtin_memcpy(&v, &bits, sizeof(v)); + std::memcpy(&v, &bits, sizeof(v)); return true; } -- cgit v1.2.3 From ab270f39d6f283975ead43092f0929fe081c2e72 Mon Sep 17 00:00:00 2001 From: SuarezPM Date: Sat, 21 Nov 2026 17:24:55 -0300 Subject: Harden RTS0/oracle path: bound sampler counts, dump blob bytes on parse The Windows oracle test segfaulted in CI. Most likely cause: an unbounded static-sampler reserve driven by real-blob bytes under a wrong layout hypothesis. Both decoders now refuse absurd sampler counts, and the oracle test prints the blob size plus the first 32 bytes before parsing so the next CI log shows the true D3D12 layout either way. --- runtime/src/root_signature.cpp | 8 ++++++++ runtime/test/root_signature_oracle_test.cpp | 8 ++++++++ 2 files changed, 16 insertions(+) (limited to 'runtime/src/root_signature.cpp') diff --git a/runtime/src/root_signature.cpp b/runtime/src/root_signature.cpp index 4892ecb8c..8b43136be 100644 --- a/runtime/src/root_signature.cpp +++ b/runtime/src/root_signature.cpp @@ -163,6 +163,10 @@ DecodeResult decode_canonical(const uint8_t* data, size_t size) { out.error = "more than 64 root parameters"; return out; } + if (nsamplers > 4096) { + out.error = "absurd sampler count"; + return out; + } out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1; out.sig.params.reserve(nparams); for (uint32_t i = 0; i < nparams; ++i) { @@ -241,6 +245,10 @@ DecodeResult parse_rts0(const uint8_t* data, size_t size) { out.error = "RTS0 with more than 64 root parameters"; return out; } + if (nsamplers > 4096) { + out.error = "RTS0 with absurd sampler count"; + return out; + } out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1; out.sig.flags = flags; out.sig.params.reserve(nparams); diff --git a/runtime/test/root_signature_oracle_test.cpp b/runtime/test/root_signature_oracle_test.cpp index 51a2017ad..33d0bfb42 100644 --- a/runtime/test/root_signature_oracle_test.cpp +++ b/runtime/test/root_signature_oracle_test.cpp @@ -48,6 +48,14 @@ D3D12_VERSIONED_ROOT_SIGNATURE_DESC make_desc( bool parse_blob(ID3DBlob* blob, rt::rootsig::RootSignature& model) { const auto* bytes = static_cast(blob->GetBufferPointer()); const size_t size = blob->GetBufferSize(); + // Diagnostics first: if the layout hypothesis is wrong, the CI log shows + // the real bytes instead of just a crash or a bare failure. + std::printf("oracle blob: %zu bytes: ", size); + for (size_t i = 0; i < 32 && i < size; ++i) { + std::printf("%02x", bytes[i]); + } + std::printf("\n"); + std::fflush(stdout); const rt::rootsig::DecodeResult d = rt::rootsig::parse_rts0(bytes, size); if (!d.ok) { std::printf("oracle parse failed: %s\n", d.error.c_str()); -- 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/src/root_signature.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/src/root_signature.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