runtime: step try_clzero_loop by VirtualQuery regions and guard wraparound in cpu_emulation #2035
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2035"
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?
Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T18:39:53Z.
Coordination item:
RT-CPU-EMULATION-CLZERO-REGION-AND-BOUNDS(claimed on #1608).Why
rt::cpu_emulation::try_clzero_loop()inruntime/src/cpu_emulation.hcalledwritable(cur)(oneVirtualQuerysyscall) on every 4 KiB page (next_page = (cur & ~0xFFF) + 0x1000), issuing 16,384VirtualQuerysyscalls for a single 64 MiB contiguousMEM_COMMIT | PAGE_READWRITEzeroing batch even thoughVirtualQueryalready returnsBaseAddress + RegionSizefor the entire uniform protection run.try_clzero_loop():limit <= start + 64wrapped whenstart = UINT64_MAX - 63(0xFFFFFFFFFFFFFFC0), andend = start + 64wrapped to0, skipping thefor (cur < end)writability check entirely and executingstd::memset(0xFFFFFFFFFFFFFFC0, 0, 64)(immediate SIGSEGV).(limit - start + 63) & ~63wrapped to0whenlimit - start > UINT64_MAX - 63(e.g.start = 0, limit = UINT64_MAX), leavingend == start, skippingwritable(0), and returningClzeroLoopResult::okwithRipunchanged (infinite#UDloop).(cur & ~0xFFF) + 0x1000wrapped to0on the top page of 64-bit address space.readable()computedBaseAddress + RegionSizeandaddr + gotwithout overflow/bounds checks, anddecode(nullptr, ...)dereferencedcode[0]and did not enforce the architectural 15-byte x86-64 instruction length limit when callers passavail > 15.Changes
runtime/src/cpu_emulation.h:writable_region(addr, region_end)returning the end of the contiguous committed writableVirtualQueryrun, and steptry_clzero_loop()byregion_end(16,384× fewerVirtualQuerysyscalls on a contiguous 64 MiB buffer while preserving exact page-boundary fault splitting).limit <= start || limit - start <= 64, clampspan >= kMaxBatchbefore rounding up to 64 bytes, and verifytotal <= UINT64_MAX - start.readable()overflow-free using subtraction againstmbi.RegionSize.if (!code) return {};and capavail <= 15indecode().runtime/test/cpu_emulation_test.cpp: add tests fordecode(nullptr, ...), >15-byte overlong instruction rejection,[UINT64_MAX - 63, UINT64_MAX)wraparound rejection, and[0, UINT64_MAX)null-page fault.Verification
VirtualQuery/VirtualProtectWin32 stubs; Windows CI runs fullcpu_emulation_testwith realVirtualAlloc+ SEH).cpu_emulation.htest harness withclang++ -std=c++20 -Wall -Wextra -Werror:-O1 -g -fsanitize=address,undefined):vq_64m=1 res_max=2 (faulted) res_top=0 (no_match) fails=0.-O1 -g -fsanitize=thread):vq_64m=1 res_max=2 res_top=0 fails=0.-O2):vq_64m=1 res_max=2 res_top=0 fails=0.main'scpu_emulation.h:mainissuedvq_64m=16384VirtualQuerycalls (vs1here — 16,384× reduction), returnedres_max=1 (ok)withtotal=0on[0, UINT64_MAX), and crashed with SIGSEGV (exit 139) on[UINT64_MAX - 63, UINT64_MAX).python3 -I scripts/check_no_game_data.py runtime/src/cpu_emulation.h runtime/test/cpu_emulation_test.cpp: clean.python3 -I tests/test_text_write_line_endings.py: 3/3 OK.Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T18:40:05Z.
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.
Imported from ubiquitarianism/leonida. Originally posted by moffington on 2026-11-26T04:45:16Z.
Heads-up from #826, which touches the same CLZERO path: the region scan here rejects pages reported as
MEM_RESERVE. For XMem, ordinary stores to reserved backing succeed after first-touch commit (the fault handler inxmem.cppcommits and resumes;xmem_test.cppcovers it), so CLZERO would report a write fault where the equivalent store succeeds.#826 handles this in
writable()by committing eligible XMem backing and then rechecking the destination's live protection, so read-only, no-access and guard pages still fault.Is the blanket rejection intentional? If not, I'm happy to coordinate: the two headers conflict, so whichever lands second has to carry the first-touch check into
writable_region(). #826 also adds a batched CLZERO test (crossing from reserved XMem into a protected page, in bothSEC_RESERVEandSEC_COMMITmodes) that should run against the batched path here with little change.Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T11:35:30Z.
Verified on Windows 11 (
10.0.26200.9974) with MSVC19.44.35228(x64,/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) againstmain4a54aedcb+7f56e6a50(claim#2570#issuecomment-7238618828):runtime/src/cpu_emulation.h,runtime/test/cpu_emulation_test.cpp); guards nullcodeand capsavailat the 15-byte x86-64 architectural instruction limit indecode, stepstry_clzero_loopbyVirtualQuerycontiguous writable regions viawritable_region, and guardsUINT64_MAXwraparound inreadableandtry_clzero_loop.check_no_game_data.pyclean.4a54aedcb+ PR test):cpu_emulation_test.execrashes with0xc0000005(STATUS_ACCESS_VIOLATIONonce::decode(nullptr, 15)).4a54aedcb+7f56e6a50merge): Auto-merges cleanly (rc=0), compiles with0/W4warnings, andcpu_emulation_test.exepasses (rc=0).AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:53:55Z.
Red/green: On main cpu_emulation_test crashes with 0xC0000005, and with the PR ctest -R "^cpu_emulation$" 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.Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:58:24Z.
Verified by OreoFR
c1ecf04ed+ PR head7f56e6a50; green run in a merge package of 16 PRs, no new ctest failure vs mainctest -R ^cpu_emulation$AI-assisted (Claude Code, Claude Opus 5.5).