diff options
| author | dracbat <dracbat@users.noreply.github.com> | 2026-11-22 14:05:53 +0000 |
|---|---|---|
| committer | dracbat <dracbat@users.noreply.github.com> | 2026-11-22 14:05:53 +0000 |
| commit | 6684d836ff16aba0d8d570e6fe4ccd9ae04e02a1 (patch) | |
| tree | de437e9533fcf658ba67ba855c27583c1b63fec8 /runtime | |
| parent | 89b047cd8fab50675a43d76722a6a8a881f55dc2 (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/shims/process.cpp | 40 | ||||
| -rw-r--r-- | runtime/test/process_cmdline_test.cpp | 68 |
2 files changed, 102 insertions, 6 deletions
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 @@ | |||
| 9 | // process-wide atexit table also holds gta6rt's own). | 9 | // process-wide atexit table 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; ucrtbased.dll in Debug) and | 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 |
| 11 | // msvcrt.dll (MinGW). Calls made inside host DLLs, not through the game's imports, are not seen here. | 11 | // 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 | ||
| 13 | // title can import them by: kernel32.dll (the classic import libraries), kernelbase.dll, and the API sets that the | ||
| 14 | // umbrella libraries a GDK title links (OneCore_apiset.lib, mincore.lib) bind them to. The retail package's own DLLs | ||
| 15 | // import ExitProcess from api-ms-win-core-processthreads-l1-1-0.dll. | ||
| 12 | #include <windows.h> | 16 | #include <windows.h> |
| 13 | 17 | ||
| 14 | #include <atomic> | 18 | #include <atomic> |
| @@ -16,6 +20,7 @@ | |||
| 16 | #include <cstdlib> | 20 | #include <cstdlib> |
| 17 | #include <cstring> | 21 | #include <cstring> |
| 18 | #include <string> | 22 | #include <string> |
| 23 | #include <type_traits> | ||
| 19 | #include <vector> | 24 | #include <vector> |
| 20 | 25 | ||
| 21 | #include "process_shim.h" | 26 | #include "process_shim.h" |
| @@ -107,6 +112,24 @@ wchar_t** __cdecl game_p_wcmdln_storage() { | |||
| 107 | return &p; | 112 | return &p; |
| 108 | } | 113 | } |
| 109 | 114 | ||
| 115 | // What the UCRT's _get_narrow/_get_wide_winmain_command_line return, and the CRT's WinMain startup passes as | ||
| 116 | // lpCmdLine: the command line after the program name and the blanks that follow it. The program name ends at the | ||
| 117 | // first character up to ' ' outside quotes, the UCRT's own scan. The comparison is unsigned, so a non-ASCII byte of | ||
| 118 | // the narrow line is part of the name; a DBCS trail byte is never '"' or a blank, so it needs no lead-byte step. | ||
| 119 | template <typename C> | ||
| 120 | C* after_program_name(C* p) { | ||
| 121 | using U = std::make_unsigned_t<C>; | ||
| 122 | bool quoted = false; | ||
| 123 | while (U(*p) > U(' ') || (*p && quoted)) { | ||
| 124 | if (*p == C('"')) quoted = !quoted; | ||
| 125 | ++p; | ||
| 126 | } | ||
| 127 | while (*p && U(*p) <= U(' ')) ++p; | ||
| 128 | return p; | ||
| 129 | } | ||
| 130 | char* __cdecl game_get_narrow_winmain_command_line() { return after_program_name(g_cmdline.data()); } | ||
| 131 | wchar_t* __cdecl game_get_wide_winmain_command_line() { return after_program_name(g_wcmdline.data()); } | ||
| 132 | |||
| 110 | // msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo) | 133 | // msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo) |
| 111 | int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) { | 134 | int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) { |
| 112 | *argc = g_argc; | 135 | *argc = g_argc; |
| @@ -197,7 +220,13 @@ using namespace rt::process; | |||
| 197 | {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_argv)}, \ | 220 | {dll, "_configure_narrow_argv", reinterpret_cast<void*>(&game_configure_argv)}, \ |
| 198 | {dll, "_configure_wide_argv", reinterpret_cast<void*>(&game_configure_argv)}, \ | 221 | {dll, "_configure_wide_argv", reinterpret_cast<void*>(&game_configure_argv)}, \ |
| 199 | {dll, "__p__acmdln", reinterpret_cast<void*>(&game_p_acmdln_storage)}, \ | 222 | {dll, "__p__acmdln", reinterpret_cast<void*>(&game_p_acmdln_storage)}, \ |
| 200 | {dll, "__p__wcmdln", reinterpret_cast<void*>(&game_p_wcmdln_storage)} | 223 | {dll, "__p__wcmdln", reinterpret_cast<void*>(&game_p_wcmdln_storage)}, \ |
| 224 | {dll, "_get_narrow_winmain_command_line", reinterpret_cast<void*>(&game_get_narrow_winmain_command_line)}, \ | ||
| 225 | {dll, "_get_wide_winmain_command_line", reinterpret_cast<void*>(&game_get_wide_winmain_command_line)} | ||
| 226 | #define EXIT_PROCESS(dll) {dll, "ExitProcess", reinterpret_cast<void*>(&game_exit_process)} | ||
| 227 | #define COMMAND_LINE(dll) \ | ||
| 228 | {dll, "GetCommandLineA", reinterpret_cast<void*>(&game_get_command_line_a)}, \ | ||
| 229 | {dll, "GetCommandLineW", reinterpret_cast<void*>(&game_get_command_line_w)} | ||
| 201 | } // namespace | 230 | } // namespace |
| 202 | 231 | ||
| 203 | SHIM_TABLE(process, | 232 | SHIM_TABLE(process, |
| @@ -209,7 +238,10 @@ SHIM_TABLE(process, | |||
| 209 | {"msvcrt.dll", "abort", reinterpret_cast<void*>(&game_abort)}, | 238 | {"msvcrt.dll", "abort", reinterpret_cast<void*>(&game_abort)}, |
| 210 | {"msvcrt.dll", "__getmainargs", reinterpret_cast<void*>(&game_getmainargs)}, | 239 | {"msvcrt.dll", "__getmainargs", reinterpret_cast<void*>(&game_getmainargs)}, |
| 211 | {"msvcrt.dll", "__wgetmainargs", reinterpret_cast<void*>(&game_wgetmainargs)}, | 240 | {"msvcrt.dll", "__wgetmainargs", reinterpret_cast<void*>(&game_wgetmainargs)}, |
| 212 | {"kernel32.dll", "ExitProcess", reinterpret_cast<void*>(&game_exit_process)}, | 241 | EXIT_PROCESS("kernel32.dll"), |
| 213 | {"kernel32.dll", "GetCommandLineA", reinterpret_cast<void*>(&game_get_command_line_a)}, | 242 | EXIT_PROCESS("kernelbase.dll"), |
| 214 | {"kernel32.dll", "GetCommandLineW", reinterpret_cast<void*>(&game_get_command_line_w)}, | 243 | EXIT_PROCESS("api-ms-win-core-processthreads-l1-1-0.dll"), |
| 244 | COMMAND_LINE("kernel32.dll"), | ||
| 245 | COMMAND_LINE("kernelbase.dll"), | ||
| 246 | COMMAND_LINE("api-ms-win-core-processenvironment-l1-1-0.dll"), | ||
| 215 | ); | 247 | ); |
diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp index 4c78835bb..5ab85e359 100644 --- a/runtime/test/process_cmdline_test.cpp +++ b/runtime/test/process_cmdline_test.cpp | |||
| @@ -1,10 +1,14 @@ | |||
| 1 | // The process shim's command line round-trips: what GetCommandLineW says must parse with CommandLineToArgvW (the | 1 | // The process shim's command line round-trips: what GetCommandLineW says must parse with CommandLineToArgvW (the |
| 2 | // documented rules the CRT's argv parser shares) back to the __wargv the shim hands out, and __wargv must hold what | 2 | // documented rules the CRT's argv parser shares) back to the __wargv the shim hands out, and __wargv must hold what |
| 3 | // main.cpp passed, whatever the system code page (RT-84). No game files. | 3 | // main.cpp passed, whatever the system code page (RT-84). The WinMain line (lpCmdLine) is the game's too, and the |
| 4 | // command line and ExitProcess are served under kernelbase.dll and the API-set names as well as kernel32.dll. | ||
| 5 | // No game files. | ||
| 4 | #include <windows.h> | 6 | #include <windows.h> |
| 5 | #include <shellapi.h> | 7 | #include <shellapi.h> |
| 6 | 8 | ||
| 7 | #include <cstdio> | 9 | #include <cstdio> |
| 10 | #include <cstring> | ||
| 11 | #include <cwchar> | ||
| 8 | #include <string> | 12 | #include <string> |
| 9 | #include <vector> | 13 | #include <vector> |
| 10 | 14 | ||
| @@ -54,6 +58,58 @@ void check_non_ascii(const std::wstring& want, const char* what) { | |||
| 54 | ++failures; | 58 | ++failures; |
| 55 | } | 59 | } |
| 56 | } | 60 | } |
| 61 | |||
| 62 | // An executable can import GetCommandLineA/W and ExitProcess from kernel32.dll, kernelbase.dll or the API set its | ||
| 63 | // umbrella library names, and the loader matches the DLL name exactly: every name must hand out the same shim. | ||
| 64 | void check_import_names() { | ||
| 65 | for (const char* dll : {"kernelbase.dll", "api-ms-win-core-processenvironment-l1-1-0.dll"}) | ||
| 66 | for (const char* name : {"GetCommandLineA", "GetCommandLineW"}) { | ||
| 67 | void* fn = rt::shims::find(dll, name); | ||
| 68 | if (!fn || fn != rt::shims::find("kernel32.dll", name)) { | ||
| 69 | std::fprintf(stderr, "FAIL %s!%s is not the kernel32.dll shim\n", dll, name); | ||
| 70 | ++failures; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | for (const char* dll : {"kernelbase.dll", "api-ms-win-core-processthreads-l1-1-0.dll"}) { | ||
| 74 | void* fn = rt::shims::find(dll, "ExitProcess"); | ||
| 75 | if (!fn || fn != rt::shims::find("kernel32.dll", "ExitProcess")) { | ||
| 76 | std::fprintf(stderr, "FAIL %s!ExitProcess is not the kernel32.dll shim\n", dll); | ||
| 77 | ++failures; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | for (const char* dll : {"api-ms-win-crt-runtime-l1-1-0.dll", "ucrtbase.dll", "ucrtbased.dll"}) | ||
| 81 | for (const char* name : {"_get_narrow_winmain_command_line", "_get_wide_winmain_command_line"}) | ||
| 82 | if (!rt::shims::find(dll, name)) { | ||
| 83 | std::fprintf(stderr, "FAIL %s!%s is not shimmed\n", dll, name); | ||
| 84 | ++failures; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | // lpCmdLine as the CRT's WinMain startup gets it from _get_wide/_get_narrow_winmain_command_line: the game's command | ||
| 89 | // line after the program name and the blanks that follow it (the narrow one in the ANSI code page). | ||
| 90 | void check_winmain(const std::wstring& program, const std::vector<std::wstring>& args, const wchar_t* want, | ||
| 91 | const char* what) { | ||
| 92 | rt::process::set_command_line(program, args); | ||
| 93 | using Wide = wchar_t*(__cdecl*)(); | ||
| 94 | using Narrow = char*(__cdecl*)(); | ||
| 95 | for (const char* dll : {"api-ms-win-crt-runtime-l1-1-0.dll", "ucrtbase.dll"}) { | ||
| 96 | const wchar_t* wide = | ||
| 97 | reinterpret_cast<Wide>(rt::shims::find(dll, "_get_wide_winmain_command_line"))(); | ||
| 98 | const char* line = reinterpret_cast<Narrow>(rt::shims::find(dll, "_get_narrow_winmain_command_line"))(); | ||
| 99 | char expected[256] = {}; | ||
| 100 | WideCharToMultiByte(CP_ACP, 0, want, -1, expected, sizeof(expected), nullptr, nullptr); | ||
| 101 | if (!wide || std::wcscmp(wide, want) != 0) { | ||
| 102 | std::fwprintf(stderr, L"FAIL %hs (%hs): the wide WinMain line is [%ls], want [%ls]\n", what, dll, | ||
| 103 | wide ? wide : L"(null)", want); | ||
| 104 | ++failures; | ||
| 105 | } | ||
| 106 | if (!line || std::strcmp(line, expected) != 0) { | ||
| 107 | std::fprintf(stderr, "FAIL %s (%s): the narrow WinMain line is [%s], want [%s]\n", what, dll, | ||
| 108 | line ? line : "(null)", expected); | ||
| 109 | ++failures; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 57 | } // namespace | 113 | } // namespace |
| 58 | 114 | ||
| 59 | int main() { | 115 | int main() { |
| @@ -67,8 +123,16 @@ int main() { | |||
| 67 | // Greek, Cyrillic and CJK together: no single-byte code page holds all of them, so this covers every PC. | 123 | // Greek, Cyrillic and CJK together: no single-byte code page holds all of them, so this covers every PC. |
| 68 | check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page"); | 124 | check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page"); |
| 69 | 125 | ||
| 126 | check_import_names(); | ||
| 127 | check_winmain(L"game.exe", {L"-foo", L"with space"}, L"-foo \"with space\"", "arguments after a plain name"); | ||
| 128 | check_winmain(L"C:\\My Games\\game.exe", {L"-x"}, L"-x", "a quoted program name with a space"); | ||
| 129 | check_winmain(L"game.exe", {}, L"", "no arguments"); | ||
| 130 | // A byte above 0x7f in the narrow program name is part of the name, not the end of it. | ||
| 131 | check_winmain(L"caf\u00e9.exe", {L"-x"}, L"-x", "a non-ASCII program name"); | ||
| 132 | |||
| 70 | if (failures) return 1; | 133 | if (failures) return 1; |
| 71 | std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact (code page %u)\n", | 134 | std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact (code page %u); " |
| 135 | "the WinMain line and the kernelbase/API-set names are the game's\n", | ||
| 72 | GetACP()); | 136 | GetACP()); |
| 73 | return 0; | 137 | return 0; |
| 74 | } | 138 | } |