Loader: lazily NOP-patch clwb/monitorx on #UD, emulate rdpid, batch IAT protection, and content-hash cached DLLs #1372
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/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?
Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T17:38:37Z.
Summary
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— passedImported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T17:42:47Z.
Review: COMMENTED
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.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:24:30Z.
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
Imported from Marvisak/VIctory. Originally posted by hadrianhola on 2026-11-26T13:29:40Z.
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
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.