Loader: emulate clwb at a page end, and read no code past readable memory (fixes #924) #1038
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!1038
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1038/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@acleofe on GitHub · 2026-11-24 15:14 UTC · original
fix/loader-clwb-page-end→main· +136 −36 · 1 files · 1 commits · closed without mergingWhat
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: theclzero/monitorx/mwaitxmatch,clwb_loop,clwb_len(now&[u8]with bounds checks) andsse4a(now a safe function over&[u8], refusing an instruction the slice does not hold whole before any register changes).Why
clwb_lenwas only tried with 15 bytes left in the page, so aclwbin 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.ud2) at the end of a page followed by an unmapped one faulted inside the handler.sse4aread 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,ud2and a 1-byte instruction there are not and do not fault, aclwb [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.clwb_lengthsand SSE4a tests pass unchanged (slices instead of arrays).cargo clippy --workspace --all-targets [--all-features] --locked -- -D warningsandcargo test --workspace --lockedpass.🤖 Generated with Claude Code
@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 asNoneinstead 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_atwithreadablemeans an instruction that straddles a page boundary into a mapped page still decodes and emulates. Mine stops at the boundary unconditionally, so aclwb [rax+0x100]starting four bytes before the end of a page is refused and reported as a crash instead of being emulated — exactly youremulate_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_lenhas 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 encodedclwbcan parse to a length longer than the read, and the handler then stepsRippast bytes it never saw.One test I would add to this PR, from mine: what the
clwb_loopfast path leaves behind after it skips the whole write-back loop in one exception —baseadvanced tolimit,Rippast the jump, and the flags of the lastcmpwith 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'sjbedepends on those flags, so a wrongEFLAGShere 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.@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-loaderpasses, 26 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).Tested with AI assistance (Claude).
@adamdotpy on GitHub · 2026-11-24 17:10 UTC · original
Thanks for the run @Zipettte — 26 passed / 0 failed on
victory-loaderat0bef0cd, baseline green on main. Noted; nothing actionable.Pull request closed