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:
authorZack <zmasi21@gmail.com>2026-11-28 01:32:59 -0800
committerZack <zmasi21@gmail.com>2026-11-28 01:32:59 -0800
commit126058daed271ea3f8497541cd2e12fc31ab3067 (patch)
treed76dd55bbb3a316b025fdddda928b1f85b89c15b /runtime/shims/process.cpp
parent67ec1d35afaa9300e0e725dbbc06e7c851496841 (diff)
parent3ca0707ddec7fd868ecd6df924d751901102fd9c (diff)
Merge remote-tracking branch 'origin/main' into results/gpu-vega20-radv
Diffstat (limited to 'runtime/shims/process.cpp')
-rw-r--r--runtime/shims/process.cpp177
1 files changed, 126 insertions, 51 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp
index 2c37292e2..a692a6224 100644
--- a/runtime/shims/process.cpp
+++ b/runtime/shims/process.cpp
@@ -11,9 +11,18 @@
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. Registration and the cleanup of exit(), quick_exit() and _cexit() (the thread-local
15// - the EXE's thread-local destructor callback (_register_thread_local_exe_atexit_callback) is kept here too: 15// callback, the table's drain and, for _cexit(), the stream flush) hold one lock of the run's own, as the UCRT's
16// exit() and _cexit() call it before the atexit handlers, as the UCRT does (see game_exit below). 16// exit lock serializes them: a second thread in _cexit(), or registering while a drain runs, waits for that
17// cleanup; the exiting thread may register from a handler (the lock is recursive). The lock is released before
18// the run ends. A handler that lets a C++ exception escape terminates the run as the UCRT's terminate() would:
19// abort(), exit code 3;
20// - the game's at_quick_exit registrations (_crt_at_quick_exit) go to a second table of the run's own, which
21// quick_exit() runs newest first; exit(), _exit() and _Exit() do not;
22// - the EXE's thread-local destructor callback (_register_thread_local_exe_atexit_callback, which the EXE's CRT
23// startup registers when the EXE has a thread_local with a destructor) is the run's own too: exit() and _cexit()
24// call it for the exiting thread before the atexit handlers, as the UCRT does (see
25// 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 26// 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. 27// 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 28// The loader matches imports by exact DLL name, so GetCommandLineA/W and ExitProcess are served under every name a
@@ -84,7 +93,12 @@ std::string narrow(const std::wstring& w) {
84 return s; 93 return s;
85} 94}
86 95
96void release_cleanup_lock();
97
87[[noreturn]] void end_run(int code) { 98[[noreturn]] void end_run(int code) {
99 // A handler may end the run from inside a drain: the cleanup lock it holds is released before the thread parks,
100 // so the host (and a later run_entry) can take it.
101 release_cleanup_lock();
88 // Read before signalling: once g_done is set, run_entry can return and clear g_running while the host is still 102 // Read before signalling: once g_done is set, run_entry can return and clear g_running while the host is still
89 // finishing the run (main.cpp's "game exited" line and cleanup), which this thread must not cut short. 103 // finishing the run (main.cpp's "game exited" line and cleanup), which this thread must not cut short.
90 const bool running = g_running; 104 const bool running = g_running;
@@ -104,38 +118,91 @@ std::string narrow(const std::wstring& w) {
104// ---- the game's atexit table 118// ---- the game's atexit table
105using AtexitFn = void(__cdecl*)(); 119using AtexitFn = void(__cdecl*)();
106using OnexitFn = int(__cdecl*)(); 120using OnexitFn = int(__cdecl*)();
107std::mutex g_atexit_lock; 121using TlsCallback = void(NTAPI*)(PVOID, DWORD, PVOID); // _tls_callback_type
122// The run's cleanup lock, as the UCRT's exit lock: registration takes it, and a cleanup (exit, quick_exit, _cexit)
123// holds it from before the thread-local callback through the table's drain, so two threads never clean up at once
124// and a registration from another thread waits for the drain. Recursive, so the exiting thread may register from a
125// handler. t_cleanup_held counts this thread's holds: end_run releases them all before the thread parks.
126std::recursive_mutex g_cleanup_lock;
127thread_local unsigned t_cleanup_held = 0;
108std::vector<AtexitFn> g_atexit; 128std::vector<AtexitFn> g_atexit;
129std::vector<AtexitFn> g_at_quick_exit;
130std::atomic<TlsCallback> g_thread_local_exit{nullptr};
109std::atomic<DWORD> g_exiting_thread{0}; 131std::atomic<DWORD> g_exiting_thread{0};
110 132
111bool register_atexit(AtexitFn fn) { 133struct CleanupLock {
112 std::lock_guard<std::mutex> lock(g_atexit_lock); 134 CleanupLock() {
135 g_cleanup_lock.lock();
136 ++t_cleanup_held;
137 }
138 ~CleanupLock() {
139 --t_cleanup_held;
140 g_cleanup_lock.unlock();
141 }
142 CleanupLock(const CleanupLock&) = delete;
143 CleanupLock& operator=(const CleanupLock&) = delete;
144};
145void release_cleanup_lock() {
146 while (t_cleanup_held) {
147 --t_cleanup_held;
148 g_cleanup_lock.unlock();
149 }
150}
151
152bool register_in(std::vector<AtexitFn>& table, AtexitFn fn) {
153 CleanupLock lock;
113 try { 154 try {
114 g_atexit.push_back(fn); 155 table.push_back(fn);
115 } catch (const std::bad_alloc&) { 156 } catch (const std::bad_alloc&) {
116 return false; // the CRT reports a full table as a failed registration 157 return false; // the CRT reports a full table as a failed registration
117 } 158 }
118 return true; 159 return true;
119} 160}
161bool register_atexit(AtexitFn fn) { return register_in(g_atexit, fn); }
120 162
121// Newest first. A handler registered while the handlers run goes on the end and so runs next, as in the UCRT. 163void __cdecl game_abort();
122void run_atexit_handlers() { 164
165// A guest cleanup callback. A C++ exception it lets escape would unwind into the runtime; the UCRT's exit calls
166// terminate() if one throws, and its default handler aborts: exit code 3, and the remaining handlers do not run.
167// A thread_local destructor is noexcept, so one that throws reaches the game's own terminate() before this catch;
168// that abort() is the game's import, game_abort, and ends the run with the same code.
169template <typename Call>
170void guarded(const char* what, Call&& call) {
171 try {
172 call();
173 } catch (...) {
174 std::fprintf(stderr, "[rt] process: a %s threw a C++ exception; terminating as the UCRT does\n", what);
175 game_abort();
176 }
177}
178
179// Newest first, with the cleanup lock held throughout (the caller took it). A handler registered while the handlers
180// run goes on the end and so runs next, as in the UCRT.
181void run_handlers(std::vector<AtexitFn>& table, const char* what) {
123 for (;;) { 182 for (;;) {
124 AtexitFn fn; 183 if (table.empty()) return;
125 { 184 const AtexitFn fn = table.back();
126 std::lock_guard<std::mutex> lock(g_atexit_lock); 185 table.pop_back();
127 if (g_atexit.empty()) return; 186 guarded(what, fn);
128 fn = g_atexit.back();
129 g_atexit.pop_back();
130 }
131 fn();
132 } 187 }
133} 188}
189// exit() and _cexit(): the exiting thread's thread_local destructors first (the UCRT calls the registered callback
190// with DLL_PROCESS_DETACH; a second call finds the thread's list empty), then the atexit handlers and static
191// destructors. The caller holds the cleanup lock.
192void run_atexit_handlers() {
193 if (const TlsCallback callback = g_thread_local_exit.load())
194 guarded("thread-local destructor", [&] { callback(nullptr, DLL_PROCESS_DETACH, nullptr); });
195 run_handlers(g_atexit, "atexit handler");
196}
134 197
135// UCRT: int _crt_atexit(_PVFV), 0 on success. MSVC's atexit() and the static destructors of an EXE reach it. 198// 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) { 199int __cdecl game_crt_atexit(AtexitFn fn) {
137 return fn && register_atexit(fn) ? 0 : -1; 200 return fn && register_atexit(fn) ? 0 : -1;
138} 201}
202// UCRT: int _crt_at_quick_exit(_PVFV), 0 on success. An EXE's at_quick_exit() reaches it.
203int __cdecl game_crt_at_quick_exit(AtexitFn fn) {
204 return fn && register_in(g_at_quick_exit, fn) ? 0 : -1;
205}
139// msvcrt.dll (MinGW): int atexit(void (*)(void)) and _onexit_t _onexit(_onexit_t). 206// 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); } 207int __cdecl game_msvcrt_atexit(AtexitFn fn) { return game_crt_atexit(fn); }
141OnexitFn __cdecl game_onexit(OnexitFn fn) { 208OnexitFn __cdecl game_onexit(OnexitFn fn) {
@@ -143,28 +210,6 @@ OnexitFn __cdecl game_onexit(OnexitFn fn) {
143 return fn && register_atexit(reinterpret_cast<AtexitFn>(fn)) ? fn : nullptr; 210 return fn && register_atexit(reinterpret_cast<AtexitFn>(fn)) ? fn : nullptr;
144} 211}
145 212
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 213// ---- 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 214// 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 215// and ends the run; any other thread that tries to leave waits forever. The owner may leave again from a handler
@@ -174,26 +219,38 @@ bool take_exit_lock() {
174 DWORD owner = 0; 219 DWORD owner = 0;
175 if (g_exiting_thread.compare_exchange_strong(owner, self)) return true; 220 if (g_exiting_thread.compare_exchange_strong(owner, self)) return true;
176 if (owner != self) { 221 if (owner != self) {
222 release_cleanup_lock(); // parked for good: a drain it was in must not keep the owner waiting
177 for (;;) Sleep(INFINITE); 223 for (;;) Sleep(INFINITE);
178 } 224 }
179 return false; 225 return false;
180} 226}
181// exit(): the game's handlers and static destructors, then the end of the run. 227// exit(): the game's handlers and static destructors, then the end of the run. The cleanup lock is taken after the
228// exit lock (a thread parked by the latter holds nothing) and released by end_run.
182void __cdecl game_exit(int code) { 229void __cdecl game_exit(int code) {
183 if (take_exit_lock()) { 230 if (take_exit_lock()) {
184 run_thread_local_exe_atexit_callback(); 231 CleanupLock lock;
185 run_atexit_handlers(); 232 run_atexit_handlers();
186 } 233 }
187 end_run(code); 234 end_run(code);
188} 235}
189// _exit(), _Exit(), quick_exit(): the end of the run without the atexit handlers. 236// _exit(), _Exit(): the end of the run without any handler.
190void __cdecl game_quick_exit(int code) { 237void __cdecl game_underscore_exit(int code) {
191 take_exit_lock(); 238 take_exit_lock();
192 end_run(code); 239 end_run(code);
193} 240}
194// _cexit() runs the handlers and flushes the streams, then returns; _c_exit() only returns. 241// quick_exit(): the at_quick_exit handlers, newest first, then the end of the run without the atexit handlers.
242void __cdecl game_quick_exit(int code) {
243 if (take_exit_lock()) {
244 CleanupLock lock;
245 run_handlers(g_at_quick_exit, "quick_exit handler");
246 }
247 end_run(code);
248}
249// _cexit() runs the handlers and flushes the streams, then returns; _c_exit() only returns. The flush is inside the
250// cleanup lock, as the UCRT's _cexit flushes under its exit lock. A second thread's _cexit() waits for the first's
251// cleanup, then finds the table empty.
195void __cdecl game_cexit() { 252void __cdecl game_cexit() {
196 run_thread_local_exe_atexit_callback(); 253 CleanupLock lock;
197 run_atexit_handlers(); 254 run_atexit_handlers();
198 std::fflush(nullptr); 255 std::fflush(nullptr);
199} 256}
@@ -278,6 +335,21 @@ void __cdecl game_abort() {
278 } 335 }
279 end_run(3); // the CRT's abort exit code 336 end_run(3); // the CRT's abort exit code
280} 337}
338// UCRT: void _register_thread_local_exe_atexit_callback(_tls_callback_type). An MSVC EXE's startup passes
339// vcruntime's TLS destructor callback, which its TLS directory also lists; run_atexit_handlers calls it as
340// (nullptr, DLL_PROCESS_DETACH, nullptr) for the exiting thread. Other threads' thread_local objects end through the
341// loader's thread-detach notification, which calls the same function from the TLS directory. The host CRT cannot
342// take it: the UCRT accepts one per process and fast-fails the second (gta6rt's own startup registers the first, so
343// the title stopped at boot on Windows), and Wine's ucrtbase replaces gta6rt's own and never calls the title's,
344// because the run ends through end_run. Here too the callback can be set once; a second registration calls
345// terminate(), which aborts.
346void __cdecl game_register_thread_local_exe_atexit_callback(TlsCallback callback) {
347 TlsCallback expected = nullptr;
348 if (!g_thread_local_exit.compare_exchange_strong(expected, callback)) {
349 std::fprintf(stderr, "[rt] process: the thread-local exit callback was registered twice\n");
350 game_abort();
351 }
352}
281 353
282// ---- arguments 354// ---- arguments
283int* __cdecl game_p_argc() { return &g_argc; } 355int* __cdecl game_p_argc() { return &g_argc; }
@@ -471,9 +543,11 @@ void set_command_line(const std::wstring& program, const std::vector<std::wstrin
471 543
472int run_entry(void* entry, size_t stack_bytes) { 544int run_entry(void* entry, size_t stack_bytes) {
473 { 545 {
474 std::lock_guard<std::mutex> lock(g_atexit_lock); 546 CleanupLock lock;
475 g_atexit.clear(); // a run never sees the handlers of an earlier one 547 g_atexit.clear(); // a run never sees the handlers of an earlier one
548 g_at_quick_exit.clear();
476 } 549 }
550 g_thread_local_exit = nullptr;
477 g_exiting_thread = 0; 551 g_exiting_thread = 0;
478 g_exited = false; 552 g_exited = false;
479 g_exit_code = 0; 553 g_exit_code = 0;
@@ -514,14 +588,15 @@ int run_entry(void* entry, size_t stack_bytes) {
514namespace { 588namespace {
515using namespace rt::process; 589using namespace rt::process;
516#define UCRT(dll) \ 590#define UCRT(dll) \
517 {dll, "exit", reinterpret_cast<void*>(&game_exit)}, {dll, "_exit", reinterpret_cast<void*>(&game_quick_exit)}, \ 591 {dll, "exit", reinterpret_cast<void*>(&game_exit)}, {dll, "_exit", reinterpret_cast<void*>(&game_underscore_exit)}, \
518 {dll, "_Exit", reinterpret_cast<void*>(&game_quick_exit)}, \ 592 {dll, "_Exit", reinterpret_cast<void*>(&game_underscore_exit)}, \
519 {dll, "quick_exit", reinterpret_cast<void*>(&game_quick_exit)}, \ 593 {dll, "quick_exit", reinterpret_cast<void*>(&game_quick_exit)}, \
520 {dll, "_crt_atexit", reinterpret_cast<void*>(&game_crt_atexit)}, \ 594 {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)}, \ 595 {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", \ 596 {dll, "_register_thread_local_exe_atexit_callback", \
524 reinterpret_cast<void*>(&game_register_thread_local_exe_atexit_callback)}, \ 597 reinterpret_cast<void*>(&game_register_thread_local_exe_atexit_callback)}, \
598 {dll, "_cexit", reinterpret_cast<void*>(&game_cexit)}, {dll, "_c_exit", reinterpret_cast<void*>(&game_c_exit)}, \
599 {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)}, \ 600 {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \
526 {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \ 601 {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \
527 {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_narrow_argv)}, \ 602 {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_narrow_argv)}, \
@@ -546,7 +621,7 @@ SHIM_TABLE(process,
546 UCRT("ucrtbase.dll"), 621 UCRT("ucrtbase.dll"),
547 UCRT("ucrtbased.dll"), // the Debug UCRT: a Debug build of the stand-in game (run_tiny_game) links it 622 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)}, 623 {"msvcrt.dll", "exit", reinterpret_cast<void*>(&game_exit)},
549 {"msvcrt.dll", "_exit", reinterpret_cast<void*>(&game_quick_exit)}, 624 {"msvcrt.dll", "_exit", reinterpret_cast<void*>(&game_underscore_exit)},
550 {"msvcrt.dll", "atexit", reinterpret_cast<void*>(&game_msvcrt_atexit)}, 625 {"msvcrt.dll", "atexit", reinterpret_cast<void*>(&game_msvcrt_atexit)},
551 {"msvcrt.dll", "_onexit", reinterpret_cast<void*>(&game_onexit)}, 626 {"msvcrt.dll", "_onexit", reinterpret_cast<void*>(&game_onexit)},
552 {"msvcrt.dll", "_cexit", reinterpret_cast<void*>(&game_cexit)}, 627 {"msvcrt.dll", "_cexit", reinterpret_cast<void*>(&game_cexit)},