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

Open
opened 2026-11-26 04:18:29 +00:00 by github-bot · 5 comments
Owner

@⁠AlpPesen on GitHub · 2026-11-23 22:17 UTC · original

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

**@⁠AlpPesen** on GitHub · 2026-11-23 22:17 UTC · [original](https://github.com/xcl-dev/xcl/issues/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`): 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).*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:18:29 +00:00
Author
Owner

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

**@⁠ivall** on GitHub · 2026-11-23 23:40 UTC · [original](https://github.com/xcl-dev/xcl/issues/278#issuecomment-7117233322) 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).
Author
Owner

@⁠Grimkaaah on GitHub · 2026-11-24 01:14 UTC · original

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

**@⁠Grimkaaah** on GitHub · 2026-11-24 01:14 UTC · [original](https://github.com/xcl-dev/xcl/issues/278#issuecomment-7118899476) 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).
Author
Owner

@⁠Grimkaaah on GitHub · 2026-11-24 02:36 UTC · original

PR for item 3 (is_writable guards): https://github.com/ivall/xcl/pull/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.

**@⁠Grimkaaah** on GitHub · 2026-11-24 02:36 UTC · [original](https://github.com/xcl-dev/xcl/issues/278#issuecomment-7120309183) PR for item 3 (`is_writable` guards): https://github.com/ivall/xcl/pull/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.
Author
Owner

@⁠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:39 UTC · [original](https://github.com/xcl-dev/xcl/issues/278#issuecomment-7120353078) Item 3 spun out to https://github.com/ivall/xcl/issues/626 (dedicated issue + complete PR with smoke). #321 remains for item 4.
Author
Owner

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

**@⁠Grimkaaah** on GitHub · 2026-11-24 02:44 UTC · [original](https://github.com/xcl-dev/xcl/issues/278#issuecomment-7120431119) Item 3: complete PR https://github.com/ivall/xcl/pull/523 on dedicated issue #626 (smoke + suite done).
This discussion has been locked. Commenting is limited to contributors.
No description provided.