[PR #2036] runtime: guard rel32 range, stub_page arithmetic, and VirtualProtect failure in clzero_patch #2043

Open
opened 2026-11-26 21:19:50 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2036
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rt-clzero-patch-rel32-and-protect-guards


📝 Commits (1)

  • a013410 runtime: guard rel32 range, stub_page arithmetic, and VirtualProtect failure in clzero_patch

📊 Changes

2 files changed (+88 additions, -23 deletions)

View changed files

📝 runtime/src/clzero_patch.h (+73 -23)
📝 runtime/test/clzero_patch_test.cpp (+15 -0)

📄 Description

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.

🔄 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/2036 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-clzero-patch-rel32-and-protect-guards` --- ### 📝 Commits (1) - [`a013410`](https://github.com/ubiquitarianism/leonida/commit/a013410f8ec354cd134adbca8266ed707eb3c409) runtime: guard rel32 range, stub_page arithmetic, and VirtualProtect failure in clzero_patch ### 📊 Changes **2 files changed** (+88 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/clzero_patch.h` (+73 -23) 📝 `runtime/test/clzero_patch_test.cpp` (+15 -0) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#2043
No description provided.