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/test/process_cmdline_test.cpp | 68 +++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) (limited to 'runtime/test/process_cmdline_test.cpp') 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 @@ // The process shim's command line round-trips: what GetCommandLineW says must parse with CommandLineToArgvW (the // documented rules the CRT's argv parser shares) back to the __wargv the shim hands out, and __wargv must hold what -// main.cpp passed, whatever the system code page (RT-84). No game files. +// main.cpp passed, whatever the system code page (RT-84). The WinMain line (lpCmdLine) is the game's too, and the +// command line and ExitProcess are served under kernelbase.dll and the API-set names as well as kernel32.dll. +// No game files. #include #include #include +#include +#include #include #include @@ -54,6 +58,58 @@ void check_non_ascii(const std::wstring& want, const char* what) { ++failures; } } + +// An executable can import GetCommandLineA/W and ExitProcess from kernel32.dll, kernelbase.dll or the API set its +// umbrella library names, and the loader matches the DLL name exactly: every name must hand out the same shim. +void check_import_names() { + for (const char* dll : {"kernelbase.dll", "api-ms-win-core-processenvironment-l1-1-0.dll"}) + for (const char* name : {"GetCommandLineA", "GetCommandLineW"}) { + void* fn = rt::shims::find(dll, name); + if (!fn || fn != rt::shims::find("kernel32.dll", name)) { + std::fprintf(stderr, "FAIL %s!%s is not the kernel32.dll shim\n", dll, name); + ++failures; + } + } + for (const char* dll : {"kernelbase.dll", "api-ms-win-core-processthreads-l1-1-0.dll"}) { + void* fn = rt::shims::find(dll, "ExitProcess"); + if (!fn || fn != rt::shims::find("kernel32.dll", "ExitProcess")) { + std::fprintf(stderr, "FAIL %s!ExitProcess is not the kernel32.dll shim\n", dll); + ++failures; + } + } + for (const char* dll : {"api-ms-win-crt-runtime-l1-1-0.dll", "ucrtbase.dll", "ucrtbased.dll"}) + for (const char* name : {"_get_narrow_winmain_command_line", "_get_wide_winmain_command_line"}) + if (!rt::shims::find(dll, name)) { + std::fprintf(stderr, "FAIL %s!%s is not shimmed\n", dll, name); + ++failures; + } +} + +// lpCmdLine as the CRT's WinMain startup gets it from _get_wide/_get_narrow_winmain_command_line: the game's command +// line after the program name and the blanks that follow it (the narrow one in the ANSI code page). +void check_winmain(const std::wstring& program, const std::vector& args, const wchar_t* want, + const char* what) { + rt::process::set_command_line(program, args); + using Wide = wchar_t*(__cdecl*)(); + using Narrow = char*(__cdecl*)(); + for (const char* dll : {"api-ms-win-crt-runtime-l1-1-0.dll", "ucrtbase.dll"}) { + const wchar_t* wide = + reinterpret_cast(rt::shims::find(dll, "_get_wide_winmain_command_line"))(); + const char* line = reinterpret_cast(rt::shims::find(dll, "_get_narrow_winmain_command_line"))(); + char expected[256] = {}; + WideCharToMultiByte(CP_ACP, 0, want, -1, expected, sizeof(expected), nullptr, nullptr); + if (!wide || std::wcscmp(wide, want) != 0) { + std::fwprintf(stderr, L"FAIL %hs (%hs): the wide WinMain line is [%ls], want [%ls]\n", what, dll, + wide ? wide : L"(null)", want); + ++failures; + } + if (!line || std::strcmp(line, expected) != 0) { + std::fprintf(stderr, "FAIL %s (%s): the narrow WinMain line is [%s], want [%s]\n", what, dll, + line ? line : "(null)", expected); + ++failures; + } + } +} } // namespace int main() { @@ -67,8 +123,16 @@ int main() { // Greek, Cyrillic and CJK together: no single-byte code page holds all of them, so this covers every PC. check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page"); + check_import_names(); + check_winmain(L"game.exe", {L"-foo", L"with space"}, L"-foo \"with space\"", "arguments after a plain name"); + check_winmain(L"C:\\My Games\\game.exe", {L"-x"}, L"-x", "a quoted program name with a space"); + check_winmain(L"game.exe", {}, L"", "no arguments"); + // A byte above 0x7f in the narrow program name is part of the name, not the end of it. + check_winmain(L"caf\u00e9.exe", {L"-x"}, L"-x", "a non-ASCII program name"); + if (failures) return 1; - std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact (code page %u)\n", + std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact (code page %u); " + "the WinMain line and the kernelbase/API-set names are the game's\n", GetACP()); return 0; } -- cgit v1.2.3 From 7f67a9aef06277802227ea2d1807f1b55471e387 Mon Sep 17 00:00:00 2001 From: dracbat Date: Sun, 22 Nov 2026 14:08:55 +0000 Subject: process_cmdline_test: report a missing WinMain shim instead of calling it Without the shim, find() returns null and the WinMain check called it, ending the test with an access violation instead of a failure. Co-Authored-By: Claude Opus 5.5 --- runtime/test/process_cmdline_test.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'runtime/test/process_cmdline_test.cpp') diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp index 5ab85e359..8f35a8ef7 100644 --- a/runtime/test/process_cmdline_test.cpp +++ b/runtime/test/process_cmdline_test.cpp @@ -93,9 +93,11 @@ void check_winmain(const std::wstring& program, const std::vector& using Wide = wchar_t*(__cdecl*)(); using Narrow = char*(__cdecl*)(); for (const char* dll : {"api-ms-win-crt-runtime-l1-1-0.dll", "ucrtbase.dll"}) { - const wchar_t* wide = - reinterpret_cast(rt::shims::find(dll, "_get_wide_winmain_command_line"))(); - const char* line = reinterpret_cast(rt::shims::find(dll, "_get_narrow_winmain_command_line"))(); + const auto wide_fn = reinterpret_cast(rt::shims::find(dll, "_get_wide_winmain_command_line")); + const auto narrow_fn = reinterpret_cast(rt::shims::find(dll, "_get_narrow_winmain_command_line")); + if (!wide_fn || !narrow_fn) continue; // check_import_names has reported the missing shim + const wchar_t* wide = wide_fn(); + const char* line = narrow_fn(); char expected[256] = {}; WideCharToMultiByte(CP_ACP, 0, want, -1, expected, sizeof(expected), nullptr, nullptr); if (!wide || std::wcscmp(wide, want) != 0) { -- cgit v1.2.3