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

Closed
opened 2026-11-26 21:18:59 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1785
Author: @IlyassRS3
Created: 11/25/2026
Status: ✅ Merged
Merged: 11/25/2026
Merged by: @aslan2142

Base: main ← Head: fix/rt-title-window-and-arena-bounds


📝 Commits (1)

  • eb4c420 runtime: harden title_window_plan and ArenaBlocks boundary and overflow checks

📊 Changes

5 files changed (+177 additions, -15 deletions)

View changed files

📝 runtime/CMakeLists.txt (+5 -0)
📝 runtime/src/arena_blocks.h (+27 -8)
📝 runtime/src/title_window_plan.h (+10 -7)
📝 runtime/test/arena_blocks_test.cpp (+38 -0)
➕ runtime/test/title_window_plan_bounds_test.cpp (+97 -0)

📄 Description

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

🔄 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/1785 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** ✅ Merged **Merged:** 11/25/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `fix/rt-title-window-and-arena-bounds` --- ### 📝 Commits (1) - [`eb4c420`](https://github.com/ubiquitarianism/leonida/commit/eb4c4204e8c4523debb0b04d91e7e258d2a29a77) runtime: harden title_window_plan and ArenaBlocks boundary and overflow checks ### 📊 Changes **5 files changed** (+177 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+5 -0) 📝 `runtime/src/arena_blocks.h` (+27 -8) 📝 `runtime/src/title_window_plan.h` (+10 -7) 📝 `runtime/test/arena_blocks_test.cpp` (+38 -0) ➕ `runtime/test/title_window_plan_bounds_test.cpp` (+97 -0) </details> ### 📄 Description ## 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 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 21:18:59 +01:00
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#1796
No description provided.