runtime: harden title_window_plan and ArenaBlocks bounds/overflow checks (RT-TITLE-WINDOW-AND-ARENA-BOUNDS) #1785

Manually merged
github merged 1 commit from gh-pr/1785/head into gh-pr/1785/base 2026-11-26 10:05:10 +00:00
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 12:08 UTC · original
IlyassRS3:fix/rt-title-window-and-arena-bounds → main · +177 −15 · 5 files · 1 commits · merged 2026-11-25 14:40 UTC

Summary

Hardens boundary, alignment, and unsigned-overflow checks in runtime/src/title_window_plan.h and runtime/src/arena_blocks.h (both on the world-start title-memory mapping and GPU arena allocation path merged in #1723).

  • Coordination item: RT-TITLE-WINDOW-AND-ARENA-BOUNDS
  • Claim: #1608

Root causes & changes

  • runtime/src/title_window_plan.h:
    • title_window_range():
      • Reject va < lo: previously only checked hi <= va, so when lo > va (e.g., if gap() returned lo > va) it returned true with range.start = lo > va (excluding va), causing offset + (va - range.start) in resolve_title_memory to underflow.
      • Reject most == 0 instead of calling std::clamp<std::uint64_t>(want, 1, 0) (UB).
      • Require power-of-two align before masking with ~(align - 1).
      • Compute raw_end = va + want and alignment round-up with separate saturation guards so UINT64_MAX - want - align cannot underflow when want + align wraps.
    • title_pool_extent():
      • Reject non-power-of-two align and reject readable_end <= va before rounding end up to align (previously a partial readable run ending before va inside the same alignment page rounded up past va and returned true for an unreadable address).
      • Apply the most cap after alignment rounding so out.size <= most always holds even when pool_lo is unaligned or most < align, and refuse if end <= va.
  • runtime/src/arena_blocks.h:
    • Replace wrapping (value + alignment - 1) / alignment * alignment with overflow-checked align_up(value, alignment, out) so large alignments or a keep_out_end near UINT64_MAX cannot wrap to 0 and spin forever inside allocate().
    • Break out of the keep_out loop cleanly if keep_out_end <= start instead of stalling.
    • Guard consistent(live_bytes) against size_ < reserved_ underflow and free_bytes_ + live_bytes overflow.
  • runtime/test/arena_blocks_test.cpp, runtime/test/title_window_plan_bounds_test.cpp, & runtime/CMakeLists.txt:
    • Extend arena_blocks_test with overflow align_up, wrapping keep_out_end, stagnant keep_out_end <= start, and consistent() bounds checks.
    • Add portable title_window_plan_bounds CTest covering all title_window_fits, title_window_range, and title_pool_extent boundary/overflow cases without <windows.h>.

Verification

  • AI-assisted: yes
  • GPUs tested: none (portable standard-library C++20 headers + tests, verified locally on macOS Apple Silicon ARM64 with Clang; Windows MSVC build and Windows-only title_window_plan_test rely on Windows CI).
  • Commands run:
    • clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=address,undefined -O1 runtime/test/arena_blocks_test.cpp — pass
    • clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=thread -O1 runtime/test/arena_blocks_test.cpp — pass
    • clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/arena_blocks_test.cpp — pass
    • clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=address,undefined -O1 runtime/test/title_window_plan_bounds_test.cpp — pass
    • clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/title_window_plan_bounds_test.cpp — pass
    • Mutation tests against unpatched main headers: both title_window_plan_bounds_test (va < lo) and arena_blocks_test (align_up overflow) fail on main, confirming regression coverage
    • python3 -I scripts/check_no_game_data.py runtime/src/title_window_plan.h runtime/src/arena_blocks.h runtime/test/arena_blocks_test.cpp runtime/test/title_window_plan_bounds_test.cpp runtime/CMakeLists.txt — pass
    • python3 -I tests/test_text_write_line_endings.py — 3/3 pass
**@⁠IlyassRS3** on GitHub · 2026-11-25 12:08 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1785) `IlyassRS3:fix/rt-title-window-and-arena-bounds` → `main` · +177 −15 · 5 files · 1 commits · merged 2026-11-25 14:40 UTC ## Summary Hardens boundary, alignment, and unsigned-overflow checks in `runtime/src/title_window_plan.h` and `runtime/src/arena_blocks.h` (both on the world-start title-memory mapping and GPU arena allocation path merged in #1723). - **Coordination item**: `RT-TITLE-WINDOW-AND-ARENA-BOUNDS` - **Claim**: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 ### Root causes & changes - [`runtime/src/title_window_plan.h`](runtime/src/title_window_plan.h): - `title_window_range()`: - Reject `va < lo`: previously only checked `hi <= va`, so when `lo > va` (e.g., if `gap()` returned `lo > va`) it returned `true` with `range.start = lo > va` (excluding `va`), causing `offset + (va - range.start)` in `resolve_title_memory` to underflow. - Reject `most == 0` instead of calling `std::clamp<std::uint64_t>(want, 1, 0)` (UB). - Require power-of-two `align` before masking with `~(align - 1)`. - Compute `raw_end = va + want` and alignment round-up with separate saturation guards so `UINT64_MAX - want - align` cannot underflow when `want + align` wraps. - `title_pool_extent()`: - Reject non-power-of-two `align` and reject `readable_end <= va` **before** rounding `end` up to `align` (previously a partial readable run ending before `va` inside the same alignment page rounded up past `va` and returned `true` for an unreadable address). - Apply the `most` cap after alignment rounding so `out.size <= most` always holds even when `pool_lo` is unaligned or `most < align`, and refuse if `end <= va`. - [`runtime/src/arena_blocks.h`](runtime/src/arena_blocks.h): - Replace wrapping `(value + alignment - 1) / alignment * alignment` with overflow-checked `align_up(value, alignment, out)` so large alignments or a `keep_out_end` near `UINT64_MAX` cannot wrap to `0` and spin forever inside `allocate()`. - Break out of the `keep_out` loop cleanly if `keep_out_end <= start` instead of stalling. - Guard `consistent(live_bytes)` against `size_ < reserved_` underflow and `free_bytes_ + live_bytes` overflow. - [`runtime/test/arena_blocks_test.cpp`](runtime/test/arena_blocks_test.cpp), [`runtime/test/title_window_plan_bounds_test.cpp`](runtime/test/title_window_plan_bounds_test.cpp), & [`runtime/CMakeLists.txt`](runtime/CMakeLists.txt): - Extend `arena_blocks_test` with overflow `align_up`, wrapping `keep_out_end`, stagnant `keep_out_end <= start`, and `consistent()` bounds checks. - Add portable `title_window_plan_bounds` CTest covering all `title_window_fits`, `title_window_range`, and `title_pool_extent` boundary/overflow cases without `<windows.h>`. ## Verification - **AI-assisted**: yes - **GPUs tested**: none (portable standard-library C++20 headers + tests, verified locally on macOS Apple Silicon ARM64 with Clang; Windows MSVC build and Windows-only `title_window_plan_test` rely on Windows CI). - Commands run: - `clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=address,undefined -O1 runtime/test/arena_blocks_test.cpp` — pass - `clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=thread -O1 runtime/test/arena_blocks_test.cpp` — pass - `clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/arena_blocks_test.cpp` — pass - `clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=address,undefined -O1 runtime/test/title_window_plan_bounds_test.cpp` — pass - `clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/title_window_plan_bounds_test.cpp` — pass - Mutation tests against unpatched `main` headers: both `title_window_plan_bounds_test` (`va < lo`) and `arena_blocks_test` (`align_up` overflow) fail on `main`, confirming regression coverage - `python3 -I scripts/check_no_game_data.py runtime/src/title_window_plan.h runtime/src/arena_blocks.h runtime/test/arena_blocks_test.cpp runtime/test/title_window_plan_bounds_test.cpp runtime/CMakeLists.txt` — pass - `python3 -I tests/test_text_write_line_endings.py` — 3/3 pass
Fix edge-case arithmetic and boundary bugs in the title-memory window
planner and GPU address arena allocator:

1. runtime/src/title_window_plan.h:
   - title_window_range(): reject va < lo (previously returned a range
     starting at lo > va that excluded va, causing va - range.start to
     underflow in resolve_title_memory), reject most == 0 (avoiding
     std::clamp(want, 1, 0) UB), require power-of-two align, and avoid
     UINT64_MAX - want - align unsigned underflow when computing end.
   - title_pool_extent(): reject non-power-of-two align and unreadable
     requests (readable_end <= va) before rounding end up to align, and
     enforce the most cap after alignment rounding so out.size <= most
     always holds and out contains va.
2. runtime/src/arena_blocks.h:
   - Make align_up() overflow-checked so high alignments or keep_out_end
     near UINT64_MAX cannot wrap to 0 and trigger an infinite loop in
     ArenaBlocks::allocate().
   - Guard against non-advancing keep_out_end <= start in allocate().
   - Make consistent() safe against reserved_ > size_ underflow and
     free_bytes_ + live_bytes overflow.

Extend runtime/test/arena_blocks_test.cpp and add portable
runtime/test/title_window_plan_bounds_test.cpp.

Item: RT-TITLE-WINDOW-AND-ARENA-BOUNDS
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:05:07 +00:00
github manually merged commit eb4c4204e8 into gh-pr/1785/base 2026-11-26 10:05:10 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 12:08 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 12:08 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1785#pullrequestreview-6057326116) · **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.
Author
Owner

@⁠lixonik on GitHub · 2026-11-25 12:13 UTC · original · reviewed

Independent A/B at eb4c4204 against its base 0d80bc5d. The new bounds suite fails against the base at the check for a requested address below the lower bound. On this head, both the new bounds suite and arena_blocks_test passed with GCC and UBSan under Ubuntu/WSL. The existing title_window_plan_test also built with MSVC and passed on native Windows, including its budget and paired-view mapping controls.

AI-assisted. CPU-only synthetic cases.

**@⁠lixonik** on GitHub · 2026-11-25 12:13 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1785#pullrequestreview-6057394430) · **reviewed** Independent A/B at `eb4c4204` against its base `0d80bc5d`. The new bounds suite fails against the base at the check for a requested address below the lower bound. On this head, both the new bounds suite and arena_blocks_test passed with GCC and UBSan under Ubuntu/WSL. The existing title_window_plan_test also built with MSVC and passed on native Windows, including its budget and paired-view mapping controls. AI-assisted. CPU-only synthetic cases.
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 14:40 UTC · original

Merged. Thank you, @⁠IlyassRS3, and thanks to @⁠lixonik for the A/B.

  • Nothing changes for valid inputs; the va < lo check covers a real, race-only path (a range added by another thread between the shader_va check and gap()), which underflowed before.
  • 8 of the 12 new checks fail a test without them; title_window_plan_bounds passes.
  • Windows suite under Wine (llvmpipe and an RDNA2 iGPU), together with #1827 and #1732 on current main: nothing goes from pass to fail.
  • Optional follow-up: end <= va in title_window_range and end <= start in title_pool_extent can never fire after the earlier checks, and the !most and raw_end checks have no case that fails without them.
  • Checked on main c3b58ef03 + eb4c4204e: it merges cleanly; the semantic suite passes (489 files, 6,288 tests, 0 files failed); the 14 docs and writer tests pass; check_no_game_data.py is clean on the 5 changed files.

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

**@⁠aslan2142** on GitHub · 2026-11-25 14:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1785#issuecomment-7165630722) Merged. Thank you, @⁠IlyassRS3, and thanks to @⁠lixonik for the A/B. - Nothing changes for valid inputs; the `va < lo` check covers a real, race-only path (a range added by another thread between the `shader_va` check and `gap()`), which underflowed before. - 8 of the 12 new checks fail a test without them; `title_window_plan_bounds` passes. - Windows suite under Wine (llvmpipe and an RDNA2 iGPU), together with #1827 and #1732 on current `main`: nothing goes from pass to fail. - Optional follow-up: `end <= va` in `title_window_range` and `end <= start` in `title_pool_extent` can never fire after the earlier checks, and the `!most` and `raw_end` checks have no case that fails without them. - **Checked on `main` `c3b58ef03` + `eb4c4204e`:** it merges cleanly; the semantic suite passes (489 files, 6,288 tests, 0 files failed); the 14 docs and writer tests pass; `check_no_game_data.py` is clean on the 5 changed files. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.