Skip to content

Runtime: GAME: paths through the host msvcp140's CRT opens (RT-358) - #5010

Open
TimIdle wants to merge 3 commits into
ItsNotRexed:mainfrom
TimIdle:rt-358-host-msvcp-paths
Open

TimIdle wants to merge 3 commits into
ItsNotRexed:mainfrom
TimIdle:rt-358-host-msvcp-paths

Conversation

@TimIdle

@TimIdle TimIdle commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: RT-358 (#4933 (comment))

Depends on: #4996 (this branch is stacked on it; its own changes are commits e754df6 and ce48467)

What and why

A mapped title's std::fstream opens through the host msvcp140.dll: basic_filebuf::open calls std::_Fiopen there, _Fiopen calls _wfsopen, and the Windows loader binds that import to the host ucrtbase. So the CRT shims of #4996 never saw a GAME: path and the open failed.

The loader already patches a host CRT copy's RtlPcToFileHeader import when it binds one (redirect_pc_to_file_header). That walk is now patch_host_imports: the same pass also points the module's CRT path imports at the shim registered under that import's own DLL and name (rt::shims::find), when there is one. The list is exactly what dumpbin shows the host CRT copies import: only msvcp140.dll has any, and only _fsopen, _wfsopen, _wremove, _wrename and _wrmdir. Host paths still go through the shim to ucrtbase unchanged. gta6rt links the static CRT and does not use msvcp140 itself, so only mapped code reaches the patched module. docs/runtime_imports.md says so in the RT-354 section.

The last three have no shim until #5027 (RT-359) lands, so until then they keep the host's; no follow-up PR is needed. With this branch and #5027 merged locally, msvcp140's own _Rename, _Unlink and _Remove_dir work on GAME: paths (a scratch probe, not committed). The two PRs conflict only in docs/runtime_imports.md (both rewrite the RT-354 bullet's "Not covered" sentence); whichever lands second gets a one-line rebase.

Not covered: std::filesystem, which msvcp140 runs on kernel32 (CreateFile2, FindFirstFileExW, GetFileAttributesExW, ...) and which deletes through a handle; the doc says so and it is posted as a finding.

Tested

  • scripts\build_runtime.ps1 (MSVC 14.44, VS Build Tools 2022), RelWithDebInfo.
  • New loader_test check: maps an image importing msvcp140.dll!?_Fiopen@std@@YAPEAU_iobuf@@PEB_WHH@Z, then the host msvcp140's _Fiopen reads a package file through GAME:\data\... and writes game:/data/written.txt into the overlay (not the package). Before the image is mapped, the same call fails. 87 checks, 0 failed.
  • Negative control: with the CRT-name match disabled, the new check fails ("std::_Fiopen could not open GAME:\data\fiopen.txt").
  • Full ctest: 100% passed, 66 of 66 (the same as main on this PC; no Vulkan SDK here, so the Vulkan tests are not built).
  • tests/test_docs_links.py, tests/test_text_encoding.py: OK.
  • Windows 11 (build 26300), NVIDIA RTX 2050 (driver 32.0.16.2124). CPU-only change; no game files used.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (not a translator change)

🤖 Generated with Claude Code

TimIdle and others added 2 commits November 23, 2026 15:17
A package DLL linked against the dynamic CRT opens files with fopen or _open
from api-ms-win-crt-stdio-l1-1-0, and ucrtbase's own CreateFileW is the
host's, so a GAME: path reached it as a relative name and the open failed.

The fopen, _fsopen, fopen_s, freopen(_s) and _open, _sopen(_s),
_sopen_dispatch families, narrow and wide, are now shimmed under that api set
and ucrtbase.dll. A console path is mapped with open_target, the CreateFile
rules, using the access and disposition the CRT itself derives from the mode
or the _O_ flags, and ucrtbase's wide function opens the host path, so the
FILE* or descriptor belongs to the CRT the caller reads it with (gta6rt links
the static CRT). Host paths go to ucrtbase unchanged.

Not covered here: the host msvcp140.dll that std::fstream goes through, and
the CRT's other path calls (remove, rename, _stat, _access, _mkdir).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A mapped title's std::fstream opens through the host msvcp140.dll:
basic_filebuf::open calls std::_Fiopen there, which calls _wfsopen, and the
Windows loader binds that import to the host ucrtbase, so the CRT shims of
RT-354 never saw a GAME: path and the open failed.

The loader already patches a host CRT copy's RtlPcToFileHeader import when it
binds one (redirect_pc_to_file_header). That walk is now patch_host_imports:
the same pass also points the module's imports of the CRT opens (an explicit
list) at the shim registered under the import's own DLL and name, when there
is one. Host paths go through the shim to ucrtbase unchanged. gta6rt links
the static CRT and does not use msvcp140 itself, so only mapped code reaches
the patched module.

loader_test maps an image importing msvcp140.dll!std::_Fiopen, then reads a
package file and writes an overlay file through GAME: with the host
msvcp140's _Fiopen; before the image is mapped the same call fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Atomic258

Copy link
Copy Markdown
Contributor

Independent check of e754df65 — pass on Windows; companion piece to #4996 (UCRT opens), covering the msvcp140 path the game actually uses.

Environment: Windows 11, MSVC 19.44 (RelWithDebInfo, Ninja), CPU only, synthetic dirs, no game files.

  • file_system_test.exe on the PR head → 171 checks, 0 failures — GAME: opens routed through host msvcp140's CRT entry points resolve to the same scratch/overlay rules, ENOENT/EINVAL errno paths intact, GAME:\.. refused
  • Negative control: same binary against main's file_system.cpp → segfaults at the CRT section — the routed opens don't exist on the parent

The pe_loader.cpp half (resolving msvcp140's import chain to the shim) is exercised by the same test's registry checks. Together with #4996 this closes the CRT-open gap for both CRT flavors.

AI-assisted (Devin).

@AKA0GHOST

Copy link
Copy Markdown

Tested e754df6 on Windows 11, NVIDIA GeForce RTX 5070 (driver 621.14), MSVC 19.44 (VS 2022 17.14), Vulkan SDK 1.4.363, scripts\build_runtime.ps1 RelWithDebInfo: pass.

ctest -R "loader|file_system": 5/5 passed (loader, pe_loader, pe_loader_bounds, loader_tls, file_system).

Full ctest -j 4 with the Vulkan SDK installed, so the Vulkan/D3D12 tests that weren't built in the PR's run are included: 169/169 passed, 0 failed. Not run (skipped/disabled): shader_library_corpus, bink_decode, bink_play_headless, bink_audio, fsr_dispatch, bvh_builder, vk_sync_validation, d3d12_rgb9e5_rtv, d3d12_first_light, gameinput.

No game files. AI-assisted (Claude).

dumpbin over every module prefer_host_copy names: only msvcp140.dll imports
CRT path calls, and only _fsopen, _wfsopen, _wremove, _wrename and _wrmdir.
The list is those five; the last three bind once their shims exist (RT-359).
The doc notes that std::filesystem, which msvcp140 runs on kernel32 and
which deletes through a handle, is not covered by this list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TimIdle

TimIdle commented Nov 23, 2026

Copy link
Copy Markdown
Author

Thanks @Atomic258 and @AKA0GHOST. The head moved after your runs: ce48467 (on top of e754df6) trims the routed list to the five CRT path calls dumpbin shows the host CRT copies import (msvcp140.dll: _fsopen, _wfsopen, _wremove, _wrename, _wrmdir); no other code changed. Re-tested ce48467: loader_test 87 checks, 0 failed; ctest 66/66 (no Vulkan SDK here); doc tests OK. With this and #5027 merged locally, msvcp140's _Rename, _Unlink and _Remove_dir also work on GAME: paths. Windows 11 build 26300, RTX 2050 (driver 32.0.16.2124), MSVC 14.44. AI-assisted (Claude Code).

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built and ran at ce48467e8 on Windows 11 (MSVC, RelWithDebInfo):

  • file_system_test -> 171 checks, 0 failures (CRT fopen_s/_wfopen and the path operations on GAME:).
  • loader_test -> 87 checks, 0 failed, including the host msvcp140.dll opens GAME: paths once a mapped image imports it.
  • ctest -R 'pe_loader|loader_tls|file_system' -> 4/4 passed (pe_loader, pe_loader_bounds, loader_tls, file_system; the first run needed the test_pe_loader_bounds target built, then 100%).
  • git diff --check clean.

patch_host_imports now routes the CRT path calls the host CRT copies import (beyond the UCRT set already mapped) through the overlay, and the loader only lists the calls it can patch, so a mapped msvcp140.dll's GAME: opens reach the package instead of the host's literal folder. This is the dependency RD-369 needs; the loader half is verified here and the new CRT entries are covered by the file-system checks. No blocking issue.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested ce48467 merged onto main 3faac55 on an RTX 5090 (driver 616.92), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, RelWithDebInfo:

  • Full ctest: 178/178.
  • file_system and loader: pass 20 of 20 runs each, and pass under LEONIDA_VK_VALIDATION=1 with 0 validation messages.
  • Warnings: none new in the files this PR changes.

AI-assisted (Claude Code).

@airp1ane

Copy link
Copy Markdown
Contributor

Security review (static): inherited untrusted DLL search order for ucrtbase.dll (CWE-427)

(Reviewed at head ce48467e8b.)


Static security review (AI-assisted, read-only diff audit; no code or game files were executed).

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.

6 participants