From 39d3ab8c7dbdce3c2fda26584bc7df8ac7f6c29a Mon Sep 17 00:00:00 2001 From: mz Date: Sun, 22 Nov 2026 16:05:38 +0000 Subject: RT-115: gta6rt --run runs the game's atexit handlers and static destructors The mapped EXE registered atexit handlers and static destructors with the host CRT's process-wide table, which gta6rt never runs, so they were lost. They now go to a table of the run's own: exit() and a return from main run it newest first (including handlers registered during exit), _cexit() runs it and returns, and _exit/_Exit/quick_exit/ExitProcess/abort skip it, as natively. exit/_exit/_Exit/quick_exit share an exit lock like the CRT's, each run starts with an empty table, and a failed registration returns an error instead of throwing into the game. New run_tiny_atexit compares a stand-in program run natively and through --run for 8 termination paths. Not covered yet: at_quick_exit and the thread_local destructor callback. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/process.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 6 deletions(-) (limited to 'runtime/shims/process.cpp') diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index 9da50e4e9..2c83190c0 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp @@ -5,8 +5,14 @@ // - argv/argc and the command line are the game's (set by run_entry), not gta6rt's; // - exit(), _exit(), quick_exit(), ExitProcess() and abort() from the game end the game's run, not the host: // the exit code is recorded, the run is signalled as finished and the calling thread waits for the host to -// exit. atexit handlers and static destructors of the game are not run (its CRT shares the host's, whose -// process-wide atexit table also holds gta6rt's own). +// exit; +// - the game's atexit registrations (_crt_atexit, which MSVC's atexit and static destructors reach; msvcrt's +// atexit and _onexit) go to a table of the run's own instead of the CRT's process-wide one, which the game +// shares with gta6rt. exit() runs it newest first, as the CRT does, before the run ends; _exit(), _Exit(), +// quick_exit(), ExitProcess() and abort() do not. _cexit() runs it and returns, _c_exit() returns. A second +// thread that calls exit(), _exit(), _Exit() or quick_exit() while the first is leaving waits, as with the +// CRT's exit lock. Not covered yet: at_quick_exit() handlers (quick_exit() runs none) and the thread-local +// destructor callback (_register_thread_local_exe_atexit_callback); both still reach the host CRT. // Covered CRTs: the Universal CRT (api-ms-win-crt-runtime-l1-1-0.dll / ucrtbase.dll, what MSVC links; ucrtbased.dll in Debug) and // msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here. // The loader matches imports by exact DLL name, so GetCommandLineA/W and ExitProcess are served under every name a @@ -19,6 +25,8 @@ #include #include #include +#include +#include #include #include #include @@ -84,8 +92,77 @@ std::string narrow(const std::wstring& w) { ExitProcess(static_cast(g_exit_code.load())); } +// ---- the game's atexit table +using AtexitFn = void(__cdecl*)(); +using OnexitFn = int(__cdecl*)(); +std::mutex g_atexit_lock; +std::vector g_atexit; +std::atomic g_exiting_thread{0}; + +bool register_atexit(AtexitFn fn) { + std::lock_guard lock(g_atexit_lock); + try { + g_atexit.push_back(fn); + } catch (const std::bad_alloc&) { + return false; // the CRT reports a full table as a failed registration + } + return true; +} + +// Newest first. A handler registered while the handlers run goes on the end and so runs next, as in the UCRT. +void run_atexit_handlers() { + for (;;) { + AtexitFn fn; + { + std::lock_guard lock(g_atexit_lock); + if (g_atexit.empty()) return; + fn = g_atexit.back(); + g_atexit.pop_back(); + } + fn(); + } +} + +// UCRT: int _crt_atexit(_PVFV), 0 on success. MSVC's atexit() and the static destructors of an EXE reach it. +int __cdecl game_crt_atexit(AtexitFn fn) { + return fn && register_atexit(fn) ? 0 : -1; +} +// msvcrt.dll (MinGW): int atexit(void (*)(void)) and _onexit_t _onexit(_onexit_t). +int __cdecl game_msvcrt_atexit(AtexitFn fn) { return game_crt_atexit(fn); } +OnexitFn __cdecl game_onexit(OnexitFn fn) { + // its int result is ignored, as the CRT ignores it + return fn && register_atexit(reinterpret_cast(fn)) ? fn : nullptr; +} + // ---- the way out -void __cdecl game_exit(int code) { end_run(code); } +// The CRT's exit lock, which exit(), _exit(), _Exit() and quick_exit() all take: the first thread to leave owns it +// and ends the run; any other thread that tries to leave waits forever. The owner may leave again from a handler +// (undefined in C): that ends the run with the new code and skips the remaining handlers. +bool take_exit_lock() { + const DWORD self = GetCurrentThreadId(); + DWORD owner = 0; + if (g_exiting_thread.compare_exchange_strong(owner, self)) return true; + if (owner != self) { + for (;;) Sleep(INFINITE); + } + return false; +} +// exit(): the game's handlers and static destructors, then the end of the run. +void __cdecl game_exit(int code) { + if (take_exit_lock()) run_atexit_handlers(); + end_run(code); +} +// _exit(), _Exit(), quick_exit(): the end of the run without the atexit handlers. +void __cdecl game_quick_exit(int code) { + take_exit_lock(); + end_run(code); +} +// _cexit() runs the handlers and flushes the streams, then returns; _c_exit() only returns. +void __cdecl game_cexit() { + run_atexit_handlers(); + std::fflush(nullptr); +} +void __cdecl game_c_exit() {} void __stdcall game_exit_process(UINT code) { end_run(static_cast(code)); } void __cdecl game_abort() { std::fprintf(stderr, "[rt] process: the game called abort()\n"); @@ -203,6 +280,11 @@ void set_command_line(const std::wstring& program, const std::vector lock(g_atexit_lock); + g_atexit.clear(); // a run never sees the handlers of an earlier one + } + g_exiting_thread = 0; g_exited = false; g_exit_code = 0; g_done = CreateEventW(nullptr, TRUE, FALSE, nullptr); @@ -231,8 +313,11 @@ int run_entry(void* entry, size_t stack_bytes) { namespace { using namespace rt::process; #define UCRT(dll) \ - {dll, "exit", reinterpret_cast(&game_exit)}, {dll, "_exit", reinterpret_cast(&game_exit)}, \ - {dll, "_Exit", reinterpret_cast(&game_exit)}, {dll, "quick_exit", reinterpret_cast(&game_exit)}, \ + {dll, "exit", reinterpret_cast(&game_exit)}, {dll, "_exit", reinterpret_cast(&game_quick_exit)}, \ + {dll, "_Exit", reinterpret_cast(&game_quick_exit)}, \ + {dll, "quick_exit", reinterpret_cast(&game_quick_exit)}, \ + {dll, "_crt_atexit", reinterpret_cast(&game_crt_atexit)}, \ + {dll, "_cexit", reinterpret_cast(&game_cexit)}, {dll, "_c_exit", reinterpret_cast(&game_c_exit)}, \ {dll, "abort", reinterpret_cast(&game_abort)}, {dll, "__p___argc", reinterpret_cast(&game_p_argc)}, \ {dll, "__p___argv", reinterpret_cast(&game_p_argv)}, \ {dll, "__p___wargv", reinterpret_cast(&game_p_wargv)}, \ @@ -253,7 +338,11 @@ SHIM_TABLE(process, UCRT("ucrtbase.dll"), UCRT("ucrtbased.dll"), // the Debug UCRT: a Debug build of the stand-in game (run_tiny_game) links it {"msvcrt.dll", "exit", reinterpret_cast(&game_exit)}, - {"msvcrt.dll", "_exit", reinterpret_cast(&game_exit)}, + {"msvcrt.dll", "_exit", reinterpret_cast(&game_quick_exit)}, + {"msvcrt.dll", "atexit", reinterpret_cast(&game_msvcrt_atexit)}, + {"msvcrt.dll", "_onexit", reinterpret_cast(&game_onexit)}, + {"msvcrt.dll", "_cexit", reinterpret_cast(&game_cexit)}, + {"msvcrt.dll", "_c_exit", reinterpret_cast(&game_c_exit)}, {"msvcrt.dll", "abort", reinterpret_cast(&game_abort)}, {"msvcrt.dll", "__getmainargs", reinterpret_cast(&game_getmainargs)}, {"msvcrt.dll", "__wgetmainargs", reinterpret_cast(&game_wgetmainargs)}, -- cgit v1.2.3