Graceful disconnect: tell the DLL to unhook everything
On an explicit Disconnect and on graceful tool exit, the host now asks the injected DLL to remove every subsystem so the game runs exactly as if it was never hooked (each hook restores its original bytes). The DLL stays injected but dormant, ready for a later reconnect -- we never eject it. Before, both paths just dropped the IPC channel (IpcServer::stop) without telling the DLL, leaving the hooks active with frozen forwarded state until the game exited. - IpcServer::request_unhook_all() sets every subsystem_disabled flag (the DLL reconciles to fully unhooked on its next tick); all_hooks_removed() reads the hook registry back so the host can confirm the game is vanilla. - InjectionPanel::disconnect_graceful() requests the unhook, waits (bounded) for the registry to clear, then stops. Wired into the Disconnect button (700ms) and the destructor (300ms). The flags persist in the section the DLL keeps alive, so the unhook completes even if the host exits before confirming. Tests (failing first): - ipc_server_test: request_unhook_all() disables all subsystems; all_hooks_removed() tracks the registry. Deterministic, no game. - mock_game_test test_graceful_disconnect: inject -> hooks installed -> request unhook-all -> every hook removed (game vanilla) while the DLL stays alive (heartbeat advancing). Full suite still passes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -958,6 +958,87 @@ void test_hook_storm(const char* backend, ID3D11Device* device, bool vk_early)
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// Number of hooks the DLL currently reports as installed (across all subsystems).
|
||||
std::uint32_t installed_hook_count(const SharedBlock* block)
|
||||
{
|
||||
std::uint32_t count = block->status.hook_entry_count;
|
||||
if (count > kMaxHookEntries)
|
||||
{
|
||||
count = kMaxHookEntries;
|
||||
}
|
||||
std::uint32_t installed = 0;
|
||||
for (std::uint32_t i = 0; i < count; ++i)
|
||||
{
|
||||
installed += block->status.hook_entries[i].installed != 0 ? 1u : 0u;
|
||||
}
|
||||
return installed;
|
||||
}
|
||||
|
||||
// Graceful disconnect contract (the host-side IpcServer::request_unhook_all path): once the host
|
||||
// asks for every subsystem off, the DLL must remove ALL its hooks -- the game runs as if it was
|
||||
// never touched -- yet stay alive (heartbeat advancing) so it can be reconnected later. Models what
|
||||
// the Disconnect button / tool-exit does (set all subsystem_disabled = 1) and asserts the outcome.
|
||||
void test_graceful_disconnect(const char* backend)
|
||||
{
|
||||
std::printf("== graceful disconnect: %s ==\n", backend);
|
||||
std::wstring wbackend;
|
||||
for (const char* p = backend; *p != '\0'; ++p)
|
||||
{
|
||||
wbackend.push_back(static_cast<wchar_t>(*p));
|
||||
}
|
||||
MockGame game = MockGame::launch(wbackend + L" 30");
|
||||
if (!game.ok)
|
||||
{
|
||||
check(false, "launch mock game (graceful disconnect)");
|
||||
return;
|
||||
}
|
||||
Sleep(800);
|
||||
|
||||
// Enable input + focus + video + MKB (all of which the mock exercises); leave audio off so the
|
||||
// test doesn't depend on an audio endpoint. Then inject.
|
||||
SharedMemory shm;
|
||||
SharedBlock* block = make_ipc(shm, game.pid(), 1u << HookSubsys_Audio);
|
||||
if (block == nullptr || !inject_retry(game.pid()))
|
||||
{
|
||||
check(false, "inject mock game (graceful disconnect)");
|
||||
game.kill();
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait for the DLL to install its hooks.
|
||||
bool installed = false;
|
||||
for (int i = 0; i < 100 && game.alive() && !installed; ++i) // up to ~5 s
|
||||
{
|
||||
Sleep(50);
|
||||
installed = installed_hook_count(block) > 0;
|
||||
}
|
||||
check(installed, "hooks installed after inject (something to unhook)");
|
||||
const std::uint32_t hb0 = block->status.heartbeat.load(std::memory_order_relaxed);
|
||||
|
||||
// Graceful disconnect: request every subsystem removed (what request_unhook_all writes).
|
||||
for (std::uint32_t s = 0; s < HookSubsys_Count; ++s)
|
||||
{
|
||||
block->control.subsystem_disabled[s].store(1u, std::memory_order_release);
|
||||
}
|
||||
|
||||
// The DLL must remove every hook -> game vanilla.
|
||||
bool vanilla = false;
|
||||
for (int i = 0; i < 100 && game.alive() && !vanilla; ++i) // up to ~5 s
|
||||
{
|
||||
Sleep(50);
|
||||
vanilla = installed_hook_count(block) == 0;
|
||||
}
|
||||
check(vanilla, "DLL removed every hook on request (game runs vanilla)");
|
||||
|
||||
// ... and stay alive (dormant), so a later reconnect can re-enable it.
|
||||
check(game.alive(), "game still alive after graceful disconnect");
|
||||
Sleep(400);
|
||||
const std::uint32_t hb1 = block->status.heartbeat.load(std::memory_order_relaxed);
|
||||
check(hb1 != hb0, "DLL heartbeat still advancing (injected but dormant)");
|
||||
|
||||
game.kill();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
kill_stray_mock_games(); // clean slate: no leftover game holding coop_hook.dll
|
||||
@@ -988,6 +1069,10 @@ int main()
|
||||
|
||||
test_av_and_hook_cycles(device);
|
||||
|
||||
// Graceful disconnect: the host asks the DLL to unhook everything; the game must return to
|
||||
// vanilla while the DLL stays injected/dormant (the reconnect-friendly teardown).
|
||||
test_graceful_disconnect("dx11");
|
||||
|
||||
// Aggressive hook/unhook storm across every backend: a separate thread thrashes every
|
||||
// subsystem on/off while the game presents, to catch an unsafe install/remove race (the
|
||||
// "spamming Mirror video crashed Brotato" use-after-free). vk uses the early-load path.
|
||||
|
||||
Reference in New Issue
Block a user