mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-27 00:20:53 +01:00
[PR #2036] runtime: guard rel32 range, stub_page arithmetic, and VirtualProtect failure in clzero_patch #2043
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#2043
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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)
a013410runtime: 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.hrewrites the title'sCLZEROloops to jump to stubs on CPUs withoutCLZERO(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:find(nullptr, size)dereferencedcodewhensize >= 3.stub_page(lo, hi, size)did not checksize == 0orhi < lo, could wraphi + gran - 1/lo + limit/at + sizenearUINT64_MAX, and subtractedlo - sizewithout checkinglo >= size(wrapping to nearUINT64_MAX).stub()andpatch()cast 64-bit jump distances directly toint32_twithout verifying they fit in signed 32-bitrel32([INT32_MIN, INT32_MAX]).patch()overwrote the title's loops withjmp rel32+0xCCbefore callingVirtualProtect(stubs, page, PAGE_EXECUTE_READ, &old)and ignoredVirtualProtect'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:if (!code || size < 12) return sites;infind().rel32_offset(from_next, to, out)and validate both loop->stub and stub->resumerel32jumps before mutating any loop bytes.stub_page()againstsize == 0,hi < lo,lo < sizeunderflow, and high-address overflow.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 forfind(nullptr, ...),stub_page()zero/inverted bounds,rel32_offset()signed 32-bit limits, andstub()rejecting out-of-range resume addresses.Verification
VirtualAlloc/VirtualProtectstubs; Windows CI runs fullclzero_patch_testexecuting patched stubs).clzero_patch.htest harness (including simulatedVirtualProtectfailure verifying 0 image bytes modified and stub freed) withclang++ -std=c++20 -Wall -Wextra -Werror:-O1 -g -fsanitize=address,undefined):clzero_patch_harness: OK (0 fails).-O1 -g -fsanitize=thread):clzero_patch_harness: OK (0 fails).-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.