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:
authorNotRexed <therealnothingfn@gmail.com>2026-11-27 17:50:37 -0500
committerNotRexed <therealnothingfn@gmail.com>2026-11-27 17:50:37 -0500
commitb87abdef5735adc68a614b2025ffcf9869a47cc5 (patch)
tree0d8b473e8e753883a01e00153175994d6b7949c2 /runtime/shims/process.cpp
parent84e728faffb36cd96a08934e5632ba9b194801f3 (diff)
parent34e33d3ddc4dd95ad14a074db886df3d25588201 (diff)
Merge mandy's main into port/d3d12-nvidia
Brings the D3D12 runtime branch (d3d12/, its launcher scripts and notes, and the pull requests reviewed and applied on it) together with mandy's main. Conflicts (67 files, none under d3d12/): docs/, make_game.py, runtime/, scripts/build_runtime.ps1, tests/ and tools/. Every one takes main's side: main is where that code is maintained, and this branch's copies were older states of the same files or the same pull requests in another form. tests/test_fspx_unpack.py is main's too, as it tests main's tools/fspx_unpack.py. Nothing under d3d12/ is changed by the merge. Checked before pushing: python -m compileall over make_game.py, tools and tests; make_game.py --help; scripts/check_no_game_data.py over the files this adds to main; tests/test_text_write_line_endings.py. The translator suite, the C++ runtime build and CI have not run on the result. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/process.cpp')
-rw-r--r--runtime/shims/process.cpp119
1 files changed, 116 insertions, 3 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp
index 95eb917df..aa33fe750 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
@@ -143,6 +144,28 @@ OnexitFn __cdecl game_onexit(OnexitFn fn) {
143 return fn && register_atexit(reinterpret_cast<AtexitFn>(fn)) ? fn : nullptr; 144 return fn && register_atexit(reinterpret_cast<AtexitFn>(fn)) ? fn : nullptr;
144} 145}
145 146
147// ---- the EXE's thread-local destructors
148// An MSVC EXE's startup passes vcruntime's TLS destructor callback, which its TLS directory also lists, to
149// _register_thread_local_exe_atexit_callback (UCRT process.h: void __cdecl (_tls_callback_type), where
150// _tls_callback_type is void (__stdcall*)(void*, unsigned long, void*)). The UCRT calls it as
151// (nullptr, DLL_PROCESS_DETACH, nullptr) when exit() or _cexit() starts, before the atexit table: the leaving thread's
152// thread_local objects end before the static ones, as C++ requires. Other threads' end through the loader's
153// thread-detach notification, which calls the same function from the TLS directory, so it is not called here then.
154// The host CRT cannot take it: the UCRT accepts one per process and fast-fails the second (gta6rt's own startup
155// registers the first, so the title stopped at boot on Windows), and Wine's ucrtbase replaces gta6rt's own and never
156// calls the title's, because the run ends through end_run.
157using TlsCallback = void(__stdcall*)(void*, unsigned long, void*);
158std::atomic<TlsCallback> g_thread_local_exit_callback{nullptr};
159
160void __cdecl game_register_thread_local_exe_atexit_callback(TlsCallback callback) {
161 g_thread_local_exit_callback.store(callback);
162}
163// Once per run, like the UCRT's exit path: a later exit() after _cexit() finds nothing left to run.
164void run_thread_local_exe_atexit_callback() {
165 if (const TlsCallback callback = g_thread_local_exit_callback.exchange(nullptr))
166 callback(nullptr, DLL_PROCESS_DETACH, nullptr);
167}
168
146// ---- the way out 169// ---- the way out
147// The CRT's exit lock, which exit(), _exit(), _Exit() and quick_exit() all take: the first thread to leave owns it 170// The CRT's exit lock, which exit(), _exit(), _Exit() and quick_exit() all take: the first thread to leave owns it
148// and ends the run; any other thread that tries to leave waits forever. The owner may leave again from a handler 171// and ends the run; any other thread that tries to leave waits forever. The owner may leave again from a handler
@@ -158,7 +181,10 @@ bool take_exit_lock() {
158} 181}
159// exit(): the game's handlers and static destructors, then the end of the run. 182// exit(): the game's handlers and static destructors, then the end of the run.
160void __cdecl game_exit(int code) { 183void __cdecl game_exit(int code) {
161 if (take_exit_lock()) run_atexit_handlers(); 184 if (take_exit_lock()) {
185 run_thread_local_exe_atexit_callback();
186 run_atexit_handlers();
187 }
162 end_run(code); 188 end_run(code);
163} 189}
164// _exit(), _Exit(), quick_exit(): the end of the run without the atexit handlers. 190// _exit(), _Exit(), quick_exit(): the end of the run without the atexit handlers.
@@ -168,11 +194,78 @@ void __cdecl game_quick_exit(int code) {
168} 194}
169// _cexit() runs the handlers and flushes the streams, then returns; _c_exit() only returns. 195// _cexit() runs the handlers and flushes the streams, then returns; _c_exit() only returns.
170void __cdecl game_cexit() { 196void __cdecl game_cexit() {
197 run_thread_local_exe_atexit_callback();
171 run_atexit_handlers(); 198 run_atexit_handlers();
172 std::fflush(nullptr); 199 std::fflush(nullptr);
173} 200}
174void __cdecl game_c_exit() {} 201void __cdecl game_c_exit() {}
175void __stdcall game_exit_process(UINT code) { end_run(static_cast<int>(code)); } 202void __stdcall game_exit_process(UINT code) { end_run(static_cast<int>(code)); }
203
204// ---- threads and processors
205// The console has cores a PC may lack, and a title pins its threads to them with masks the host rejects: Windows
206// answers a mask with no CPU of the process (or one outside it) with 0 and ERROR_INVALID_PARAMETER, and a title that
207// checks the previous mask then aborts. The call succeeds on the console, so it does here: the thread stays where it
208// is and the answer is the mask the thread had (the process mask for a thread never pinned). A bad handle and an empty
209// mask still fail, as on Windows. The fact (answer instead of failing when the host call returns 0) is from sammwyy/xvalkyrie src/runtime/modules/kernel.cpp (commit
210// 53af7df77c10, GPL-3.0), reimplemented here in our own words and checked against the Win32 behaviour.
211DWORD_PTR __stdcall game_set_thread_affinity_mask(HANDLE thread, DWORD_PTR mask) {
212 DWORD_PTR previous = SetThreadAffinityMask(thread, mask);
213 if (previous != 0) return previous;
214 // An empty mask names no CPU on any machine: it fails as on Windows (the host's own answer, ERROR_INVALID_PARAMETER).
215 if (mask == 0) {
216 SetLastError(ERROR_INVALID_PARAMETER);
217 return 0;
218 }
219 DWORD_PTR process = 0, system = 0;
220 if (GetLastError() != ERROR_INVALID_PARAMETER || !GetProcessAffinityMask(GetCurrentProcess(), &process, &system))
221 return 0;
222 // The caller expects the mask the thread had, which may be narrower than the process mask (it may restore it
223 // later). The host reports it only on a successful call, so widen the thread to the process mask, read the old
224 // mask from that call, and put the old mask back: the thread ends where it started.
225 const DWORD_PTR actual = SetThreadAffinityMask(thread, process);
226 if (actual == 0) return 0; // the host's error stands (a bad handle)
227 if (actual != process) SetThreadAffinityMask(thread, actual);
228 // The first boot shows which masks the title asks for: the first few are named, the rest only counted.
229 static std::atomic<int> substituted{0};
230 const int n = ++substituted;
231 if (n <= 8)
232 std::fprintf(stderr, "[rt] process: SetThreadAffinityMask(0x%llx) names cores the host lacks; the thread stays, "
233 "answered its mask 0x%llx (call %d)\n",
234 static_cast<unsigned long long>(mask), static_cast<unsigned long long>(actual), n);
235 // The log may have failed (a closed stderr pipe leaves ERROR_NO_DATA); the call itself succeeded.
236 SetLastError(ERROR_SUCCESS);
237 return actual;
238}
239// The retail title imports the group form (processtopology API sets), not SetThreadAffinityMask. The same rule holds: a
240// GROUP_AFFINITY naming cores or a processor group the PC lacks is answered, not failed. The thread stays where it is,
241// the call returns TRUE and the out parameter gets the affinity the thread had. An empty mask names no CPU on any
242// machine and a bad handle is the host's own error: both are passed to the host and fail as on Windows.
243BOOL __stdcall game_set_thread_group_affinity(HANDLE thread, const GROUP_AFFINITY* requested, PGROUP_AFFINITY previous) {
244 // An empty mask names no CPU on any machine. Some Windows builds accept it and clear the affinity instead of
245 // failing, so answer the documented error before the host call rather than depending on the build.
246 if (!requested || requested->Mask == 0) {
247 SetLastError(ERROR_INVALID_PARAMETER);
248 return FALSE;
249 }
250 if (SetThreadGroupAffinity(thread, requested, previous)) return TRUE;
251 if (GetLastError() != ERROR_INVALID_PARAMETER) return FALSE;
252 GROUP_AFFINITY actual = {};
253 if (!GetThreadGroupAffinity(thread, &actual)) return FALSE; // the host's error stands
254 if (previous) *previous = actual;
255 static std::atomic<int> substituted{0};
256 const int n = ++substituted;
257 if (n <= 8)
258 std::fprintf(stderr, "[rt] process: SetThreadGroupAffinity(group %u, 0x%llx) names cores or a group the host lacks; "
259 "the thread stays, answered group %u mask 0x%llx (call %d)\n",
260 static_cast<unsigned>(requested->Group), static_cast<unsigned long long>(requested->Mask),
261 static_cast<unsigned>(actual.Group), static_cast<unsigned long long>(actual.Mask), n);
262 // The log may have failed (a closed stderr pipe leaves ERROR_NO_DATA); the call itself succeeded.
263 SetLastError(ERROR_SUCCESS);
264 return TRUE;
265}
266// A console-only kernel32 export that pins a thread pool to cores. The host has no such call, and a pool with the
267// host's own affinity runs the same work, so it succeeds and changes nothing.
268BOOL __stdcall game_set_threadpool_affinity_mask(PTP_POOL, DWORD_PTR) { return TRUE; }
176void __cdecl game_abort() { 269void __cdecl game_abort() {
177 std::fprintf(stderr, "[rt] process: the game called abort()\n"); 270 std::fprintf(stderr, "[rt] process: the game called abort()\n");
178 // The game's signal() import is not shimmed: its handler goes into the host CRT's process-wide table, which 271 // The game's signal() import is not shimmed: its handler goes into the host CRT's process-wide table, which
@@ -474,6 +567,8 @@ BOOL WINAPI game_terminate_process(HANDLE process, UINT code) {
474 {dll, "_crt_atexit", reinterpret_cast<void*>(&game_crt_atexit)}, \ 567 {dll, "_crt_atexit", reinterpret_cast<void*>(&game_crt_atexit)}, \
475 {dll, "_cexit", reinterpret_cast<void*>(&game_cexit)}, {dll, "_c_exit", reinterpret_cast<void*>(&game_c_exit)}, \ 568 {dll, "_cexit", reinterpret_cast<void*>(&game_cexit)}, {dll, "_c_exit", reinterpret_cast<void*>(&game_c_exit)}, \
476 {dll, "abort", reinterpret_cast<void*>(&game_abort)}, {dll, "__p___argc", reinterpret_cast<void*>(&game_p_argc)}, \ 569 {dll, "abort", reinterpret_cast<void*>(&game_abort)}, {dll, "__p___argc", reinterpret_cast<void*>(&game_p_argc)}, \
570 {dll, "_register_thread_local_exe_atexit_callback", \
571 reinterpret_cast<void*>(&game_register_thread_local_exe_atexit_callback)}, \
477 {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \ 572 {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \
478 {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \ 573 {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \
479 {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_narrow_argv)}, \ 574 {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_narrow_argv)}, \
@@ -483,6 +578,11 @@ BOOL WINAPI game_terminate_process(HANDLE process, UINT code) {
483 {dll, "_get_narrow_winmain_command_line", reinterpret_cast<void*>(&game_get_narrow_winmain_command_line)}, \ 578 {dll, "_get_narrow_winmain_command_line", reinterpret_cast<void*>(&game_get_narrow_winmain_command_line)}, \
484 {dll, "_get_wide_winmain_command_line", reinterpret_cast<void*>(&game_get_wide_winmain_command_line)} 579 {dll, "_get_wide_winmain_command_line", reinterpret_cast<void*>(&game_get_wide_winmain_command_line)}
485#define EXIT_PROCESS(dll) {dll, "ExitProcess", reinterpret_cast<void*>(&game_exit_process)} 580#define EXIT_PROCESS(dll) {dll, "ExitProcess", reinterpret_cast<void*>(&game_exit_process)}
581#define THREAD_AFFINITY(dll) {dll, "SetThreadAffinityMask", reinterpret_cast<void*>(&game_set_thread_affinity_mask)}
582#define THREAD_GROUP_AFFINITY(dll) \
583 {dll, "SetThreadGroupAffinity", reinterpret_cast<void*>(&game_set_thread_group_affinity)}
584#define POOL_AFFINITY(dll) \
585 {dll, "SetThreadpoolAffinityMask", reinterpret_cast<void*>(&game_set_threadpool_affinity_mask)}
486#define COMMAND_LINE(dll) \ 586#define COMMAND_LINE(dll) \
487 {dll, "GetCommandLineA", reinterpret_cast<void*>(&game_get_command_line_a)}, \ 587 {dll, "GetCommandLineA", reinterpret_cast<void*>(&game_get_command_line_a)}, \
488 {dll, "GetCommandLineW", reinterpret_cast<void*>(&game_get_command_line_w)} 588 {dll, "GetCommandLineW", reinterpret_cast<void*>(&game_get_command_line_w)}
@@ -504,6 +604,19 @@ SHIM_TABLE(process,
504 EXIT_PROCESS("kernel32.dll"), 604 EXIT_PROCESS("kernel32.dll"),
505 EXIT_PROCESS("kernelbase.dll"), 605 EXIT_PROCESS("kernelbase.dll"),
506 EXIT_PROCESS("api-ms-win-core-processthreads-l1-1-0.dll"), 606 EXIT_PROCESS("api-ms-win-core-processthreads-l1-1-0.dll"),
607 THREAD_AFFINITY("kernel32.dll"),
608 THREAD_AFFINITY("kernelbase.dll"),
609 THREAD_AFFINITY("api-ms-win-core-processthreads-l1-1-0.dll"),
610 THREAD_AFFINITY("api-ms-win-core-processthreads-l1-1-1.dll"),
611 THREAD_AFFINITY("api-ms-win-core-processthreads-l1-1-2.dll"),
612 THREAD_AFFINITY("api-ms-win-core-processthreads-l1-1-3.dll"),
613 THREAD_GROUP_AFFINITY("kernel32.dll"),
614 THREAD_GROUP_AFFINITY("kernelbase.dll"),
615 THREAD_GROUP_AFFINITY("api-ms-win-core-processtopology-l1-1-0.dll"),
616 THREAD_GROUP_AFFINITY("api-ms-win-core-processtopology-l1-1-1.dll"),
617 THREAD_GROUP_AFFINITY("api-ms-win-core-processtopology-l1-1-2.dll"),
618 POOL_AFFINITY("kernel32.dll"),
619 POOL_AFFINITY("kernelbase.dll"),
507 COMMAND_LINE("kernel32.dll"), 620 COMMAND_LINE("kernel32.dll"),
508 COMMAND_LINE("kernelbase.dll"), 621 COMMAND_LINE("kernelbase.dll"),
509 DEBUG_OUTPUT("kernel32.dll"), 622 DEBUG_OUTPUT("kernel32.dll"),