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

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

@⁠IlyassRS3 on GitHub · 2026-11-25 18:42 UTC · original
IlyassRS3:fix/rt-clzero-patch-rel32-and-protect-guards → main · +88 −23 · 2 files · 1 commits · open

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.
**@⁠IlyassRS3** on GitHub · 2026-11-25 18:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2036) `IlyassRS3:fix/rt-clzero-patch-rel32-and-protect-guards` → `main` · +88 −23 · 2 files · 1 commits · open 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:15:42 +00:00
Author
Owner

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

Merge

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