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/test/process_cmdline_test.cpp | |
| parent | 7e4992dd863df9fb7e21f48d14aad8f96fe5ec4d (diff) | |
runtime: honor mapped executable argv startup modes (#6117)
Squash-merged after diff review of PR #6117
Diffstat (limited to 'runtime/test/process_cmdline_test.cpp')
| -rw-r--r-- | runtime/test/process_cmdline_test.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp index a345c6ac5..fb0a0498c 100644 --- a/runtime/test/process_cmdline_test.cpp +++ b/runtime/test/process_cmdline_test.cpp | |||
| @@ -28,8 +28,23 @@ char** argv() { | |||
| 28 | return *reinterpret_cast<Fn>(rt::shims::find("ucrtbase.dll", "__p___argv"))(); | 28 | return *reinterpret_cast<Fn>(rt::shims::find("ucrtbase.dll", "__p___argv"))(); |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | int configure(bool wide, int mode) { | ||
| 32 | using Fn = int(__cdecl*)(int); | ||
| 33 | const auto fn = reinterpret_cast<Fn>(rt::shims::find( | ||
| 34 | "ucrtbase.dll", wide ? "_configure_wide_argv" : "_configure_narrow_argv")); | ||
| 35 | if (fn) return fn(mode); | ||
| 36 | std::fprintf(stderr, "FAIL missing argv configuration shim\n"); | ||
| 37 | ++failures; | ||
| 38 | return -1; | ||
| 39 | } | ||
| 40 | |||
| 31 | void round_trip(const std::vector<std::wstring>& args, const char* what) { | 41 | void round_trip(const std::vector<std::wstring>& args, const char* what) { |
| 32 | rt::process::set_command_line(L"game.exe", args); | 42 | rt::process::set_command_line(L"game.exe", args); |
| 43 | if (configure(false, 1) || configure(true, 1)) { | ||
| 44 | std::fprintf(stderr, "FAIL %s: unexpanded argv configuration\n", what); | ||
| 45 | ++failures; | ||
| 46 | return; | ||
| 47 | } | ||
| 33 | using Fn = LPWSTR(WINAPI*)(); | 48 | using Fn = LPWSTR(WINAPI*)(); |
| 34 | const LPWSTR line = reinterpret_cast<Fn>(rt::shims::find("kernel32.dll", "GetCommandLineW"))(); | 49 | const LPWSTR line = reinterpret_cast<Fn>(rt::shims::find("kernel32.dll", "GetCommandLineW"))(); |
| 35 | int count = 0; | 50 | int count = 0; |
| @@ -43,6 +58,34 @@ void round_trip(const std::vector<std::wstring>& args, const char* what) { | |||
| 43 | LocalFree(parsed); | 58 | LocalFree(parsed); |
| 44 | } | 59 | } |
| 45 | 60 | ||
| 61 | void check_argv_reset() { | ||
| 62 | const int host_argc = __argc; | ||
| 63 | char** const host_argv = __argv; | ||
| 64 | wchar_t** const host_wargv = __wargv; | ||
| 65 | using Argc = int*(__cdecl*)(); | ||
| 66 | const auto count = reinterpret_cast<Argc>(rt::shims::find("ucrtbase.dll", "__p___argc")); | ||
| 67 | auto check = [](bool ok, const char* what) { | ||
| 68 | if (!ok) { std::fprintf(stderr, "FAIL argv state: %s\n", what); ++failures; } | ||
| 69 | }; | ||
| 70 | rt::process::set_command_line(L"first.exe", {L"one", L"two"}); | ||
| 71 | check(*count() == 0 && !argv() && !wargv(), "new run starts unconfigured"); | ||
| 72 | check(configure(false, 0) == 0 && configure(true, 0) == 0 && *count() == 0 && !argv() && !wargv(), | ||
| 73 | "no-argument startup leaves the initial state alone"); | ||
| 74 | check(configure(false, 1) == 0 && *count() == 3 && argv() && !wargv(), "configure only narrow argv"); | ||
| 75 | char** const narrow = argv(); | ||
| 76 | check(configure(true, 1) == 0 && *count() == 3 && argv() == narrow && wargv(), | ||
| 77 | "wide configuration retains the narrow table"); | ||
| 78 | wchar_t** const wide = wargv(); | ||
| 79 | check(configure(false, 0) == 0 && configure(true, 0) == 0 && *count() == 3 && argv() == narrow && wargv() == wide, | ||
| 80 | "no-argument mode preserves configured counts and pointers"); | ||
| 81 | rt::process::set_command_line(L"second.exe", {L"new"}); | ||
| 82 | check(*count() == 0 && !argv() && !wargv(), "another run forgets both configured widths"); | ||
| 83 | check(configure(true, 1) == 0 && *count() == 2 && !argv() && wargv() && | ||
| 84 | std::wcscmp(wargv()[0], L"second.exe") == 0 && std::wcscmp(wargv()[1], L"new") == 0 && !wargv()[2], | ||
| 85 | "the new run configures its own program name and arguments"); | ||
| 86 | check(__argc == host_argc && __argv == host_argv && __wargv == host_wargv, "host CRT argument globals are untouched"); | ||
| 87 | } | ||
| 88 | |||
| 46 | // __wargv[1] must be `want` exactly, and the narrow argv[1] its ANSI code page text (the CRT's replacement for a | 89 | // __wargv[1] must be `want` exactly, and the narrow argv[1] its ANSI code page text (the CRT's replacement for a |
| 47 | // character the code page lacks), not a crash. | 90 | // character the code page lacks), not a crash. |
| 48 | void check_non_ascii(const std::wstring& want, const char* what) { | 91 | void check_non_ascii(const std::wstring& want, const char* what) { |
| @@ -186,6 +229,7 @@ int main() { | |||
| 186 | check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page"); | 229 | check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page"); |
| 187 | 230 | ||
| 188 | check_import_names(); | 231 | check_import_names(); |
| 232 | check_argv_reset(); | ||
| 189 | check_getmainargs_environment(); | 233 | check_getmainargs_environment(); |
| 190 | check_winmain(L"game.exe", {L"-foo", L"with space"}, L"-foo \"with space\"", "arguments after a plain name"); | 234 | check_winmain(L"game.exe", {L"-foo", L"with space"}, L"-foo \"with space\"", "arguments after a plain name"); |
| 191 | check_winmain(L"C:\\My Games\\game.exe", {L"-x"}, L"-x", "a quoted program name with a space"); | 235 | check_winmain(L"C:\\My Games\\game.exe", {L"-x"}, L"-x", "a quoted program name with a space"); |