Avoid crash handler / exceptions by patching unsupported instructions #1363

Open
opened 2026-11-26 18:26:21 +00:00 by github-bot · 2 comments
Owner

@⁠JayFoxRox on GitHub · 2026-11-26 18:24 UTC · original

There's an unhandled exception handler in github.com/fxceless/victory-fork@e4245f3c1b/crates/victory-loader/src/crash.rs (L359)

It's currently unknown how often this triggers, so it's unknown if there's a performance potential.
However, like #1305, the code could be patched after the first encounter of an unsupported instruction.
It's a low hanging fruit and it would make the code "cleaner" by remaining in user-code.

Instead of emulating the code in the high level exception handler, rewrite/patch the code to avoid future exceptions (either jumping to the emulation handler, or by actually generating new code / nop-ing instructions which don't need emulation to avoid the trampoline).

Such framework could also be used to patch/emulate AVX2 (maybe even AVX) which isn't present in some - otherwise potentially fast enough - Xeon CPUs (not just for GTA VI, but maybe also if code gets reused for other games). However, this is a relatively niche feature so maybe it's better for a fork.

Note: There's an additional issue here https://github.com/Marvisak/VIctory/pull/45/changes#r4684301102 that was probably never resolved that might break the instruction emulation for clwb.

**@⁠JayFoxRox** on GitHub · 2026-11-26 18:24 UTC · [original](https://github.com/fxceless/victory-fork/issues/1363) There's an unhandled exception handler in https://github.com/fxceless/victory-fork/blob/e4245f3c1b0490250d9cf0946a2113000be98687/crates/victory-loader/src/crash.rs#L359 It's currently unknown how often this triggers, so it's unknown if there's a performance potential. However, like #1305, the code could be patched after the first encounter of an unsupported instruction. It's a low hanging fruit and it would make the code "cleaner" by remaining in user-code. Instead of emulating the code in the high level exception handler, rewrite/patch the code to avoid future exceptions (either jumping to the emulation handler, or by actually generating new code / `nop`-ing instructions which don't need emulation to avoid the trampoline). Such framework could also be used to patch/emulate AVX2 (maybe even AVX) which isn't present in some - otherwise *potentially* fast enough - Xeon CPUs (not just for GTA VI, but maybe also if code gets reused for other games). However, this is a relatively niche feature so maybe it's better for a fork. Note: There's an additional issue here https://github.com/Marvisak/VIctory/pull/45/changes#r4684301102 that was probably never resolved that might break the instruction emulation for `clwb`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 18:26:21 +00:00
Author
Owner

@⁠liuycsbxbxn on GitHub · 2026-11-26 18:52 UTC · original

Good catch, and I think this is a better first landing spot than #1305: the crash handler exists today, and its frequency is measurable, whereas the guard-page path needs the card to mean anything.

I already built the three pieces this needs, as a standalone crate, green on a plain Windows box (26 tests):

  • exact-length x86-64 decoder. It refuses VEX/EVEX rather than guessing a length, because a wrong length corrupts whatever follows the copied instruction.
  • a fault-site recorder that aggregates per (module, RVA) and patches nothing. That is what answers "it is currently unknown how often this triggers" without guessing, and its output is what should decide the emulation subset instead of me picking instructions.
  • a patcher, plus a live test on a synthetic PAGE_NOACCESS page: the first access faults exactly once, the handler rewrites the whole instruction in place, and later calls never fault. Byte-exact restore is covered too.

For this issue the same machinery applies verbatim: record from the crash handler, then patch the site to jump to the emulation handler, or NOP instructions that need no emulation, exactly as you described. AVX2/AVX would be the same mechanism with a different handler table, so the framework does not need to know why a site is unsupported.

One thing worth settling first, given the clwb note you linked: patching makes an emulation bug permanent for that site, so a handler should be correct and covered by a test before the patch path is enabled for it. Otherwise we bake the bug into the running binary and lose the crash handler that would have caught it.

Also a design question before I wire it: should the patch cache key on (module build id, RVA, hash of the original bytes)? That is what makes pre-patching or cross-run reuse safe when a game update moves code, and I would rather build it in now than retrofit it.

Happy to land it here first and keep #1305 as the follow-up.

**@⁠liuycsbxbxn** on GitHub · 2026-11-26 18:52 UTC · [original](https://github.com/fxceless/victory-fork/issues/1363#issuecomment-7200952384) Good catch, and I think this is a better first landing spot than #1305: the crash handler exists today, and its frequency is measurable, whereas the guard-page path needs the card to mean anything. I already built the three pieces this needs, as a standalone crate, green on a plain Windows box (26 tests): - exact-length x86-64 decoder. It refuses VEX/EVEX rather than guessing a length, because a wrong length corrupts whatever follows the copied instruction. - a fault-site recorder that aggregates per (module, RVA) and patches nothing. That is what answers "it is currently unknown how often this triggers" without guessing, and its output is what should decide the emulation subset instead of me picking instructions. - a patcher, plus a live test on a synthetic PAGE_NOACCESS page: the first access faults exactly once, the handler rewrites the whole instruction in place, and later calls never fault. Byte-exact restore is covered too. For this issue the same machinery applies verbatim: record from the crash handler, then patch the site to jump to the emulation handler, or NOP instructions that need no emulation, exactly as you described. AVX2/AVX would be the same mechanism with a different handler table, so the framework does not need to know why a site is unsupported. One thing worth settling first, given the clwb note you linked: patching makes an emulation bug permanent for that site, so a handler should be correct and covered by a test before the patch path is enabled for it. Otherwise we bake the bug into the running binary and lose the crash handler that would have caught it. Also a design question before I wire it: should the patch cache key on (module build id, RVA, hash of the original bytes)? That is what makes pre-patching or cross-run reuse safe when a game update moves code, and I would rather build it in now than retrofit it. Happy to land it here first and keep #1305 as the follow-up.
Author
Owner

@⁠JayFoxRox on GitHub · 2026-11-26 19:01 UTC · original · edited 2026-11-26 19:44 UTC

One thing worth settling first, given the clwb note you linked: patching makes an emulation bug permanent for that site, so a handler should be correct and covered by a test before the patch path is enabled for it. Otherwise we bake the bug into the running binary and lose the crash handler that would have caught it.

It simply wouldn't detect clwb (assuming instruction decoding before patching, instead of generic catch-all trampoline patch like the current exception handler), so nothing would get patched - hence the bug would not get embedded into the running binary.
If that bug occurs, VIctory simply won't recognize the illegal instruction as clwb and fails.

There was existing detection code for SSE4a which actually uses let b = |i: usize| unsafe { code.add(i).read() }; to avoid that kind of bug. Overal, the integration of clwb (and amd_only handler) in particular was rather poor and didn't follow the preexisting code style / was integrated a bit differently from the rest (so, yes, should be cleaned/refactored when integrating the patching).

**@⁠JayFoxRox** on GitHub · 2026-11-26 19:01 UTC · [original](https://github.com/fxceless/victory-fork/issues/1363#issuecomment-7201118694) · edited 2026-11-26 19:44 UTC > One thing worth settling first, given the clwb note you linked: patching makes an emulation bug permanent for that site, so a handler should be correct and covered by a test before the patch path is enabled for it. Otherwise we bake the bug into the running binary and lose the crash handler that would have caught it. It simply wouldn't detect `clwb` (assuming instruction decoding before patching, instead of generic catch-all trampoline patch like the current exception handler), so nothing would get patched - hence the bug would *not* get embedded into the running binary. *If* that bug occurs, VIctory simply won't recognize the illegal instruction as `clwb` and fails. There was existing detection code for SSE4a which actually uses `let b = |i: usize| unsafe { code.add(i).read() };` to avoid that kind of bug. Overal, the integration of `clwb` (and `amd_only` handler) in particular was rather poor and didn't follow the preexisting code style / was integrated a bit differently from the rest (so, yes, should be cleaned/refactored when integrating the patching).
This discussion has been locked. Commenting is limited to contributors.
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/fxceless--victory-fork#1363
No description provided.