runtime: harden RTS0/DXBC offset checks and pre-size canonical root-signature encoding (RT-ROOT-SIGNATURE-RTS0-OFFSET-WRAP-AND-RESERVE) #2007

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/2007 into main
IlyassRS3 commented 2026-11-25 17:58:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T17:58:19Z.


Claim: RT-ROOT-SIGNATURE-RTS0-OFFSET-WRAP-AND-RESERVE (claim comment #7170207565 on #1608).

Problem

In runtime/src/root_signature.cpp:

  1. 32-bit uint32_t offset addition in parse_rts0_payload(): dtype == 1 (Constants) and dtype == 2..4 (Descriptor) passed poff + 4 and poff + 8 as uint32_t before promoting to size_t (unlike Table, which already promoted static_cast<size_t>(poff) + 4), wrapping offsets near UINT32_MAX.
  2. Addition-based bounds checks in Reader::u32() and parse_rts0(): pos_ + 4 > size_, off + 4 > size, static_cast<size_t>(off) + 8 > size, and static_cast<size_t>(off) + 8 + payload_size > size can wrap on 32-bit size_t, and parse_rts0() repeated the identical off + 8 > size check inside the RTS0 branch.
  3. Repeated vector reallocations and per-byte push_back in encode_canonical(): encode_canonical() grew an unreserved std::vector<uint8_t> one byte at a time (put_u32 doing 4 push_back calls per dword).

Fix

  • Promote poff to size_t pbase = static_cast<size_t>(poff) once per parameter in parse_rts0_payload() and validate payload spans with subtraction (pbase > size || need > size - pbase) for Constants, Descriptor, and Table.
  • Replace addition-based bounds checks in Reader::u32() and parse_rts0() with overflow-free subtraction checks and remove the redundant RTS0 chunk-header check.
  • Pre-compute the exact canonical byte count in encode_canonical() and write little-endian dwords directly into a single pre-sized buffer (write_u32 / write_f32).
  • Add regression tests in runtime/test/root_signature_test.cpp (0 CMakeLists.txt edits) covering wrapping poff (0xFFFFFFFC / 0xFFFFFFF8 / 0xFFFFFFFF) across tables/constants/descriptors, wrapping DXBC chunk offsets and chunk payload sizes, and rich V1.2 canonical round-trips.

Benchmark (clang++ -std=c++20 -O2, bit-exact A/B, 200,000 encodes of a 12-table / 48-range / 8-sampler V1.2 signature, 2,008 bytes)

  • Before (main): 538.91 ms
  • After: 18.26 ms (29.52x faster, byte-identical output verified)

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Ran runtime/test/root_signature_test.cpp cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Ran python3 -I scripts/check_no_game_data.py runtime/src/root_signature.cpp runtime/test/root_signature_test.cpp and python3 -I tests/test_text_write_line_endings.py: all passed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5496465038 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2007). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T17:58:19Z. --- Claim: `RT-ROOT-SIGNATURE-RTS0-OFFSET-WRAP-AND-RESERVE` (claim comment `#7170207565` on #1608). ### Problem In `runtime/src/root_signature.cpp`: 1. **32-bit `uint32_t` offset addition in `parse_rts0_payload()`**: `dtype == 1` (`Constants`) and `dtype == 2..4` (`Descriptor`) passed `poff + 4` and `poff + 8` as `uint32_t` before promoting to `size_t` (unlike `Table`, which already promoted `static_cast<size_t>(poff) + 4`), wrapping offsets near `UINT32_MAX`. 2. **Addition-based bounds checks in `Reader::u32()` and `parse_rts0()`**: `pos_ + 4 > size_`, `off + 4 > size`, `static_cast<size_t>(off) + 8 > size`, and `static_cast<size_t>(off) + 8 + payload_size > size` can wrap on 32-bit `size_t`, and `parse_rts0()` repeated the identical `off + 8 > size` check inside the `RTS0` branch. 3. **Repeated vector reallocations and per-byte `push_back` in `encode_canonical()`**: `encode_canonical()` grew an unreserved `std::vector<uint8_t>` one byte at a time (`put_u32` doing 4 `push_back` calls per dword). ### Fix - Promote `poff` to `size_t pbase = static_cast<size_t>(poff)` once per parameter in `parse_rts0_payload()` and validate payload spans with subtraction (`pbase > size || need > size - pbase`) for `Constants`, `Descriptor`, and `Table`. - Replace addition-based bounds checks in `Reader::u32()` and `parse_rts0()` with overflow-free subtraction checks and remove the redundant `RTS0` chunk-header check. - Pre-compute the exact canonical byte count in `encode_canonical()` and write little-endian dwords directly into a single pre-sized buffer (`write_u32` / `write_f32`). - Add regression tests in `runtime/test/root_signature_test.cpp` (0 `CMakeLists.txt` edits) covering wrapping `poff` (`0xFFFFFFFC` / `0xFFFFFFF8` / `0xFFFFFFFF`) across tables/constants/descriptors, wrapping DXBC chunk offsets and chunk payload sizes, and rich V1.2 canonical round-trips. ### Benchmark (`clang++ -std=c++20 -O2`, bit-exact A/B, 200,000 encodes of a 12-table / 48-range / 8-sampler V1.2 signature, 2,008 bytes) - Before (`main`): `538.91 ms` - After: `18.26 ms` (**29.52x faster**, byte-identical output verified) ### Verification - **AI-assisted**: yes (Antigravity agent by Google DeepMind). - **GPUs tested**: none (host is macOS arm64; no retail files). - Ran `runtime/test/root_signature_test.cpp` cleanly under: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror` - Ran `python3 -I scripts/check_no_game_data.py runtime/src/root_signature.cpp runtime/test/root_signature_test.cpp` and `python3 -I tests/test_text_write_line_endings.py`: all passed.
copilot-pull-request-reviewer[bot] commented 2026-11-25 17:58:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T17:58:29Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6062487877 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2007#pullrequestreview-6062487877). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T17:58:29Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
zmasi commented 2026-11-28 21:24:24 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T21:24:24Z.


Independent Verification Report — 2d2d645d3a + origin/main (aa918e5d9f)

Environment: Windows 11 (26200), AMD Ryzen 9 5950X, MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8, Debug /Od /MDd & Release /O2 /MD).
Verdict: PASS (Ready to merge) — clean merge with origin/main (7cd4a795d5f0) and combined Batch 9 set (a422ed80f598).

1. Red -> Green Verification

  • Red (origin/main root_signature.cpp on 64-bit sparse 4 GiB VirtualAlloc buffer): With dtype = 1 (Constants) and poff = 0xFFFFFFFC (4 GiB - 4), origin/main evaluated poff + 4 and poff + 8 in 32-bit uint32_t arithmetic, wrapping around to offsets 0 and 4 and erroneously returning ok = true (shader_register = 77, register_space = 2, num_values = 1) instead of rejecting the truncated parameter payload.
  • Green (2d2d645d3a + origin/main):
    • The same 4 GiB poff = 0xFFFFFFFC probe is cleanly rejected (ok = false, "truncated RTS0 constants payload").
    • root_signature_test (Debug & Release): PASS (0 warnings under /W4 /permissive-), including all wrapping poff (0xFFFFFFF8, 0xFFFFFFFC, 0xFFFFFFFF), DXBC chunk-offset/length wrap cases, and rich V1.2 encode_canonical round-trip checks.
    • root_signature_oracle_test (against Windows 11 d3d12.dll serializer): PASS (0 warnings).

2. encode_canonical Pre-Sizing Throughput (/O2 /MD, 500,000 iterations, 8-table / 32-range / 6-sampler V1.2 signature, 1,384 B output)

  • origin/main: 1,575.32 ms (3,150.6 ns/call)
  • 2d2d645d3a: 107.35 ms (214.7 ns/call) — 14.7x faster with identical 1,384-byte canonical output.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7245996111 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2007#issuecomment-7245996111). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T21:24:24Z. --- ### Independent Verification Report — `2d2d645d3a` + `origin/main` (`aa918e5d9f`) **Environment:** Windows 11 (`26200`), AMD Ryzen 9 5950X, MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /W4 /permissive- /utf-8`, Debug `/Od /MDd` & Release `/O2 /MD`). **Verdict:** **PASS (Ready to merge)** — clean merge with `origin/main` (`7cd4a795d5f0`) and combined Batch 9 set (`a422ed80f598`). #### 1. Red -> Green Verification - **Red (`origin/main` `root_signature.cpp` on 64-bit sparse 4 GiB `VirtualAlloc` buffer):** With `dtype = 1` (`Constants`) and `poff = 0xFFFFFFFC` (`4 GiB - 4`), `origin/main` evaluated `poff + 4` and `poff + 8` in 32-bit `uint32_t` arithmetic, wrapping around to offsets `0` and `4` and erroneously returning `ok = true` (`shader_register = 77, register_space = 2, num_values = 1`) instead of rejecting the truncated parameter payload. - **Green (`2d2d645d3a` + `origin/main`):** - The same 4 GiB `poff = 0xFFFFFFFC` probe is cleanly rejected (`ok = false`, `"truncated RTS0 constants payload"`). - `root_signature_test` (Debug & Release): **PASS** (`0` warnings under `/W4 /permissive-`), including all wrapping `poff` (`0xFFFFFFF8`, `0xFFFFFFFC`, `0xFFFFFFFF`), DXBC chunk-offset/length wrap cases, and rich V1.2 `encode_canonical` round-trip checks. - `root_signature_oracle_test` (against Windows 11 `d3d12.dll` serializer): **PASS** (`0` warnings). #### 2. `encode_canonical` Pre-Sizing Throughput (`/O2 /MD`, 500,000 iterations, 8-table / 32-range / 6-sampler V1.2 signature, 1,384 B output) - `origin/main`: `1,575.32 ms` (`3,150.6 ns/call`) - `2d2d645d3a`: `107.35 ms` (`214.7 ns/call`) — **14.7x faster** with identical 1,384-byte canonical output. AI-assisted.
OreoFR commented 2026-11-28 21:57:54 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:57:54Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head 2d2d645d3; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R ^root_signature$
  • Red on main: OK (PR test diff applied; main changed the file)
  • Green with the PR: OK
  • Verdict: NO RED (the new RTS0/DXBC cases already pass on main)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246399413 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2007#issuecomment-7246399413). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:57:54Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `2d2d645d3`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R ^root_signature$` - Red on main: OK (PR test diff applied; main changed the file) - Green with the PR: OK - Verdict: NO RED (the new RTS0/DXBC cases already pass on main) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:41:02 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:41:02Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7318028709 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2007#issuecomment-7318028709). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:41:02Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2007
No description provided.