Skip to content

Runtime: RtlPcToFileHeader for a mapped image's own imports (static-CRT C++ exceptions) - #1839

Open
dracbat wants to merge 3 commits into
ItsNotRexed:mainfrom
dracbat:rt-pc-to-file-header
Open

dracbat wants to merge 3 commits into
ItsNotRexed:mainfrom
dracbat:rt-pc-to-file-header

Conversation

@dracbat

@dracbat dracbat commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #1548 (comment) (second new RT item)

Depends on: #1786. This is stacked on it because both regenerate the same rows of the docs/runtime_imports.md table; only the last commit is this PR's.

What and why

_CxxThrowException puts the throwing image's base in the exception record, taken from RtlPcToFileHeader(pThrowInfo), and the frame handler adds the ThrowInfo's RVAs to it. Windows' RtlPcToFileHeader only knows images the Windows loader mapped, so for ours it returns NULL. The loader already redirects that import in the host copies of the MSVC runtime (redirect_pc_to_file_header), which covers a /MD executable throwing through the host vcruntime140.

A mapped image that imports RtlPcToFileHeader itself still bound it to Windows. That covers a static-CRT (/MT) executable, whose _CxxThrowException is linked into the image, and any crash or stack-walk helper that calls it directly. Such an executable's first C++ throw recorded image base 0 and was never caught. The package's own XCurl.dll, vcruntime140.dll and msvcp140.dll import it from api-ms-win-core-rtlsupport-l1-1-0.dll, the set OneCore_apiset.lib/mincore.lib bind it to, so the executable may well do the same.

The change (runtime/src/pe_loader.cpp):

  • A shim table serves pc_to_file_header for RtlPcToFileHeader under kernel32.dll, kernelbase.dll, ntdll.dll and api-ms-win-core-rtlsupport-l1-1-0/-l1-2-0. owns_dll already treats all of these as host DLLs, so GetModuleHandle for them is unchanged.
  • pc_to_file_header called g_host_pc_to_file_header without a null check, and that pointer is only set once a host copy has been redirected. It now falls back to ntdll's export on first use.
  • docs/runtime_imports.md: a paragraph, and the dry-run table. Against Runtime: the game's command line and ExitProcess under every import name (WinMain line, kernelbase, API sets) #1786, XCurl.dll, msvcp140.dll, vcruntime140.dll and vcruntime140_1.dll each move one import, this one, from host to shim.

Tested

Windows 11, AMD Radeon RX 5700 (Adrenalin 26.11.2), MSVC 14.44, Ninja RelWithDebInfo, Vulkan SDK 1.4.363.1:

  • pe_loader_test's new test_pc_to_file_header runs before any host CRT is redirected. It checks:
    • every name hands out the same function;
    • an address in the test executable gets Windows' answer;
    • Windows' own function returns NULL for the mapped tiny_dll, while ours returns its base;
    • after the Loader is gone, that address gets NULL.
  • New tiny_game_static (the stand-in game linked with the static CRT) and run_tiny_game_static_eh, which run its C++ exception check under gta6rt --run.
  • Negative control: with main's pe_loader.cpp, pe_loader fails its 5 new name checks and run_tiny_game_static_eh fails, because the game can't catch its own exception. With this branch, both pass, and so does run_tiny_game_eh.
  • Full runtime ctest: 102/103 pass. The exception is d3d12_draw, whose bundle-blend check fails on this machine on main too: the same binary passed earlier today and fails every run since (finding). It's unrelated to this change.
  • gta6rt --dry-run on the 16 package DLLs of my own 1.0.2.1 copy: every import binds, 0 unimplemented, with the counts in the table.

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. Only import names and counts.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (N/A: no translator change.)

🤖 Generated with Claude Code

dracbat and others added 3 commits November 22, 2026 14:05
…port name

The UCRT's _get_narrow_winmain_command_line and _get_wide_winmain_command_line
were not shimmed, so a /MD WinMain executable got gta6rt's own command line as
lpCmdLine. GetCommandLineA/W and ExitProcess were shimmed under kernel32.dll
only, and the loader matches DLL names exactly. Imported from kernelbase.dll, or
from the API sets the umbrella libraries bind them to
(api-ms-win-core-processenvironment-l1-1-0, api-ms-win-core-processthreads-l1-1-0),
they went to the host. The package's vcomp140.dll imports ExitProcess from the
latter.

process_cmdline_test checks every name and the WinMain line.
docs/runtime_imports.md: the dry-run table regenerated at this commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…g it

Without the shim, find() returns null and the WinMain check called it, ending
the test with an access violation instead of a failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Windows' RtlPcToFileHeader returns NULL for an image it didn't map. The loader
redirected only the host copies of the MSVC runtime, so a mapped image that
imports it itself, such as a static-CRT _CxxThrowException or a crash or
stack-walk helper, got NULL. A static-CRT C++ throw then records image base 0,
and its catch never matches.

Serve RtlPcToFileHeader under kernel32, kernelbase, ntdll and
api-ms-win-core-rtlsupport-l1-1-0/-l1-2-0. The package's XCurl.dll,
vcruntime140.dll and msvcp140.dll import it from the l1-1-0 set. Look the
Windows function up on first use when no host copy has been redirected yet.

pe_loader_test checks every name and the answers. tiny_game_static, the
stand-in game linked with the static CRT, runs its C++ exception check under
gta6rt. docs/runtime_imports.md: the dry-run table at this commit.

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

@Flyander Flyander 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.

Re-reviewed current head. The shim preserves the documented RtlPcToFileHeader contract: it uses Windows for loader-mapped images, adds the base for custom-mapped images, and returns NULL after the custom loader releases them. The alias table covers the package imports identified in the report, and the static-CRT exception test exercises the failure mode from #1786. The GitHub checks are still pending; the reported Windows/Vulkan test run is meaningful coverage. I found no blocker in this diff.

@tupacfan04

Copy link
Copy Markdown
Contributor

Tests pass but it cannot merge as-is.

Built clean (VS 18/MSVC 19.51, Ninja): pe_loader, pe_loader_bounds, process_cmdline, run_tiny_game{,_exit_code,_tls,_eh} and run_tiny_game_static_eh all pass 8/8; compileall and test_docs_links clean. Negative control: reverting only the pe_loader.cpp hunks fails pe_loader and run_tiny_game_static_eh, matching the description.

Conflicts: runtime/CMakeLists.txt:424-444 (main's tiny_game_fixed vs this PR's tiny_game_static - keep both and one closing paren) and runtime/test/pe_loader_test.cpp:755-759 (keep both calls). The branch also predates main's startup_trace.h/base_reservation.h includes, so taking its pe_loader.cpp wholesale breaks the build - rebase and keep both sides.

The docs/runtime_imports.md:48-55 count shifts need the game package, so they are unverified here.

AI-assisted review, no game files, no GPU.

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.

3 participants