Make inline-hook install AND remove safe to spam
The uncapped, input-polling mock_game_test storm (thousands of presents/s, now
also driving the input/focus/MKB hooks) drove out a family of install/remove races
the slow vsync'd mock had masked. Fixes (hook/src/hook_install.hpp + hook_guard.hpp):
- Persistent hooks. The old model created a hook on install and DESTROYED it on
remove (= {}), freeing the trampoline; a detour about to call it (.stdcall) then
hit freed memory -> 0xC0000005. drain() can't fully close that window (a thread
can be inside the detour but not past its Guard ctor). So hooks are now created
ONCE and only enable()/disable()d across install/remove cycles -- never destroyed
during the session -- so a stale detour always calls a live trampoline (disabled,
it just runs the original). Reused, so no churn and no leak. remove_* therefore
disable()s + drain()s but does not destroy; install guards check .enabled().
- Install race. create_inline() enables the hook before the result is move-assigned
into the global the detour reads; a call landing in the detour mid-assign reads a
torn hook -> AV. install_inline() creates StartDisabled, assigns, then enable()s.
- drain() Sleep(1)s BEFORE each zero-check, so a thread that entered the detour but
hasn't reached its Guard registers before we conclude zero.
- Focus: publish g_orig_proc before SetWindowLongPtr activates the subclass (and
subclass_proc falls back to DefWindowProc if null); and disable the focus-query
hooks in reverse install order, because GetForegroundWindow shares user32 code
with GetActiveWindow (keep GFW hooked until GAW is unhooked).
- disable()/enable() [[nodiscard]] results are handled (logged), not (void)-discarded.
Storm now survives on every backend across repeated runs.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include <safetyhook.hpp>
|
||||
|
||||
#include "hook_guard.hpp"
|
||||
#include "hook_install.hpp"
|
||||
#include "hook_registry.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
@@ -174,7 +175,8 @@ void hook_export(HMODULE module, const char* name, void* detour, int registry_id
|
||||
}
|
||||
if (void* target = reinterpret_cast<void*>(GetProcAddress(module, name)))
|
||||
{
|
||||
g_hooks.emplace_back(safetyhook::create_inline(target, detour));
|
||||
g_hooks.emplace_back();
|
||||
install_inline(g_hooks.back(), target, detour); // assign-then-enable (no install race)
|
||||
hook_set_installed(registry_id, true);
|
||||
}
|
||||
}
|
||||
@@ -187,7 +189,8 @@ void hook_ordinal(HMODULE module, WORD ordinal, void* detour, int registry_id)
|
||||
}
|
||||
if (void* target = reinterpret_cast<void*>(GetProcAddress(module, MAKEINTRESOURCEA(ordinal))))
|
||||
{
|
||||
g_hooks.emplace_back(safetyhook::create_inline(target, detour));
|
||||
g_hooks.emplace_back();
|
||||
install_inline(g_hooks.back(), target, detour); // assign-then-enable (no install race)
|
||||
hook_set_installed(registry_id, true);
|
||||
}
|
||||
}
|
||||
@@ -240,7 +243,7 @@ void remove_xinput_hooks()
|
||||
// rate could hit (the same class as the mock-game present-storm crash).
|
||||
for (auto& h : g_hooks)
|
||||
{
|
||||
(void)h.disable();
|
||||
disable_for_removal(h);
|
||||
}
|
||||
hook_set_installed(g_id_getstate, false);
|
||||
hook_set_installed(g_id_getstateex, false);
|
||||
|
||||
Reference in New Issue
Block a user