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

Open
opened 2026-11-23 22:17:06 +00:00 by real-alponzo · 5 comments
real-alponzo commented 2026-11-23 22:17:06 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by real-alponzo on 2026-11-23T22:17:06Z.


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

<!-- greyfinch-sync:ivall/xcl:issue:6569962756 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/278). Originally posted by [real-alponzo](https://github.com/real-alponzo) on 2026-11-23T22:17:06Z. --- 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).*
ivall commented 2026-11-24 07:04:42 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T23:40:43Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7117233322 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/278#issuecomment-7117233322). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T23:40:43Z. --- 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).
Grimkaaah commented 2026-11-24 07:05:12 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by Grimkaaah on 2026-11-24T01:14:19Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7118899476 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/278#issuecomment-7118899476). Originally posted by [Grimkaaah](https://github.com/Grimkaaah) on 2026-11-24T01:14:19Z. --- 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 commented 2026-11-24 07:05:37 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by Grimkaaah on 2026-11-24T02:36:25Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7120309183 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/278#issuecomment-7120309183). Originally posted by [Grimkaaah](https://github.com/Grimkaaah) on 2026-11-24T02:36:25Z. --- 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 commented 2026-11-24 07:05:37 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by Grimkaaah on 2026-11-24T02:39:12Z.


Item 3 spun out to https://github.com/ivall/xcl/issues/626 (dedicated issue + complete PR with smoke). #321 remains for item 4.

<!-- greyfinch-sync:ivall/xcl:comment:7120353078 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/278#issuecomment-7120353078). Originally posted by [Grimkaaah](https://github.com/Grimkaaah) on 2026-11-24T02:39:12Z. --- Item 3 spun out to https://github.com/ivall/xcl/issues/626 (dedicated issue + complete PR with smoke). #321 remains for item 4.
Grimkaaah commented 2026-11-24 07:05:38 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by Grimkaaah on 2026-11-24T02:44:14Z.


Item 3: complete PR https://github.com/ivall/xcl/pull/523 on dedicated issue #626 (smoke + suite done).

<!-- greyfinch-sync:ivall/xcl:comment:7120431119 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/278#issuecomment-7120431119). Originally posted by [Grimkaaah](https://github.com/Grimkaaah) on 2026-11-24T02:44:14Z. --- Item 3: complete PR https://github.com/ivall/xcl/pull/523 on dedicated issue #626 (smoke + suite done).
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl#275
No description provided.