Skip to content

RT-115: gta6rt --run runs the game's atexit handlers and static destructors - #2152

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
martim-oliveiradev:rt-115-atexit-handlers
Nov 23, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
martim-oliveiradev:rt-115-atexit-handlers

Conversation

@martim-oliveiradev

@martim-oliveiradev martim-oliveiradev commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-115 (claim, status)

Depends on: #1895
(No code dependency: RT-119 and this PR both edit runtime/shims/process.cpp. Merged together there is one conflict, in run_entry, where #1895 adds the stack-size line and this PR adds the table reset; keeping both lines is the whole resolution. Checked below.)

What and why

Under gta6rt --run, the mapped EXE registered its atexit handlers and static destructors with the host CRT's process-wide table (through _crt_atexit), which it shares with gta6rt. main.cpp ends with ExitProcess so that table is never run, so the game's handlers never ran; shims/process.cpp documented this. Measured with a stand-in program: natively return and exit(7) print the handlers' and destructors' lines; under --run none appeared (exit codes matched). _cexit() only looked right by accident: it ran the shared table, gta6rt's own handlers included.

runtime/shims/process.cpp:

  • The EXE's _crt_atexit (UCRT; MSVC's atexit and static destructors reach it) and msvcrt's atexit/_onexit register into a table of the run's own.
  • exit() runs it newest first, including handlers registered while it runs (as the UCRT does), then ends the run. exit() again from a handler ends the run with that code.
  • _exit, _Exit, quick_exit, ExitProcess and abort run no handlers.
  • Like the CRT's exit lock, exit, _exit, _Exit and quick_exit from a second thread wait while the first thread is leaving (natively, _exit from another thread during exit waits for every handler; before this the run ended mid-handler).
  • _cexit() runs the table and flushes the streams, then returns; _c_exit() returns.
  • Each run_entry starts with an empty table and no exiting thread, so a run never sees another run's handlers. A registration that cannot be stored (out of memory) returns -1 / NULL instead of throwing into the game.

Not covered yet (documented in the file and runtime/README.md): at_quick_exit handlers (quick_exit runs none) and the thread-local destructor callback (_register_thread_local_exe_atexit_callback); both still reach the host CRT.

Tested

New run_tiny_atexit (CTest; test/check_atexit.cmake + test/fixtures/tiny_atexit.cpp): a stand-in program run natively and through --run for return, exit(7), _exit(8), quick_exit(9), ExitProcess(10), _cexit(), _c_exit(), and _exit() from a second thread while exit() runs a handler. Both runs must print the same lines and end with the same exit code, and the native run must match the sequence written in the script, so the two cannot agree on a wrong answer. One handler registers another during exit. It goes through CMAKE_CROSSCOMPILING_EMULATOR like run_tiny_async.

  • Before the change: return, exit and c_exit failed (handlers and destructors missing); the native runs matched the written sequences.
  • This branch (main f847748c + 39d3ab8c + b40df7a5; rebases only touched the runtime/CMakeLists.txt blocks next to other new fixtures, all kept): run_tiny_atexit 20/20 repeats; runtime suite 51/51, 0 failures (46 passed, 5 skipped because they need game files, gameinput disabled without SDL3).
  • Earlier bases: main 49475796 + d50114e2: 45/45, 0 failures (40 passed), run_tiny_atexit 20/20. Main 4eb6155d + f3a14170: 43/43, 0 failures (38 passed), run_tiny_atexit 30/30; main alone there: 42/42 (37 passed).
  • Under Wine (RUNNER set) the native leg runs Wine's CRT, which differs from Windows (a handler registered during exit is dropped; _exit from a second thread doesn't wait for the exit lock), so b40df7a5 holds --run to the expected Windows sequence directly and only reports a native difference there. On Windows it is the same check, since the native run must match that sequence too. Checked here by running the script with and without a pass-through RUNNER: both pass, and a fault that makes exit run no handlers fails both. Not re-run under Wine on my side; see kmorales13's Wine/Proton check in the comments.
  • Nine faults injected into process.cpp were each caught by run_tiny_atexit: handlers oldest first, exit running none, _exit running them, quick_exit running them, registrations during exit dropped, _c_exit running them, _cexit running none, _crt_atexit left to the host CRT, _exit ignoring the exit lock.
  • With Runtime: give game threads the image's stack reserve (RT-119) #1895 (RT-119, head 0af9b816) merged in as well (on f3a14170): run_tiny_atexit, run_stack_reserve, stack_reserve_shim and process_cmdline 10/10 each; full suite 0 failures.

Windows 11 (build 26300), MSVC 19.51 (VS 2026 Build Tools), Windows SDK 10.0.26100, Ninja, RelWithDebInfo. CPU tests only: no GPU involved, Vulkan tools not built. Not tested here: Wine/Proton (independent checks in the comments: Windows by Ingenioeren, Wine/Proton by kmorales13), MinGW (the msvcrt bindings follow the same contract but no MinGW build was run).

AI-assisted (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 applicable: no translator change.)

@Ingenioeren

Copy link
Copy Markdown

Independent Windows check of f3a14170 with #1895 at 0af9b816, replayed on main at 49475796. I retained both PRs’ run_entry changes and the newer main import registrations, and used the SDK 612 compatibility fix to build with MSVC 19.43.

  • All 17 selected headless CTests passed, including run_tiny_atexit, run_stack_reserve, stack_reserve_shim, process_cmdline, and the bundled-DLL startup fixture.
  • run_tiny_atexit and run_stack_reserve each passed 12 consecutive runs. The atexit test compares all eight termination paths with native Windows, including _exit from a second thread during exit.
  • I also reran those eight paths with --trace enabled. Every JSONL trace contained the mapped EXE and a final exit record with the same code as the process returned.

Synthetic inputs only. AI-assisted testing.

@kmorales13

Copy link
Copy Markdown
Contributor

Independent Wine/Proton check of d50114e2 (base 49475796) on Linux. CachyOS, CPU tests only; clang-cl 23.1.5 + xwin (MSVC CRT 14.44, SDK 10.0.26100) with runtime/cmake/clang-cl-xwin.cmake.

  • The shim is right under Wine. gta6rt --run of tiny_atexit gives the expected Windows sequence in all 8 modes: on Wine 11.22 (10 repeats) and on Proton Experimental's wine 11.0 (fresh prefix).

  • The bug reproduces on base. With the new fixture, the base loader prints no handlers for return, exit and concurrent_exit.

  • run_tiny_atexit still fails under Wine and Proton, because the native leg differs from Windows in 5 modes:

    • Wine's UCRT drops a handler that a handler registers while handlers run (atexit late); with _cexit it runs it at the final exit instead.
    • In concurrent_exit, Wine's _exit from the second thread doesn't wait for the exit lock, so the native run ends after slow handler started.

    So the expected sequence is right and Wine's CRT is what differs. CI's Wine job skips cmake -P tests, so CI won't show this; a local ctest under Wine or Proton will. A suggestion: under Wine (wine_get_version in ntdll), keep holding --run to the expected sequence and report the native difference without failing.

  • Full suite, Wine 11.22, ctest -j4:

    • head: 107 passed, 2 failed, 16 not run. The failures are device_proxy (the known vkd3d failure, also on base) and run_tiny_atexit (above).
    • base: 106 passed, 2 failed, 16 not run. The failures are device_proxy and d3d12_present; this PR doesn't touch d3d12_present and I didn't look into it.
  • Not tested: MinGW (no toolchain here).

AI-assisted (Claude-Opus).

…uctors

The mapped EXE registered atexit handlers and static destructors with the
host CRT's process-wide table, which gta6rt never runs, so they were lost.
They now go to a table of the run's own: exit() and a return from main run
it newest first (including handlers registered during exit), _cexit() runs
it and returns, and _exit/_Exit/quick_exit/ExitProcess/abort skip it, as
natively. exit/_exit/_Exit/quick_exit share an exit lock like the CRT's,
each run starts with an empty table, and a failed registration returns an
error instead of throwing into the game.

New run_tiny_atexit compares a stand-in program run natively and through
--run for 8 termination paths. Not covered yet: at_quick_exit and the
thread_local destructor callback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
run_tiny_atexit compared `gta6rt --run` with the native run. Under Wine the
native leg uses Wine's own CRT, which differs from Windows in two places
(it drops a handler registered while handlers run, and its _exit from a
second thread does not wait for the exit lock), so the test failed there
although --run gave the Windows sequence (reported on ItsNotRexed#2152).

--run is now held to the expected sequence directly. On Windows this is the
same check, since the native run must match that sequence too. With RUNNER
set (Wine), a native difference is reported without failing.

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

Copy link
Copy Markdown
Contributor Author

Thanks for the Wine/Proton check, @kmorales13. That's the part I couldn't test here.

You were right that the native leg was the problem, and b40df7a does what you suggested without needing wine_get_version: --run is now held directly to the expected (Windows) sequence. On Windows that is the same check as before, since the native run must match that sequence too. When RUNNER is set (the Wine path), a native difference is printed as a status message instead of failing, so Wine's CRT differences (the dropped atexit late, _exit not waiting for the exit lock) still show in the log.

Also rebased on main f847748c (only the runtime/CMakeLists.txt blocks next to tiny_startup_cmdline, both kept). Windows: suite 51/51, 0 failures (46 passed, 5 skipped for game files); run_tiny_atexit 20/20; a fault that makes exit run no handlers still fails it with and without RUNNER. Not re-run under Wine on my side; ctest -R run_tiny_atexit there should now pass.

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested b40df7a on Windows 11 (MSVC RelWithDebInfo): full runtime build, then ctest for the shim/loader tests (loader, pe_loader, loader_tls, dll_thread_calls, unimplemented_policy, startup_trace, xmem, process_cmdline, file_system, winhttp_offline, xgameruntime, run_tiny_*, allocator_proxy) — 26 of 26 passed. AI-assisted.

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.

5 participants