diff options
| author | Hiiva <32042291+Hiiva@users.noreply.github.com> | 2026-11-23 18:36:08 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-23 10:36:08 -0600 |
| commit | 5cf3dafe3ec90e740f90bf73df9e4417143f5253 (patch) | |
| tree | ab4c9b43232905e2b1fe4eb2ae126ba8166e1a1c /runtime/shims/process.cpp | |
| parent | 7e4992dd863df9fb7e21f48d14aad8f96fe5ec4d (diff) | |
runtime: honor mapped executable argv startup modes (#6117)
Squash-merged after diff review of PR #6117
Diffstat (limited to 'runtime/shims/process.cpp')
| -rw-r--r-- | runtime/shims/process.cpp | 118 |
1 files changed, 101 insertions, 17 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index 77bcde936..b66c2e590 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | // | 2 | // |
| 3 | // `gta6rt --run` calls a mapped EXE's entry point inside the gta6rt process. The EXE's CRT startup asks for its | 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: | 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; | 5 | // - the command line is the game's, and its CRT configures its own argv/argc at startup; |
| 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; | 8 | // exit; |
| @@ -22,10 +22,12 @@ | |||
| 22 | #include <windows.h> | 22 | #include <windows.h> |
| 23 | 23 | ||
| 24 | #include <atomic> | 24 | #include <atomic> |
| 25 | #include <cerrno> | ||
| 25 | #include <csignal> | 26 | #include <csignal> |
| 26 | #include <cstdio> | 27 | #include <cstdio> |
| 27 | #include <cstdlib> | 28 | #include <cstdlib> |
| 28 | #include <cstring> | 29 | #include <cstring> |
| 30 | #include <cwchar> | ||
| 29 | #include <mutex> | 31 | #include <mutex> |
| 30 | #include <new> | 32 | #include <new> |
| 31 | #include <string> | 33 | #include <string> |
| @@ -38,6 +40,8 @@ | |||
| 38 | namespace rt::process { | 40 | namespace rt::process { |
| 39 | namespace { | 41 | namespace { |
| 40 | 42 | ||
| 43 | std::vector<std::string> g_input_args; | ||
| 44 | std::vector<std::wstring> g_input_wargs; | ||
| 41 | std::vector<std::string> g_args; | 45 | std::vector<std::string> g_args; |
| 42 | std::vector<std::wstring> g_wargs; | 46 | std::vector<std::wstring> g_wargs; |
| 43 | std::vector<char*> g_argv; | 47 | std::vector<char*> g_argv; |
| @@ -186,7 +190,86 @@ void __cdecl game_abort() { | |||
| 186 | int* __cdecl game_p_argc() { return &g_argc; } | 190 | int* __cdecl game_p_argc() { return &g_argc; } |
| 187 | char*** __cdecl game_p_argv() { return &g_argv_ptr; } | 191 | char*** __cdecl game_p_argv() { return &g_argv_ptr; } |
| 188 | wchar_t*** __cdecl game_p_wargv() { return &g_wargv_ptr; } | 192 | wchar_t*** __cdecl game_p_wargv() { return &g_wargv_ptr; } |
| 189 | int __cdecl game_configure_argv(int) { return 0; } // already parsed: run_entry set them | 193 | |
| 194 | std::wstring widen_argument(const std::string& text) { | ||
| 195 | const int size = MultiByteToWideChar(CP_ACP, 0, text.c_str(), -1, nullptr, 0); | ||
| 196 | std::wstring wide(size > 0 ? static_cast<size_t>(size - 1) : 0, L'\0'); | ||
| 197 | if (size > 1) MultiByteToWideChar(CP_ACP, 0, text.c_str(), -1, wide.data(), size); | ||
| 198 | return wide; | ||
| 199 | } | ||
| 200 | |||
| 201 | // UCRT startup expands each wildcard argument against the host working directory, even when the argument was | ||
| 202 | // quoted. A failed search retains the pattern; a successful search omits "." and ".." and keeps the path prefix. | ||
| 203 | // Narrow startup searches its ACP-converted text, which can have different wildcard matches from wide startup. | ||
| 204 | template <typename C> | ||
| 205 | std::vector<std::basic_string<C>> expand_arguments(const std::vector<std::basic_string<C>>& input) { | ||
| 206 | std::vector<std::basic_string<C>> result; | ||
| 207 | for (const auto& argument : input) { | ||
| 208 | std::wstring pattern; | ||
| 209 | if constexpr (std::is_same_v<C, wchar_t>) pattern = argument; | ||
| 210 | else pattern = widen_argument(argument); | ||
| 211 | const size_t wildcard = pattern.find_first_of(L"*?"); | ||
| 212 | if (wildcard == std::wstring::npos) { | ||
| 213 | result.push_back(argument); | ||
| 214 | continue; | ||
| 215 | } | ||
| 216 | const size_t separator = pattern.find_last_of(L"\\/:", wildcard); | ||
| 217 | if (separator != std::wstring::npos && pattern[separator] == L':' && separator != 1) { | ||
| 218 | result.push_back(argument); | ||
| 219 | continue; | ||
| 220 | } | ||
| 221 | WIN32_FIND_DATAW found{}; | ||
| 222 | const HANDLE handle = ::FindFirstFileExW(pattern.c_str(), FindExInfoStandard, &found, | ||
| 223 | FindExSearchNameMatch, nullptr, 0); | ||
| 224 | if (handle == INVALID_HANDLE_VALUE) { | ||
| 225 | result.push_back(argument); | ||
| 226 | continue; | ||
| 227 | } | ||
| 228 | struct Search { | ||
| 229 | HANDLE handle; | ||
| 230 | ~Search() { ::FindClose(handle); } | ||
| 231 | } search{handle}; | ||
| 232 | const std::wstring prefix = pattern.substr(0, separator == std::wstring::npos ? 0 : separator + 1); | ||
| 233 | do { | ||
| 234 | if (std::wcscmp(found.cFileName, L".") == 0 || std::wcscmp(found.cFileName, L"..") == 0) continue; | ||
| 235 | const std::wstring match = prefix + found.cFileName; | ||
| 236 | if constexpr (std::is_same_v<C, wchar_t>) result.push_back(match); | ||
| 237 | else result.push_back(narrow(match)); | ||
| 238 | } while (::FindNextFileW(handle, &found)); | ||
| 239 | } | ||
| 240 | return result; | ||
| 241 | } | ||
| 242 | |||
| 243 | template <typename C> | ||
| 244 | int configure_argv(int mode, const std::vector<std::basic_string<C>>& input, | ||
| 245 | std::vector<std::basic_string<C>>& strings, std::vector<C*>& pointers, C**& published) { | ||
| 246 | // _crt_argv_no_arguments is a no-op, including after another width or mode has been configured. | ||
| 247 | if (mode == 0) return 0; | ||
| 248 | if (mode != 1 && mode != 2) return errno = EINVAL; | ||
| 249 | try { | ||
| 250 | auto next_strings = mode == 2 ? expand_arguments(input) : input; | ||
| 251 | std::vector<C*> next_pointers; | ||
| 252 | next_pointers.reserve(next_strings.size() + 1); | ||
| 253 | for (auto& argument : next_strings) next_pointers.push_back(argument.data()); | ||
| 254 | next_pointers.push_back(nullptr); | ||
| 255 | // Publish only after every allocation succeeds. The other width keeps its own strings and pointer table. | ||
| 256 | strings.swap(next_strings); | ||
| 257 | pointers.swap(next_pointers); | ||
| 258 | published = pointers.data(); | ||
| 259 | g_argc = static_cast<int>(strings.size()); | ||
| 260 | return 0; | ||
| 261 | } catch (const std::bad_alloc&) { | ||
| 262 | return errno = ENOMEM; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | int __cdecl game_configure_narrow_argv(int mode) { | ||
| 267 | return configure_argv(mode, g_input_args, g_args, g_argv, g_argv_ptr); | ||
| 268 | } | ||
| 269 | int __cdecl game_configure_wide_argv(int mode) { | ||
| 270 | return configure_argv(mode, g_input_wargs, g_wargs, g_wargv, g_wargv_ptr); | ||
| 271 | } | ||
| 272 | |||
| 190 | LPSTR __stdcall game_get_command_line_a() { return g_cmdline.data(); } | 273 | LPSTR __stdcall game_get_command_line_a() { return g_cmdline.data(); } |
| 191 | LPWSTR __stdcall game_get_command_line_w() { return g_wcmdline.data(); } | 274 | LPWSTR __stdcall game_get_command_line_w() { return g_wcmdline.data(); } |
| 192 | char** __cdecl game_p_acmdln_storage() { | 275 | char** __cdecl game_p_acmdln_storage() { |
| @@ -243,12 +326,14 @@ wchar_t** current_wide_environment() { | |||
| 243 | 326 | ||
| 244 | // msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo) | 327 | // msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo) |
| 245 | int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) { | 328 | int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) { |
| 329 | if (const int error = game_configure_narrow_argv(1)) return error; | ||
| 246 | *argc = g_argc; | 330 | *argc = g_argc; |
| 247 | *argv = g_argv_ptr; | 331 | *argv = g_argv_ptr; |
| 248 | if (env) *env = current_environment(); | 332 | if (env) *env = current_environment(); |
| 249 | return 0; | 333 | return 0; |
| 250 | } | 334 | } |
| 251 | int __cdecl game_wgetmainargs(int* argc, wchar_t*** argv, wchar_t*** env, int, void*) { | 335 | int __cdecl game_wgetmainargs(int* argc, wchar_t*** argv, wchar_t*** env, int, void*) { |
| 336 | if (const int error = game_configure_wide_argv(1)) return error; | ||
| 252 | *argc = g_argc; | 337 | *argc = g_argc; |
| 253 | *argv = g_wargv_ptr; | 338 | *argv = g_wargv_ptr; |
| 254 | if (env) *env = current_wide_environment(); | 339 | if (env) *env = current_wide_environment(); |
| @@ -270,26 +355,25 @@ DWORD WINAPI game_thread(void* p) { | |||
| 270 | } // namespace | 355 | } // namespace |
| 271 | 356 | ||
| 272 | void set_command_line(const std::wstring& program, const std::vector<std::wstring>& args) { | 357 | void set_command_line(const std::wstring& program, const std::vector<std::wstring>& args) { |
| 273 | g_wargs.clear(); | 358 | g_input_wargs.clear(); |
| 274 | g_wargs.push_back(program); | 359 | g_input_wargs.push_back(program); |
| 275 | g_wargs.insert(g_wargs.end(), args.begin(), args.end()); | 360 | g_input_wargs.insert(g_input_wargs.end(), args.begin(), args.end()); |
| 276 | g_args.clear(); | 361 | g_input_args.clear(); |
| 277 | g_wcmdline.clear(); | 362 | g_wcmdline.clear(); |
| 278 | for (auto& a : g_wargs) { | 363 | for (const auto& a : g_input_wargs) { |
| 279 | g_args.push_back(narrow(a)); | 364 | g_input_args.push_back(narrow(a)); |
| 280 | if (!g_wcmdline.empty()) g_wcmdline += L' '; | 365 | if (!g_wcmdline.empty()) g_wcmdline += L' '; |
| 281 | g_wcmdline += quote(a); | 366 | g_wcmdline += quote(a); |
| 282 | } | 367 | } |
| 283 | g_cmdline = narrow(g_wcmdline); | 368 | g_cmdline = narrow(g_wcmdline); |
| 369 | // GetCommandLine is available before TLS/DllMain, but CRT argv starts unconfigured on every run. | ||
| 370 | g_args.clear(); | ||
| 371 | g_wargs.clear(); | ||
| 284 | g_argv.clear(); | 372 | g_argv.clear(); |
| 285 | g_wargv.clear(); | 373 | g_wargv.clear(); |
| 286 | for (auto& a : g_args) g_argv.push_back(a.data()); | 374 | g_argc = 0; |
| 287 | for (auto& a : g_wargs) g_wargv.push_back(a.data()); | 375 | g_argv_ptr = nullptr; |
| 288 | g_argv.push_back(nullptr); | 376 | g_wargv_ptr = nullptr; |
| 289 | g_wargv.push_back(nullptr); | ||
| 290 | g_argc = static_cast<int>(g_args.size()); | ||
| 291 | g_argv_ptr = g_argv.data(); | ||
| 292 | g_wargv_ptr = g_wargv.data(); | ||
| 293 | } | 377 | } |
| 294 | 378 | ||
| 295 | int run_entry(void* entry, size_t stack_bytes) { | 379 | int run_entry(void* entry, size_t stack_bytes) { |
| @@ -345,8 +429,8 @@ using namespace rt::process; | |||
| 345 | {dll, "abort", reinterpret_cast<void*>(&game_abort)}, {dll, "__p___argc", reinterpret_cast<void*>(&game_p_argc)}, \ | 429 | {dll, "abort", reinterpret_cast<void*>(&game_abort)}, {dll, "__p___argc", reinterpret_cast<void*>(&game_p_argc)}, \ |
| 346 | {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \ | 430 | {dll, "__p___argv", reinterpret_cast<void*>(&game_p_argv)}, \ |
| 347 | {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \ | 431 | {dll, "__p___wargv", reinterpret_cast<void*>(&game_p_wargv)}, \ |
| 348 | {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_argv)}, \ | 432 | {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_narrow_argv)}, \ |
| 349 | {dll, "_configure_wide_argv", reinterpret_cast<void*>(&game_configure_argv)}, \ | 433 | {dll, "_configure_wide_argv", reinterpret_cast<void*>(&game_configure_wide_argv)}, \ |
| 350 | {dll, "__p__acmdln", reinterpret_cast<void*>(&game_p_acmdln_storage)}, \ | 434 | {dll, "__p__acmdln", reinterpret_cast<void*>(&game_p_acmdln_storage)}, \ |
| 351 | {dll, "__p__wcmdln", reinterpret_cast<void*>(&game_p_wcmdln_storage)}, \ | 435 | {dll, "__p__wcmdln", reinterpret_cast<void*>(&game_p_wcmdln_storage)}, \ |
| 352 | {dll, "_get_narrow_winmain_command_line", reinterpret_cast<void*>(&game_get_narrow_winmain_command_line)}, \ | 436 | {dll, "_get_narrow_winmain_command_line", reinterpret_cast<void*>(&game_get_narrow_winmain_command_line)}, \ |