upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/process_cmdline_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/process_cmdline_test.cpp')
-rw-r--r--runtime/test/process_cmdline_test.cpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp
index 79aeed11b..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
31int 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
31void round_trip(const std::vector<std::wstring>& args, const char* what) { 41void 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
61void 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.
48void check_non_ascii(const std::wstring& want, const char* what) { 91void 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");
@@ -204,6 +248,27 @@ int main() {
204 } 248 }
205 } 249 }
206 250
251 // The game thread that ends a run must park, not ExitProcess, even when run_entry has already returned and
252 // cleared the run: main.cpp still prints "game exited with code N" and cleans up after it. On one CPU the
253 // host preempts the game thread right after SetEvent(g_done), and Sleep lets the game thread run again before
254 // the next run starts. A thread that exits the process here ends this test with code 77 before its last line.
255 {
256 auto returns_77 = +[](void*) -> DWORD { return 77; };
257 DWORD_PTR process_mask = 0, system_mask = 0;
258 GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask);
259 SetProcessAffinityMask(GetCurrentProcess(), process_mask & (~process_mask + 1)); // its lowest CPU
260 for (int i = 0; i < 50; ++i) {
261 const int code = rt::process::run_entry(reinterpret_cast<void*>(returns_77), 64 << 10);
262 if (code != 77) {
263 std::fprintf(stderr, "FAIL run %d of an entry point returning 77 returned %d\n", i, code);
264 ++failures;
265 break;
266 }
267 Sleep(1);
268 }
269 SetProcessAffinityMask(GetCurrentProcess(), process_mask);
270 }
271
207 if (failures) return 1; 272 if (failures) return 1;
208 std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact (code page %u); " 273 std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact (code page %u); "
209 "the WinMain line and the kernelbase/API-set names are the game's\n", 274 "the WinMain line and the kernelbase/API-set names are the game's\n",