upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/process.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-26 13:15:17 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-26 13:15:17 -0500
commit48f87a9c9bea054b0072364ebf0e51e2d59f354d (patch)
tree28bfe653dba490c592a94585e3694bd1e945bdc9 /runtime/shims/process.cpp
parent95d18113f37cfd42f8d8264fc3223efd7f2d60cb (diff)
process: call the EXE's thread-local destructor callback with its real signature, before the atexit handlers (maintainer fix on #2315)
#2315 kept `_register_thread_local_exe_atexit_callback` away from the host UCRT, which fast-fails a second registration in the process (gta6rt's own startup makes the first). Review points fixed on top: - The callback is a `_tls_callback_type` (UCRT process.h: `void (__stdcall*)(void*, unsigned long, void*)`), not `void (*)()`: vcruntime's TLS destructor callback reads its reason argument, which was left as whatever rdx held. It is now called as `(nullptr, DLL_PROCESS_DETACH, nullptr)`, as the UCRT does. - `exit` and `_cexit` call it before the atexit table, as the UCRT does: the leaving thread's thread_local objects end before the static ones ([basic.start.term]). #2315 ran it after the static destructors, and not in `_cexit`. - The extra call on every thread detach is dropped, with the per-thread flag and the loader's include of the process shim: the loader already calls the same function from the executable's TLS directory with DLL_THREAD_DETACH, and the UCRT never calls the registered one there. The call after a returning entry point is dropped too (MSVC's mainCRTStartup calls exit itself). - run_tiny_atexit gains thread_local_exit and thread_local_cexit: a thread_local object of the main thread must end before the handlers and destructors. On main the --run path never ends it (Wine) or stops at the second registration (Windows). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/process.cpp')
-rw-r--r--runtime/shims/process.cpp32
1 files changed, 26 insertions, 6 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp
index 2a0331194..2c37292e2 100644
--- a/runtime/shims/process.cpp
+++ b/runtime/shims/process.cpp
@@ -12,6 +12,8 @@
12// quick_exit(), ExitProcess() and abort() do not. _cexit() runs it and returns, _c_exit() returns. A second 12// quick_exit(), ExitProcess() and abort() do not. _cexit() runs it and returns, _c_exit() returns. A second
13// thread that calls exit(), _exit(), _Exit() or quick_exit() while the first is leaving waits, as with the 13// thread that calls exit(), _exit(), _Exit() or quick_exit() while the first is leaving waits, as with the
14// CRT's exit lock. Not covered yet: at_quick_exit() handlers (quick_exit() runs none). 14// CRT's exit lock. Not covered yet: at_quick_exit() handlers (quick_exit() runs none).
15// - the EXE's thread-local destructor callback (_register_thread_local_exe_atexit_callback) is kept here too:
16// exit() and _cexit() call it before the atexit handlers, as the UCRT does (see game_exit below).
15// Covered CRTs: the Universal CRT (api-ms-win-crt-runtime-l1-1-0.dll / ucrtbase.dll, what MSVC links; ucrtbased.dll in Debug) and 17// Covered CRTs: the Universal CRT (api-ms-win-crt-runtime-l1-1-0.dll / ucrtbase.dll, what MSVC links; ucrtbased.dll in Debug) and
16// msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here. 18// msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here.
17// The loader matches imports by exact DLL name, so GetCommandLineA/W and ExitProcess are served under every name a 19// The loader matches imports by exact DLL name, so GetCommandLineA/W and ExitProcess are served under every name a
@@ -141,6 +143,28 @@ OnexitFn __cdecl game_onexit(OnexitFn fn) {
141 return fn && register_atexit(reinterpret_cast<AtexitFn>(fn)) ? fn : nullptr; 143 return fn && register_atexit(reinterpret_cast<AtexitFn>(fn)) ? fn : nullptr;
142} 144}
143 145
146// ---- the EXE's thread-local destructors
147// An MSVC EXE's startup passes vcruntime's TLS destructor callback, which its TLS directory also lists, to
148// _register_thread_local_exe_atexit_callback (UCRT process.h: void __cdecl (_tls_callback_type), where
149// _tls_callback_type is void (__stdcall*)(void*, unsigned long, void*)). The UCRT calls it as
150// (nullptr, DLL_PROCESS_DETACH, nullptr) when exit() or _cexit() starts, before the atexit table: the leaving thread's
151// thread_local objects end before the static ones, as C++ requires. Other threads' end through the loader's
152// thread-detach notification, which calls the same function from the TLS directory, so it is not called here then.
153// The host CRT cannot take it: the UCRT accepts one per process and fast-fails the second (gta6rt's own startup
154// registers the first, so the title stopped at boot on Windows), and Wine's ucrtbase replaces gta6rt's own and never
155// calls the title's, because the run ends through end_run.
156using TlsCallback = void(__stdcall*)(void*, unsigned long, void*);
157std::atomic<TlsCallback> g_thread_local_exit_callback{nullptr};
158
159void __cdecl game_register_thread_local_exe_atexit_callback(TlsCallback callback) {
160 g_thread_local_exit_callback.store(callback);
161}
162// Once per run, like the UCRT's exit path: a later exit() after _cexit() finds nothing left to run.
163void run_thread_local_exe_atexit_callback() {
164 if (const TlsCallback callback = g_thread_local_exit_callback.exchange(nullptr))
165 callback(nullptr, DLL_PROCESS_DETACH, nullptr);
166}
167
144// ---- the way out 168// ---- the way out
145// The CRT's exit lock, which exit(), _exit(), _Exit() and quick_exit() all take: the first thread to leave owns it 169// The CRT's exit lock, which exit(), _exit(), _Exit() and quick_exit() all take: the first thread to leave owns it
146// and ends the run; any other thread that tries to leave waits forever. The owner may leave again from a handler 170// and ends the run; any other thread that tries to leave waits forever. The owner may leave again from a handler
@@ -157,8 +181,8 @@ bool take_exit_lock() {
157// exit(): the game's handlers and static destructors, then the end of the run. 181// exit(): the game's handlers and static destructors, then the end of the run.
158void __cdecl game_exit(int code) { 182void __cdecl game_exit(int code) {
159 if (take_exit_lock()) { 183 if (take_exit_lock()) {
160 run_atexit_handlers();
161 run_thread_local_exe_atexit_callback(); 184 run_thread_local_exe_atexit_callback();
185 run_atexit_handlers();
162 } 186 }
163 end_run(code); 187 end_run(code);
164} 188}
@@ -169,6 +193,7 @@ void __cdecl game_quick_exit(int code) {
169} 193}
170// _cexit() runs the handlers and flushes the streams, then returns; _c_exit() only returns. 194// _cexit() runs the handlers and flushes the streams, then returns; _c_exit() only returns.
171void __cdecl game_cexit() { 195void __cdecl game_cexit() {
196 run_thread_local_exe_atexit_callback();
172 run_atexit_handlers(); 197 run_atexit_handlers();
173 std::fflush(nullptr); 198 std::fflush(nullptr);
174} 199}
@@ -254,10 +279,6 @@ void __cdecl game_abort() {
254 end_run(3); // the CRT's abort exit code 279 end_run(3); // the CRT's abort exit code
255} 280}
256 281
257void __cdecl game_register_thread_local_exe_atexit_callback(ThreadLocalExeAtexitCallback callback) {
258 register_thread_local_exe_atexit_callback(callback);
259}
260
261// ---- arguments 282// ---- arguments
262int* __cdecl game_p_argc() { return &g_argc; } 283int* __cdecl game_p_argc() { return &g_argc; }
263char*** __cdecl game_p_argv() { return &g_argv_ptr; } 284char*** __cdecl game_p_argv() { return &g_argv_ptr; }
@@ -421,7 +442,6 @@ DWORD WINAPI game_thread(void* p) {
421 // An EXE entry point (mainCRTStartup and friends) takes the PEB pointer and returns the process exit code. 442 // An EXE entry point (mainCRTStartup and friends) takes the PEB pointer and returns the process exit code.
422 auto entry = reinterpret_cast<DWORD(WINAPI*)(void*)>(call->entry); 443 auto entry = reinterpret_cast<DWORD(WINAPI*)(void*)>(call->entry);
423 const DWORD code = entry(nullptr); 444 const DWORD code = entry(nullptr);
424 run_thread_local_exe_atexit_callback();
425 end_run(static_cast<int>(code)); // returning from the entry point is an exit too 445 end_run(static_cast<int>(code)); // returning from the entry point is an exit too
426} 446}
427 447