mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 21:50:52 +01:00
[PR #2035] runtime: step try_clzero_loop by VirtualQuery regions and guard wraparound in cpu_emulation #2042
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#2042
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/2035
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open
Base:
main← Head:fix/rt-cpu-emulation-clzero-region-and-bounds📝 Commits (1)
7f56e6aruntime: step try_clzero_loop by VirtualQuery regions and guard wraparound in cpu_emulation📊 Changes
2 files changed (+56 additions, -14 deletions)
View changed files
📝
runtime/src/cpu_emulation.h(+33 -14)📝
runtime/test/cpu_emulation_test.cpp(+23 -0)📄 Description
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.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.