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/shims/process.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/shims/process.cpp')
| -rw-r--r-- | runtime/shims/process.cpp | 28 |
1 files changed, 22 insertions, 6 deletions
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; | |||
| 38 | int g_argc = 0; | 38 | int g_argc = 0; |
| 39 | char** g_argv_ptr = nullptr; | 39 | char** g_argv_ptr = nullptr; |
| 40 | wchar_t** g_wargv_ptr = nullptr; | 40 | wchar_t** g_wargv_ptr = nullptr; |
| 41 | char** g_env = nullptr; | ||
| 42 | wchar_t** g_wenv = nullptr; | ||
| 43 | 41 | ||
| 44 | std::atomic<bool> g_running{false}; | 42 | std::atomic<bool> g_running{false}; |
| 45 | std::atomic<bool> g_exited{false}; | 43 | std::atomic<bool> g_exited{false}; |
| @@ -130,17 +128,37 @@ C* after_program_name(C* p) { | |||
| 130 | char* __cdecl game_get_narrow_winmain_command_line() { return after_program_name(g_cmdline.data()); } | 128 | char* __cdecl game_get_narrow_winmain_command_line() { return after_program_name(g_cmdline.data()); } |
| 131 | wchar_t* __cdecl game_get_wide_winmain_command_line() { return after_program_name(g_wcmdline.data()); } | 129 | wchar_t* __cdecl game_get_wide_winmain_command_line() { return after_program_name(g_wcmdline.data()); } |
| 132 | 130 | ||
| 131 | // The host CRT's environment tables, read when the game asks (RT-136). _environ and _wenviron are not stable: the host | ||
| 132 | // CRT builds the wide table only when a wide environment function first runs (a host started through main() has | ||
| 133 | // none until then), and _putenv/_wputenv reallocate both, so a pointer taken in set_command_line would be null or | ||
| 134 | // dangling by the time a wide-character startup calls __wgetmainargs. A lookup of a name no one sets makes the CRT | ||
| 135 | // create the table if it does not exist yet (from the other table or the OS block) without changing it. | ||
| 136 | char** current_environment() { | ||
| 137 | if (!_environ) { | ||
| 138 | size_t size = 0; | ||
| 139 | (void)getenv_s(&size, nullptr, 0, "LEONIDA_RT_ENV_INIT"); | ||
| 140 | } | ||
| 141 | return _environ; | ||
| 142 | } | ||
| 143 | wchar_t** current_wide_environment() { | ||
| 144 | if (!_wenviron) { | ||
| 145 | size_t size = 0; | ||
| 146 | (void)_wgetenv_s(&size, nullptr, 0, L"LEONIDA_RT_ENV_INIT"); | ||
| 147 | } | ||
| 148 | return _wenviron; | ||
| 149 | } | ||
| 150 | |||
| 133 | // msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo) | 151 | // msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo) |
| 134 | int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) { | 152 | int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) { |
| 135 | *argc = g_argc; | 153 | *argc = g_argc; |
| 136 | *argv = g_argv_ptr; | 154 | *argv = g_argv_ptr; |
| 137 | if (env) *env = g_env; | 155 | if (env) *env = current_environment(); |
| 138 | return 0; | 156 | return 0; |
| 139 | } | 157 | } |
| 140 | int __cdecl game_wgetmainargs(int* argc, wchar_t*** argv, wchar_t*** env, int, void*) { | 158 | int __cdecl game_wgetmainargs(int* argc, wchar_t*** argv, wchar_t*** env, int, void*) { |
| 141 | *argc = g_argc; | 159 | *argc = g_argc; |
| 142 | *argv = g_wargv_ptr; | 160 | *argv = g_wargv_ptr; |
| 143 | if (env) *env = g_wenv; | 161 | if (env) *env = current_wide_environment(); |
| 144 | return 0; | 162 | return 0; |
| 145 | } | 163 | } |
| 146 | 164 | ||
| @@ -179,8 +197,6 @@ void set_command_line(const std::wstring& program, const std::vector<std::wstrin | |||
| 179 | g_argc = static_cast<int>(g_args.size()); | 197 | g_argc = static_cast<int>(g_args.size()); |
| 180 | g_argv_ptr = g_argv.data(); | 198 | g_argv_ptr = g_argv.data(); |
| 181 | g_wargv_ptr = g_wargv.data(); | 199 | g_wargv_ptr = g_wargv.data(); |
| 182 | g_env = _environ; | ||
| 183 | g_wenv = _wenviron; | ||
| 184 | } | 200 | } |
| 185 | 201 | ||
| 186 | int run_entry(void* entry, size_t stack_bytes) { | 202 | int run_entry(void* entry, size_t stack_bytes) { |