Skip to content

process: cover the game's thread-local exe-atexit callback - #2354

Closed
ERROR890 wants to merge 2 commits into
ProjectLeonida:mainfrom
ERROR890:fix/thread-local-atexit
Closed

ERROR890 wants to merge 2 commits into
ProjectLeonida:mainfrom
ERROR890:fix/thread-local-atexit

Conversation

@ERROR890

Copy link
Copy Markdown

Claimed in the coordination issue as: BOOT-TLS-ATEXIT-CALLBACK
(#1608 (comment))

Depends on: none.

Fixes: none (no issue tracks this).

What and why

shims/process.cpp answers the retail title's process-level imports (its command line, exit/abort,
_crt_atexit, ...) and its header lists what is still missing: "Not covered yet: at_quick_exit() handlers ...
and the thread-local destructor callback (_register_thread_local_exe_atexit_callback); both still reach the host
CRT."
This covers the second one.

It is also where the title stopped on a73a57e1. Running the 1.0.2.1 package through gta6rt --run there, the
process ended with 0xC0000409 in ucrtbase after the static initialisers and before any D3D12 call. An int3
planted on ucrtbase!terminate fired first, and the return address on the stack was inside
_register_thread_local_exe_atexit_callback (the call terminate of its cold path): the UCRT's own consistency
check failed in this process and its CRT ended the run. The fast-fail exception information was 0x7
(FAST_FAIL_RANGE_CHECK_FAILURE), the code that path raises. The runtime's own shim header already named the gap.

On current main (fbf100b1b) that stop is already gone - it was cleared by the TLS/thread work that landed after
a73a57e1 (#2195 and neighbours) - so please read this as the coverage the header promises plus its test, not as a
crash fix.

What changes:

  • the shim keeps the callback per thread and returns the previous one, as the UCRT does;
  • the loader runs it in the DLL_THREAD_DETACH branch of on_host_thread_event, after the DLL slots' callbacks and
    the executable's own, which is where the UCRT would run it; a throw is caught so the Windows loader lock is never
    left held;
  • process_cmdline_test gains a case that fails on main (there is no such shim) and passes with this change.

Tested

  • process_cmdline_test (cmake --build <build> --target process_cmdline_test) built and run on main plus this
    change: exit 0, including the new case (shim found; first registration returns no previous callback and runs
    nothing; the runner runs it once and not twice; a later registration returns the callback it replaces).
  • The retail 1.0.2.1 title through gta6rt --run, with the shader workspace this checkout translated, on main plus
    this change: reaches the Vulkan device and presents ([rt] d3d12_xs: PresentX ready (... client area 1904x1041)).
    Plain main on the same machine does the same, so no regression from this change.

GPUs tested: NVIDIA GeForce RTX 5070, driver 621.40, Windows 11 26H2 (build 26300.9974) - boot only; the GPU test
suite was not run for this change.

AI-assisted: yes.

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • 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. (No translator
    change in this pull request.)

_register_thread_local_exe_atexit_callback is the CRT's per-thread hook for an EXE's thread_local destructors. The
runtime left it to the host UCRT, which is the gap shims/process.cpp's header records (''both still reach the host
CRT''). On a73a57e that path is where the retail title stopped: the UCRT's consistency check failed in this process,
its cold path called ucrtbase!terminate, and the title aborted with 0xC0000409 after the static initialisers and
before its first D3D12 call.

Keep the callback per thread in process.cpp and return the previous one as the UCRT does. The loader runs it in the
DLL_THREAD_DETACH branch of on_host_thread_event, after the DLL slots' callbacks and the executable's own, which is
where the UCRT would run it; a throw is caught so the Windows loader lock is never left held.

process_cmdline_test covers it: the shim is found, the first registration returns no previous callback and runs
nothing yet, the runner runs it once and not twice, and a later registration returns the callback it replaces.
@ERROR890

Copy link
Copy Markdown
Author

Companion pull request for the hardware this was found and tested on:
#2358 gpu-test-results: RTX 5070, driver 621.40, Windows 11 26H2 - the GPU test result from the same machine
(it currently sits as a draft; the account is not permitted to publish it right now, so it needs "Ready for review").

Claimed in the coordination issue as BOOT-TLS-ATEXIT-CALLBACK:
#1608 (comment)

For the record on where this was found: the boot stop on a73a57e1 was 0xC0000409 in ucrtbase after the static
initialisers and before the first D3D12 call. An int3 on ucrtbase!terminate fired first, and the return address on
the stack was inside _register_thread_local_exe_atexit_callback (the call terminate of its cold path); the
fast-fail exception information was 0x7 (FAST_FAIL_RANGE_CHECK_FAILURE), the code that path raises. Current main
no longer stops there, so this pull request is the coverage and the test, as the description says.

@lixonik lixonik 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 process_cmdline_test with MSVC on Windows 11. CTest passed at this head, including the thread-local executable teardown callback tests. Synthetic process fixtures only.

AI-assisted.

clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 26, 2026
…wCache hit rate, console epoch skip, and vendor ankerl+XXH3

- Task 1 (P0 Compute PSO Starvation): Add role-balanced select_pso_compile_queue_balanced / assign_pso_worker_role in compile_budget.h (UrgentComputeFirst, UrgentGraphicsFirst, General), raise kMaxReplacementWorkers 2->6 with same-role replacement, switch urgent_queue_ to FIFO with tiny non-mesh fast-lane (<=128 KiB), and inline-compile tiny compute shaders (<=128 KiB) in materialize_compute()/lazy_compute().
- Task 2 (P0 DEVICE_LOST/UAF): Defer VkPipeline/VkPipelineLayout/VkShaderModule destruction and arena_retire in ~PipelineState(), route sparse_levels_ retirement via defer_callback when bindless_group == nullptr, and raise xs_drain_retired_owned_textures kMinSafeRetireGracePresents 4->8 with <=1024 releases/drain burst cap.
- Task 3 (P0 XsPlacedViewCache 6.7%->100%): Remove per-frame owned-texture invalidation (update last_used_present once per frame via weak_ptr), replace global gen wipe with 64-entry XsPlacedViewInvalidationRing + full_epoch + in-place cache_gen promotion, and collapse 4 SEH frames to 1.
- Task 4 (Perf, Vulkan Features & #2354): Vendor MIT ankerl::unordered_dense (v4.5.0) and BSD-2 XXH3 (v0.8.3) with THIRD_PARTY_NOTICES.md attribution; use XXH3_128bits for XS buffer/zero hashing and ankerl::unordered_dense::map for hot XS maps and persistent VsharpPassMemo; fix title_shadow_sync.h can_use_epoch when dirty != nullptr; decouple skip_clean_shadows from untracked shadows and gate drain_title_write_back_locked; add scoped LEONIDA_HOST_IMPORT_RING_MB (default 64 MiB); enable Vulkan 1.2 shaderFloat16, VK_KHR_ray_tracing_maintenance1, and LEONIDA_RT_AS=hybrid; shim _register_thread_local_exe_atexit_callback via Win32 TlsAlloc() + DLL_THREAD_DETACH hook (ProjectLeonida/mandy#2354).
@ERROR890

Copy link
Copy Markdown
Author

Closing as superseded: main now implements this itself.

While checking this branch against current main I also found that the additional call this pull request made from
the loader's thread-detach path is harmful: the executable's callback is already listed in its TLS directory, which
on_host_thread_event runs on DLL_THREAD_DETACH (the same thing the Windows loader does), so calling it a second
time from there double-runs vcruntime's thread-local destructors. Measured on 1.0.2.1, RTX 5070 / driver 621.40 /
Windows 11 26H2: with that extra call the run ended in an unhandled 0xC0000005 inside a minute, twice; with the same
tree and only that call removed it ran its full 75 s window and presented normally. So the loader-side hook should
not be merged, and there is nothing left here that main does not already do.

Thanks to whoever wrote #2315 and its review fix; the signature detail (void (__stdcall*)(void*, unsigned long, void*) and the (nullptr, DLL_PROCESS_DETACH, nullptr) arguments) is right where this branch guessed.

No further work is planned on this branch.

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.

2 participants