Loader: emulate clwb at a page end, and read no code past readable memory (fixes #924) #1038

Closed
github-bot wants to merge 1 commit from gh-pr/1038/head into gh-pr/1038/base
Owner

@⁠acleofe on GitHub · 2026-11-24 15:14 UTC · original
fix/loader-clwb-page-end → main · +136 −36 · 1 files · 1 commits · closed without merging

What

The illegal-instruction handler reads the faulting code once, through a new code_at: the bytes left in its page, and up to 32 when the next page is committed and readable (VirtualQuery). Every decoder then works on that slice: the clzero/monitorx/mwaitx match, clwb_loop, clwb_len (now &[u8] with bounds checks) and sse4a (now a safe function over &[u8], refusing an instruction the slice does not hold whole before any register changes).

Why

  • clwb_len was only tried with 15 bytes left in the page, so a clwb in the last 14 bytes of a page was not emulated and the title died with an illegal instruction on CPUs without CLWB (Intel before Ice Lake), #924.
  • The first match read 3 bytes at RIP before any page check: a 1- or 2-byte illegal instruction (ud2) at the end of a page followed by an unmapped one faulted inside the handler.
  • sse4a read through a raw pointer with the same exposure.

The faulting instruction's own bytes are always readable (the CPU fetched them), so an instruction across a page boundary is still decoded whole when its second page is readable.

Test

  • instructions_at_a_page_end_are_read_only_as_far_as_memory_allows: in a committed page before a reserved one, clwb [rax] in its last 4 bytes is emulated, ud2 and a 1-byte instruction there are not and do not fault, a clwb [rax+disp32] cut off by the page end is refused; with the next page committed, it is emulated whole (8 bytes). Before this change the first assertion fails.
  • Existing clwb_lengths and SSE4a tests pass unchanged (slices instead of arrays).
  • cargo clippy --workspace --all-targets [--all-features] --locked -- -D warnings and cargo test --workspace --locked pass.

🤖 Generated with Claude Code

**@⁠acleofe** on GitHub · 2026-11-24 15:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/1038) `fix/loader-clwb-page-end` → `main` · +136 −36 · 1 files · 1 commits · closed without merging ## What The illegal-instruction handler reads the faulting code once, through a new `code_at`: the bytes left in its page, and up to 32 when the next page is committed and readable (`VirtualQuery`). Every decoder then works on that slice: the `clzero`/`monitorx`/`mwaitx` match, `clwb_loop`, `clwb_len` (now `&[u8]` with bounds checks) and `sse4a` (now a safe function over `&[u8]`, refusing an instruction the slice does not hold whole before any register changes). ## Why - `clwb_len` was only tried with 15 bytes left in the page, so a `clwb` in the last 14 bytes of a page was not emulated and the title died with an illegal instruction on CPUs without CLWB (Intel before Ice Lake), #924. - The first match read 3 bytes at RIP before any page check: a 1- or 2-byte illegal instruction (`ud2`) at the end of a page followed by an unmapped one faulted inside the handler. - `sse4a` read through a raw pointer with the same exposure. The faulting instruction's own bytes are always readable (the CPU fetched them), so an instruction across a page boundary is still decoded whole when its second page is readable. ## Test - `instructions_at_a_page_end_are_read_only_as_far_as_memory_allows`: in a committed page before a reserved one, `clwb [rax]` in its last 4 bytes is emulated, `ud2` and a 1-byte instruction there are not and do not fault, a `clwb [rax+disp32]` cut off by the page end is refused; with the next page committed, it is emulated whole (8 bytes). Before this change the first assertion fails. - Existing `clwb_lengths` and SSE4a tests pass unchanged (slices instead of arrays). - `cargo clippy --workspace --all-targets [--all-features] --locked -- -D warnings` and `cargo test --workspace --locked` pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
clwb was only emulated with 15 bytes left in its page, so one in a
page's last bytes killed the title on CPUs without CLWB. The handler
also read 3 bytes at RIP before any page check, so a short illegal
instruction before an unmapped page faulted inside it. All decoders now
read one slice bounded by readable memory.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:17:55 +00:00
github-bot closed this pull request 2026-11-26 04:17:55 +00:00
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 15:35 UTC · original

Another implementation of this same fix, for comparison. I hit #924 independently and ended up with the same two changes — stop the unconditional 3-byte read at Rip, and make the decoders take a slice so a cut-off instruction reads as None instead of decoding bytes that were never read. Mine is a local branch I have not pushed, since this PR already proposes it; I would rather see one of these merged than two.

The comparison, in case it is useful:

Yours is better on the case that matters most. code_at with readable means an instruction that straddles a page boundary into a mapped page still decodes and emulates. Mine stops at the boundary unconditionally, so a clwb [rax+0x100] starting four bytes before the end of a page is refused and reported as a crash instead of being emulated — exactly your emulate_in_place(cut) == Some(8) case. If the title's compilers ever lay a write-back out that way, mine costs a run and yours does not.

Same conclusion on the length guard: clwb_len has to check the parsed length against what was read ((len <= code.len()).then_some(len) here), not just parse the bytes. With a zero-padded fixed buffer a legitimately encoded clwb can parse to a length longer than the read, and the handler then steps Rip past bytes it never saw.

One test I would add to this PR, from mine: what the clwb_loop fast path leaves behind after it skips the whole write-back loop in one exception — base advanced to limit, Rip past the jump, and the flags of the last cmp with the other bits untouched. The "entered with the base already past the limit, so it runs once" case is easy to get wrong, and the loop's jbe depends on those flags, so a wrong EFLAGS here turns a skipped loop into a walk off the end of the buffer. It is the only part of the emulation I found that a page-boundary test does not exercise.

**@⁠adamdotpy** on GitHub · 2026-11-24 15:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1038#issuecomment-7136284450) Another implementation of this same fix, for comparison. I hit #924 independently and ended up with the same two changes — stop the unconditional 3-byte read at `Rip`, and make the decoders take a slice so a cut-off instruction reads as `None` instead of decoding bytes that were never read. Mine is a local branch I have not pushed, since this PR already proposes it; I would rather see one of these merged than two. The comparison, in case it is useful: **Yours is better on the case that matters most.** `code_at` with `readable` means an instruction that straddles a page boundary into a *mapped* page still decodes and emulates. Mine stops at the boundary unconditionally, so a `clwb [rax+0x100]` starting four bytes before the end of a page is refused and reported as a crash instead of being emulated — exactly your `emulate_in_place(cut) == Some(8)` case. If the title's compilers ever lay a write-back out that way, mine costs a run and yours does not. **Same conclusion on the length guard**: `clwb_len` has to check the parsed length against what was read (`(len <= code.len()).then_some(len)` here), not just parse the bytes. With a zero-padded fixed buffer a legitimately encoded `clwb` can parse to a length longer than the read, and the handler then steps `Rip` past bytes it never saw. **One test I would add to this PR**, from mine: what the `clwb_loop` fast path leaves behind after it skips the whole write-back loop in one exception — `base` advanced to `limit`, `Rip` past the jump, and the flags of the last `cmp` with the other bits untouched. The "entered with the base already past the limit, so it runs once" case is easy to get wrong, and the loop's `jbe` depends on those flags, so a wrong `EFLAGS` here turns a skipped loop into a walk off the end of the buffer. It is the only part of the emulation I found that a page-boundary test does not exercise.
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 16:30 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 0bef0cd: cargo test -p victory-loader passes, 26 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 16:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1038#issuecomment-7137772224) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 0bef0cd: `cargo test -p victory-loader` passes, 26 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 17:10 UTC · original

Thanks for the run @⁠Zipettte — 26 passed / 0 failed on victory-loader at 0bef0cd, baseline green on main. Noted; nothing actionable.

**@⁠adamdotpy** on GitHub · 2026-11-24 17:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/1038#issuecomment-7138793801) Thanks for the run @⁠Zipettte — 26 passed / 0 failed on `victory-loader` at `0bef0cd`, baseline green on main. Noted; nothing actionable.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1038
No description provided.