Flush instruction cache after (un)hooking so the ARM64 emulator re-JITs

MinHook flushes only the 5 patched bytes; under the x86-on-ARM64
emulator (xtajit) that can leave a stale JIT translation of the
modified prologue and trap with a non-continuable breakpoint. Flush the
whole prologue and the trampoline on install, and the restored prologue
on teardown, so the emulator re-translates them. No-op on native x86.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc
2026-06-10 18:03:36 +00:00
parent 13abd4e546
commit e762e1b716
2 changed files with 17 additions and 0 deletions
+13
View File
@@ -32,6 +32,10 @@ namespace {
// --- IDirect3D*Texture9 (IUnknown layout) ---------------------------
constexpr int kResource_Release = 2;
// Bytes to flush from a hook target / trampoline so the x86-on-ARM64 emulator re-JITs
// it. Covers MinHook's max prologue patch and its 64-byte trampoline slot.
constexpr SIZE_T kFlushSpan = 64;
void** GetVTable(void* com_object)
{
return *static_cast<void***>(com_object);
@@ -98,6 +102,13 @@ namespace {
Warning("D3D9Hooks: MH_EnableHook failed for %p\n", target);
return false;
}
// Under the x86-on-ARM64 emulator, patched code is only re-translated when its
// instruction cache is flushed. MinHook flushes just the 5 patched bytes, which can
// leave a stale/guarded JIT block and trap; flush the whole prologue and the freshly
// built trampoline so the emulator re-JITs both. No-op on native x86.
const HANDLE proc = GetCurrentProcess();
FlushInstructionCache(proc, target, kFlushSpan);
if (original && *original) FlushInstructionCache(proc, *original, kFlushSpan);
g_hooked_targets.push_back(target);
return true;
}
@@ -361,9 +372,11 @@ void RemoveAllD3D9Hooks()
// Signal first: a detour reached after this (e.g. a surviving patch) falls through.
g_unhooked = true;
const HANDLE proc = GetCurrentProcess();
for (void* target : g_hooked_targets) {
MH_DisableHook(target);
MH_RemoveHook(target);
FlushInstructionCache(proc, target, kFlushSpan); // re-JIT the restored prologue under emulation
}
g_hooked_targets.clear();
+4
View File
@@ -252,6 +252,10 @@ void InitInstance(HINSTANCE hModule)
if (GetProcAddress_fn) {
MH_CreateHook(GetProcAddress_fn, OnGetProcAddress, (void**)&GetProcAddress_ret);
MH_EnableHook(GetProcAddress_fn);
// Re-JIT the patched prologue + trampoline under the x86-on-ARM64 emulator (no-op native).
const HANDLE proc = GetCurrentProcess();
FlushInstructionCache(proc, (void*)GetProcAddress_fn, 64);
if (GetProcAddress_ret) FlushInstructionCache(proc, (void*)GetProcAddress_ret, 64);
}
}