runtime: guard base_reservation parsing and VirtualQuery loop overflow #2041

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

@⁠IlyassRS3 on GitHub · 2026-11-25 18:48 UTC · original
IlyassRS3:fix/rt-base-reservation-overflow-and-parse-guards → main · +48 −9 · 2 files · 1 commits · open

Coordination item: RT-BASE-RESERVATION-OVERFLOW-AND-PARSE-GUARDS (claimed on #1608).

Why

runtime/src/base_reservation.h (used on macOS CrossOver / arm64 Wine FEX to keep a relocation-less PE image's preferred base free) had several edge-case parsing and arithmetic bugs:

  1. needed() looped with for (uint64_t at = r.base; at < r.base + r.size;) and stepped at = info.BaseAddress + info.RegionSize. If r.base + r.size wrapped 64-bit address space, the loop skipped immediately; if info.RegionSize == 0 or BaseAddress + RegionSize wrapped around UINT64_MAX, at failed to advance and looped infinitely.
  2. inherited() used swscanf_s(value, L"%llx:%llx", ...) which accepted trailing garbage ("140000000:30000000junk"), wrapped >64-bit hex digits, and accepted ranges where base + size wrapped 64 bits.
  3. commit_reserved() did not reject size == 0 or base + size wraparound before querying/committing.

Changes

  • runtime/src/base_reservation.h:
    • Extract strict parse_range(const wchar_t*) that parses <hex>:<hex> with exact full-string match, overflow-checked hex accumulation, non-zero size, and base <= UINT64_MAX - size.
    • Guard size == 0 and base > UINT64_MAX - size in commit_reserved().
    • Use overflow-free subtraction against info.RegionSize in needed() so at strictly advances and never wraps or infinite-loops.
  • runtime/test/pe_loader_test.cpp: expand test_inherited_reservation() to verify rejection of trailing garbage, base + size wraparound, and >64-bit hex overflow.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (CPU-only verification on macOS arm64 using local Win32 VirtualQuery/env stubs; Windows CI runs full pe_loader_test).
  • Compiled and ran base_reservation.h test harness (including synthetic PE32+ files, multi-region free walk, pathological RegionSize == 0 non-infinite-loop test, and wrapping ImageBase) with clang++ -std=c++20 -Wall -Wextra -Werror:
    • ASan+UBSan (-O1 -g -fsanitize=address,undefined): base_reservation_harness: OK (0 fails).
    • TSan (-O1 -g -fsanitize=thread): base_reservation_harness: OK (0 fails).
    • Release (-O2): base_reservation_harness: OK (0 fails).
  • python3 -I scripts/check_no_game_data.py runtime/src/base_reservation.h runtime/test/pe_loader_test.cpp: clean.
  • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.
**@⁠IlyassRS3** on GitHub · 2026-11-25 18:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2041) `IlyassRS3:fix/rt-base-reservation-overflow-and-parse-guards` → `main` · +48 −9 · 2 files · 1 commits · open Coordination item: `RT-BASE-RESERVATION-OVERFLOW-AND-PARSE-GUARDS` (claimed on #1608). ### Why `runtime/src/base_reservation.h` (used on macOS CrossOver / arm64 Wine FEX to keep a relocation-less PE image's preferred base free) had several edge-case parsing and arithmetic bugs: 1. `needed()` looped with `for (uint64_t at = r.base; at < r.base + r.size;)` and stepped `at = info.BaseAddress + info.RegionSize`. If `r.base + r.size` wrapped 64-bit address space, the loop skipped immediately; if `info.RegionSize == 0` or `BaseAddress + RegionSize` wrapped around `UINT64_MAX`, `at` failed to advance and looped infinitely. 2. `inherited()` used `swscanf_s(value, L"%llx:%llx", ...)` which accepted trailing garbage (`"140000000:30000000junk"`), wrapped >64-bit hex digits, and accepted ranges where `base + size` wrapped 64 bits. 3. `commit_reserved()` did not reject `size == 0` or `base + size` wraparound before querying/committing. ### Changes - `runtime/src/base_reservation.h`: - Extract strict `parse_range(const wchar_t*)` that parses `<hex>:<hex>` with exact full-string match, overflow-checked hex accumulation, non-zero size, and `base <= UINT64_MAX - size`. - Guard `size == 0` and `base > UINT64_MAX - size` in `commit_reserved()`. - Use overflow-free subtraction against `info.RegionSize` in `needed()` so `at` strictly advances and never wraps or infinite-loops. - `runtime/test/pe_loader_test.cpp`: expand `test_inherited_reservation()` to verify rejection of trailing garbage, `base + size` wraparound, and >64-bit hex overflow. ### Verification - AI-assisted: yes (Antigravity agent by Google DeepMind). - GPUs tested: none (CPU-only verification on macOS arm64 using local Win32 `VirtualQuery`/env stubs; Windows CI runs full `pe_loader_test`). - Compiled and ran `base_reservation.h` test harness (including synthetic PE32+ files, multi-region free walk, pathological `RegionSize == 0` non-infinite-loop test, and wrapping `ImageBase`) with `clang++ -std=c++20 -Wall -Wextra -Werror`: - ASan+UBSan (`-O1 -g -fsanitize=address,undefined`): `base_reservation_harness: OK (0 fails)`. - TSan (`-O1 -g -fsanitize=thread`): `base_reservation_harness: OK (0 fails)`. - Release (`-O2`): `base_reservation_harness: OK (0 fails)`. - `python3 -I scripts/check_no_game_data.py runtime/src/base_reservation.h runtime/test/pe_loader_test.cpp`: clean. - `python3 -I tests/test_text_write_line_endings.py`: 3/3 OK.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:15:50 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 18:48 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 18:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2041#pullrequestreview-6063163442) · **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/2041/head:github-bot/gh-2041'
git switch 'github-bot/gh-2041'

Merge

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