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:
authorMike7581 <168859477+Mike7581@users.noreply.github.com>2026-11-25 01:25:16 -0300
committerMike7581 <168859477+Mike7581@users.noreply.github.com>2026-11-27 16:09:18 -0300
commit6f7963abddd7e545f43097c50f687e8e71626d72 (patch)
tree4802cab582c64108d3283e9ed6f4c6908ffe2b0e /runtime/shims/process.cpp
parent43a9d562d355e7c5c428e486071b456353de8b05 (diff)
process: the game's at_quick_exit handlers and thread-local exit callback are the run's own
The game's at_quick_exit() (_crt_at_quick_exit) and _register_thread_local_exe_atexit_callback still reached the host CRT, which the game shares with gta6rt. Under `gta6rt --run`: - quick_exit() ran none of the game's at_quick_exit handlers; - exit() and _cexit() never ran the exiting thread's thread_local destructors, and a second registration in the host's one-slot callback would terminate gta6rt. Now _crt_at_quick_exit goes to a second table of the run's own, which quick_exit() runs newest first under the exit lock; _exit() and _Exit() run none (they no longer share game_quick_exit). The thread-local callback is the run's own: exit() and _cexit() call it with DLL_PROCESS_DETACH before the atexit handlers, as the UCRT does, and a second registration aborts, as the UCRT's terminate() does. Both reset per run. tiny_atexit gains five modes (at_quick_exit, at_quick_exit_exit, at_quick_exit__exit, thread_local_exit, thread_local_cexit). Their expected sequences in check_atexit.cmake were measured by running the fixture natively on Windows 11; before this change --run differed on at_quick_exit, thread_local_exit and thread_local_cexit. run_tiny_atexit now holds 13 paths to the Windows sequence.
Diffstat (limited to 'runtime/shims/process.cpp')
-rw-r--r--runtime/shims/process.cpp102
1 files changed, 59 insertions, 43 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp
index 2c37292e2..5f96e9f96 100644
--- a/runtime/shims/process.cpp
+++ b/runtime/shims/process.cpp
@@ -11,9 +11,13 @@
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). 14// CRT's exit lock;
15// - the EXE's thread-local destructor callback (_register_thread_local_exe_atexit_callback) is kept here too: 15// - the game's at_quick_exit registrations (_crt_at_quick_exit) go to a second table of the run's own, which
16// exit() and _cexit() call it before the atexit handlers, as the UCRT does (see game_exit below). 16// quick_exit() runs newest first; exit(), _exit() and _Exit() do not;
17// - the EXE's thread-local destructor callback (_register_thread_local_exe_atexit_callback, which the EXE's CRT
18// startup registers when the EXE has a thread_local with a destructor) is the run's own too: exit() and _cexit()
19// call it for the exiting thread before the atexit handlers, as the UCRT does (see
20// game_register_thread_local_exe_atexit_callback below). A second registration terminates, as in the UCRT.
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 21// Covered CRTs: the Universal CRT (api-ms-win-crt-runtime-l1-1-0.dll / ucrtbase.dll, what MSVC links; ucrtbased.dll in Debug) and
18// msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here. 22// msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here.
19// The loader matches imports by exact DLL name, so GetCommandLineA/W and ExitProcess are served under every name a 23// The loader matches imports by exact DLL name, so GetCommandLineA/W and ExitProcess are served under every name a
@@ -104,38 +108,53 @@ std::string narrow(const std::wstring& w) {
104// ---- the game's atexit table 108// ---- the game's atexit table
105using AtexitFn = void(__cdecl*)(); 109using AtexitFn = void(__cdecl*)();
106using OnexitFn = int(__cdecl*)(); 110using OnexitFn = int(__cdecl*)();
111using TlsCallback = void(NTAPI*)(PVOID, DWORD, PVOID); // _tls_callback_type
107std::mutex g_atexit_lock; 112std::mutex g_atexit_lock;
108std::vector<AtexitFn> g_atexit; 113std::vector<AtexitFn> g_atexit;
114std::vector<AtexitFn> g_at_quick_exit;
115std::atomic<TlsCallback> g_thread_local_exit{nullptr};
109std::atomic<DWORD> g_exiting_thread{0}; 116std::atomic<DWORD> g_exiting_thread{0};
110 117
111bool register_atexit(AtexitFn fn) { 118bool register_in(std::vector<AtexitFn>& table, AtexitFn fn) {
112 std::lock_guard<std::mutex> lock(g_atexit_lock); 119 std::lock_guard<std::mutex> lock(g_atexit_lock);
113 try { 120 try {
114 g_atexit.push_back(fn); 121 table.push_back(fn);
115 } catch (const std::bad_alloc&) { 122 } catch (const std::bad_alloc&) {
116 return false; // the CRT reports a full table as a failed registration 123 return false; // the CRT reports a full table as a failed registration
117 } 124 }
118 return true; 125 return true;
119} 126}
127bool register_atexit(AtexitFn fn) { return register_in(g_atexit, fn); }
120 128
121// Newest first. A handler registered while the handlers run goes on the end and so runs next, as in the UCRT. 129// Newest first. A handler registered while the handlers run goes on the end and so runs next, as in the UCRT.
122void run_atexit_handlers() { 130void run_handlers(std::vector<AtexitFn>& table) {
123 for (;;) { 131 for (;;) {
124 AtexitFn fn; 132 AtexitFn fn;
125 { 133 {
126 std::lock_guard<std::mutex> lock(g_atexit_lock); 134 std::lock_guard<std::mutex> lock(g_atexit_lock);
127 if (g_atexit.empty()) return; 135 if (table.empty()) return;
128 fn = g_atexit.back(); 136 fn = table.back();
129 g_atexit.pop_back(); 137 table.pop_back();
130 } 138 }
131 fn(); 139 fn();
132 } 140 }
133} 141}
142// exit() and _cexit(): the exiting thread's thread_local destructors first (the UCRT calls the registered callback
143// with DLL_PROCESS_DETACH; a second call finds the thread's list empty), then the atexit handlers and static
144// destructors.
145void run_atexit_handlers() {
146 if (const TlsCallback callback = g_thread_local_exit.load()) callback(nullptr, DLL_PROCESS_DETACH, nullptr);
147 run_handlers(g_atexit);
148}
134 149
135// UCRT: int _crt_atexit(_PVFV), 0 on success. MSVC's atexit() and the static destructors of an EXE reach it. 150// UCRT: int _crt_atexit(_PVFV), 0 on success. MSVC's atexit() and the static destructors of an EXE reach it.
136int __cdecl game_crt_atexit(AtexitFn fn) { 151int __cdecl game_crt_atexit(AtexitFn fn) {
137 return fn && register_atexit(fn) ? 0 : -1; 152 return fn && register_atexit(fn) ? 0 : -1;
138} 153}
154// UCRT: int _crt_at_quick_exit(_PVFV), 0 on success. An EXE's at_quick_exit() reaches it.
155int __cdecl game_crt_at_quick_exit(AtexitFn fn) {
156 return fn && register_in(g_at_quick_exit, fn) ? 0 : -1;
157}
139// msvcrt.dll (MinGW): int atexit(void (*)(void)) and _onexit_t _onexit(_onexit_t). 158// msvcrt.dll (MinGW): int atexit(void (*)(void)) and _onexit_t _onexit(_onexit_t).
140int __cdecl game_msvcrt_atexit(AtexitFn fn) { return game_crt_atexit(fn); } 159int __cdecl game_msvcrt_atexit(AtexitFn fn) { return game_crt_atexit(fn); }
141OnexitFn __cdecl game_onexit(OnexitFn fn) { 160OnexitFn __cdecl game_onexit(OnexitFn fn) {
@@ -143,28 +162,6 @@ OnexitFn __cdecl game_onexit(OnexitFn fn) {
143 return fn && register_atexit(reinterpret_cast<AtexitFn>(fn)) ? fn : nullptr; 162 return fn && register_atexit(reinterpret_cast<AtexitFn>(fn)) ? fn : nullptr;
144} 163}
145 164
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
168// ---- the way out 165// ---- the way out
169// The CRT's exit lock, which exit(), _exit(), _Exit() and quick_exit() all take: the first thread to leave owns it 166// The CRT's exit lock, which exit(), _exit(), _Exit() and quick_exit() all take: the first thread to leave owns it
170// and ends the run; any other thread that tries to leave waits forever. The owner may leave again from a handler 167// and ends the run; any other thread that tries to leave waits forever. The owner may leave again from a handler
@@ -180,20 +177,21 @@ bool take_exit_lock() {
180} 177}
181// exit(): the game's handlers and static destructors, then the end of the run. 178// exit(): the game's handlers and static destructors, then the end of the run.
182void __cdecl game_exit(int code) { 179void __cdecl game_exit(int code) {
183 if (take_exit_lock()) { 180 if (take_exit_lock()) run_atexit_handlers();
184 run_thread_local_exe_atexit_callback();
185 run_atexit_handlers();
186 }
187 end_run(code); 181 end_run(code);
188} 182}
189// _exit(), _Exit(), quick_exit(): the end of the run without the atexit handlers. 183// _exit(), _Exit(): the end of the run without any handler.
190void __cdecl game_quick_exit(int code) { 184void __cdecl game_underscore_exit(int code) {
191 take_exit_lock(); 185 take_exit_lock();
192 end_run(code); 186 end_run(code);
193} 187}
188// quick_exit(): the at_quick_exit handlers, newest first, then the end of the run without the atexit handlers.
189void __cdecl game_quick_exit(int code) {
190 if (take_exit_lock()) run_handlers(g_at_quick_exit);
191 end_run(code);
192}
194// _cexit() runs the handlers and flushes the streams, then returns; _c_exit() only returns. 193// _cexit() runs the handlers and flushes the streams, then returns; _c_exit() only returns.
195void __cdecl game_cexit() { 194void __cdecl game_cexit() {
196 run_thread_local_exe_atexit_callback();
197 run_atexit_handlers(); 195 run_atexit_handlers();
198 std::fflush(nullptr); 196 std::fflush(nullptr);
199} 197}
@@ -278,6 +276,21 @@ void __cdecl game_abort() {
278 } 276 }
279 end_run(3); // the CRT's abort exit code 277 end_run(3); // the CRT's abort exit code
280} 278}
279// UCRT: void _register_thread_local_exe_atexit_callback(_tls_callback_type). An MSVC EXE's startup passes
280// vcruntime's TLS destructor callback, which its TLS directory also lists; run_atexit_handlers calls it as
281// (nullptr, DLL_PROCESS_DETACH, nullptr) for the exiting thread. Other threads' thread_local objects end through the
282// loader's thread-detach notification, which calls the same function from the TLS directory. The host CRT cannot
283// take it: the UCRT accepts one per process and fast-fails the second (gta6rt's own startup registers the first, so
284// the title stopped at boot on Windows), and Wine's ucrtbase replaces gta6rt's own and never calls the title's,
285// because the run ends through end_run. Here too the callback can be set once; a second registration calls
286// terminate(), which aborts.
287void __cdecl game_register_thread_local_exe_atexit_callback(TlsCallback callback) {
288 TlsCallback expected = nullptr;
289 if (!g_thread_local_exit.compare_exchange_strong(expected, callback)) {
290 std::fprintf(stderr, "[rt] process: the thread-local exit callback was registered twice\n");
291 game_abort();
292 }
293}
281 294
282// ---- arguments 295// ---- arguments
283int* __cdecl game_p_argc() { return &g_argc; } 296int* __cdecl game_p_argc() { return &g_argc; }
@@ -473,7 +486,9 @@ int run_entry(void* entry, size_t stack_bytes) {
473 { 486 {
474 std::lock_guard<std::mutex> lock(g_atexit_lock); 487 std::lock_guard<std::mutex> lock(g_atexit_lock);
475 g_atexit.clear(); // a run never sees the handlers of an earlier one 488 g_atexit.clear(); // a run never sees the handlers of an earlier one
489 g_at_quick_exit.clear();
476 } 490 }
491 g_thread_local_exit = nullptr;
477 g_exiting_thread = 0; 492 g_exiting_thread = 0;
478 g_exited = false; 493 g_exited = false;
479 g_exit_code = 0; 494 g_exit_code = 0;
@@ -514,14 +529,15 @@ int run_entry(void* entry, size_t stack_bytes) {
514namespace { 529namespace {
515using namespace rt::process; 530using namespace rt::process;
516#define UCRT(dll) \ 531#define UCRT(dll) \
517 {dll, "exit", reinterpret_cast<void*>(&game_exit)}, {dll, "_exit", reinterpret_cast<void*>(&game_quick_exit)}, \ 532 {dll, "exit", reinterpret_cast<void*>(&game_exit)}, {dll, "_exit", reinterpret_cast<void*>(&game_underscore_exit)}, \
518 {dll, "_Exit", reinterpret_cast<void*>(&game_quick_exit)}, \ 533 {dll, "_Exit", reinterpret_cast<void*>(&game_underscore_exit)}, \
519 {dll, "quick_exit", reinterpret_cast<void*>(&game_quick_exit)}, \ 534 {dll, "quick_exit", reinterpret_cast<void*>(&game_quick_exit)}, \
520 {dll, "_crt_atexit", reinterpret_cast<void*>(&game_crt_atexit)}, \ 535 {dll, "_crt_atexit", reinterpret_cast<void*>(&game_crt_atexit)}, \
521 {dll, "_cexit", reinterpret_cast<void*>(&game_cexit)}, {dll, "_c_exit", reinterpret_cast<void*>(&game_c_exit)}, \ 536 {dll, "_crt_at_quick_exit", reinterpret_cast<void*>(&game_crt_at_quick_exit)}, \
522 {dll, "abort", reinterpret_cast<void*>(&game_abort)}, {dll, "__p___argc", reinterpret_cast<void*>(&game_p_argc)}, \
523 {dll, "_register_thread_local_exe_atexit_callback", \ 537 {dll, "_register_thread_local_exe_atexit_callback", \
524 reinterpret_cast<void*>(&game_register_thread_local_exe_atexit_callback)}, \ 538 reinterpret_cast<void*>(&game_register_thread_local_exe_atexit_callback)}, \
539 {dll, "_cexit", reinterpret_cast<void*>(&game_cexit)}, {dll, "_c_exit", reinterpret_cast<void*>(&game_c_exit)}, \
540 {dll, "abort", reinterpret_cast<void*>(&game_abort)}, {dll, "__p___argc", reinterpret_cast<void*>(&game_p_argc)}, \
525 {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \ 541 {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \
526 {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \ 542 {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \
527 {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_narrow_argv)}, \ 543 {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_narrow_argv)}, \
@@ -546,7 +562,7 @@ SHIM_TABLE(process,
546 UCRT("ucrtbase.dll"), 562 UCRT("ucrtbase.dll"),
547 UCRT("ucrtbased.dll"), // the Debug UCRT: a Debug build of the stand-in game (run_tiny_game) links it 563 UCRT("ucrtbased.dll"), // the Debug UCRT: a Debug build of the stand-in game (run_tiny_game) links it
548 {"msvcrt.dll", "exit", reinterpret_cast<void*>(&game_exit)}, 564 {"msvcrt.dll", "exit", reinterpret_cast<void*>(&game_exit)},
549 {"msvcrt.dll", "_exit", reinterpret_cast<void*>(&game_quick_exit)}, 565 {"msvcrt.dll", "_exit", reinterpret_cast<void*>(&game_underscore_exit)},
550 {"msvcrt.dll", "atexit", reinterpret_cast<void*>(&game_msvcrt_atexit)}, 566 {"msvcrt.dll", "atexit", reinterpret_cast<void*>(&game_msvcrt_atexit)},
551 {"msvcrt.dll", "_onexit", reinterpret_cast<void*>(&game_onexit)}, 567 {"msvcrt.dll", "_onexit", reinterpret_cast<void*>(&game_onexit)},
552 {"msvcrt.dll", "_cexit", reinterpret_cast<void*>(&game_cexit)}, 568 {"msvcrt.dll", "_cexit", reinterpret_cast<void*>(&game_cexit)},