You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Console CPU emulation: ClzeroLoop decoder/semantics, is_writable guards, and CLWB hot-patch race #278
Code audit on main (63b9930) found four correctness / safety bugs in crates/xcl/src/insn.rs and crates/xcl/src/eh.rs affecting Intel CPUs (especially pre-Ice Lake CPUs lacking CLWB and CLZERO):
ClzeroLoop false-positive match (crates/xcl/src/insn.rs:120-130)
Does not mask start down to a 64-byte cache line (start & !63), unlike single Clzero (eh.rs:311).
When (end - start) is not a multiple of 64, it zeroes [start..end) and sets ctx.Rax = end instead of zeroing full 64-byte cache lines and advancing Rax by multiples of 0x40.
When end <= start (or end - start >= 2 GiB), it skips zeroing the first cache line completely yet still overwrites ctx.Rax = end.
ctx.EFlags is never updated for the cmp instruction.
Does not exclude PAGE_GUARD (0x100): PAGE_READWRITE | PAGE_GUARD (0x104) passes & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) != 0, risking a guard-page fault inside the VEH.
Queries only a single VirtualQuery region (fails if ClzeroLoop crosses adjacent committed regions) and uses unchecked va + bytes.
Does not call kernelx::lazy::ensure(va, bytes), so clzero on lazily-committed physical memory (MEM_RESERVE) returns 0 (0xC000001D) instead of committing and zeroing.
VirtualProtect(RWX) -> byte-by-byte code.fill(0x90) -> VirtualProtect(old) has no synchronization across threads faulting on the same 4 KiB page (can restore PAGE_EXECUTE_READ while another thread is writing, or leave the page permanently RWX).
Byte-by-byte NOP writing on 66 0F AE 30 (clwb [rax]) transiently exposes 90 0F AE 30 (nop; xsaveopt [rax]) to concurrent threads; FlushInstructionCache is also not called.
Planned split (one PR per item, per CONTRIBUTING.md):
PR 1: Console CPU instructions: fix ClzeroLoop ModRM check and loop semantics
PR 2: Console CPU instructions: harden eh::is_writable for multi-region, PAGE_GUARD, and lazy memory
PR 3: Console CPU instructions: serialize CLWB hot-patching and flush icache
Findings verified directly against 63b9930 with Antigravity (Gemini 4 Neon).
Items 1 and 2 (the CLZERO loop decoder and its semantics) are fixed by #290, merged. #306 (no VirtualQuery at 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).
I'll take item 3 (is_writable): reject PAGE_GUARD, walk adjacent VirtualQuery regions for a ClzeroLoop span, checked_add the end, and call lazy::ensure before the check so a lazy physical page commits instead of returning 0xC000001D. Unit tests with VirtualAlloc for guard, multi-region, hole, overflow, and a lazy page. Complements #501 (sparse fast-path) and does not touch the CLWB race (#321).
Hardens eh::is_writable for PAGE_GUARD, multi-region ClzeroLoop spans, wrapping ends, and lazy pages via lazy::ensure (same shape as dstorage::writable). Complements #501; does not touch item 4 / #321.
Activity
Console CPU instructions: fix ClzeroLoop ModRM check and loop semantics
ivall commentedon Nov 23, 2026
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).Console CPU instructions: serialize CLWB hot-patching and flush icache (
Grimkaaah commentedon Nov 24, 2026
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).eh: harden is_writable for PAGE_GUARD, multi-region spans, lazy pages (…
Grimkaaah commentedon Nov 24, 2026
PR for item 3 (
is_writableguards): #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 commentedon Nov 24, 2026
Item 3 spun out to #626 (dedicated issue + complete PR with smoke). #321 remains for item 4.
Grimkaaah commentedon Nov 24, 2026
Item 3: complete PR #523 on dedicated issue #626 (smoke + suite done).