Skip to content

runtime: a C++/CX title gets its own command line from vccorlib140 (GetCmdArguments), not gta6rt's or none under Wine - #6984

Open
tedin7 wants to merge 2 commits into
ItsNotRexed:mainfrom
tedin7:xv-cx-args
Open

tedin7 wants to merge 2 commits into
ItsNotRexed:mainfrom
tedin7:xv-cx-args

Conversation

@tedin7

@tedin7 tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in #6750 (comment)

@ItsNotRexed: an exe PR, as you asked.

What and why

A C++/CX program's main(Platform::Array<Platform::String^>^) wrapper gets its arguments from vccorlib's
Platform::Details::GetCmdArguments(int* argc). Under gta6rt --run this import gave the guest the wrong answer:

  • On Windows the loader binds the host's vccorlib140.dll (prefer_host_copy). vccorlib reads argv from the host CRT, so the guest gets gta6rt's arguments.
  • Under Wine there is no vccorlib140.dll, so the import is unimplemented and returns 0. A C++/CX main would then read a null array.

The fix serves ?GetCmdArguments@Details@Platform@@YAPEAPEA_WPEAH@Z from runtime/shims/process.cpp. It returns the same wide argv and argc as the guest's __p___wargv / __p___argc.

Serving one vccorlib name must not make the shims "own" the DLL. Otherwise a run-time LoadLibrary("vccorlib140.dll") would get a shim pseudo-handle, and GetProcAddress on it would fail for every other vccorlib export. So owns_dll (runtime/shims/registry.cpp) now lists vccorlib140.dll / vccorlib140d.dll with the other host DLLs, of which only a few functions are replaced.

Where the fact comes from (and what we checked ourselves)

  • The idea that titles read their arguments through this export comes from sammwyy/xvalkyrie, src/runtime/common/execute.cpp:206-211 at commit 53af7df77c10 (GPL-3.0). It is reimplemented here in Leonida's process shim. No code was copied.
  • We checked the export name ourselves against vccorlib.lib of MSVC 14.44 (llvm-nm: __imp_?GetCmdArguments@Details@Platform@@YAPEAPEA_WPEAH@Z). vccorlib.h does not declare it.
  • We measured the before state under Wine 9.0: the import was listed as UNIMPLEMENTED vccorlib140.DLL!?GetCmdArguments... and returned null with argc untouched.
  • We do not know whether the retail exe imports it. RT-5: the retail executable maps under gta6rt --dry-run, with 30 unimplemented imports in seven Xbox-only DLLs #6715 counts 432 host imports but does not list them. The package ships vccorlib140.dll (docs/runtime_imports.md), so C++/CX code in the title or its DLLs is plausible. The fix costs nothing if no one calls it.
  • Where we differ from xvalkyrie: none on this fact. We also keep the DLL un-owned, so a run-time load of vccorlib still reaches the host copy. xvalkyrie has no such registry to worry about.

Tests

  • New fixture runtime/test/fixtures/tiny_cx_args.cpp. It imports GetCmdArguments and checks it against the CRT's __wargv and the expected hello / two words. It also does a run-time LoadLibraryW("vccorlib140.dll").
  • New ctest run_cx_args (runtime/test/check_cx_args.cmake). It runs the fixture through gta6rt --run, and it fails if the loader logs that vccorlib140.dll is served by the shims.
  • Cross-built with clang-cl 19 + xwin (MSVC 14.44), run under Wine 9.0 (Ubuntu 24.04):
    • on origin/main 1cc6e3c: run_cx_args fails (GetCmdArguments argc=-1, FAIL);
    • with the shim but without the owns_dll line: fails on the negative check (vccorlib140.dll is served by the shims (run-time load));
    • with this PR: passes. run_startup_cmdline, run_tiny_gdk_load, run_tiny_bundled_load, winhttp_offline and xgameruntime also pass.
  • Not run on native Windows.

Coordination: process.cpp is also touched by #6410 and #6419 (kacper-serewis). This PR does not change their lines (the includes and game_thread): it adds one function after the WinMain command-line helpers and two table entries.

AI-assisted (Claude Code). No game files used.

A C++/CX main() reads its arguments through vccorlib140.dll's
Platform::Details::GetCmdArguments. The host copy answered with gta6rt's
argv, and Wine has no vccorlib140.dll, so the import was unimplemented.
Serve it from the process shim with the game's wide argv, and keep
vccorlib140.dll un-owned so a run-time LoadLibrary still reaches the host.

Fact from sammwyy/xvalkyrie execute.cpp:206 at 53af7df77c10 (GPL-3.0),
reimplemented. Test: run_cx_args (tiny_cx_args fixture) under Wine.
Microsoft's x64 vccorlib140.dll exports GetCmdArguments as ordinal 130, and
GetProcAddress by that ordinal, or a static or forwarded import by it,
skipped the shim and reached the host's argv. The registry is keyed by name
because ordinals change between builds of a host DLL, so an ordinal lookup
now compares the host's address with the host's address for each name the
registry serves for that DLL (shims::find_host_export), in rt::Loader's
resolve() and resolve_forwarder() and in the GetProcAddress shim.

The test now uses a stand-in host vccorlib140.dll (Wine has none) and fails
when it cannot be loaded, instead of accepting a failed LoadLibrary. It checks
the import by name, GetProcAddress by name and by ordinal, a package DLL's
import by ordinal only, that a bare-name LoadLibrary returns the host module,
and that an export the shims don't serve is still the host's.

Review of ItsNotRexed#6984.
@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All three findings were reproduced. Two are fixed in d0aad69; the third is the subject of an existing open PR.

1. Ordinal lookups skipped the shim. Fixed.
I did not add an #130 entry: an ordinal only names an export in one build of a host DLL, and a fixed ordinal could catch a different function in another vccorlib build. Instead, shims::find_host_export (runtime/shims/registry.cpp) takes the address the host's GetProcAddress returned for the ordinal. It compares that address with the host's address for each name the registry serves for that DLL, and returns the shim on a match. This is used in three places:

  • Loader::resolve (static ordinal imports);
  • Loader::resolve_forwarder (forwarders by ordinal);
  • the GetProcAddress shim when the name is an ordinal (IS_INTRESOURCE).

2. Stale handle cache after FreeLibrary. Not changed here.
The cache bug predates this PR. Open #4861 fixes it the same way you suggest: free_library evicts the entry and bumps an epoch. Doing it here too would duplicate #4861 and conflict with it. I agree this PR makes the stale entry more visible, because a reused handle can now resolve to the vccorlib shim. If you'd rather not merge this before #4861, I'm fine marking it "Depends on #4861".

3. The test accepted a failed LoadLibrary. Fixed.
The test now loads a stand-in host vccorlib140.dll (cx_vccorlib_stub, outside the game folder; Wine has no vccorlib140). It exports GetCmdArguments by name and as ordinal 130, answering "host-copy", plus one export the shims don't serve. The fixture fails if the stand-in does not load. It checks each of these:

  • the guest's own import, by name;
  • GetProcAddress by name;
  • GetProcAddress by ordinal;
  • a package DLL (cx_ordinal_user) whose import is by ordinal only (NONAME import library), bound by rt::Loader at run time;
  • a bare-name LoadLibraryW("vccorlib140.dll") returns the host module, not a pseudo-handle;
  • the unserved export is still the host's.

Checks, on Wine 9.0, clang-cl 19 + xwin (MSVC 14.44):

  • New test with the previous runtime code (7f2a48e): run_cx_args fails on exactly two checks, "GetProcAddress by ordinal" and "a package DLL's import by ordinal".
  • With d0aad69: run_cx_args passes.
  • With d0aad69 and the stand-in's path pointed at a missing file (the injected load failure): fails with "the stand-in vccorlib140.dll loads by full path". The old fixture passed in that case.
  • Also passing: loader, pe_loader_bounds, loader_tls, run_startup_cmdline, run_tiny_gdk_load, run_tiny_bundled_load, winhttp_offline, xgameruntime.
  • Not run on native Windows.

@thehollowedone

Copy link
Copy Markdown

Independent native Windows check of d0aad69c, MSVC 19.44 and Windows SDK 10.0.26100. Both stock tests, run_cx_args and run_startup_cmdline, passed.

The named and ordinal lookups returned the guest arguments. The fixture reused its locally built vccorlib stand-in for an unrelated export, which returned the expected value. Two additional runs preserved a single Unicode argument containing a space through both startup and C++/CX argument lookup. The test used the synthetic stand-in throughout.

AI-assisted.

@dea7hsmiless

Copy link
Copy Markdown
Contributor

Tested d0aad69c542d222259201a92ecdd601ce442e9fd on Windows 11 (build 26300.9974), MSVC 19.44.35229, RelWithDebInfo + Ninja: pass.

  • Build clean; run_cx_args (CTest Runtime: add a README for the loader, shims and tests #87) passes.
  • Negative control: with runtime/shims/process.cpp from the pre-PR base (HEAD~2) and the rest of the PR kept, run_cx_args fails on the shim-dependent paths: FAIL import by name, FAIL GetProcAddress by name, FAIL GetProcAddress by ordinal, FAIL a package DLL's import by ordinal. Restoring the PR's process.cpp and rebuilding passes again.
  • Code read: find_host_export compares the host address returned for an ordinal with the host address of each name the registry serves, so no fixed ordinal is baked in; owns_dll still lists vccorlib as host-owned, so other exports remain the stand-in's. No concerns.
  • This complements thehollowedone's pass (both stock tests and Unicode arguments) with the negative control.

Synthetic stand-in only; no game files or GPU. AI-assisted (OpenCode).

@quackottu

Copy link
Copy Markdown
Contributor

Tested d0aad69c against the retail engine exe (1.0.2.1): merged onto main d2c2ab778, Windows 11, MSVC 19.44, RelWithDebInfo. These results do not depend on the boot. The full gta6rt --run boot test is on hold until the game copy here is complete.

  • run_cx_args (ctest): 1/1 passed.
  • gta6rt --dry-run on the exe: same as main, with 5 modules mapped and 29 unimplemented imports. Neither the exe nor the four modules it maps import vccorlib140.dll (no GetCmdArguments binding before or after), so this change doesn't reach the retail boot path. It still matters for a C++/CX title, as the description says, but the retail exe doesn't exercise it.

@moistman7

Copy link
Copy Markdown

Tested d0aad69c merged onto main 4da521de (clean) on native Windows 11 26200, MSVC 2022 Build Tools, RelWithDebInfo, with the host C:\Windows\System32\vccorlib140.dll present: pass. This covers the native case the PR lists as not run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants