Skip to content

Port upstream #941: a clwb in the last bytes of a page is emulated instead of faulting - #46

Merged
DesertGamer merged 1 commit into
DesertGamer:mainfrom
lcg007xd-c:port/upstream-941-clwb-page-end
Nov 26, 2026
Merged

DesertGamer merged 1 commit into
DesertGamer:mainfrom
lcg007xd-c:port/upstream-941-clwb-page-end

Conversation

@lcg007xd-c

Copy link
Copy Markdown
Contributor

What this changes

This ports Marvisak/VIctory#941 by BastienRdz. It is cherry-picked with -x, so the commit keeps their authorship.

Intel CPUs before Ice Lake have no clwb and raise STATUS_ILLEGAL_INSTRUCTION on it. The loader's handler in crates/viport-loader/src/crash.rs emulates the instruction as a no-op, because the PC's memory is coherent. It copies only the bytes left in the faulting page, since the next page may be unmapped. But it calls clwb_len only when n >= 15, because clwb_len takes a fixed [u8; 15].

So a 4- to 10-byte clwb that starts in the last 14 bytes of a page is never emulated. The same happens when a clwb_loop that straddles the page end falls back to clwb_len. The title then dies with an unhandled illegal instruction on those CPUs.

The fix makes clwb_len take &[u8], bounds-check each byte it reads, and return Some(len) only when len <= code.len(). The n >= 15 gate is dropped. #941's tests come with it: exact-length and cut-short slices, plus an emulation at the last 4 and 5 bytes of a page followed by a PAGE_NOACCESS page.

VIPort has the upstream merge of #941 in its history, but the change is not in the tree. It was lost in a later merge. It applied cleanly.

Checked

  • No game data of any kind (files, extracts, screenshots, dumps, saves): see docs/asset-policy.md
  • No binary files or archives
  • No new dependency, build.rs, git hook, .cargo config, CI change or script (or an issue explains why)
  • No download, no started program, no network connection added (or an issue explains why)
  • cargo test --workspace passes (not run, see below)
  • cargo clippy --workspace --all-targets --locked -- -D warnings passes (Windows target, run on Linux)
  • The code I wrote is formatted with rustfmt; I did not reformat lines I did not change

What I ran

  • On Linux, the new clwb_len compiled standalone with the non-Windows asserts of a_clwb_at_the_end_of_a_page_is_decoded (exact-length slices decode; cut-short ones return None without panicking): all pass.
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings, cross-checked from Linux on this branch (rebased on the current main): clean.
  • cargo test -p viport-loader --lib --locked on Linux also shows 4 failures in prepare::tests. They read the test binary as a PE, which it is only on Windows, and they fail the same way on main.

Not run: the page-end emulation part of the test, or a pre-Ice Lake Intel CPU. It needs Windows exception handling. The Windows CI test step will run it.

Overlaps

None. No open PR touches viport-loader/src/crash.rs.

🤖 Generated with Claude Code

…lting

emulate_amd_only copies only the bytes left in the faulting page and
gated clwb_len on n >= 15 because clwb_len took a fixed [u8; 15]. A
4- to 10-byte clwb starting in the last 14 bytes of a page (or falling
back from a clwb_loop that straddles the page end) was therefore
skipped, leaving pre-Ice Lake Intel CPUs with an unhandled
STATUS_ILLEGAL_INSTRUCTION.

Change clwb_len to take &[u8], bounds-checking each byte read and
returning Some(len) only when len <= code.len(), and drop the n >= 15
guard in emulate_amd_only.

Fixes #924.

Co-Authored-By: Gemini 4 Neon <noreply@google.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 60d2cf9)
@DesertGamer
DesertGamer merged commit adfec95 into DesertGamer:main Nov 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants