Console instructions: a fault raced by its own patch runs the new bytes #2614

Closed
github-bot wants to merge 1 commit from github-bot/gh-2614 into main AGit
Owner

@⁠gabrielecabrini on GitHub · 2026-11-29 14:58 UTC · original
gabrielecabrini:fix/console-insn-patch-race → main · +30 −1 · 1 files · 1 commits · closed without merging

What

A console-only instruction (insertq, extrq, monitorx, mwaitx, a clzero loop, clwb) is replaced in the title's code the first time it faults. When two threads hit the same site, one can fault on the old bytes while the other replaces them. The late thread's handler then reads the new bytes at its rip (our jmp to the stub, or the no-ops), which do not decode as console-only, so emulate returned 0 and the title died with an unhandled 0xc000001d.

Evidence

4.5 minutes into play on an Intel i5-14600KF (no SSE4a), Proton Experimental, RTX 4070 SUPER (2026-11-29):

VIctory: unhandled exception 0xc000001d at victory-image-….dll+0x5084782
(code e9 19 bb bf fa 90 c4 e2 79 21 c0 …)

e9 …. 90 at the faulting address is our own jump over a 6-byte insertq: an illegal-instruction fault on a valid jmp, which only a fault taken on the old bytes explains.

Fix

crates/victory-loader/src/console_insn.rs:

  • patch() (waits, clzero loops, SSE4a) and the clwb path note the address they replaced in a PATCHED list.
  • In emulate, when the bytes at rip do not decode and rip is a noted place, it returns CONTINUE_EXECUTION without moving rip: the thread runs the new code.
  • Before this, VICTORY_PATCH_SSE4A=0 VICTORY_PATCH_WAITS=0 VICTORY_PATCH_CLZERO=0 was the only way around it, at the cost of one exception per execution (one insertq alone faulted ~3,700 times a second).

Tests

  • an_sse4a_instruction_becomes_sse2 now also faults a second time at the patched place, as the late thread would, and expects CONTINUE_EXECUTION with rip unchanged.
  • The 9 console_insn tests pass (cross-built with cargo xwin, run under Proton).
  • cargo clippy -p victory-loader --all-targets -- -D warnings stops at an unused crash::unwind_copy without diagnostics; that error was already on main before this change.
  • Not yet confirmed in a long play session.
**@⁠gabrielecabrini** on GitHub · 2026-11-29 14:58 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2614) `gabrielecabrini:fix/console-insn-patch-race` → `main` · +30 −1 · 1 files · 1 commits · closed without merging ## What A console-only instruction (`insertq`, `extrq`, `monitorx`, `mwaitx`, a `clzero` loop, `clwb`) is replaced in the title's code the first time it faults. When two threads hit the same site, one can fault on the old bytes while the other replaces them. The late thread's handler then reads the *new* bytes at its `rip` (our `jmp` to the stub, or the no-ops), which do not decode as console-only, so `emulate` returned 0 and the title died with an unhandled `0xc000001d`. ## Evidence 4.5 minutes into play on an Intel i5-14600KF (no SSE4a), Proton Experimental, RTX 4070 SUPER (2026-11-29): VIctory: unhandled exception 0xc000001d at victory-image-….dll+0x5084782 (code e9 19 bb bf fa 90 c4 e2 79 21 c0 …) `e9 …. 90` at the faulting address is our own jump over a 6-byte `insertq`: an illegal-instruction fault on a valid `jmp`, which only a fault taken on the old bytes explains. ## Fix `crates/victory-loader/src/console_insn.rs`: - `patch()` (waits, `clzero` loops, SSE4a) and the `clwb` path note the address they replaced in a `PATCHED` list. - In `emulate`, when the bytes at `rip` do not decode and `rip` is a noted place, it returns `CONTINUE_EXECUTION` without moving `rip`: the thread runs the new code. - Before this, `VICTORY_PATCH_SSE4A=0 VICTORY_PATCH_WAITS=0 VICTORY_PATCH_CLZERO=0` was the only way around it, at the cost of one exception per execution (one `insertq` alone faulted ~3,700 times a second). ## Tests - `an_sse4a_instruction_becomes_sse2` now also faults a second time at the patched place, as the late thread would, and expects `CONTINUE_EXECUTION` with `rip` unchanged. - The 9 `console_insn` tests pass (cross-built with `cargo xwin`, run under Proton). - `cargo clippy -p victory-loader --all-targets -- -D warnings` stops at an unused `crash::unwind_copy` without `diagnostics`; that error was already on `main` before this change. - Not yet confirmed in a long play session.
A console-only instruction (insertq, extrq, monitorx, mwaitx, a clzero
loop, clwb) is replaced the first time it faults. A second thread that
faulted on the old bytes while the first one replaced them found the
new ones at its rip (the jmp to the stub, or the no-ops), which do not
decode as console-only, so emulate returned 0 and the fault reached
the title as an unhandled 0xc000001d.

Seen 4.5 minutes into play on an i5-14600KF under Proton Experimental
(2026-11-29): the crash report's bytes at the faulting address were
`e9 .. .. .. .. 90`, our own jump over a 6-byte insertq.

The places replaced are now noted; a fault at one whose bytes no
longer decode continues execution without moving rip, so the thread
runs the jump. an_sse4a_instruction_becomes_sse2 now also faults a
second time at the patched place and expects that.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 15:03:21 +00:00
Author
Owner

@⁠dyshay on GitHub · 2026-11-29 15:40 UTC · original

Merged into our fork's integration branch for testing (dyshay/VIctory-NVIDIA, branch janus-prs). I read the full diff first: no network access, process spawning, binaries, build or dependency changes, or game data. It builds with --profile diag --features diagnostics, and the CPU tests of the touched crates pass. Runs on an RTX 4090 are queued (Series X 1440p, and Series S with ray tracing, DLSS Quality). Frame times will follow here.

**@⁠dyshay** on GitHub · 2026-11-29 15:40 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2614#issuecomment-7258271602) Merged into our fork's integration branch for testing ([dyshay/VIctory-NVIDIA](https://github.com/dyshay/VIctory-NVIDIA/tree/janus-prs), branch `janus-prs`). I read the full diff first: no network access, process spawning, binaries, build or dependency changes, or game data. It builds with `--profile diag --features diagnostics`, and the CPU tests of the touched crates pass. Runs on an RTX 4090 are queued (Series X 1440p, and Series S with ray tracing, DLSS Quality). Frame times will follow here.
Author
Owner

@⁠AimLogin on GitHub · 2026-11-29 19:27 UTC · original

Review note on the race fix (read from the diff, not run):

One window is still open. note_patched is called only after the cmpxchg16b store, VirtualProtect and FlushInstructionCache, and was_patched takes only PATCHED, not PATCHING. A thread that faulted on the old bytes can therefore read the new bytes at rip while the patching thread is between the store and note_patched. It finds no entry, emulate returns 0, and the title still dies with 0xc000001d. The two system calls make that gap wider than it looks. The clwb path has the same order (note_patched(rip) after the second VirtualProtect).

Either change closes it:

  • take PATCHING before the lookup in was_patched, so a late thread waits for a patch in flight, which has recorded its site before it releases the lock;
  • or record the site before the store becomes visible (and remove the record again if the store fails).

#2585 fixes the same #2393 by reading the fault bytes under the patch lock and matching them exactly against the recorded replacement, so the two PRs overlap and probably only one should land.

Review was AI-assisted.

**@⁠AimLogin** on GitHub · 2026-11-29 19:27 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2614#issuecomment-7261291775) Review note on the race fix (read from the diff, not run): One window is still open. `note_patched` is called only after the `cmpxchg16b` store, `VirtualProtect` and `FlushInstructionCache`, and `was_patched` takes only `PATCHED`, not `PATCHING`. A thread that faulted on the old bytes can therefore read the new bytes at `rip` while the patching thread is between the store and `note_patched`. It finds no entry, `emulate` returns 0, and the title still dies with `0xc000001d`. The two system calls make that gap wider than it looks. The `clwb` path has the same order (`note_patched(rip)` after the second `VirtualProtect`). Either change closes it: - take `PATCHING` before the lookup in `was_patched`, so a late thread waits for a patch in flight, which has recorded its site before it releases the lock; - or record the site before the store becomes visible (and remove the record again if the store fails). #2585 fixes the same #2393 by reading the fault bytes under the patch lock and matching them exactly against the recorded replacement, so the two PRs overlap and probably only one should land. _Review was AI-assisted._
github-bot closed this pull request 2026-11-29 22:12:22 +00:00
Author
Owner

@⁠gabrielecabrini on GitHub · 2026-11-29 22:08 UTC · original

Closing in favour of #2585, which fixes the same #2393 and closes the window AimLogin pointed out here (the replacement is recorded under PATCHING and the late fault is matched under the same lock). One fix for this is enough.

**@⁠gabrielecabrini** on GitHub · 2026-11-29 22:08 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2614#issuecomment-7263433330) Closing in favour of #2585, which fixes the same #2393 and closes the window AimLogin pointed out here (the replacement is recorded under `PATCHING` and the late fault is matched under the same lock). One fix for this is enough.

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/trustno1-coder--VIctory-NVIDIA!2614
No description provided.