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:
authorIAmNotSiob <273832158+IAmNotSiob@users.noreply.github.com>2026-11-21 23:50:12 +0100
committerroot <root@m29531.contaboserver.net>2026-11-21 23:50:31 +0100
commit0ca9bb8bc034ac2f5f322ce5b428af964bffca4e (patch)
treedf89c76031bd3b2dbdc63a2e8172e07d659fb4d1 /runtime/shims/process.cpp
parent8bd0087d2d0e45fc18fa78fb40b70ecfc6648854 (diff)
Runtime: gta6rt --run, and a stand-in game executable run through the loader
gta6rt --run <exe> [-- args] maps and binds the executable, initialises its modules and calls its entry point on a game thread. shims/process.cpp answers the process-level imports through the game's own import table: argv/argc and GetCommandLine are the game's (UCRT api-set/ucrtbase and msvcrt forms), and exit/_exit/quick_exit/ExitProcess/abort end the run with that exit code instead of the host. gta6rt then exits with the game's code directly, so the game's atexit handlers don't run during host shutdown. tiny_game.exe (CTest run_tiny_game, run_tiny_game_exit_code) does what a title does in its first second: arguments, a static initializer, a 4 MiB allocation, a file round trip, a thread, and xgameruntime.dll imports, which the shim registry answers instead of the link-time stub. thread_local and a C++ exception are opt-in (LEONIDA_RUN_TLS / LEONIDA_RUN_EH) until #93 and #176.
Diffstat (limited to 'runtime/shims/process.cpp')
-rw-r--r--runtime/shims/process.cpp199
1 files changed, 199 insertions, 0 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp
new file mode 100644
index 000000000..79fb73de9
--- /dev/null
+++ b/runtime/shims/process.cpp
@@ -0,0 +1,199 @@
1// Process-level imports of a mapped executable: its command line and its way out.
2//
3// `gta6rt --run` calls a mapped EXE's entry point inside the gta6rt process. The EXE's CRT startup asks for its
4// arguments and finally calls exit(); both go through the EXE's own import table, so they can be answered here:
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:
7// the exit code is recorded, the run is signalled as finished and the calling thread ends. atexit handlers
8// and static destructors of the game are not run (its CRT shares the host's, whose process-wide atexit table
9// also holds gta6rt's own).
10// Covered CRTs: the Universal CRT (api-ms-win-crt-runtime-l1-1-0.dll / ucrtbase.dll, what MSVC links) and
11// msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here.
12#include <windows.h>
13
14#include <atomic>
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18#include <string>
19#include <vector>
20
21#include "process.h"
22#include "shims.h"
23
24namespace rt::process {
25namespace {
26
27std::vector<std::string> g_args;
28std::vector<std::wstring> g_wargs;
29std::vector<char*> g_argv;
30std::vector<wchar_t*> g_wargv;
31std::string g_cmdline;
32std::wstring g_wcmdline;
33int g_argc = 0;
34char** g_argv_ptr = nullptr;
35wchar_t** g_wargv_ptr = nullptr;
36char** g_env = nullptr;
37wchar_t** g_wenv = nullptr;
38
39std::atomic<bool> g_exited{false};
40std::atomic<int> g_exit_code{0};
41HANDLE g_done = nullptr;
42
43std::string quote(const std::string& a) {
44 if (!a.empty() && a.find_first_of(" \t\"") == std::string::npos) return a;
45 std::string out = "\"";
46 for (char c : a) {
47 if (c == '"') out += '\\';
48 out += c;
49 }
50 return out + "\"";
51}
52
53std::wstring widen(const std::string& s) {
54 int n = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, nullptr, 0);
55 std::wstring w(n > 0 ? n - 1 : 0, L'\0');
56 if (n > 1) MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, w.data(), n);
57 return w;
58}
59
60[[noreturn]] void end_run(int code) {
61 bool expected = false;
62 if (g_exited.compare_exchange_strong(expected, true)) {
63 g_exit_code = code;
64 if (g_done) SetEvent(g_done);
65 ExitThread(static_cast<DWORD>(code));
66 }
67 // The run is already over: this is game code running during host shutdown (an atexit handler the game
68 // registered with the shared CRT calling exit again). Finish the process with the game's exit code; ending
69 // just this thread here would make the host's last thread, and so the process, exit with `code` instead.
70 ExitProcess(static_cast<UINT>(g_exit_code.load()));
71}
72
73// ---- the way out
74void __cdecl game_exit(int code) { end_run(code); }
75void __stdcall game_exit_process(UINT code) { end_run(static_cast<int>(code)); }
76void __cdecl game_abort() {
77 std::fprintf(stderr, "[rt] process: the game called abort()\n");
78 end_run(3); // the CRT's abort exit code
79}
80
81// ---- arguments
82int* __cdecl game_p_argc() { return &g_argc; }
83char*** __cdecl game_p_argv() { return &g_argv_ptr; }
84wchar_t*** __cdecl game_p_wargv() { return &g_wargv_ptr; }
85int __cdecl game_configure_argv(int) { return 0; } // already parsed: run_entry set them
86LPSTR __stdcall game_get_command_line_a() { return g_cmdline.data(); }
87LPWSTR __stdcall game_get_command_line_w() { return g_wcmdline.data(); }
88char** __cdecl game_p_acmdln_storage() {
89 static char* p = nullptr;
90 p = g_cmdline.data();
91 return &p;
92}
93wchar_t** __cdecl game_p_wcmdln_storage() {
94 static wchar_t* p = nullptr;
95 p = g_wcmdline.data();
96 return &p;
97}
98
99// msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo)
100int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) {
101 *argc = g_argc;
102 *argv = g_argv_ptr;
103 if (env) *env = g_env;
104 return 0;
105}
106int __cdecl game_wgetmainargs(int* argc, wchar_t*** argv, wchar_t*** env, int, void*) {
107 *argc = g_argc;
108 *argv = g_wargv_ptr;
109 if (env) *env = g_wenv;
110 return 0;
111}
112
113struct EntryCall {
114 void* entry;
115};
116
117DWORD WINAPI game_thread(void* p) {
118 auto* call = static_cast<EntryCall*>(p);
119 // An EXE entry point (mainCRTStartup and friends) takes the PEB pointer and returns the process exit code.
120 auto entry = reinterpret_cast<DWORD(WINAPI*)(void*)>(call->entry);
121 const DWORD code = entry(nullptr);
122 end_run(static_cast<int>(code)); // returning from the entry point is an exit too
123}
124
125} // namespace
126
127void set_command_line(const std::string& program, const std::vector<std::string>& args) {
128 g_args.clear();
129 g_args.push_back(program);
130 g_args.insert(g_args.end(), args.begin(), args.end());
131 g_wargs.clear();
132 g_cmdline.clear();
133 for (auto& a : g_args) {
134 g_wargs.push_back(widen(a));
135 if (!g_cmdline.empty()) g_cmdline += ' ';
136 g_cmdline += quote(a);
137 }
138 g_wcmdline = widen(g_cmdline);
139 g_argv.clear();
140 g_wargv.clear();
141 for (auto& a : g_args) g_argv.push_back(a.data());
142 for (auto& a : g_wargs) g_wargv.push_back(a.data());
143 g_argv.push_back(nullptr);
144 g_wargv.push_back(nullptr);
145 g_argc = static_cast<int>(g_args.size());
146 g_argv_ptr = g_argv.data();
147 g_wargv_ptr = g_wargv.data();
148 g_env = _environ;
149 g_wenv = _wenviron;
150}
151
152int run_entry(void* entry, size_t stack_bytes) {
153 g_exited = false;
154 g_exit_code = 0;
155 g_done = CreateEventW(nullptr, TRUE, FALSE, nullptr);
156 EntryCall call{entry};
157 HANDLE thread = CreateThread(nullptr, stack_bytes, game_thread, &call, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
158 if (!thread) {
159 CloseHandle(g_done);
160 g_done = nullptr;
161 return -1;
162 }
163 // The run ends when any game thread exits the process (exit/ExitProcess/abort) or the entry point returns.
164 HANDLE waits[] = {g_done, thread};
165 WaitForMultipleObjects(2, waits, FALSE, INFINITE);
166 CloseHandle(thread);
167 CloseHandle(g_done);
168 g_done = nullptr;
169 return g_exit_code.load();
170}
171
172} // namespace rt::process
173
174namespace {
175using namespace rt::process;
176#define UCRT(dll) \
177 {dll, "exit", reinterpret_cast<void*>(&game_exit)}, {dll, "_exit", reinterpret_cast<void*>(&game_exit)}, \
178 {dll, "_Exit", reinterpret_cast<void*>(&game_exit)}, {dll, "quick_exit", reinterpret_cast<void*>(&game_exit)}, \
179 {dll, "abort", reinterpret_cast<void*>(&game_abort)}, {dll, "__p___argc", reinterpret_cast<void*>(&game_p_argc)}, \
180 {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \
181 {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \
182 {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_argv)}, \
183 {dll, "_configure_wide_argv", reinterpret_cast<void*>(&game_configure_argv)}, \
184 {dll, "__p__acmdln", reinterpret_cast<void*>(&game_p_acmdln_storage)}, \
185 {dll, "__p__wcmdln", reinterpret_cast<void*>(&game_p_wcmdln_storage)}
186} // namespace
187
188SHIM_TABLE(process,
189 UCRT("api-ms-win-crt-runtime-l1-1-0.dll"),
190 UCRT("ucrtbase.dll"),
191 {"msvcrt.dll", "exit", reinterpret_cast<void*>(&game_exit)},
192 {"msvcrt.dll", "_exit", reinterpret_cast<void*>(&game_exit)},
193 {"msvcrt.dll", "abort", reinterpret_cast<void*>(&game_abort)},
194 {"msvcrt.dll", "__getmainargs", reinterpret_cast<void*>(&game_getmainargs)},
195 {"msvcrt.dll", "__wgetmainargs", reinterpret_cast<void*>(&game_wgetmainargs)},
196 {"kernel32.dll", "ExitProcess", reinterpret_cast<void*>(&game_exit_process)},
197 {"kernel32.dll", "GetCommandLineA", reinterpret_cast<void*>(&game_get_command_line_a)},
198 {"kernel32.dll", "GetCommandLineW", reinterpret_cast<void*>(&game_get_command_line_w)},
199);