Retry in-flight faults after instruction patching #3599

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

@⁠slyd3 on GitHub · 2026-11-30 01:33 UTC · original
slyd3:fix-sse4a-patch-race → main · +47 −6 · 1 files · 1 commits · closed without merging

Summary

  • Snapshot instruction bytes under the patch lock and record installed replacements.
  • Retry an illegal-instruction fault at the same address only if it matches one of our recorded patches. This handles another thread patching an instruction after the CPU faults but before the handler decodes it.
  • Add a regression test for an in-flight SSE4a fault and reject unrelated or changed jumps.

Evidence and limits

A gameplay crash reported an illegal instruction at a site whose original instruction was SSE4a, but whose crash-time bytes had already been replaced with a jump. The race is consistent with that evidence, but the game has not yet been retested with this change. Other memory, VRAM and Streamline warnings in those sessions are not addressed here.

Validation

  • cargo test -p victory-loader --lib (76 passed)
  • cargo check -p victory-loader --features diagnostics
  • git diff --check
  • cargo fmt -p victory-loader -- --check reports extensive existing formatting differences across the package.
**@⁠slyd3** on GitHub · 2026-11-30 01:33 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3599) `slyd3:fix-sse4a-patch-race` → `main` · +47 −6 · 1 files · 1 commits · closed without merging ## Summary - Snapshot instruction bytes under the patch lock and record installed replacements. - Retry an illegal-instruction fault at the same address only if it matches one of our recorded patches. This handles another thread patching an instruction after the CPU faults but before the handler decodes it. - Add a regression test for an in-flight SSE4a fault and reject unrelated or changed jumps. ## Evidence and limits A gameplay crash reported an illegal instruction at a site whose original instruction was SSE4a, but whose crash-time bytes had already been replaced with a jump. The race is consistent with that evidence, but the game has not yet been retested with this change. Other memory, VRAM and Streamline warnings in those sessions are not addressed here. ## Validation - `cargo test -p victory-loader --lib` (76 passed) - `cargo check -p victory-loader --features diagnostics` - `git diff --check` - `cargo fmt -p victory-loader -- --check` reports extensive existing formatting differences across the package.
Retry an illegal-instruction fault when another thread has already installed an instruction replacement at that address. Snapshot code and check registered replacements under the patch lock.

Co-authored-by: Copilot <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 01:37:22 +00:00
Author
Owner

@⁠babuakhil on GitHub · 2026-11-30 18:45 UTC · original

Three open PRs fix the same race in console_insn::emulate: this one, #4194 and #4196. Thread Y faults on a console instruction, thread X's handler replaces it, and Y's handler then reads the replacement, decodes nothing, and returns 0, the unhandled 0xc000001d that #4196 logged at sites starting e9. I compared all three on Windows 11 (Rust 1.98.1, pinned), each applied on current main (e1251c31); all three cherry-pick cleanly. cargo test -p victory-loader --lib passes on each (75 on main, 76 with #4194 or this PR, which add a test), and Clippy on victory-loader is clean with -D warnings on all three. Not tested in the game.

What each one covers. The module replaces four kinds of instruction, and not all of them with a jump: SSE4a and clzero loops become a jmp to a stub, while monitorx and mwaitx are overwritten in place (0f 1f 00, f3 90 90). A throwaway test (not part of these PRs) ran emulate for thread X on each kind, which replaces it, then emulate for a thread Y whose context still points there:

Y's handler, after X replaced the instruction main #4194 #4196 this PR
SSE4a insertq (jump to a stub) 0, crash CONTINUE CONTINUE CONTINUE
clzero loop (jump to a stub) 0, crash CONTINUE CONTINUE CONTINUE
monitorx (replaced in place) 0, crash CONTINUE 0, crash CONTINUE
mwaitx (replaced in place) 0, crash CONTINUE 0, crash CONTINUE

#4196's is_our_jump only recognises a jump into STUBS, so the wait instructions, which spin loops sit on and where many threads meet the same site at once, still crash with it. #4194 and this PR record every replacement patch installs, so they cover all four. (#4194 retries once per thread and address, and a second immediate fault at the same place returns 0, a guard against looping. This PR retries whenever the bytes are still ours.)

Cost. #4194 and this PR take PATCHING on every emulated instruction. Emulating an insertq that can't be replaced (it crosses its 16 bytes), release build, best of three:

main #4194 #4196 this PR
1 thread, ns per instruction 17 19 17 20
16 threads, ns per instruction per thread 1745 1910 1725 1705

The extra lock is lost in what the path already costs under contention on main, and a real fault costs microseconds before emulate runs, so I don't think speed decides it.

So I'd take this PR or #4194, which behave the same here, and not #4196 alone. #4196's crash logs and its Intel reproduction are the best evidence for the race, and they apply equally to the other two.

**@⁠babuakhil** on GitHub · 2026-11-30 18:45 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3599#issuecomment-7288323696) Three open PRs fix the same race in `console_insn::emulate`: this one, #4194 and #4196. Thread Y faults on a console instruction, thread X's handler replaces it, and Y's handler then reads the replacement, decodes nothing, and returns 0, the unhandled `0xc000001d` that #4196 logged at sites starting `e9`. I compared all three on Windows 11 (Rust 1.98.1, pinned), each applied on current `main` (`e1251c31`); all three cherry-pick cleanly. `cargo test -p victory-loader --lib` passes on each (75 on `main`, 76 with #4194 or this PR, which add a test), and Clippy on `victory-loader` is clean with `-D warnings` on all three. Not tested in the game. **What each one covers.** The module replaces four kinds of instruction, and not all of them with a jump: SSE4a and `clzero` loops become a `jmp` to a stub, while `monitorx` and `mwaitx` are overwritten in place (`0f 1f 00`, `f3 90 90`). A throwaway test (not part of these PRs) ran `emulate` for thread X on each kind, which replaces it, then `emulate` for a thread Y whose context still points there: | Y's handler, after X replaced the instruction | `main` | #4194 | #4196 | this PR | |---|---|---|---|---| | SSE4a `insertq` (jump to a stub) | 0, crash | CONTINUE | CONTINUE | CONTINUE | | `clzero` loop (jump to a stub) | 0, crash | CONTINUE | CONTINUE | CONTINUE | | `monitorx` (replaced in place) | 0, crash | CONTINUE | **0, crash** | CONTINUE | | `mwaitx` (replaced in place) | 0, crash | CONTINUE | **0, crash** | CONTINUE | #4196's `is_our_jump` only recognises a jump into `STUBS`, so the wait instructions, which spin loops sit on and where many threads meet the same site at once, still crash with it. #4194 and this PR record every replacement `patch` installs, so they cover all four. (#4194 retries once per thread and address, and a second immediate fault at the same place returns 0, a guard against looping. This PR retries whenever the bytes are still ours.) **Cost.** #4194 and this PR take `PATCHING` on every emulated instruction. Emulating an `insertq` that can't be replaced (it crosses its 16 bytes), release build, best of three: | | `main` | #4194 | #4196 | this PR | |---|---|---|---|---| | 1 thread, ns per instruction | 17 | 19 | 17 | 20 | | 16 threads, ns per instruction per thread | 1745 | 1910 | 1725 | 1705 | The extra lock is lost in what the path already costs under contention on `main`, and a real fault costs microseconds before `emulate` runs, so I don't think speed decides it. So I'd take this PR or #4194, which behave the same here, and not #4196 alone. #4196's crash logs and its Intel reproduction are the best evidence for the race, and they apply equally to the other two.
Author
Owner

@⁠trustno1-coder on GitHub · 2026-12-01 18:50 UTC · original

Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

**@⁠trustno1-coder** on GitHub · 2026-12-01 18:50 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3599#issuecomment-7321657114) Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.
github-bot closed this pull request 2026-12-01 18:58:12 +00:00

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!3599
No description provided.