runtime: guard rel32 range, stub_page arithmetic, and VirtualProtect failure in clzero_patch #2036
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!2036
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/rt-clzero-patch-rel32-and-protect-guards"
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 successfully merged and closed
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.Verified on Windows 11 (
10.0.26200.9974) with MSVC19.44.35228(x64,/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) againstmain4a54aedcb+a013410f8(claim#2570#issuecomment-7238618828):runtime/src/clzero_patch.h,runtime/test/clzero_patch_test.cpp); guards null/short buffers infind, validates signed 32-bit branch range viarel32_offsetinstubandpatch, guards overflow/underflow and inverted[lo, hi)instub_page, and verifiesVirtualProtect(..., PAGE_EXECUTE_READ)succeeds (freeingstubson failure) before overwriting image loops.check_no_game_data.pyclean.4a54aedcb): PR test fails compilation (C2039: 'rel32_offset': is not a member of 'rt::clzero_patch'), and runtime probe onmain'sclzero_patch.hcrashes with0xc0000005(STATUS_ACCESS_VIOLATION) oncz::find(nullptr, 64)and silently truncates >2 GiBresumedisplacements incz::stub.4a54aedcb+a013410f8merge): Merges cleanly (rc=0), compiles with0/W4warnings, andclzero_patch_test.exepasses (clzero_patch_test: OK (20 checks),rc=0).AI-assisted.
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.
Verified by OreoFR
c1ecf04ed+ PR heada013410f8; green run in a merge package of 16 PRs, no new ctest failure vs mainctest -R ^clzero_patch$AI-assisted (Claude Code, Claude Opus 5.5).