diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 16:55:04 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 16:55:04 -0500 |
| commit | df92abca8ee2a8f9093efea7fcaa4dadc3ddaebc (patch) | |
| tree | 97ef8f95544fe263049d3f7360823f16100de209 /runtime/shims/process.cpp | |
| parent | 49a0b69fcdb80db1f2a44ce7ca0ab8dfdec1e403 (diff) | |
| parent | e5a7c8942678b323f655939c81f5d8855d20c248 (diff) | |
Merge main e5a7c8942 into shadow-aliased-guard (#1867)
One conflict, runtime/README.md: this branch rewrote the LEONIDA_SHADOW_ALIAS_MS
row, main (#2163) added the LEONIDA_SHADOW_KEEP row after it. Both rows kept;
the keep row's "aliased" now reads "aliased while they can't be guarded at every
address", since aliased pages are no longer copied without a sign of a write.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/process.cpp')
| -rw-r--r-- | runtime/shims/process.cpp | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index 08023ea78..2c37292e2 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp | |||
| @@ -11,8 +11,9 @@ | |||
| 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. | 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). | ||
| 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 | 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 |
| 17 | // 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. |
| 18 | // 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 |
| @@ -142,6 +143,28 @@ OnexitFn __cdecl game_onexit(OnexitFn fn) { | |||
| 142 | return fn && register_atexit(reinterpret_cast<AtexitFn>(fn)) ? fn : nullptr; | 143 | return fn && register_atexit(reinterpret_cast<AtexitFn>(fn)) ? fn : nullptr; |
| 143 | } | 144 | } |
| 144 | 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. | ||
| 156 | using TlsCallback = void(__stdcall*)(void*, unsigned long, void*); | ||
| 157 | std::atomic<TlsCallback> g_thread_local_exit_callback{nullptr}; | ||
| 158 | |||
| 159 | void __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. | ||
| 163 | void 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 | |||
| 145 | // ---- the way out | 168 | // ---- the way out |
| 146 | // 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 |
| 147 | // 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,7 +180,10 @@ bool take_exit_lock() { | |||
| 157 | } | 180 | } |
| 158 | // 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. |
| 159 | void __cdecl game_exit(int code) { | 182 | void __cdecl game_exit(int code) { |
| 160 | if (take_exit_lock()) run_atexit_handlers(); | 183 | if (take_exit_lock()) { |
| 184 | run_thread_local_exe_atexit_callback(); | ||
| 185 | run_atexit_handlers(); | ||
| 186 | } | ||
| 161 | end_run(code); | 187 | end_run(code); |
| 162 | } | 188 | } |
| 163 | // _exit(), _Exit(), quick_exit(): the end of the run without the atexit handlers. | 189 | // _exit(), _Exit(), quick_exit(): the end of the run without the atexit handlers. |
| @@ -167,6 +193,7 @@ void __cdecl game_quick_exit(int code) { | |||
| 167 | } | 193 | } |
| 168 | // _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. |
| 169 | void __cdecl game_cexit() { | 195 | void __cdecl game_cexit() { |
| 196 | run_thread_local_exe_atexit_callback(); | ||
| 170 | run_atexit_handlers(); | 197 | run_atexit_handlers(); |
| 171 | std::fflush(nullptr); | 198 | std::fflush(nullptr); |
| 172 | } | 199 | } |
| @@ -493,6 +520,8 @@ using namespace rt::process; | |||
| 493 | {dll, "_crt_atexit", reinterpret_cast<void*>(&game_crt_atexit)}, \ | 520 | {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)}, \ | 521 | {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)}, \ | 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", \ | ||
| 524 | reinterpret_cast<void*>(&game_register_thread_local_exe_atexit_callback)}, \ | ||
| 496 | {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \ | 525 | {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \ |
| 497 | {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \ | 526 | {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \ |
| 498 | {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_narrow_argv)}, \ | 527 | {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_narrow_argv)}, \ |