Commit Graph

2 Commits

Author SHA1 Message Date
48f87a9c9b process: call the EXE's thread-local destructor callback with its real signature, before the atexit handlers (maintainer fix on #2315)
#2315 kept `_register_thread_local_exe_atexit_callback` away from the host UCRT, which fast-fails a second
registration in the process (gta6rt's own startup makes the first). Review points fixed on top:

- The callback is a `_tls_callback_type` (UCRT process.h: `void (__stdcall*)(void*, unsigned long, void*)`), not
  `void (*)()`: vcruntime's TLS destructor callback reads its reason argument, which was left as whatever rdx held.
  It is now called as `(nullptr, DLL_PROCESS_DETACH, nullptr)`, as the UCRT does.
- `exit` and `_cexit` call it before the atexit table, as the UCRT does: the leaving thread's thread_local objects end
  before the static ones ([basic.start.term]). #2315 ran it after the static destructors, and not in `_cexit`.
- The extra call on every thread detach is dropped, with the per-thread flag and the loader's include of the process
  shim: the loader already calls the same function from the executable's TLS directory with DLL_THREAD_DETACH, and
  the UCRT never calls the registered one there. The call after a returning entry point is dropped too (MSVC's
  mainCRTStartup calls exit itself).
- run_tiny_atexit gains thread_local_exit and thread_local_cexit: a thread_local object of the main thread must end
  before the handlers and destructors. On main the --run path never ends it (Wine) or stops at the second
  registration (Windows).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 13:15:17 -05:00
mz
39d3ab8c7d RT-115: gta6rt --run runs the game's atexit handlers and static destructors
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>
2026-11-22 20:21:20 +00:00