runtime: guard base_reservation parsing and VirtualQuery loop overflow #2041

Merged
IlyassRS3 merged 1 commit from upstream-pr/2041 into main 2026-11-28 17:52:49 +00:00
IlyassRS3 commented 2026-11-25 18:48:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T18:48:07Z.


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.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5497192470 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2041). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T18:48:07Z. --- 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.
copilot-pull-request-reviewer[bot] commented 2026-11-25 18:48:15 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T18:48:15Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6063163442 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2041#pullrequestreview-6063163442). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T18:48:15Z. **Review:** COMMENTED --- 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.
zmasi commented 2026-11-28 13:07:22 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T13:07:22Z.


Verified on Windows 11 (10.0.26200.9974) with MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) against main d48a957bb + 2e8a92d72a (claim #2570#issuecomment-7239583640):

  • Line-by-line diff audit: Clean (runtime/src/base_reservation.h, runtime/test/pe_loader_test.cpp); replaces swscanf_s in LEONIDA_BASE_RESERVED parsing with an exact hex parser (parse_range) that rejects trailing characters, zero sizes, >64-bit hex overflow, and base + size 64-bit wraparound, and bounds the VirtualQuery walk in needed() against region/address-space wraparound. check_no_game_data.py clean.
  • Red (d48a957bb + PR test): Full pe_loader_test.exe (tiny_dll.dll, tiny_exe.exe, gta6rt_tls_host.dll) fails (rc=1, pe_loader_test: FAILED (82 checks, 3 failures) on trailing junk, base + size wraparound, and overflowing 64-bit hex base).
  • Green (d48a957bb + 2e8a92d72a merge): Auto-merges cleanly (rc=0), compiles base_reservation.h / pe_loader_test.cpp with 0 warnings under /W4 /permissive-, and passes (pe_loader_test: OK (82 checks), rc=0).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239707264 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2041#issuecomment-7239707264). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T13:07:22Z. --- Verified on Windows 11 (`10.0.26200.9974`) with MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2`) against `main` `d48a957bb` + `2e8a92d72a` (claim `#2570#issuecomment-7239583640`): - **Line-by-line diff audit**: Clean (`runtime/src/base_reservation.h`, `runtime/test/pe_loader_test.cpp`); replaces `swscanf_s` in `LEONIDA_BASE_RESERVED` parsing with an exact hex parser (`parse_range`) that rejects trailing characters, zero sizes, >64-bit hex overflow, and `base + size` 64-bit wraparound, and bounds the `VirtualQuery` walk in `needed()` against region/address-space wraparound. `check_no_game_data.py` clean. - **Red (`d48a957bb` + PR test)**: Full `pe_loader_test.exe` (`tiny_dll.dll`, `tiny_exe.exe`, `gta6rt_tls_host.dll`) fails (`rc=1`, `pe_loader_test: FAILED (82 checks, 3 failures)` on trailing junk, `base + size` wraparound, and overflowing 64-bit hex base). - **Green (`d48a957bb` + `2e8a92d72a` merge)**: Auto-merges cleanly (`rc=0`), compiles `base_reservation.h` / `pe_loader_test.cpp` with `0` warnings under `/W4 /permissive-`, and passes (`pe_loader_test: OK (82 checks)`, `rc=0`). AI-assisted.
MoonEyes commented 2026-11-28 17:52:46 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:52:46Z.


Red/green: On main pe_loader_test fails 3 of 82 checks (LEONIDA_BASE_RESERVED with a trailing suffix, a 64-bit base+size overflow and an oversized hex base are accepted), and with the PR ctest -R "^pe_loader$" passes.
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243358133 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2041#issuecomment-7243358133). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:52:46Z. --- Red/green: On main pe_loader_test fails 3 of 82 checks (LEONIDA_BASE_RESERVED with a trailing suffix, a 64-bit base+size overflow and an oversized hex base are accepted), and with the PR ctest -R "^pe_loader$" passes. Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
OreoFR commented 2026-11-28 21:58:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:58:48Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head 2e8a92d72; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R ^pe_loader$
  • Red on main: 3 failures of 82 (LEONIDA_BASE_RESERVED with a suffix, base + size overflowing 64 bits, too-large hex base accepted)
  • Green with the PR: OK
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246409808 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2041#issuecomment-7246409808). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:58:48Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `2e8a92d72`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R ^pe_loader$` - Red on main: 3 failures of 82 (LEONIDA_BASE_RESERVED with a suffix, base + size overflowing 64 bits, too-large hex base accepted) - Green with the PR: OK - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!2041
No description provided.