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/shims/process.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'runtime/shims/process.cpp') diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index d3f76a684..6673d4bb1 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp @@ -38,8 +38,6 @@ std::wstring g_wcmdline; int g_argc = 0; char** g_argv_ptr = nullptr; wchar_t** g_wargv_ptr = nullptr; -char** g_env = nullptr; -wchar_t** g_wenv = nullptr; std::atomic g_running{false}; std::atomic g_exited{false}; @@ -130,17 +128,37 @@ C* after_program_name(C* p) { char* __cdecl game_get_narrow_winmain_command_line() { return after_program_name(g_cmdline.data()); } wchar_t* __cdecl game_get_wide_winmain_command_line() { return after_program_name(g_wcmdline.data()); } +// The host CRT's environment tables, read when the game asks (RT-136). _environ and _wenviron are not stable: the host +// CRT builds the wide table only when a wide environment function first runs (a host started through main() has +// 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. +char** current_environment() { + if (!_environ) { + size_t size = 0; + (void)getenv_s(&size, nullptr, 0, "LEONIDA_RT_ENV_INIT"); + } + return _environ; +} +wchar_t** current_wide_environment() { + if (!_wenviron) { + size_t size = 0; + (void)_wgetenv_s(&size, nullptr, 0, L"LEONIDA_RT_ENV_INIT"); + } + return _wenviron; +} + // msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo) int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) { *argc = g_argc; *argv = g_argv_ptr; - if (env) *env = g_env; + if (env) *env = current_environment(); return 0; } int __cdecl game_wgetmainargs(int* argc, wchar_t*** argv, wchar_t*** env, int, void*) { *argc = g_argc; *argv = g_wargv_ptr; - if (env) *env = g_wenv; + if (env) *env = current_wide_environment(); return 0; } @@ -179,8 +197,6 @@ void set_command_line(const std::wstring& program, const std::vector(g_args.size()); g_argv_ptr = g_argv.data(); g_wargv_ptr = g_wargv.data(); - g_env = _environ; - g_wenv = _wenviron; } int run_entry(void* entry, size_t stack_bytes) { -- 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/shims/process.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