diff options
| author | pwu2775-dev <267844174+pwu2775-dev@users.noreply.github.com> | 2026-11-23 00:03:20 +0800 |
|---|---|---|
| committer | pwu2775-dev <267844174+pwu2775-dev@users.noreply.github.com> | 2026-11-23 00:03:20 +0800 |
| commit | 2e6b237729049f121b5441ac45526c2c3a8663d1 (patch) | |
| tree | 33bce12ec0f9b2fde1fdf70c014e71a4aea70a76 /runtime/test/process_cmdline_test.cpp | |
| parent | 4eb6155d3ac4f9d3fadb0d095c4ce13e76783615 (diff) | |
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.
Diffstat (limited to 'runtime/test/process_cmdline_test.cpp')
| -rw-r--r-- | runtime/test/process_cmdline_test.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
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<std::wstring>& | |||
| 112 | } | 112 | } |
| 113 | } | 113 | } |
| 114 | } | 114 | } |
| 115 | |||
| 116 | // msvcrt.dll's __getmainargs/__wgetmainargs (what a MinGW or old-MSVC CRT startup calls) hand out the game's argv and | ||
| 117 | // the process environment. The wide environment (_wenviron) does not exist until a wide environment function has run, | ||
| 118 | // and the narrow table is reallocated by _putenv, so both must be read at the call, not remembered by | ||
| 119 | // set_command_line (RT-136). `name=value` must be found in `env`, whatever was set before or after set_command_line. | ||
| 120 | template <typename C, typename Fn> | ||
| 121 | bool env_has(Fn getmainargs, const C* entry) { | ||
| 122 | int argc = 0; | ||
| 123 | C** argv = nullptr; | ||
| 124 | C** env = nullptr; | ||
| 125 | if (getmainargs(&argc, &argv, &env, 0, nullptr) != 0 || argc < 1 || !argv || !argv[0] || !env) return false; | ||
| 126 | for (C** e = env; *e; ++e) { | ||
| 127 | if constexpr (sizeof(C) == 1) { | ||
| 128 | if (std::strcmp(reinterpret_cast<const char*>(*e), reinterpret_cast<const char*>(entry)) == 0) return true; | ||
| 129 | } else { | ||
| 130 | if (std::wcscmp(reinterpret_cast<const wchar_t*>(*e), reinterpret_cast<const wchar_t*>(entry)) == 0) | ||
| 131 | return true; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | return false; | ||
| 135 | } | ||
| 136 | |||
| 137 | void check_getmainargs_environment() { | ||
| 138 | using Narrow = int(__cdecl*)(int*, char***, char***, int, void*); | ||
| 139 | using Wide = int(__cdecl*)(int*, wchar_t***, wchar_t***, int, void*); | ||
| 140 | const auto narrow_fn = reinterpret_cast<Narrow>(rt::shims::find("msvcrt.dll", "__getmainargs")); | ||
| 141 | const auto wide_fn = reinterpret_cast<Wide>(rt::shims::find("msvcrt.dll", "__wgetmainargs")); | ||
| 142 | if (!narrow_fn || !wide_fn) { | ||
| 143 | std::fprintf(stderr, "FAIL msvcrt.dll!__getmainargs/__wgetmainargs are not shimmed\n"); | ||
| 144 | ++failures; | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | // First use of the wide environment in this process: set_command_line runs before anything made _wenviron. | ||
| 148 | rt::process::set_command_line(L"game.exe", {L"-x"}); | ||
| 149 | _putenv_s("LEONIDA_RT136_A", "before"); // narrow only; the wide table is built from the OS block on first use | ||
| 150 | if (!env_has<wchar_t>(wide_fn, L"LEONIDA_RT136_A=before")) { | ||
| 151 | std::fprintf(stderr, "FAIL __wgetmainargs: no environment (or one without LEONIDA_RT136_A) at the first call\n"); | ||
| 152 | ++failures; | ||
| 153 | } | ||
| 154 | if (!env_has<char>(narrow_fn, "LEONIDA_RT136_A=before")) { | ||
| 155 | std::fprintf(stderr, "FAIL __getmainargs: no environment (or one without LEONIDA_RT136_A)\n"); | ||
| 156 | ++failures; | ||
| 157 | } | ||
| 158 | // A variable set after set_command_line reallocates the CRT's table; a pointer kept from before would dangle. | ||
| 159 | _putenv_s("LEONIDA_RT136_B", "after"); | ||
| 160 | _wputenv_s(L"LEONIDA_RT136_C", L"after"); | ||
| 161 | if (!env_has<wchar_t>(wide_fn, L"LEONIDA_RT136_B=after") || !env_has<wchar_t>(wide_fn, L"LEONIDA_RT136_C=after")) { | ||
| 162 | std::fprintf(stderr, "FAIL __wgetmainargs: an environment variable set after set_command_line is missing\n"); | ||
| 163 | ++failures; | ||
| 164 | } | ||
| 165 | if (!env_has<char>(narrow_fn, "LEONIDA_RT136_B=after") || !env_has<char>(narrow_fn, "LEONIDA_RT136_C=after")) { | ||
| 166 | std::fprintf(stderr, "FAIL __getmainargs: an environment variable set after set_command_line is missing\n"); | ||
| 167 | ++failures; | ||
| 168 | } | ||
| 169 | } | ||
| 115 | } // namespace | 170 | } // namespace |
| 116 | 171 | ||
| 117 | int main() { | 172 | int main() { |
| @@ -126,6 +181,7 @@ int main() { | |||
| 126 | check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page"); | 181 | check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page"); |
| 127 | 182 | ||
| 128 | check_import_names(); | 183 | check_import_names(); |
| 184 | check_getmainargs_environment(); | ||
| 129 | check_winmain(L"game.exe", {L"-foo", L"with space"}, L"-foo \"with space\"", "arguments after a plain name"); | 185 | check_winmain(L"game.exe", {L"-foo", L"with space"}, L"-foo \"with space\"", "arguments after a plain name"); |
| 130 | check_winmain(L"C:\\My Games\\game.exe", {L"-x"}, L"-x", "a quoted program name with a space"); | 186 | check_winmain(L"C:\\My Games\\game.exe", {L"-x"}, L"-x", "a quoted program name with a space"); |
| 131 | check_winmain(L"game.exe", {}, L"", "no arguments"); | 187 | check_winmain(L"game.exe", {}, L"", "no arguments"); |