diff options
| author | SuarezPM <SuarezPM@users.noreply.github.com> | 2026-11-21 17:31:08 -0300 |
|---|---|---|
| committer | SuarezPM <SuarezPM@users.noreply.github.com> | 2026-11-21 17:31:08 -0300 |
| commit | 87989cea5f1080dac729d5407aa3fa19cb3f8fd3 (patch) | |
| tree | 83f0812413e3315487fccdda5f6e654f12870001 /runtime/src/root_signature.cpp | |
| parent | ab270f39d6f283975ead43092f0929fe081c2e72 (diff) | |
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.
Diffstat (limited to 'runtime/src/root_signature.cpp')
| -rw-r--r-- | runtime/src/root_signature.cpp | 64 |
1 files changed, 63 insertions, 1 deletions
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) { | |||
| 225 | // u32 num_static_samplers, u32 flags, then per-parameter fields in d3d12.h | 225 | // u32 num_static_samplers, u32 flags, then per-parameter fields in d3d12.h |
| 226 | // order. Static samplers follow the same field order as D3D12_STATIC_SAMPLER_DESC. | 226 | // order. Static samplers follow the same field order as D3D12_STATIC_SAMPLER_DESC. |
| 227 | // Anything else (v1.2 blobs, extra dwords, truncation) is a hard error. | 227 | // Anything else (v1.2 blobs, extra dwords, truncation) is a hard error. |
| 228 | DecodeResult parse_rts0(const uint8_t* data, size_t size) { | 228 | // Payload decoder: the RTS0 chunk contents (see header for the hypothesis). |
| 229 | static DecodeResult parse_rts0_payload(const uint8_t* data, size_t size) { | ||
| 229 | DecodeResult out; | 230 | DecodeResult out; |
| 230 | Reader r(data == nullptr ? reinterpret_cast<const uint8_t*>("") : data, size); | 231 | Reader r(data == nullptr ? reinterpret_cast<const uint8_t*>("") : data, size); |
| 231 | uint32_t ver = 0, nparams = 0, nsamplers = 0, flags = 0; | 232 | uint32_t ver = 0, nparams = 0, nsamplers = 0, flags = 0; |
| @@ -324,4 +325,65 @@ DecodeResult parse_rts0(const uint8_t* data, size_t size) { | |||
| 324 | return out; | 325 | return out; |
| 325 | } | 326 | } |
| 326 | 327 | ||
| 328 | DecodeResult parse_rts0(const uint8_t* data, size_t size) { | ||
| 329 | DecodeResult out; | ||
| 330 | if ((data == nullptr && size != 0) || (data != nullptr && size < 4)) { | ||
| 331 | out.error = "blob too small for a DXBC container"; | ||
| 332 | return out; | ||
| 333 | } | ||
| 334 | if (data == nullptr) { | ||
| 335 | out.error = "null blob"; | ||
| 336 | return out; | ||
| 337 | } | ||
| 338 | // A bare payload (unit-test path) has no DXBC magic: decode directly. | ||
| 339 | if (!(data[0] == 'D' && data[1] == 'X' && data[2] == 'B' && data[3] == 'C')) { | ||
| 340 | return parse_rts0_payload(data, size); | ||
| 341 | } | ||
| 342 | // DXBC container walk (mirrors input_signature in tools/rdna2spirv.py): | ||
| 343 | // +28: chunk count, +32: chunk offsets, each chunk is FourCC + u32 size. | ||
| 344 | auto at = [&](size_t off, uint32_t& v) { | ||
| 345 | if (off + 4 > size) return false; | ||
| 346 | v = static_cast<uint32_t>(data[off]) | (static_cast<uint32_t>(data[off + 1]) << 8) | | ||
| 347 | (static_cast<uint32_t>(data[off + 2]) << 16) | (static_cast<uint32_t>(data[off + 3]) << 24); | ||
| 348 | return true; | ||
| 349 | }; | ||
| 350 | uint32_t nchunks = 0; | ||
| 351 | if (!at(28, nchunks)) { | ||
| 352 | out.error = "truncated DXBC header"; | ||
| 353 | return out; | ||
| 354 | } | ||
| 355 | if (nchunks > 1024) { | ||
| 356 | out.error = "absurd DXBC chunk count"; | ||
| 357 | return out; | ||
| 358 | } | ||
| 359 | for (uint32_t k = 0; k < nchunks; ++k) { | ||
| 360 | uint32_t off = 0; | ||
| 361 | // NOTE: off and payload_size are u32; promote to size_t BEFORE adding | ||
| 362 | // or a hostile container wraps the check and turns it into an OOB read. | ||
| 363 | if (!at(32 + 4 * static_cast<size_t>(k), off) || | ||
| 364 | static_cast<size_t>(off) + 8 > size) { | ||
| 365 | out.error = "truncated DXBC chunk table"; | ||
| 366 | return out; | ||
| 367 | } | ||
| 368 | if (data[off] == 'R' && data[off + 1] == 'T' && data[off + 2] == 'S' && data[off + 3] == '0') { | ||
| 369 | uint32_t payload_size = 0; | ||
| 370 | if (static_cast<size_t>(off) + 8 > size) { | ||
| 371 | out.error = "truncated RTS0 chunk header"; | ||
| 372 | return out; | ||
| 373 | } | ||
| 374 | payload_size = static_cast<uint32_t>(data[off + 4]) | | ||
| 375 | (static_cast<uint32_t>(data[off + 5]) << 8) | | ||
| 376 | (static_cast<uint32_t>(data[off + 6]) << 16) | | ||
| 377 | (static_cast<uint32_t>(data[off + 7]) << 24); | ||
| 378 | if (static_cast<size_t>(off) + 8 + payload_size > size) { | ||
| 379 | out.error = "RTS0 chunk overruns its container"; | ||
| 380 | return out; | ||
| 381 | } | ||
| 382 | return parse_rts0_payload(data + off + 8, payload_size); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | out.error = "no RTS0 chunk in DXBC container"; | ||
| 386 | return out; | ||
| 387 | } | ||
| 388 | |||
| 327 | } // namespace rt::rootsig | 389 | } // namespace rt::rootsig |