From 6684d836ff16aba0d8d570e6fe4ccd9ae04e02a1 Mon Sep 17 00:00:00 2001 From: dracbat Date: Sun, 22 Nov 2026 14:05:53 +0000 Subject: Runtime: serve the game's command line and ExitProcess under every import name The UCRT's _get_narrow_winmain_command_line and _get_wide_winmain_command_line were not shimmed, so a /MD WinMain executable got gta6rt's own command line as lpCmdLine. GetCommandLineA/W and ExitProcess were shimmed under kernel32.dll only, and the loader matches DLL names exactly. Imported from kernelbase.dll, or from the API sets the umbrella libraries bind them to (api-ms-win-core-processenvironment-l1-1-0, api-ms-win-core-processthreads-l1-1-0), they went to the host. The package's vcomp140.dll imports ExitProcess from the latter. process_cmdline_test checks every name and the WinMain line. docs/runtime_imports.md: the dry-run table regenerated at this commit. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/process.cpp | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) (limited to 'runtime/shims/process.cpp') diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index c0070782f..d3f76a684 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp @@ -9,6 +9,10 @@ // process-wide atexit table also holds gta6rt's own). // Covered CRTs: the Universal CRT (api-ms-win-crt-runtime-l1-1-0.dll / ucrtbase.dll, what MSVC links; ucrtbased.dll in Debug) and // msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here. +// The loader matches imports by exact DLL name, so GetCommandLineA/W and ExitProcess are served under every name a +// title can import them by: kernel32.dll (the classic import libraries), kernelbase.dll, and the API sets that the +// umbrella libraries a GDK title links (OneCore_apiset.lib, mincore.lib) bind them to. The retail package's own DLLs +// import ExitProcess from api-ms-win-core-processthreads-l1-1-0.dll. #include #include @@ -16,6 +20,7 @@ #include #include #include +#include #include #include "process_shim.h" @@ -107,6 +112,24 @@ wchar_t** __cdecl game_p_wcmdln_storage() { return &p; } +// What the UCRT's _get_narrow/_get_wide_winmain_command_line return, and the CRT's WinMain startup passes as +// lpCmdLine: the command line after the program name and the blanks that follow it. The program name ends at the +// first character up to ' ' outside quotes, the UCRT's own scan. The comparison is unsigned, so a non-ASCII byte of +// the narrow line is part of the name; a DBCS trail byte is never '"' or a blank, so it needs no lead-byte step. +template +C* after_program_name(C* p) { + using U = std::make_unsigned_t; + bool quoted = false; + while (U(*p) > U(' ') || (*p && quoted)) { + if (*p == C('"')) quoted = !quoted; + ++p; + } + while (*p && U(*p) <= U(' ')) ++p; + return p; +} +char* __cdecl game_get_narrow_winmain_command_line() { return after_program_name(g_cmdline.data()); } +wchar_t* __cdecl game_get_wide_winmain_command_line() { return after_program_name(g_wcmdline.data()); } + // msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo) int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) { *argc = g_argc; @@ -197,7 +220,13 @@ using namespace rt::process; {dll, "_configure_narrow_argv", reinterpret_cast(&game_configure_argv)}, \ {dll, "_configure_wide_argv", reinterpret_cast(&game_configure_argv)}, \ {dll, "__p__acmdln", reinterpret_cast(&game_p_acmdln_storage)}, \ - {dll, "__p__wcmdln", reinterpret_cast(&game_p_wcmdln_storage)} + {dll, "__p__wcmdln", reinterpret_cast(&game_p_wcmdln_storage)}, \ + {dll, "_get_narrow_winmain_command_line", reinterpret_cast(&game_get_narrow_winmain_command_line)}, \ + {dll, "_get_wide_winmain_command_line", reinterpret_cast(&game_get_wide_winmain_command_line)} +#define EXIT_PROCESS(dll) {dll, "ExitProcess", reinterpret_cast(&game_exit_process)} +#define COMMAND_LINE(dll) \ + {dll, "GetCommandLineA", reinterpret_cast(&game_get_command_line_a)}, \ + {dll, "GetCommandLineW", reinterpret_cast(&game_get_command_line_w)} } // namespace SHIM_TABLE(process, @@ -209,7 +238,10 @@ SHIM_TABLE(process, {"msvcrt.dll", "abort", reinterpret_cast(&game_abort)}, {"msvcrt.dll", "__getmainargs", reinterpret_cast(&game_getmainargs)}, {"msvcrt.dll", "__wgetmainargs", reinterpret_cast(&game_wgetmainargs)}, - {"kernel32.dll", "ExitProcess", reinterpret_cast(&game_exit_process)}, - {"kernel32.dll", "GetCommandLineA", reinterpret_cast(&game_get_command_line_a)}, - {"kernel32.dll", "GetCommandLineW", reinterpret_cast(&game_get_command_line_w)}, + EXIT_PROCESS("kernel32.dll"), + EXIT_PROCESS("kernelbase.dll"), + EXIT_PROCESS("api-ms-win-core-processthreads-l1-1-0.dll"), + COMMAND_LINE("kernel32.dll"), + COMMAND_LINE("kernelbase.dll"), + COMMAND_LINE("api-ms-win-core-processenvironment-l1-1-0.dll"), ); -- cgit v1.2.3