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

Open
github-bot wants to merge 1 commit from github-bot/gh-2007 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 17:58 UTC · original
IlyassRS3:fix/rt-root-signature-rts0-offset-wrap → main · +139 −65 · 2 files · 1 commits · open

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.
**@⁠IlyassRS3** on GitHub · 2026-11-25 17:58 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2007) `IlyassRS3:fix/rt-root-signature-rts0-offset-wrap` → `main` · +139 −65 · 2 files · 1 commits · open 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:33 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 17:58 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 17:58 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2007#pullrequestreview-6062487877) · **reviewed** 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2007/head:github-bot/gh-2007'
git switch 'github-bot/gh-2007'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2007'
git switch 'github-bot/gh-2007'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2007'
git switch 'github-bot/gh-2007'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2007'
git switch 'main'
git merge --squash 'github-bot/gh-2007'
git switch 'main'
git merge --ff-only 'github-bot/gh-2007'
git switch 'main'
git merge 'github-bot/gh-2007'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.