diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index 2c37292e20..5eb056d0d6 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp @@ -11,6 +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. 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). // CRT's exit lock. Not covered yet: at_quick_exit() handlers (quick_exit() runs none). // - the EXE's thread-local destructor callback (_register_thread_local_exe_atexit_callback) is kept here too: // exit() and _cexit() call it before the atexit handlers, as the UCRT does (see game_exit below). @@ -136,6 +138,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) { @@ -509,6 +527,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 { @@ -518,6 +544,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, "_register_thread_local_exe_atexit_callback", \ 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",