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/shims/process.cpp | |
| 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/shims/process.cpp')
| -rw-r--r-- | runtime/shims/process.cpp | 40 |
1 files changed, 36 insertions, 4 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 | ); |