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:
authorZroFreaks <42916282+ZroIsHere@users.noreply.github.com>2026-11-26 08:52:38 +0100
committerZroFreaks <42916282+ZroIsHere@users.noreply.github.com>2026-11-26 08:52:38 +0100
commit338dc11ea622cb3ab0406cafd9af3f08a3740e50 (patch)
tree1bafd06087f63aa27cfb3286e2f5a3a72dc7b609 /runtime/shims/process.cpp
parenta73a57e1339be072baf5522351c8ec7f1482b56b (diff)
runtime: own the executable TLS exit callback
Diffstat (limited to 'runtime/shims/process.cpp')
-rw-r--r--runtime/shims/process.cpp15
1 files changed, 12 insertions, 3 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp
index 08023ea78..2a0331194 100644
--- a/runtime/shims/process.cpp
+++ b/runtime/shims/process.cpp
@@ -11,8 +11,7 @@
11// shares with gta6rt. exit() runs it newest first, as the CRT does, before the run ends; _exit(), _Exit(), 11// shares with gta6rt. exit() runs it newest first, as the CRT does, before the run ends; _exit(), _Exit(),
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) and the thread-local 14// CRT's exit lock. Not covered yet: at_quick_exit() handlers (quick_exit() runs none).
15// destructor callback (_register_thread_local_exe_atexit_callback); both still reach the host CRT.
16// Covered CRTs: the Universal CRT (api-ms-win-crt-runtime-l1-1-0.dll / ucrtbase.dll, what MSVC links; ucrtbased.dll in Debug) and 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// msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here. 16// msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here.
18// The loader matches imports by exact DLL name, so GetCommandLineA/W and ExitProcess are served under every name a 17// The loader matches imports by exact DLL name, so GetCommandLineA/W and ExitProcess are served under every name a
@@ -157,7 +156,10 @@ bool take_exit_lock() {
157} 156}
158// exit(): the game's handlers and static destructors, then the end of the run. 157// exit(): the game's handlers and static destructors, then the end of the run.
159void __cdecl game_exit(int code) { 158void __cdecl game_exit(int code) {
160 if (take_exit_lock()) run_atexit_handlers(); 159 if (take_exit_lock()) {
160 run_atexit_handlers();
161 run_thread_local_exe_atexit_callback();
162 }
161 end_run(code); 163 end_run(code);
162} 164}
163// _exit(), _Exit(), quick_exit(): the end of the run without the atexit handlers. 165// _exit(), _Exit(), quick_exit(): the end of the run without the atexit handlers.
@@ -252,6 +254,10 @@ void __cdecl game_abort() {
252 end_run(3); // the CRT's abort exit code 254 end_run(3); // the CRT's abort exit code
253} 255}
254 256
257void __cdecl game_register_thread_local_exe_atexit_callback(ThreadLocalExeAtexitCallback callback) {
258 register_thread_local_exe_atexit_callback(callback);
259}
260
255// ---- arguments 261// ---- arguments
256int* __cdecl game_p_argc() { return &g_argc; } 262int* __cdecl game_p_argc() { return &g_argc; }
257char*** __cdecl game_p_argv() { return &g_argv_ptr; } 263char*** __cdecl game_p_argv() { return &g_argv_ptr; }
@@ -415,6 +421,7 @@ DWORD WINAPI game_thread(void* p) {
415 // An EXE entry point (mainCRTStartup and friends) takes the PEB pointer and returns the process exit code. 421 // An EXE entry point (mainCRTStartup and friends) takes the PEB pointer and returns the process exit code.
416 auto entry = reinterpret_cast<DWORD(WINAPI*)(void*)>(call->entry); 422 auto entry = reinterpret_cast<DWORD(WINAPI*)(void*)>(call->entry);
417 const DWORD code = entry(nullptr); 423 const DWORD code = entry(nullptr);
424 run_thread_local_exe_atexit_callback();
418 end_run(static_cast<int>(code)); // returning from the entry point is an exit too 425 end_run(static_cast<int>(code)); // returning from the entry point is an exit too
419} 426}
420 427
@@ -493,6 +500,8 @@ using namespace rt::process;
493 {dll, "_crt_atexit", reinterpret_cast<void*>(&game_crt_atexit)}, \ 500 {dll, "_crt_atexit", reinterpret_cast<void*>(&game_crt_atexit)}, \
494 {dll, "_cexit", reinterpret_cast<void*>(&game_cexit)}, {dll, "_c_exit", reinterpret_cast<void*>(&game_c_exit)}, \ 501 {dll, "_cexit", reinterpret_cast<void*>(&game_cexit)}, {dll, "_c_exit", reinterpret_cast<void*>(&game_c_exit)}, \
495 {dll, "abort", reinterpret_cast<void*>(&game_abort)}, {dll, "__p___argc", reinterpret_cast<void*>(&game_p_argc)}, \ 502 {dll, "abort", reinterpret_cast<void*>(&game_abort)}, {dll, "__p___argc", reinterpret_cast<void*>(&game_p_argc)}, \
503 {dll, "_register_thread_local_exe_atexit_callback", \
504 reinterpret_cast<void*>(&game_register_thread_local_exe_atexit_callback)}, \
496 {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \ 505 {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \
497 {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \ 506 {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \
498 {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_narrow_argv)}, \ 507 {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_narrow_argv)}, \