Loader: lazily NOP-patch clwb/monitorx on #UD, emulate rdpid, batch IAT protection, and content-hash cached DLLs #1372
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!1372
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1372"
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?
@wbkepler on GitHub · 2026-11-24 17:38 UTC · original
wbkepler:loader/intel-ud-patch-and-startup→main· +567 −39 · 3 files · 1 commits · openSummary
Rebased cleanly onto
upstream/main(ffe310e):crash.rs:clwb(notclwb_loop) andmonitorxinstructions on#UDwith canonical x86-64 multi-byte NOPs (1..=15 bytes) using a single hardware-atomic store (AtomicU64::compare_exchangeorlock cmpxchg16b), serialized via non-blockingtry_lock(), restricted to already-executable pages within a single 4 KiB page, followed byFlushInstructionCacheand exact protection restore (falling back toRip += lenif patching is unsafe or refused).rdpid r64(F3 [REX] 0F C7 /7) emulation returning the current logical processor ID fromGetCurrentProcessorNumberEx((Group << 6) | Number) without modifyingEFLAGS.prepare.rs&windows.rs:write_slots()VirtualProtectcalls by 4 KiB page (preserving per-section protections and falling back cleanly for page-straddling slots), cuttingVirtualProtectcalls from 2x slots down to 2x pages.prepared.bytesinPreparedImage::file_name()sowrite_if_changed()checks metadata length (0 bytes read on cache hit) and stages misses via unique per-writer temp files before atomic rename.rdpiddecoding/emulation, live executable-page atomic NOP patching (AtomicU64+cmpxchg16bpaths + execution + protection restore), page-batchedwrite_slots, and content-hashed cache hits.Verification
cargo clippy -p victory-loader --all-targets— passedcargo test -p victory-loader— passedcargo xtask policy— passed- `crash.rs`: - Lazily overwrite standalone `clwb` (not `clwb_loop`) and `monitorx` instructions on `#UD` with canonical x86-64 multi-byte NOPs (1..=15 bytes) using a single hardware-atomic store (`AtomicU64::compare_exchange` or `lock cmpxchg16b`), serialized via non-blocking `try_lock()`, restricted to already-executable pages within a single 4 KiB page, followed by `FlushInstructionCache` and exact protection restore (falling back to `Rip += len` if patching is unsafe or refused). - Add `rdpid r64` (`F3 [REX] 0F C7 /7`) emulation returning the current logical processor ID from `GetCurrentProcessorNumberEx` (`(Group << 6) | Number`) without modifying `EFLAGS`. - `prepare.rs` & `windows.rs`: - Batch `write_slots()` `VirtualProtect` calls by 4 KiB page (preserving per-section protections and falling back cleanly for page-straddling slots), cutting `VirtualProtect` calls from 2x slots down to 2x pages. - Include a 16-hex BLAKE3 digest of `prepared.bytes` in `PreparedImage::file_name()` so `write_if_changed()` checks metadata length (0 bytes read on cache hit) and stages misses via unique per-writer temp files before atomic rename. Adds 4 unit tests covering `rdpid` decoding/emulation, live executable-page atomic NOP patching (`AtomicU64` + `cmpxchg16b` paths + execution + protection restore), page-batched `write_slots`, and content-hashed cache hits.@Marvisak on GitHub · 2026-11-25 01:24 UTC · original
Automated triage for the maintainers.
Needs rebase. This pull request conflicts with
main(conflicting:crates/victory-loader/src/crash.rs,crates/victory-loader/src/windows.rs). It will be reviewed once it is rebased onto the currentmain.Generated by Claude Code
@catminou4 on GitHub · 2026-11-24 17:42 UTC · original · reviewed
Loader crash-handler work: lazy NOP-patch of clwb/monitorx on #UD, rdpid emulation via GetCurrentProcessorNumberEx, page-scatter batching with VirtualQuery/VirtualProtect/FlushInstructionCache. All Windows-only (windows_sys), so macOS cannot compile or test it — static review only, and the SAFETY comments on the exception-path pointer reads are the right discipline for this code. Two notes: (1) victory-loader's own tests already fail on macOS/Linux on main (they parse the test binary as PE; 'Unknown file magic') — pre-existing, same class as the victory-pe image tests I flagged on #529, worth a cfg(windows) gate someday; (2) NOP-patching text pages means FlushInstructionCache + VirtualProtect round-trips per patch — the batching described in the title suggests that's handled, but please double-check the patch cache is keyed per-page so repeated #UDs on the same page don't re-patch.
@hadrianhola on GitHub · 2026-11-26 13:29 UTC · original
Some measurements for what this PR would save, from a CPU that takes these traps: i7-5820K (Haswell-E, 3.3 GHz, no
clwb), Windows 10 19045,mainat63bcca4, Series X profile, 10 minutes (menus, then world entry).1. How many traps. A local counter in
emulate_amd_only(not in any PR), cumulative over the run:clwbclzeromonitorx/mwaitx2. What a trap costs. Cycles spent inside the handler (the counter,
rdtscaround the emulation):clwb202,clzero1,219,monitorx51,mwaitx360. That leaves out the #UD round trip itself. A separate micro-benchmark (ud2in a loop, a vectored handler that only movesRip, 5 × 500,000 traps on the same machine) gives 0.91 µs, about 3,000 TSC cycles, per round trip, so the trap costs far more than the emulation.3. What that adds up to, at ~3,000 + handler cycles per trap:
clwb): about 2.8 G cycles, ~8 % of one core for those 10 s;So on this machine the lazy NOP patch would mostly help the
clwbburst at world entry, and only by a few percent of one core there; the frames at that point take 400 ms to 17 s for GPU reasons. Caveats: one run; the round trip was measured in a small test process, not inside the title (where other vectored handlers may run first); andclwb_loopsites count once per loop, not per cache line.AI-assisted (Claude Code).
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.