diff options
| author | Nolan Cotter <186326284+NolanCotter@users.noreply.github.com> | 2026-11-22 20:39:59 -0700 |
|---|---|---|
| committer | Nolan Cotter <186326284+NolanCotter@users.noreply.github.com> | 2026-11-22 20:39:59 -0700 |
| commit | fa5f26db8eafd2cc27f627b68c0a5a5949bcf2ff (patch) | |
| tree | fc57e238be829295fc216dff89a6d93e64c16b50 /runtime/shims/process.cpp | |
| parent | 9b693ecd8a2f90799f6c47f8f06cf062feb9820c (diff) | |
| parent | 7b7d73f890f9fbbf5967bbb694390bbc99e33ba6 (diff) | |
Merge origin/main: DXGI device fields coexist with the console-ABI device state
Diffstat (limited to 'runtime/shims/process.cpp')
| -rw-r--r-- | runtime/shims/process.cpp | 101 |
1 files changed, 95 insertions, 6 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index 1fc4fee7f..0bca28443 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp | |||
| @@ -5,8 +5,14 @@ | |||
| 5 | // - argv/argc and the command line are the game's (set by run_entry), not gta6rt's; | 5 | // - argv/argc and the command line are the game's (set by run_entry), not gta6rt's; |
| 6 | // - exit(), _exit(), quick_exit(), ExitProcess() and abort() from the game end the game's run, not the host: | 6 | // - exit(), _exit(), quick_exit(), ExitProcess() and abort() from the game end the game's run, not the host: |
| 7 | // the exit code is recorded, the run is signalled as finished and the calling thread waits for the host to | 7 | // the exit code is recorded, the run is signalled as finished and the calling thread waits for the host to |
| 8 | // exit. atexit handlers and static destructors of the game are not run (its CRT shares the host's, whose | 8 | // exit; |
| 9 | // process-wide atexit table also holds gta6rt's own). | 9 | // - the game's atexit registrations (_crt_atexit, which MSVC's atexit and static destructors reach; msvcrt's |
| 10 | // atexit and _onexit) go to a table of the run's own instead of the CRT's process-wide one, which the game | ||
| 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 | ||
| 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 | ||
| 15 | // destructor callback (_register_thread_local_exe_atexit_callback); both still reach the host CRT. | ||
| 10 | // 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 | // Covered CRTs: the Universal CRT (api-ms-win-crt-runtime-l1-1-0.dll / ucrtbase.dll, what MSVC links; ucrtbased.dll in Debug) and |
| 11 | // msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here. | 17 | // msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here. |
| 12 | // The loader matches imports by exact DLL name, so GetCommandLineA/W and ExitProcess are served under every name a | 18 | // The loader matches imports by exact DLL name, so GetCommandLineA/W and ExitProcess are served under every name a |
| @@ -19,6 +25,8 @@ | |||
| 19 | #include <cstdio> | 25 | #include <cstdio> |
| 20 | #include <cstdlib> | 26 | #include <cstdlib> |
| 21 | #include <cstring> | 27 | #include <cstring> |
| 28 | #include <mutex> | ||
| 29 | #include <new> | ||
| 22 | #include <string> | 30 | #include <string> |
| 23 | #include <type_traits> | 31 | #include <type_traits> |
| 24 | #include <vector> | 32 | #include <vector> |
| @@ -84,8 +92,77 @@ std::string narrow(const std::wstring& w) { | |||
| 84 | ExitProcess(static_cast<UINT>(g_exit_code.load())); | 92 | ExitProcess(static_cast<UINT>(g_exit_code.load())); |
| 85 | } | 93 | } |
| 86 | 94 | ||
| 95 | // ---- the game's atexit table | ||
| 96 | using AtexitFn = void(__cdecl*)(); | ||
| 97 | using OnexitFn = int(__cdecl*)(); | ||
| 98 | std::mutex g_atexit_lock; | ||
| 99 | std::vector<AtexitFn> g_atexit; | ||
| 100 | std::atomic<DWORD> g_exiting_thread{0}; | ||
| 101 | |||
| 102 | bool register_atexit(AtexitFn fn) { | ||
| 103 | std::lock_guard<std::mutex> lock(g_atexit_lock); | ||
| 104 | try { | ||
| 105 | g_atexit.push_back(fn); | ||
| 106 | } catch (const std::bad_alloc&) { | ||
| 107 | return false; // the CRT reports a full table as a failed registration | ||
| 108 | } | ||
| 109 | return true; | ||
| 110 | } | ||
| 111 | |||
| 112 | // Newest first. A handler registered while the handlers run goes on the end and so runs next, as in the UCRT. | ||
| 113 | void run_atexit_handlers() { | ||
| 114 | for (;;) { | ||
| 115 | AtexitFn fn; | ||
| 116 | { | ||
| 117 | std::lock_guard<std::mutex> lock(g_atexit_lock); | ||
| 118 | if (g_atexit.empty()) return; | ||
| 119 | fn = g_atexit.back(); | ||
| 120 | g_atexit.pop_back(); | ||
| 121 | } | ||
| 122 | fn(); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | // UCRT: int _crt_atexit(_PVFV), 0 on success. MSVC's atexit() and the static destructors of an EXE reach it. | ||
| 127 | int __cdecl game_crt_atexit(AtexitFn fn) { | ||
| 128 | return fn && register_atexit(fn) ? 0 : -1; | ||
| 129 | } | ||
| 130 | // msvcrt.dll (MinGW): int atexit(void (*)(void)) and _onexit_t _onexit(_onexit_t). | ||
| 131 | int __cdecl game_msvcrt_atexit(AtexitFn fn) { return game_crt_atexit(fn); } | ||
| 132 | OnexitFn __cdecl game_onexit(OnexitFn fn) { | ||
| 133 | // its int result is ignored, as the CRT ignores it | ||
| 134 | return fn && register_atexit(reinterpret_cast<AtexitFn>(fn)) ? fn : nullptr; | ||
| 135 | } | ||
| 136 | |||
| 87 | // ---- the way out | 137 | // ---- the way out |
| 88 | void __cdecl game_exit(int code) { end_run(code); } | 138 | // The CRT's exit lock, which exit(), _exit(), _Exit() and quick_exit() all take: the first thread to leave owns it |
| 139 | // and ends the run; any other thread that tries to leave waits forever. The owner may leave again from a handler | ||
| 140 | // (undefined in C): that ends the run with the new code and skips the remaining handlers. | ||
| 141 | bool take_exit_lock() { | ||
| 142 | const DWORD self = GetCurrentThreadId(); | ||
| 143 | DWORD owner = 0; | ||
| 144 | if (g_exiting_thread.compare_exchange_strong(owner, self)) return true; | ||
| 145 | if (owner != self) { | ||
| 146 | for (;;) Sleep(INFINITE); | ||
| 147 | } | ||
| 148 | return false; | ||
| 149 | } | ||
| 150 | // exit(): the game's handlers and static destructors, then the end of the run. | ||
| 151 | void __cdecl game_exit(int code) { | ||
| 152 | if (take_exit_lock()) run_atexit_handlers(); | ||
| 153 | end_run(code); | ||
| 154 | } | ||
| 155 | // _exit(), _Exit(), quick_exit(): the end of the run without the atexit handlers. | ||
| 156 | void __cdecl game_quick_exit(int code) { | ||
| 157 | take_exit_lock(); | ||
| 158 | end_run(code); | ||
| 159 | } | ||
| 160 | // _cexit() runs the handlers and flushes the streams, then returns; _c_exit() only returns. | ||
| 161 | void __cdecl game_cexit() { | ||
| 162 | run_atexit_handlers(); | ||
| 163 | std::fflush(nullptr); | ||
| 164 | } | ||
| 165 | void __cdecl game_c_exit() {} | ||
| 89 | void __stdcall game_exit_process(UINT code) { end_run(static_cast<int>(code)); } | 166 | void __stdcall game_exit_process(UINT code) { end_run(static_cast<int>(code)); } |
| 90 | void __cdecl game_abort() { | 167 | void __cdecl game_abort() { |
| 91 | std::fprintf(stderr, "[rt] process: the game called abort()\n"); | 168 | std::fprintf(stderr, "[rt] process: the game called abort()\n"); |
| @@ -203,6 +280,11 @@ void set_command_line(const std::wstring& program, const std::vector<std::wstrin | |||
| 203 | } | 280 | } |
| 204 | 281 | ||
| 205 | int run_entry(void* entry, size_t stack_bytes) { | 282 | int run_entry(void* entry, size_t stack_bytes) { |
| 283 | { | ||
| 284 | std::lock_guard<std::mutex> lock(g_atexit_lock); | ||
| 285 | g_atexit.clear(); // a run never sees the handlers of an earlier one | ||
| 286 | } | ||
| 287 | g_exiting_thread = 0; | ||
| 206 | g_exited = false; | 288 | g_exited = false; |
| 207 | g_exit_code = 0; | 289 | g_exit_code = 0; |
| 208 | g_done = CreateEventW(nullptr, TRUE, FALSE, nullptr); | 290 | g_done = CreateEventW(nullptr, TRUE, FALSE, nullptr); |
| @@ -242,8 +324,11 @@ int run_entry(void* entry, size_t stack_bytes) { | |||
| 242 | namespace { | 324 | namespace { |
| 243 | using namespace rt::process; | 325 | using namespace rt::process; |
| 244 | #define UCRT(dll) \ | 326 | #define UCRT(dll) \ |
| 245 | {dll, "exit", reinterpret_cast<void*>(&game_exit)}, {dll, "_exit", reinterpret_cast<void*>(&game_exit)}, \ | 327 | {dll, "exit", reinterpret_cast<void*>(&game_exit)}, {dll, "_exit", reinterpret_cast<void*>(&game_quick_exit)}, \ |
| 246 | {dll, "_Exit", reinterpret_cast<void*>(&game_exit)}, {dll, "quick_exit", reinterpret_cast<void*>(&game_exit)}, \ | 328 | {dll, "_Exit", reinterpret_cast<void*>(&game_quick_exit)}, \ |
| 329 | {dll, "quick_exit", reinterpret_cast<void*>(&game_quick_exit)}, \ | ||
| 330 | {dll, "_crt_atexit", reinterpret_cast<void*>(&game_crt_atexit)}, \ | ||
| 331 | {dll, "_cexit", reinterpret_cast<void*>(&game_cexit)}, {dll, "_c_exit", reinterpret_cast<void*>(&game_c_exit)}, \ | ||
| 247 | {dll, "abort", reinterpret_cast<void*>(&game_abort)}, {dll, "__p___argc", reinterpret_cast<void*>(&game_p_argc)}, \ | 332 | {dll, "abort", reinterpret_cast<void*>(&game_abort)}, {dll, "__p___argc", reinterpret_cast<void*>(&game_p_argc)}, \ |
| 248 | {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \ | 333 | {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \ |
| 249 | {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \ | 334 | {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \ |
| @@ -264,7 +349,11 @@ SHIM_TABLE(process, | |||
| 264 | UCRT("ucrtbase.dll"), | 349 | UCRT("ucrtbase.dll"), |
| 265 | UCRT("ucrtbased.dll"), // the Debug UCRT: a Debug build of the stand-in game (run_tiny_game) links it | 350 | UCRT("ucrtbased.dll"), // the Debug UCRT: a Debug build of the stand-in game (run_tiny_game) links it |
| 266 | {"msvcrt.dll", "exit", reinterpret_cast<void*>(&game_exit)}, | 351 | {"msvcrt.dll", "exit", reinterpret_cast<void*>(&game_exit)}, |
| 267 | {"msvcrt.dll", "_exit", reinterpret_cast<void*>(&game_exit)}, | 352 | {"msvcrt.dll", "_exit", reinterpret_cast<void*>(&game_quick_exit)}, |
| 353 | {"msvcrt.dll", "atexit", reinterpret_cast<void*>(&game_msvcrt_atexit)}, | ||
| 354 | {"msvcrt.dll", "_onexit", reinterpret_cast<void*>(&game_onexit)}, | ||
| 355 | {"msvcrt.dll", "_cexit", reinterpret_cast<void*>(&game_cexit)}, | ||
| 356 | {"msvcrt.dll", "_c_exit", reinterpret_cast<void*>(&game_c_exit)}, | ||
| 268 | {"msvcrt.dll", "abort", reinterpret_cast<void*>(&game_abort)}, | 357 | {"msvcrt.dll", "abort", reinterpret_cast<void*>(&game_abort)}, |
| 269 | {"msvcrt.dll", "__getmainargs", reinterpret_cast<void*>(&game_getmainargs)}, | 358 | {"msvcrt.dll", "__getmainargs", reinterpret_cast<void*>(&game_getmainargs)}, |
| 270 | {"msvcrt.dll", "__wgetmainargs", reinterpret_cast<void*>(&game_wgetmainargs)}, | 359 | {"msvcrt.dll", "__wgetmainargs", reinterpret_cast<void*>(&game_wgetmainargs)}, |