From 7cd22658315af07faa32b69b05b8ba5d96e65355 Mon Sep 17 00:00:00 2001 From: ERROR890 <71877675+ERROR890@users.noreply.github.com> Date: Thu, 26 Nov 2026 21:59:18 +0700 Subject: [PATCH] process: cover the game's thread-local exe-atexit callback _register_thread_local_exe_atexit_callback is the CRT's per-thread hook for an EXE's thread_local destructors. The runtime left it to the host UCRT, which is the gap shims/process.cpp's header records (''both still reach the host CRT''). On a73a57e1 that path is where the retail title stopped: the UCRT's consistency check failed in this process, its cold path called ucrtbase!terminate, and the title aborted with 0xC0000409 after the static initialisers and before its first D3D12 call. Keep the callback per thread in process.cpp and return the previous one as the UCRT does. The loader runs it in the DLL_THREAD_DETACH branch of on_host_thread_event, after the DLL slots' callbacks and the executable's own, which is where the UCRT would run it; a throw is caught so the Windows loader lock is never left held. process_cmdline_test covers it: the shim is found, the first registration returns no previous callback and runs nothing yet, the runner runs it once and not twice, and a later registration returns the callback it replaces. --- runtime/shims/process.cpp | 29 ++++++++++++++-- runtime/shims/process_shim.h | 5 +++ runtime/src/pe_loader.cpp | 12 +++++-- runtime/test/process_cmdline_test.cpp | 49 +++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 4 deletions(-) diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index 08023ea78d..4531e8ec7e 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp @@ -11,8 +11,8 @@ // 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. +// CRT's exit lock. The thread-local destructor callback is kept here too, one per thread, and run by the +// loader when that thread ends. Not covered yet: at_quick_exit() handlers (quick_exit() runs none). // 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 @@ -135,6 +135,22 @@ void run_atexit_handlers() { int __cdecl game_crt_atexit(AtexitFn fn) { return fn && register_atexit(fn) ? 0 : -1; } + +// UCRT: _tls_callback_fn _register_thread_local_exe_atexit_callback(_tls_callback_fn), the previous one back. The +// UCRT keeps one callback per thread and runs it when that thread ends, for the thread_local destructors of an EXE; +// it is also where a title that a shim has to answer reaches ucrtbase!terminate, whose UCRT-side consistency check +// fails in this process (ucrtbase!abort, FAST_FAIL_FATAL_APP_EXIT, before any D3D12 call: measured on 1.0.2.1 with +// the 14.29 runtime the package ships against this host's UCRT). Keeping the registration here, per thread, leaves +// the CRT out of it. +using TlsCallbackFn = void(__cdecl*)(); +thread_local TlsCallbackFn g_thread_local_exe_atexit_callback = nullptr; + +TlsCallbackFn __cdecl game_register_thread_local_exe_atexit_callback(TlsCallbackFn callback) { + TlsCallbackFn previous = g_thread_local_exe_atexit_callback; + g_thread_local_exe_atexit_callback = callback; + return previous; +} + // 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) { @@ -482,6 +498,14 @@ int run_entry(void* entry, size_t stack_bytes) { return g_exit_code.load(); } +// Outside the anonymous namespace: the loader calls it when a thread ends (process_shim.h). +void run_thread_local_exe_atexit_callback() { + if (TlsCallbackFn callback = g_thread_local_exe_atexit_callback) { + g_thread_local_exe_atexit_callback = nullptr; // once, even if it throws + callback(); + } +} + } // namespace rt::process namespace { @@ -491,6 +515,7 @@ using namespace rt::process; {dll, "_Exit", reinterpret_cast(&game_quick_exit)}, \ {dll, "quick_exit", reinterpret_cast(&game_quick_exit)}, \ {dll, "_crt_atexit", reinterpret_cast(&game_crt_atexit)}, \ + {dll, "_register_thread_local_exe_atexit_callback", reinterpret_cast(&game_register_thread_local_exe_atexit_callback)}, \ {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)}, \ diff --git a/runtime/shims/process_shim.h b/runtime/shims/process_shim.h index f6946719b2..57785ea8c4 100644 --- a/runtime/shims/process_shim.h +++ b/runtime/shims/process_shim.h @@ -16,4 +16,9 @@ void set_command_line(const std::wstring& program, const std::vector #include @@ -16,6 +16,7 @@ #include #include +#include "../shims/process_shim.h" #include "../shims/shims.h" #include "../shims/startup_trace.h" #include "base_reservation.h" @@ -1205,6 +1206,13 @@ void __cdecl on_host_thread_event(DWORD reason) { release_thread_slot(i); // installed by a module whose Loader is gone (or nothing to do) } } + // The executable's thread-local destructors, which its CRT would run at this point: after the DLL slots' + // callbacks, on the thread that is ending. A throw from one must not leave the Windows loader lock held. + try { + rt::process::run_thread_local_exe_atexit_callback(); + } catch (const std::exception& e) { + std::fprintf(stderr, "[rt] loader: thread detach, thread-local exe-atexit callback: %s\n", e.what()); + } } } @@ -1450,7 +1458,7 @@ Module* Loader::find_narrow_module(const std::string& dll_lower) const { for (const auto& entry : by_name_) if (entry.second->narrow_key == dll_lower) return entry.second; // The key is ASCII case-folded only. A name with letters outside ASCII (Cyrillic in code page 1251, say) - // compares case-insensitively through the code page, as GetModuleHandleA does: GUEST_ДА.EXE finds guest_да.exe. + // compares case-insensitively through the code page, as GetModuleHandleA does: GUEST_ДА.EXE finds guest_да.exe. // Widening the folded key is safe here: rt::lower() folds ASCII letters only, and no ANSI code page's multibyte // sequences contain ASCII letter bytes, so both sides widen to the same characters they were spelt with. if (const std::wstring wide = acp_to_wide(dll_lower); !wide.empty()) diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp index bf55821d16..5245fb523a 100644 --- a/runtime/test/process_cmdline_test.cpp +++ b/runtime/test/process_cmdline_test.cpp @@ -439,6 +439,55 @@ int main() { SetProcessAffinityMask(GetCurrentProcess(), process_mask); } + // The thread-local destructor callback a title registers: the shim keeps it per thread instead of letting the + // UCRT hold it, registering returns the previous one and runs nothing yet, and the runner runs the current one + // once (the loader calls the runner when a thread ends). + { + using TlsFn = void(__cdecl*)(); + using RegisterFn = TlsFn(__cdecl*)(TlsFn); + const auto reg = + reinterpret_cast(rt::shims::find("ucrtbase.dll", "_register_thread_local_exe_atexit_callback")); + if (!reg) { + std::fprintf(stderr, "FAIL missing _register_thread_local_exe_atexit_callback shim\n"); + ++failures; + } else { + static int ran = 0; + TlsFn first = +[] { ++ran; }; + if (reg(first) != nullptr) { + std::fprintf(stderr, "FAIL the first registration did not return no previous callback\n"); + ++failures; + } + if (ran != 0) { + std::fprintf(stderr, "FAIL the callback ran before the thread ended\n"); + ++failures; + } + rt::process::run_thread_local_exe_atexit_callback(); + if (ran != 1) { + std::fprintf(stderr, "FAIL the runner ran the callback %d time(s), not once\n", ran); + ++failures; + } + rt::process::run_thread_local_exe_atexit_callback(); + if (ran != 1) { + std::fprintf(stderr, "FAIL the runner ran the callback again (%d)\n", ran); + ++failures; + } + TlsFn second = +[] { }; + if (reg(second) != nullptr) { + std::fprintf(stderr, "FAIL a registration after the run did not return no previous callback\n"); + ++failures; + } + if (reg(first) != second) { + std::fprintf(stderr, "FAIL a registration did not return the callback it replaces\n"); + ++failures; + } + rt::process::run_thread_local_exe_atexit_callback(); + if (ran != 2) { + std::fprintf(stderr, "FAIL the runner ran the replacing callback %d time(s), not once\n", ran); + ++failures; + } + } + } + if (failures) return 1; std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact (code page %u); " "the WinMain line and the kernelbase/API-set names are the game's\n",