loader: retry in-flight faults at already-patched instruction sites #184

Manually merged
github merged 1 commit from gh-pr/184/head into gh-pr/184/base 2026-12-01 20:16:36 +00:00
Owner

@⁠Androtim on GitHub · 2026-11-30 02:14 UTC · original
Androtim:feat/iter181-loader-retry-inflight-patch-fault → main · +52 −7 · 1 files · 1 commits · merged 2026-11-30 03:06 UTC

Summary

  • Serialize instruction emulation snapshots in console_insn::emulate with PATCHING, which now records each committed replacement keyed by instruction address (BTreeMap<usize, Vec<u8>>).
  • When a thread had already entered an illegal-instruction fault just as another thread patched that site (via patch or clwb NOP fill), emulate observes our recorded bytes at RIP and returns CONTINUE_EXECUTION without advancing RIP so the thread retries execution cleanly on the patched instruction instead of failing to decode it and crashing.
  • Added unit test console_insn::tests::fault_already_in_flight_retries_only_our_unchanged_patch covering retry of our unchanged patch, unpatched non-console bytes, and overwritten code at the previously patched address.

Verification

  • cargo +1.98.1-x86_64-pc-windows-gnu run -p xtask -- policy (311 files ok)
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-loader --lib (59 passed; 0 failed)
  • cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets -- -D warnings
  • cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --features diagnostics -- -D warnings
**@⁠Androtim** on GitHub · 2026-11-30 02:14 UTC · [original](https://github.com/Androtim/MiniVI/pull/184) `Androtim:feat/iter181-loader-retry-inflight-patch-fault` → `main` · +52 −7 · 1 files · 1 commits · merged 2026-11-30 03:06 UTC ## Summary - Serialize instruction emulation snapshots in `console_insn::emulate` with `PATCHING`, which now records each committed replacement keyed by instruction address (`BTreeMap<usize, Vec<u8>>`). - When a thread had already entered an illegal-instruction fault just as another thread patched that site (via `patch` or `clwb` NOP fill), `emulate` observes our recorded bytes at `RIP` and returns `CONTINUE_EXECUTION` without advancing `RIP` so the thread retries execution cleanly on the patched instruction instead of failing to decode it and crashing. - Added unit test `console_insn::tests::fault_already_in_flight_retries_only_our_unchanged_patch` covering retry of our unchanged patch, unpatched non-console bytes, and overwritten code at the previously patched address. ## Verification - `cargo +1.98.1-x86_64-pc-windows-gnu run -p xtask -- policy` (`311 files ok`) - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-loader --lib` (`59 passed; 0 failed`) - `cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets -- -D warnings` - `cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --features diagnostics -- -D warnings`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 20:16:34 +00:00
github manually merged commit a563b18067 into gh-pr/184/base 2026-12-01 20:16:36 +00:00
Author
Owner

@⁠Androtim on GitHub · 2026-11-30 03:04 UTC · original

Automated triage for the maintainers (not an approval).

Verdict: merge-candidate · CI (check): passing

Review Summary

Clean, well-scoped fix for a real multithreaded #UD exception race in crates/victory-loader/src/console_insn.rs.

Verified Details

  • Serialization & retry logic (PATCHING, patch, emulate): Changing PATCHING from Mutex<()> to Mutex<BTreeMap<usize, Vec<u8>>> and snapshotting rip under PATCHING in emulate ensures a thread with an in-flight fault cannot observe half-written patch bytes or miss the recorded replacement. When patches.get(&rip) matches bytes[..n], returning CONTINUE_EXECUTION without advancing ctx.Rip cleanly re-executes the patched instruction.
  • No lock-ordering / self-deadlock hazard: emulate drops the PATCHING guard before decoding and before any ConsoleOnly::Clwb, Monitorx/Mwaitx, or ClzeroLoop branch re-acquires PATCHING (directly or via patch()/patch_clzero()).
  • Safe against stale/overwritten code: Checking bytes[..n].starts_with(patch) prevents infinite retry loops if code at a previously patched address is later overwritten.
  • Tests & static flags: tests::fault_already_in_flight_retries_only_our_unchanged_patch covers all three cases (retry on unchanged patch, return 0 on unpatched non-console bytes, return 0 on overwritten bytes at a patched site). The VirtualAlloc/PAGE_EXECUTE_READWRITE static-flag hits are confined to this unit test, mirroring the existing clzero_loops_far_apart_are_each_replaced test.

CI is passing and this change is purely CPU instruction emulation in victory-loader (no GPU/shader/host/launcher impact), so it is ready as a merge candidate.


Generated with Antigravity CLI (agy).

**@⁠Androtim** on GitHub · 2026-11-30 03:04 UTC · [original](https://github.com/Androtim/MiniVI/pull/184#issuecomment-7267557056) <!-- minivi-triage v1 head=a563b18067a7b285827257c70e3280d2c4c519dd verdict=merge-candidate ci=passing at=2026-11-30T03:04:38Z --> Automated triage for the maintainers (not an approval). **Verdict:** `merge-candidate` · **CI (`check`):** `passing` ### Review Summary Clean, well-scoped fix for a real multithreaded `#UD` exception race in `crates/victory-loader/src/console_insn.rs`. ### Verified Details - **Serialization & retry logic (`PATCHING`, `patch`, `emulate`)**: Changing `PATCHING` from `Mutex<()>` to `Mutex<BTreeMap<usize, Vec<u8>>>` and snapshotting `rip` under `PATCHING` in `emulate` ensures a thread with an in-flight fault cannot observe half-written patch bytes or miss the recorded replacement. When `patches.get(&rip)` matches `bytes[..n]`, returning `CONTINUE_EXECUTION` without advancing `ctx.Rip` cleanly re-executes the patched instruction. - **No lock-ordering / self-deadlock hazard**: `emulate` drops the `PATCHING` guard before decoding and before any `ConsoleOnly::Clwb`, `Monitorx`/`Mwaitx`, or `ClzeroLoop` branch re-acquires `PATCHING` (directly or via `patch()`/`patch_clzero()`). - **Safe against stale/overwritten code**: Checking `bytes[..n].starts_with(patch)` prevents infinite retry loops if code at a previously patched address is later overwritten. - **Tests & static flags**: `tests::fault_already_in_flight_retries_only_our_unchanged_patch` covers all three cases (retry on unchanged patch, return `0` on unpatched non-console bytes, return `0` on overwritten bytes at a patched site). The `VirtualAlloc`/`PAGE_EXECUTE_READWRITE` static-flag hits are confined to this unit test, mirroring the existing `clzero_loops_far_apart_are_each_replaced` test. CI is passing and this change is purely CPU instruction emulation in `victory-loader` (no GPU/shader/host/launcher impact), so it is ready as a merge candidate. --- _Generated with Antigravity CLI (`agy`)._
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/Androtim--MiniVI!184
No description provided.