Skip to content

RT-25: loader TLS on every thread and MSVC C++ exceptions in mapped images - #965

Merged
ItsNotRexed merged 10 commits into
ItsNotRexed:mainfrom
omardev29:rt-loader-tls-eh
Nov 22, 2026
Merged

ItsNotRexed merged 10 commits into
ItsNotRexed:mainfrom
omardev29:rt-loader-tls-eh

Conversation

@omardev29

@omardev29 omardev29 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-25 (#634 (comment))

What and why

Two loader gaps that tiny_game already showed, and that the real executable would hit early:

  1. TLS on other threads. Only the thread that called load() had the mapped image's static TLS. Any other thread read gta6rt_tls_host.dll's block through _tls_index. That includes the thread gta6rt --run starts the entry point on. On main, the opt-in check gives FAIL thread_local starts at its initializer on each thread.
  2. MSVC C++ exceptions. _CxxThrowException puts the throwing image's base in the exception record, from RtlPcToFileHeader. Windows only knows images its own loader mapped, so for a mapped image that base is 0 and no catch matches. On main, the opt-in check ends the run with 0xE06D7363. The package's FidelityFX upscaler and XboxBvhBuilder.dll import _CxxThrowException from vcruntime140.dll.

How

  • TLS. The Windows loader already calls gta6rt_tls_host.dll's DllMain with DLL_THREAD_ATTACH on every new thread, after it builds that thread's TLS array, and with DLL_THREAD_DETACH before it frees it. The helper now passes those events to the loader:
    • On attach, the thread gets its own copy of the template.
    • On detach, the helper's block goes back before Windows frees it.
    • Once initialise() has run the module, the events also run its TLS callbacks, with the copy in place. Windows orders it the same way: no thread events before DLL_PROCESS_ATTACH.
    • An SRW lock orders the owner's publication, initialised and its retirement in ~Loader against the events.
    • A thread that outlives its Loader frees its copy from its own record.
    • ~Loader (RT-71: deliver process detach before unmapping PE modules #1172) clears initialised before the process detach; that write goes through the same lock, so from then on the thread events stop running the module's callbacks.
    • install_tls_for_thread is still there for threads that existed before load().
  • C++ exceptions. When the loader binds a host copy of the MSVC runtime (the prefer_host_copy list), it points that DLL's imports of RtlPcToFileHeader at a version that asks Windows first and then the live Loaders' mapped modules. It does the same for the module an import actually resolves into, since GetProcAddress follows forwarders.
    • In System32, vcruntime140.dll, vcruntime140_1.dll and msvcp140.dll import it (llvm-readobj --coff-imports).
    • Under Wine, the second part is what makes the check pass. Which Wine DLL implements the throw was not checked.
    • The redirect is done once per DLL, before mapped code runs. With no live Loader it answers exactly as Windows does.
  • tiny_game. The opt-in checks now take arguments (tls, eh) instead of LEONIDA_RUN_TLS/LEONIDA_RUN_EH, because scripts/run_wine_tests.py passes a test's command line but not its ENVIRONMENT. CTest runs each check in its own test, run_tiny_game_tls and run_tiny_game_eh, and stdout is unbuffered.

Not covered

  • Mapped DLLs' DllMain still gets no thread events; only the TLS owner's callbacks do.
  • Threads that existed before load() get nothing automatically.
  • An image that links the CRT statically and imports RtlPcToFileHeader itself is not redirected. No measured image here does.
  • bvh_builder_test still ends with 0xC0000409 after device slot 62 on my machine, with and without this PR, so this is not its cause.

Tested

  • Windows 11, MSVC 14.51: ctest 81/81 after rebasing on c2967a5e (over RT-71: deliver process detach before unmapping PE modules #1172). Each commit builds and passes ctest on its own.
  • run_tiny_game_tls: PASS thread_local starts at its initializer on each thread. run_tiny_game_eh: PASS a C++ exception is thrown and caught inside the image.
  • Negative controls (Windows): with only the redirect disabled, the run ends with 0xE06D7363 again; with only the thread events disabled, the thread_local check fails and loader_tls fails.
  • loader_tls: 45 checks, including one DLL_THREAD_ATTACH before the thread's code with the template readable in the callback, one DLL_THREAD_DETACH, and the copy freed when a thread of an initialised module ends after its Loader has unmapped the image, with no callback into it.
  • Wine, on my fork's CI:
    • loader_tls, run_tiny_game_tls and run_tiny_game_eh pass.
    • For one run I made the runner print passing tests' output too (a throwaway change, not in this PR), and it shows both PASS lines and loader_tls_test: OK (45 checks).
    • Before the forwarder commit, run_tiny_game_eh faulted under Wine.
  • With RT-24: FSR's Xbox backend descriptor, the console command-list map, and two runtime fixes (XMem, device slot 67) #864 merged on top, the FidelityFX DLL's throw reaches the handler with its real image base (0x180000000, was 0x0). The DLL doesn't catch it there, so fsr_dispatch_test reports the same stop.

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.)

AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 7e0d3ac on Windows 11, MSVC 14.51: ctest 35/35 on an RTX 4080 SUPER, including the new TLS and C++-exception loader tests. On an Intel UHD 770 it's 34/35, and the one failure is d3d12_vulkan_device's OPTIONS1 comparison against the other GPU's host device. That's unrelated to this PR, and #962 addresses it.

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

Tested 7e0d3ac on NVIDIA GeForce RTX 5070, driver 621.24, Windows 11 Pro (26300), VS 2022 Build Tools, Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1 and LEONIDA_D3D12=vulkan: pass

  • ctest -C RelWithDebInfo: 100% tests passed, 0 tests failed out of 35
  • 39 test programs in the build pass with 0 validation errors
  • Not run (they take an input file, not covered here): vk_overlapping_occlusion_test
  • d3d12_clear_test fails (R8G8B8A8_UNORM/SNORM 0.5 ties and a D16 clear byte differ from WARP) exactly as on main 4a36bb1 here, so not from this PR; #521 addresses it

Implicit Vulkan layers (overlays) were off through an empty VK_IMPLICIT_LAYER_PATH.

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 7e0d3ac on RTX 4090 (driver 621.14), Windows 11 26200, MSVC 19.44: pass (no regression against main).

  • Clean RelWithDebInfo Ninja build of runtime/ (Vulkan SDK 1.4.363.0), then ctest with validation on: 35/35 pass.
  • GPU tests with LEONIDA_VK_VALIDATION=1 and the Khronos validation layer: d3d12_com_test, d3d12_descriptor_test, d3d12_fence_timing_test, d3d12_fence_waiter_test, d3d12_gcl8_test, d3d12_method_variants_test, d3d12_node_mask_test, d3d12_resource_desc_test, d3d12_sampler2_test, d3d12_state_map_test, d3d12_typeless_rtv_test, d3d12_uav_clear_test, d3d12_vulkan_device_test, vk_command_replay_test pass, with 0 validation messages (no VUIDs).
  • d3d12_clear_test fails, the same as on plain main dcd8ecf on this GPU (NVIDIA rounds the 0.5 clear on 8-bit UNORM/SNORM down, Vulkan 7f, WARP 80, plus one depth-format-55 case). This is not caused by this PR.
  • loader_tls_test (new) passes under ctest.

The vk_* harness tests that need translated SPIR-V weren't run. No game files used. AI-assisted run (Claude Code).

@UlteriorMotive00

Copy link
Copy Markdown
Contributor

Tested 7e0d3ac on Windows: pass.

Windows 11 Home 10.0.26300, MSVC 14.44.35207 (the PR was tested with 14.51), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, Ninja RelWithDebInfo (scripts\build_runtime.ps1), LEONIDA_VK_VALIDATION=1, VK_LOADER_LAYERS_DISABLE=~implicit~. Two GPUs on one PC, picked with VK_LOADER_DRIVERS_SELECT: AMD Radeon RDNA2 iGPU (2026.04.17) and RTX 5070 (617.42).

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

@omardev29

Copy link
Copy Markdown
Contributor Author

Rebased on main (c2967a5e). The conflict was #1172: ~Loader now clears initialised through the TLS lock, so the thread events stop running a module's callbacks once the process detach starts, and the thread that outlives its Loader in loader_tls_test now belongs to an initialised module.

The red windows job is d3d12_present, which fails the same way on main at c2967a5e; #908 addresses it. Thanks for testing.

The Windows loader calls gta6rt_tls_host.dll's DllMain with DLL_THREAD_ATTACH
on every new thread, after it has built the thread's TLS array, and with
DLL_THREAD_DETACH before it frees it. A new export,
gta6rt_tls_host_set_thread_event, lets the loader receive those events, so it
can give each thread the mapped image's TLS. DllMain still touches no TLS (the
helper's own slot is the one the loader swaps); the callback pointer is a plain
global. Nothing registers it yet.
Until now only the thread that called load() had the mapped image's TLS; any
other thread read gta6rt_tls_host.dll's block through _tls_index, so a
thread_local on the game's own threads (gta6rt runs the entry point on a new
thread) started from the wrong data: tiny_game's thread_local check fails on
main with LEONIDA_RUN_TLS=1.

The loader now registers for the helper's thread events. On DLL_THREAD_ATTACH
the new thread gets a fresh copy of the template in the reserved slot; on
DLL_THREAD_DETACH the helper's own block goes back before the Windows loader
frees the array. Once initialise() has run the module, the events also run its
TLS callbacks (as Windows does: thread events only after DLL_PROCESS_ATTACH),
with the copy already in place.

An SRW lock orders the owner's publication, its initialised flag and its
retirement in ~Loader against the events, which run under the loader lock; the
exclusive sections only write fields. ~Loader retires the owner before
uninstalling, so a thread that outlives its Loader frees its copy from its own
record without touching the freed Module. Module::tls_block stays the loading
thread's copy, now set by initialize_tls only. install_tls_for_thread remains
for threads that existed before load(); on a thread that already has the copy
it is a no-op, which the updated worker check shows.
…utlives its Loader

tiny_tls counts DLL_THREAD_ATTACH/DETACH only for a thread the test names, so
threads the process starts for itself can't change the counts; the test starts
that thread suspended and names it before it runs. Checks: one attach before the
thread's code runs, with the template value already readable in the callback;
one detach when it ends; and a thread that is still running when its Loader is
destroyed gets its copy freed when it ends, with the process carrying on.
…dress

Answers from the same per-Loader lookups the unimplemented-import report uses,
under the same lock. Used by the next commit.
…ceptions work

_CxxThrowException (vcruntime140.dll) stores the throwing image's base in the
exception record, from RtlPcToFileHeader(pThrowInfo), and the frame handler adds
the throw's RVAs to it. Windows only knows images its own loader mapped, so for
a mapped image the base is 0, no catch matches, and the process ends with
0xE06D7363: tiny_game's LEONIDA_RUN_EH=1 check on main.

When the loader binds a host copy of the MSVC runtime (prefer_host_copy, direct
or through a forwarder), it points that DLL's imports of RtlPcToFileHeader by
name at a version that asks Windows first and then the live Loaders' mapped
modules. vcruntime140.dll, vcruntime140_1.dll and msvcp140.dll in System32
import it (llvm-readobj --coff-imports); the package's FidelityFX upscaler and
XboxBvhBuilder.dll import _CxxThrowException from vcruntime140.dll. The redirect
is done once per DLL, before any mapped code runs, and stays for the process
lifetime; with no live Loader it answers as Windows does.
The thread_local and C++ exception checks were switched on by LEONIDA_RUN_TLS=1
and LEONIDA_RUN_EH=1. scripts/run_wine_tests.py passes a test's command line but
not its ENVIRONMENT property, so under Wine an environment-based test would only
ever see the SKIP path. They now take arguments after "hello 42" (`tls`, `eh`),
and CTest runs each in its own test, run_tiny_game_tls and run_tiny_game_eh.
run_tiny_game keeps running without them. stdout is unbuffered, so a crash still
shows how far the run got.
runtime/README.md: the RtlPcToFileHeader redirect, and the thread events in
place of "the loader does not deliver thread attach/detach events"; mapped
DLLs' DllMain still gets no thread events. design.md: the CPU-code row.
…esolves into

GetProcAddress follows forwarders, so a host import can land in another DLL
than the one named in the import table. The throw calls the RtlPcToFileHeader
import of the module that implements _CxxThrowException, so that module is
redirected too. On Windows it is vcruntime140.dll itself (no change).

Under Wine, without this, run_tiny_game_eh faults inside a Wine DLL after the
throw, reading 0xE080, an offset inside tiny_game's image size (consistent with
a base of 0). With it, the fork's CI run of this branch prints "PASS a C++
exception is thrown and caught inside the image" under Wine. Which Wine DLL
implements _CxxThrowException was not checked.
…e process detach

~Loader (ItsNotRexed#1172) clears initialised before delivering DLL_PROCESS_DETACH; with
the thread events that write goes through the TLS lock, so a thread that ends
during or after the detach no longer runs the module's callbacks.
…itialised module

With initialise() run, the module's TLS callbacks take thread events, so the
thread that ends after ~Loader has delivered the process detach and unmapped
the image checks that no DLL_THREAD_DETACH callback runs into freed memory.
@omardev29

Copy link
Copy Markdown
Contributor Author

The red check job is tests/test_mem_diff.py (buffer_format), which fails the same way on main at bb2e1842 with the same tools/ and tests/ (this PR changes neither); #1396 fixes it. windows is green.

@ItsNotRexed
ItsNotRexed merged commit ddcff1f into ItsNotRexed:main Nov 22, 2026
10 of 11 checks passed
omardev29 added a commit to omardev29/Leonida that referenced this pull request Nov 22, 2026
…tsNotRexed#965

d3d12_command_objects.md 3.5a: slot 67 is DeserializeComputePipelineStateX
with its full desc (kind 0, 1, 6, 7 pointer/size pairs), and the open
"what FSPX holds" line now points at fspx.md (13 of 29 translate).
d3d12_vulkan.md A.3: the same name, and the throw: before ItsNotRexed#965 it carried
an image base of 0; with ItsNotRexed#965 it carries the DLL's base and still reaches
the caller (fsr_dispatch_test, LEONIDA_FSR_NO_STANDIN=1).
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