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

Merged
leonida/mandy
quietfold merged 1 commit from fix/rt-title-window-and-arena-bounds into main 2026-11-25 14:40:06 +00:00

Pull request successfully merged and closed

quietfold commented 2026-11-25 12:08:24 +00:00

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).

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
## 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://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7161907524 ### 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
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 12:08:36 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
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.
lixonik reviewed 2026-11-25 12:13:40 +00:00
lixonik left a comment
No description provided.
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.
aslan2142 commented 2026-11-25 14:40:09 +00:00

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).

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).
Sign in to join this conversation.
No description provided.