runtime: guard rel32 range, stub_page arithmetic, and VirtualProtect failure in clzero_patch #2036

Merged
IlyassRS3 merged 1 commit from upstream-pr/2036 into main 2026-11-28 17:53:35 +00:00
IlyassRS3 commented 2026-11-25 18:42:20 +00:00 (Migrated from github.com)

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


Coordination item: RT-CLZERO-PATCH-REL32-AND-PROTECT-GUARDS (claimed on #1608).

Why

runtime/src/clzero_patch.h rewrites the title's CLZERO loops to jump to stubs on CPUs without CLZERO (all Intel CPUs and macOS Rosetta/FEX), with a documented invariant: "On any failure nothing is patched." Several paths broke that invariant or had unchecked integer arithmetic:

  1. find(nullptr, size) dereferenced code when size >= 3.
  2. stub_page(lo, hi, size) did not check size == 0 or hi < lo, could wrap hi + gran - 1 / lo + limit / at + size near UINT64_MAX, and subtracted lo - size without checking lo >= size (wrapping to near UINT64_MAX).
  3. stub() and patch() cast 64-bit jump distances directly to int32_t without verifying they fit in signed 32-bit rel32 ([INT32_MIN, INT32_MAX]).
  4. patch() overwrote the title's loops with jmp rel32 + 0xCC before calling VirtualProtect(stubs, page, PAGE_EXECUTE_READ, &old) and ignored VirtualProtect's return value: if making the stub page executable failed, the title's code had already been permanently redirected to non-executable memory.

Changes

  • runtime/src/clzero_patch.h:
    • Guard if (!code || size < 12) return sites; in find().
    • Add overflow-free rel32_offset(from_next, to, out) and validate both loop->stub and stub->resume rel32 jumps before mutating any loop bytes.
    • Harden stub_page() against size == 0, hi < lo, lo < size underflow, and high-address overflow.
    • Populate the stub page and verify VirtualProtect(stubs, page, PAGE_EXECUTE_READ, &old) succeeds before overwriting the title's loops; on failure, free the stub page and leave the image untouched.
  • runtime/test/clzero_patch_test.cpp: add unit checks for find(nullptr, ...), stub_page() zero/inverted bounds, rel32_offset() signed 32-bit limits, and stub() rejecting out-of-range resume addresses.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (CPU-only verification on macOS arm64 using local Win32 VirtualAlloc/VirtualProtect stubs; Windows CI runs full clzero_patch_test executing patched stubs).
  • Compiled and ran clzero_patch.h test harness (including simulated VirtualProtect failure verifying 0 image bytes modified and stub freed) with clang++ -std=c++20 -Wall -Wextra -Werror:
    • ASan+UBSan (-O1 -g -fsanitize=address,undefined): clzero_patch_harness: OK (0 fails).
    • TSan (-O1 -g -fsanitize=thread): clzero_patch_harness: OK (0 fails).
    • Release (-O2): clzero_patch_harness: OK (0 fails).
  • python3 -I scripts/check_no_game_data.py runtime/src/clzero_patch.h runtime/test/clzero_patch_test.cpp: clean.
  • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5497106820 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2036). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T18:42:20Z. --- Coordination item: `RT-CLZERO-PATCH-REL32-AND-PROTECT-GUARDS` (claimed on #1608). ### Why `runtime/src/clzero_patch.h` rewrites the title's `CLZERO` loops to jump to stubs on CPUs without `CLZERO` (all Intel CPUs and macOS Rosetta/FEX), with a documented invariant: "On any failure nothing is patched." Several paths broke that invariant or had unchecked integer arithmetic: 1. `find(nullptr, size)` dereferenced `code` when `size >= 3`. 2. `stub_page(lo, hi, size)` did not check `size == 0` or `hi < lo`, could wrap `hi + gran - 1` / `lo + limit` / `at + size` near `UINT64_MAX`, and subtracted `lo - size` without checking `lo >= size` (wrapping to near `UINT64_MAX`). 3. `stub()` and `patch()` cast 64-bit jump distances directly to `int32_t` without verifying they fit in signed 32-bit `rel32` (`[INT32_MIN, INT32_MAX]`). 4. `patch()` overwrote the title's loops with `jmp rel32` + `0xCC` **before** calling `VirtualProtect(stubs, page, PAGE_EXECUTE_READ, &old)` and ignored `VirtualProtect`'s return value: if making the stub page executable failed, the title's code had already been permanently redirected to non-executable memory. ### Changes - `runtime/src/clzero_patch.h`: - Guard `if (!code || size < 12) return sites;` in `find()`. - Add overflow-free `rel32_offset(from_next, to, out)` and validate both loop->stub and stub->resume `rel32` jumps before mutating any loop bytes. - Harden `stub_page()` against `size == 0`, `hi < lo`, `lo < size` underflow, and high-address overflow. - Populate the stub page and verify `VirtualProtect(stubs, page, PAGE_EXECUTE_READ, &old)` succeeds **before** overwriting the title's loops; on failure, free the stub page and leave the image untouched. - `runtime/test/clzero_patch_test.cpp`: add unit checks for `find(nullptr, ...)`, `stub_page()` zero/inverted bounds, `rel32_offset()` signed 32-bit limits, and `stub()` rejecting out-of-range resume addresses. ### Verification - AI-assisted: yes (Antigravity agent by Google DeepMind). - GPUs tested: none (CPU-only verification on macOS arm64 using local Win32 `VirtualAlloc`/`VirtualProtect` stubs; Windows CI runs full `clzero_patch_test` executing patched stubs). - Compiled and ran `clzero_patch.h` test harness (including simulated `VirtualProtect` failure verifying 0 image bytes modified and stub freed) with `clang++ -std=c++20 -Wall -Wextra -Werror`: - ASan+UBSan (`-O1 -g -fsanitize=address,undefined`): `clzero_patch_harness: OK (0 fails)`. - TSan (`-O1 -g -fsanitize=thread`): `clzero_patch_harness: OK (0 fails)`. - Release (`-O2`): `clzero_patch_harness: OK (0 fails)`. - `python3 -I scripts/check_no_game_data.py runtime/src/clzero_patch.h runtime/test/clzero_patch_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:42:33 +00:00 (Migrated from github.com)

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

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:6063085973 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2036#pullrequestreview-6063085973). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T18:42:33Z. **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 11:35:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T11:35:31Z.


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 4a54aedcb + a013410f8 (claim #2570#issuecomment-7238618828):

  • Line-by-line diff audit: Clean (runtime/src/clzero_patch.h, runtime/test/clzero_patch_test.cpp); guards null/short buffers in find, validates signed 32-bit branch range via rel32_offset in stub and patch, guards overflow/underflow and inverted [lo, hi) in stub_page, and verifies VirtualProtect(..., PAGE_EXECUTE_READ) succeeds (freeing stubs on failure) before overwriting image loops. check_no_game_data.py clean.
  • Red (4a54aedcb): PR test fails compilation (C2039: 'rel32_offset': is not a member of 'rt::clzero_patch'), and runtime probe on main's clzero_patch.h crashes with 0xc0000005 (STATUS_ACCESS_VIOLATION) on cz::find(nullptr, 64) and silently truncates >2 GiB resume displacements in cz::stub.
  • Green (4a54aedcb + a013410f8 merge): Merges cleanly (rc=0), compiles with 0 /W4 warnings, and clzero_patch_test.exe passes (clzero_patch_test: OK (20 checks), rc=0).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238653204 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2036#issuecomment-7238653204). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T11:35:31Z. --- 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` `4a54aedcb` + `a013410f8` (claim `#2570#issuecomment-7238618828`): - **Line-by-line diff audit**: Clean (`runtime/src/clzero_patch.h`, `runtime/test/clzero_patch_test.cpp`); guards null/short buffers in `find`, validates signed 32-bit branch range via `rel32_offset` in `stub` and `patch`, guards overflow/underflow and inverted `[lo, hi)` in `stub_page`, and verifies `VirtualProtect(..., PAGE_EXECUTE_READ)` succeeds (freeing `stubs` on failure) before overwriting image loops. `check_no_game_data.py` clean. - **Red (`4a54aedcb`)**: PR test fails compilation (`C2039: 'rel32_offset': is not a member of 'rt::clzero_patch'`), and runtime probe on `main`'s `clzero_patch.h` crashes with `0xc0000005` (`STATUS_ACCESS_VIOLATION`) on `cz::find(nullptr, 64)` and silently truncates >2 GiB `resume` displacements in `cz::stub`. - **Green (`4a54aedcb` + `a013410f8` merge)**: Merges cleanly (`rc=0`), compiles with `0` `/W4` warnings, and `clzero_patch_test.exe` passes (`clzero_patch_test: OK (20 checks)`, `rc=0`). AI-assisted.
MoonEyes commented 2026-11-28 17:53:32 +00:00 (Migrated from github.com)

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


Red/green: On main clzero_patch_test does not compile (rt::clzero_patch::rel32_offset is missing) while the main header crashes on find(nullptr) and silently truncates an out-of-range rel32 in stub, and with the PR ctest -R "^clzero_patch$" 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:7243368308 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2036#issuecomment-7243368308). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:53:32Z. --- Red/green: On main clzero_patch_test does not compile (rt::clzero_patch::rel32_offset is missing) while the main header crashes on find(nullptr) and silently truncates an out-of-range rel32 in stub, and with the PR ctest -R "^clzero_patch$" 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:30 +00:00 (Migrated from github.com)

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


Verified by OreoFR

  • Base: main c1ecf04ed + PR head a013410f8; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R ^clzero_patch$
  • Red on main: does not compile (rt::clzero_patch::rel32_offset missing on main)
  • 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:7246406492 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2036#issuecomment-7246406492). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:58:30Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `a013410f8`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R ^clzero_patch$` - Red on main: does not compile (rt::clzero_patch::rel32_offset missing on main) - 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!2036
No description provided.