Console CPU emulation: ClzeroLoop decoder/semantics, is_writable guards, and CLWB hot-patch race #278
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl#278
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?
@AlpPesen on GitHub · 2026-11-23 22:17 UTC · original
Code audit on
main(63b9930) found four correctness / safety bugs incrates/xcl/src/insn.rsandcrates/xcl/src/eh.rsaffecting Intel CPUs (especially pre-Ice Lake CPUs lackingCLWBandCLZERO):ClzeroLoopfalse-positive match (crates/xcl/src/insn.rs:120-130)[48|4C] 39 <modrm> 72 F4without checking(modrm & 0xC7) == 0xC0(mod == 3 && rm == 0, i.e.cmp rax, <reg>).cmp rdx, rcxor memory compares (cmp [rax], rdx) falsely decode asClzeroLoop.ClzeroLoopemulation semantics (crates/xcl/src/eh.rs:318-334)startdown to a 64-byte cache line (start & !63), unlike singleClzero(eh.rs:311).(end - start)is not a multiple of 64, it zeroes[start..end)and setsctx.Rax = endinstead of zeroing full 64-byte cache lines and advancingRaxby multiples of0x40.end <= start(orend - start >= 2 GiB), it skips zeroing the first cache line completely yet still overwritesctx.Rax = end.ctx.EFlagsis never updated for thecmpinstruction.eh::is_writablegaps (crates/xcl/src/eh.rs:248-256)PAGE_GUARD(0x100):PAGE_READWRITE | PAGE_GUARD(0x104) passes& (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) != 0, risking a guard-page fault inside the VEH.VirtualQueryregion (fails ifClzeroLoopcrosses adjacent committed regions) and uses uncheckedva + bytes.kernelx::lazy::ensure(va, bytes), soclzeroon lazily-committed physical memory (MEM_RESERVE) returns0(0xC000001D) instead of committing and zeroing.CLWBhot-patch multi-thread race (crates/xcl/src/eh.rs:294-302)VirtualProtect(RWX)-> byte-by-bytecode.fill(0x90)->VirtualProtect(old)has no synchronization across threads faulting on the same 4 KiB page (can restorePAGE_EXECUTE_READwhile another thread is writing, or leave the page permanently RWX).66 0F AE 30(clwb [rax]) transiently exposes90 0F AE 30(nop; xsaveopt [rax]) to concurrent threads;FlushInstructionCacheis also not called.Planned split (one PR per item, per
CONTRIBUTING.md):Console CPU instructions: fix ClzeroLoop ModRM check and loop semanticsConsole CPU instructions: harden eh::is_writable for multi-region, PAGE_GUARD, and lazy memoryConsole CPU instructions: serialize CLWB hot-patching and flush icacheFindings verified directly against
63b9930with Antigravity (Gemini 4 Neon).@ivall on GitHub · 2026-11-23 23:40 UTC · original
Items 1 and 2 (the CLZERO loop decoder and its semantics) are fixed by #290, merged. #306 (no
VirtualQueryat every emulated instruction) is being merged now. Items 3 (is_writable: PAGE_GUARD, regions, lazy pages) and 4 (the CLWB hot-patch race, #321) stay open here until their PRs land; #295 overlaps #290 and would need a rebase onto it for what it adds (the 256 MiB per fault limit, the wrap check).@Grimkaaah on GitHub · 2026-11-24 01:14 UTC · original
I'll take item 3 (is_writable): reject
PAGE_GUARD, walk adjacentVirtualQueryregions for a ClzeroLoop span,checked_addthe end, and calllazy::ensurebefore the check so a lazy physical page commits instead of returning0xC000001D. Unit tests withVirtualAllocfor guard, multi-region, hole, overflow, and a lazy page. Complements #501 (sparse fast-path) and does not touch the CLWB race (#321).@Grimkaaah on GitHub · 2026-11-24 02:36 UTC · original
PR for item 3 (
is_writableguards): https://github.com/ivall/xcl/pull/523Hardens
eh::is_writableforPAGE_GUARD, multi-regionClzeroLoopspans, wrapping ends, and lazy pages vialazy::ensure(same shape asdstorage::writable). Complements #501; does not touch item 4 / #321.@Grimkaaah on GitHub · 2026-11-24 02:39 UTC · original
Item 3 spun out to https://github.com/ivall/xcl/issues/626 (dedicated issue + complete PR with smoke). #321 remains for item 4.
@Grimkaaah on GitHub · 2026-11-24 02:44 UTC · original
Item 3: complete PR https://github.com/ivall/xcl/pull/523 on dedicated issue #626 (smoke + suite done).