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

Open
opened 2026-11-26 21:19:44 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2007
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rt-root-signature-rts0-offset-wrap


📝 Commits (1)

  • 2d2d645 runtime: harden RTS0/DXBC offset checks and pre-size canonical root-signature encoding

📊 Changes

2 files changed (+139 additions, -65 deletions)

View changed files

📝 runtime/src/root_signature.cpp (+72 -65)
📝 runtime/test/root_signature_test.cpp (+67 -0)

📄 Description

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2007 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-root-signature-rts0-offset-wrap` --- ### 📝 Commits (1) - [`2d2d645`](https://github.com/ubiquitarianism/leonida/commit/2d2d645d3a92764c97d857b7c1b2b8ecf6d2f6ab) runtime: harden RTS0/DXBC offset checks and pre-size canonical root-signature encoding ### 📊 Changes **2 files changed** (+139 additions, -65 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/root_signature.cpp` (+72 -65) 📝 `runtime/test/root_signature_test.cpp` (+67 -0) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2015
No description provided.