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

Closed
gabrielecabrini wants to merge 1 commit from upstream-pr/2614 into main
gabrielecabrini commented 2026-11-29 14:58:13 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by gabrielecabrini on 2026-11-29T14:58:13Z.


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.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5551925882 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2614). Originally posted by [gabrielecabrini](https://github.com/gabrielecabrini) on 2026-11-29T14:58:13Z. --- ## 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.
dyshay commented 2026-11-29 15:40:09 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by dyshay on 2026-11-29T15:40:09Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7258271602 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2614#issuecomment-7258271602). Originally posted by [dyshay](https://github.com/dyshay) on 2026-11-29T15:40:09Z. --- 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.
AimLogin commented 2026-11-29 19:27:14 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by AimLogin on 2026-11-29T19:27:14Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7261291775 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2614#issuecomment-7261291775). Originally posted by [AimLogin](https://github.com/AimLogin) on 2026-11-29T19:27:14Z. --- 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._
gabrielecabrini commented 2026-11-29 22:08:18 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by gabrielecabrini on 2026-11-29T22:08:18Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7263433330 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2614#issuecomment-7263433330). Originally posted by [gabrielecabrini](https://github.com/gabrielecabrini) on 2026-11-29T22:08:18Z. --- 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

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
trustno1-coder/VIctory-NVIDIA!2614
No description provided.