From 2e6b237729049f121b5441ac45526c2c3a8663d1 Mon Sep 17 00:00:00 2001 From: pwu2775-dev <267844174+pwu2775-dev@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:03:20 +0800 Subject: runtime: __getmainargs/__wgetmainargs read the host environment at call time (RT-136) set_command_line kept _environ/_wenviron pointers. The host CRT has no wide environment until a wide environment function first runs, and _putenv/_wputenv reallocate both tables, so a wide-character startup calling __wgetmainargs got a null or dangling environment. The shims now read the tables when asked and make the CRT create a missing one first. process_cmdline_test covers both functions: the first wide call, and variables set before and after set_command_line. --- runtime/test/process_cmdline_test.cpp | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (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 8f35a8ef7..53294b6e5 100644 --- a/runtime/test/process_cmdline_test.cpp +++ b/runtime/test/process_cmdline_test.cpp @@ -112,6 +112,61 @@ void check_winmain(const std::wstring& program, const std::vector& } } } + +// msvcrt.dll's __getmainargs/__wgetmainargs (what a MinGW or old-MSVC CRT startup calls) hand out the game's argv and +// the process environment. The wide environment (_wenviron) does not exist until a wide environment function has run, +// and the narrow table is reallocated by _putenv, so both must be read at the call, not remembered by +// set_command_line (RT-136). `name=value` must be found in `env`, whatever was set before or after set_command_line. +template +bool env_has(Fn getmainargs, const C* entry) { + int argc = 0; + C** argv = nullptr; + C** env = nullptr; + if (getmainargs(&argc, &argv, &env, 0, nullptr) != 0 || argc < 1 || !argv || !argv[0] || !env) return false; + for (C** e = env; *e; ++e) { + if constexpr (sizeof(C) == 1) { + if (std::strcmp(reinterpret_cast(*e), reinterpret_cast(entry)) == 0) return true; + } else { + if (std::wcscmp(reinterpret_cast(*e), reinterpret_cast(entry)) == 0) + return true; + } + } + return false; +} + +void check_getmainargs_environment() { + using Narrow = int(__cdecl*)(int*, char***, char***, int, void*); + using Wide = int(__cdecl*)(int*, wchar_t***, wchar_t***, int, void*); + const auto narrow_fn = reinterpret_cast(rt::shims::find("msvcrt.dll", "__getmainargs")); + const auto wide_fn = reinterpret_cast(rt::shims::find("msvcrt.dll", "__wgetmainargs")); + if (!narrow_fn || !wide_fn) { + std::fprintf(stderr, "FAIL msvcrt.dll!__getmainargs/__wgetmainargs are not shimmed\n"); + ++failures; + return; + } + // First use of the wide environment in this process: set_command_line runs before anything made _wenviron. + rt::process::set_command_line(L"game.exe", {L"-x"}); + _putenv_s("LEONIDA_RT136_A", "before"); // narrow only; the wide table is built from the OS block on first use + if (!env_has(wide_fn, L"LEONIDA_RT136_A=before")) { + std::fprintf(stderr, "FAIL __wgetmainargs: no environment (or one without LEONIDA_RT136_A) at the first call\n"); + ++failures; + } + if (!env_has(narrow_fn, "LEONIDA_RT136_A=before")) { + std::fprintf(stderr, "FAIL __getmainargs: no environment (or one without LEONIDA_RT136_A)\n"); + ++failures; + } + // A variable set after set_command_line reallocates the CRT's table; a pointer kept from before would dangle. + _putenv_s("LEONIDA_RT136_B", "after"); + _wputenv_s(L"LEONIDA_RT136_C", L"after"); + if (!env_has(wide_fn, L"LEONIDA_RT136_B=after") || !env_has(wide_fn, L"LEONIDA_RT136_C=after")) { + std::fprintf(stderr, "FAIL __wgetmainargs: an environment variable set after set_command_line is missing\n"); + ++failures; + } + if (!env_has(narrow_fn, "LEONIDA_RT136_B=after") || !env_has(narrow_fn, "LEONIDA_RT136_C=after")) { + std::fprintf(stderr, "FAIL __getmainargs: an environment variable set after set_command_line is missing\n"); + ++failures; + } +} } // namespace int main() { @@ -126,6 +181,7 @@ int main() { check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page"); check_import_names(); + check_getmainargs_environment(); 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"); -- cgit v1.2.3 From 21ea2bb1d01f9d0ac648a0694740eea9c1aad7d0 Mon Sep 17 00:00:00 2001 From: pwu2775-dev <267844174+pwu2775-dev@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:10:18 +0800 Subject: runtime: RT-136 review follow-up: check the test's setup, say what the returned table is valid for process_cmdline_test now checks the return values of _putenv_s/_wputenv_s and says when _wenviron already exists at startup, so the first-call check cannot pass silently without exercising the lazy wide-table creation. process.cpp notes that the returned table is the CRT's own and valid until the next _putenv, as with the msvcrt.dll original. --- runtime/shims/process.cpp | 3 +++ runtime/test/process_cmdline_test.cpp | 13 +++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) (limited to 'runtime/test/process_cmdline_test.cpp') diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index 6673d4bb1..9da50e4e9 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp @@ -133,6 +133,9 @@ wchar_t* __cdecl game_get_wide_winmain_command_line() { return after_program_nam // none until then), and _putenv/_wputenv reallocate both, so a pointer taken in set_command_line would be null or // dangling by the time a wide-character startup calls __wgetmainargs. A lookup of a name no one sets makes the CRT // create the table if it does not exist yet (from the other table or the OS block) without changing it. +// Like the msvcrt.dll original, the returned table is the CRT's own and is only valid until the next _putenv: a CRT +// startup reads it once, before the game has other threads, and a host that edits its environment concurrently with +// a game's startup is not covered. char** current_environment() { if (!_environ) { size_t size = 0; diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp index 53294b6e5..01f1b38a5 100644 --- a/runtime/test/process_cmdline_test.cpp +++ b/runtime/test/process_cmdline_test.cpp @@ -145,9 +145,12 @@ void check_getmainargs_environment() { return; } // First use of the wide environment in this process: set_command_line runs before anything made _wenviron. + // main() hosts start with only the narrow table; if a toolchain change made _wenviron exist, the first-call + // checks below no longer prove the creation path, so say so rather than pass silently. + if (_wenviron) std::printf("note: _wenviron already exists at startup; its lazy creation is not exercised\n"); rt::process::set_command_line(L"game.exe", {L"-x"}); - _putenv_s("LEONIDA_RT136_A", "before"); // narrow only; the wide table is built from the OS block on first use - if (!env_has(wide_fn, L"LEONIDA_RT136_A=before")) { + if (_putenv_s("LEONIDA_RT136_A", "before") != 0 || // narrow only; the wide table is built on first use + !env_has(wide_fn, L"LEONIDA_RT136_A=before")) { std::fprintf(stderr, "FAIL __wgetmainargs: no environment (or one without LEONIDA_RT136_A) at the first call\n"); ++failures; } @@ -156,8 +159,10 @@ void check_getmainargs_environment() { ++failures; } // A variable set after set_command_line reallocates the CRT's table; a pointer kept from before would dangle. - _putenv_s("LEONIDA_RT136_B", "after"); - _wputenv_s(L"LEONIDA_RT136_C", L"after"); + if (_putenv_s("LEONIDA_RT136_B", "after") != 0 || _wputenv_s(L"LEONIDA_RT136_C", L"after") != 0) { + std::fprintf(stderr, "FAIL _putenv_s/_wputenv_s did not set the test variables\n"); + ++failures; + } if (!env_has(wide_fn, L"LEONIDA_RT136_B=after") || !env_has(wide_fn, L"LEONIDA_RT136_C=after")) { std::fprintf(stderr, "FAIL __wgetmainargs: an environment variable set after set_command_line is missing\n"); ++failures; -- cgit v1.2.3