Avoid crash handler / exceptions by patching unsupported instructions #1363
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/fxceless--victory-fork#1363
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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.@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):
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.
@JayFoxRox on GitHub · 2026-11-26 19:01 UTC · original · edited 2026-11-26 19:44 UTC
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
clwband 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 ofclwb(andamd_onlyhandler) 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).