Skip to content

Console CPU emulation: ClzeroLoop decoder/semantics, is_writable guards, and CLWB hot-patch race #278

Backlog

Description

@real-alponzo

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):

  1. ClzeroLoop false-positive match (crates/xcl/src/insn.rs:120-130)

    • Matches [48|4C] 39 <modrm> 72 F4 without checking (modrm & 0xC7) == 0xC0 (mod == 3 && rm == 0, i.e. cmp rax, <reg>).
    • Sequences such as cmp rdx, rcx or memory compares (cmp [rax], rdx) falsely decode as ClzeroLoop.
  2. ClzeroLoop emulation semantics (crates/xcl/src/eh.rs:318-334)

    • 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.
  3. eh::is_writable gaps (crates/xcl/src/eh.rs:248-256)

    • 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.
  4. CLWB hot-patch multi-thread race (crates/xcl/src/eh.rs:294-302)

    • 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).

Activity

  1. ivall commented on Nov 23, 2026

    @ivall
    Collaborator

    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).

  2. Grimkaaah commented on Nov 24, 2026

    @Grimkaaah

    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).

  3. Grimkaaah commented on Nov 24, 2026

    @Grimkaaah

    PR for item 3 (is_writable guards): #523

    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.

  4. Grimkaaah commented on Nov 24, 2026

    @Grimkaaah

    Item 3 spun out to #626 (dedicated issue + complete PR with smoke). #321 remains for item 4.

  5. Grimkaaah commented on Nov 24, 2026

    @Grimkaaah

    Item 3: complete PR #523 on dedicated issue #626 (smoke + suite done).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions