Skip to content

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

Closed
dracbat wants to merge 6 commits into
ProjectLeonida:mainfrom
dracbat:rt-pc-to-file-header
Closed

dracbat wants to merge 6 commits into
ProjectLeonida: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)

Updated 23 Nov: #1786 has merged, and main (3faac55) is merged into this branch with the conflicts resolved as the reviews asked, so the diff against main is only this PR.

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, msvcp140.dll, vcruntime140.dll and vcruntime140_1.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, regenerated on the merged branch. XCurl.dll, msvcp140.dll, vcruntime140.dll and vcruntime140_1.dll each move one import, this one, from host to shim. The table's other changed rows were already stale on main, which serves shims it never counted (the WinHTTP offline functions, more file-system calls and UCRT runtime exports).

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 on the merged branch (main 3faac55), without the three window tests: 176 run, 167 pass, 9 skip, 0 fail. The skips need a variable or a feature this machine doesn't have (package, movie, corpus, validation layer, first-light folder, RGB9E5 render targets). Before the merge it was 102/103; the one failure, d3d12_draw, came from AMD's warm pipeline cache (finding), and that test passes here now.
  • 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

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.

Both sides added code at the same places, so both are kept:
tiny_game_static next to main's tiny_game_fixed and run_tiny_game_dir
(runtime/CMakeLists.txt), test_pc_to_file_header next to
test_inherited_reservation (runtime/test/pe_loader_test.cpp), and the
RtlPcToFileHeader shim table next to cleanup_tls_clones
(runtime/src/pe_loader.cpp). docs/runtime_imports.md takes main's table
here; a later commit regenerates it from the dry-run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Regenerated from gta6rt --dry-run on the 16 package DLLs (1.0.2.1) with
this branch's build. Four rows change because of this PR: XCurl,
msvcp140, vcruntime140 and vcruntime140_1 each move RtlPcToFileHeader
(from api-ms-win-core-rtlsupport-l1-1-0) from host to shim. The other
changed rows were already stale on main, which serves shims the table
never counted (the WinHTTP offline functions, more file-system calls and
UCRT runtime exports). The comment above the shim table now names
vcruntime140_1.dll too.

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

dracbat commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@tupacfan04 @Flyander thanks for the reviews. I merged main (3faac55) into the branch (merge 25e6f43, then 690d6c1 for the docs table) instead of rebasing, so there's no force-push and the approved commit is unchanged. The conflicts are resolved the way you described:

  • runtime/CMakeLists.txt: main's tiny_game_fixed and run_tiny_game_dir next to tiny_game_static and run_tiny_game_static_eh.
  • runtime/test/pe_loader_test.cpp: both calls.
  • runtime/src/pe_loader.cpp: main's file (its startup_trace.h/base_reservation.h includes and cleanup_tls_clones) with this PR's hunks on top.
  • docs/runtime_imports.md: main's text plus this PR's paragraph, with the table regenerated (below).

Tested on the merged branch: Windows 11, AMD Radeon RX 5700 (driver 32.0.21045.29003, Adrenalin 26.11.2, Vulkan 1.4.315), MSVC 14.44, Vulkan SDK 1.4.363.1, CMake 4.4.5 and Ninja 1.13.2, RelWithDebInfo.

  • pe_loader, pe_loader_bounds, process_cmdline, run_tiny_game, run_tiny_game_exit_code, run_tiny_game_tls, run_tiny_game_eh, run_tiny_game_static_eh and run_tiny_game_dir: 9/9 pass. run_tiny_game_dir failed once in seven runs: gta6rt's last line was cut off after game exited, with exit code 0. It passed both reruns on its own, and this PR doesn't touch that path.
  • Full runtime ctest without the three window tests (d3d12_present, vk_presenter_sync, xbox_present): 176 run, 167 pass, 0 fail, 9 skip. Eight skip because a variable isn't set (package, movie, corpus, validation layer, first-light folder), and d3d12_rgb9e5_rtv skips because this GPU has no RGB9E5 render targets.
  • Negative control: with only SHIM_TABLE(pc_to_file_header, ...) removed, pe_loader fails its 5 name checks and run_tiny_game_static_eh fails (the throw ends as an unhandled C++ exception, 0xE06D7363), while run_tiny_game_eh still passes. With the file restored, all 9 pass again.
  • gta6rt --dry-run on the 16 DLLs of my own 1.0.2.1 package (counts only): every DLL maps at 0x180000000 with no relocation and 0 unimplemented imports, and the Xbox-specific imports are the same six (eight pairs). I regenerated the table from it. Against main's table, every row except vcomp140.dll changes:
    • XCurl.dll, msvcp140.dll, vcruntime140.dll and vcruntime140_1.dll each move one import from host to shim: RtlPcToFileHeader from api-ms-win-core-rtlsupport-l1-1-0. That's this PR.
    • 13 rows were already stale on main. They didn't count shims main serves: the WinHTTP offline functions, more file-system calls and UCRT runtime exports. A dry-run of the same build without this PR's shim table differs from the new table only by that one import in those four rows.

The comment above the shim table now names vcruntime140_1.dll too, since it imports the function from the same set.

AI-assisted (Claude Code).

@thehollowedone

Copy link
Copy Markdown

Independent native Windows check of 690d6c1691ed8ec07b105f67960a1b90a9b0a00e on 3faac557. Windows 11 x64 build 26300, MSVC 19.44.35230, Windows SDK 10.0.26100.0, RelWithDebInfo, CPU only.

Built gta6rt, tiny_game and tiny_game_static from the current PR. The two stock CTests, run_tiny_game_eh and run_tiny_game_static_eh, passed (2/2). Both printed that the C++ exception was thrown and caught inside the mapped image and exited 0. The static CRT run bound KERNEL32.dll!RtlPcToFileHeader to the shim.

Focused exception tests only, using synthetic fixtures. AI-assisted.

dracbat and others added 2 commits November 23, 2026 12:15
The only conflict is docs/runtime_imports.md: main's table (with its new
--init column) and paragraphs are kept, plus this branch's
RtlPcToFileHeader paragraph. The table's counts are regenerated from the
dry-run in the next commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…561b65a)

Regenerated with this branch's gta6rt --dry-run and --init on the 16
package DLLs (1.0.2.1), in main's new table format with the --init
column. Every --init exits 0. Four rows move RtlPcToFileHeader from host
to shim (this PR); the other changed rows count shims main already
serves that its table did not.

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

dracbat commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Updated: main (561b65a) is merged in again, still with no force-push, as afec3ac2 plus the docs commit.

Thanks @Roberto0953 for the rebase branch and @thehollowedone for the check. Your conflict notes matched what I found.

  • The only conflict was docs/runtime_imports.md. I kept main's table format (with its new --init column) and its paragraphs, plus this PR's RtlPcToFileHeader paragraph.
  • The counts are from a fresh gta6rt --dry-run and --init on the 16 package DLLs of my own 1.0.2.1 copy with this build, not summed by hand. Every --init exits 0.
    • Four rows move RtlPcToFileHeader from host to shim: XCurl.dll, msvcp140.dll, vcruntime140.dll and vcruntime140_1.dll. That's this PR.
    • The other changed rows count shims main already serves that its table doesn't (for example XCurl.dll is 27/113 measured, against 24/116 in main's table).
  • Tests (Windows 11, RX 5700, MSVC 14.44): pe_loader, pe_loader_bounds, process_cmdline, run_tiny_game, run_tiny_game_exit_code, run_tiny_game_tls, run_tiny_game_eh, run_tiny_game_static_eh and run_tiny_game_dir: 9/9 pass.
  • Unrelated failures in the full suite: six D3D12 device tests fail on this build: d3d12_texture_tiles, d3d12_indirect_dispatch_count, d3d12_pipeline_library_host, d3d12_m2_textured, d3d12_m2_formats and d3d12_m3_frame. This PR touches no D3D12 code, and the same tests passed on main 3faac55 on this GPU, so they came in with main since then. I'm looking at them separately.

AI-assisted (Claude Code).

@searyecheesepeaceful

Copy link
Copy Markdown

Tested 62f15fd on Intel UHD 770, driver 32.0.101.7088, MSVC 19.44: gta6rt, tiny_game_static and pe_loader_test build, and ctest passes pe_loader (#15, "pe_loader_test: OK (88 checks)", including "PASS kernel32.dll!RtlPcToFileHeader is served by the loader", "PASS an address in the mapped image gives the image's base" and "PASS after the Loader is gone the address is no image's") and run_tiny_game_static_eh (#100, "tiny_game: PASS a C++ exception is thrown and caught inside the image" and "game exited with code 0"). Neither test opens a Vulkan device. pe_loader_bounds and the full ctest suite were not run, and I did not run main's pe_loader.cpp, so this does not show the new checks failing without the change.

AI-assisted.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested 62f15fd merged onto main 561b65a on an RTX 5090 (driver 616.92), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, RelWithDebInfo:

  • Full ctest: 244/247. The failures main 561b65a also has without this PR: d3d12_bindless_churn_validated, d3d12_indirect_dispatch_count, d3d12_indirect_rt.
  • pe_loader: passes 20 of 20 runs, and passes under LEONIDA_VK_VALIDATION=1 with 0 validation messages.
  • Warnings: none new in the files this PR changes.

AI-assisted (Claude Code).

runtime/CMakeLists.txt keeps this branch's run_tiny_game_static_eh next to main's gta6rt_stack and run_tiny_game_stack.

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

dracbat commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Merged the latest main (8575d36) in again, with no force-push. The one conflict was runtime/CMakeLists.txt, where this PR's run_tiny_game_static_eh sits next to main's new gta6rt_stack and run_tiny_game_stack, so I kept all three. main still binds a mapped image's own RtlPcToFileHeader import to Windows, with no fallback when the host pointer is unset, so this PR still applies. On an RX 5700 (Windows 11, MSVC 14.44), pe_loader, pe_loader_bounds, process_cmdline, every run_tiny_game* and gta6rt_stack pass 15/15.

AI-assisted (Claude Code).

@LaugeM LaugeM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This should go in first of the module identity PRs (#2075 carries it as a merge, so #2075 shrinks once it lands).

On upstream/main 5d74c27 the code still merges cleanly. git merge-tree reports one conflict, in docs/runtime_imports.md, the per-DLL table. runtime/CMakeLists.txt and runtime/src/pe_loader.cpp auto-merge, b7a3fed's /GA TLS change included.

  • Main still binds a mapped image's own RtlPcToFileHeader import to Windows. The only redirect is redirect_pc_to_file_header for host CRT copies (runtime/src/pe_loader.cpp around 849-890 on main), and pc_to_file_header calls g_host_pc_to_file_header.load() with no null check, so the fallback to ntdll's export in host_pc_to_file_header() is needed as well as the table.
  • The five names in the new SHIM_TABLE(pc_to_file_header, ...) don't interact with #5731's "shim-owned modules skip the host" change. kernel32, kernelbase and ntdll are in owns_dll's host list, and api-ms-* is excluded by prefix (runtime/shims/registry.cpp:487-496). So unshimmed imports from those DLLs still bind to the host whichever order the two land in.
  • The table conflict has come back on every main merge so far, because most of its changed rows count shims main already serves, not this PR. One way to stop that: keep only the new paragraph, and have whoever next regenerates the table pick up the four RtlPcToFileHeader moves.

AI-assisted review (Opus 5.5). Read the diff against main; not built (merge-tree against upstream/main only).

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.

8 participants