Files
mandy/runtime/shims/process_shim.h
Jan Trencansky 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

20 lines
1.0 KiB
C++

// The game's command line and exit, for `gta6rt --run` (shims/process.cpp).
#pragma once
#include <cstddef>
#include <string>
#include <vector>
namespace rt::process {
// Set GetCommandLine to `program` then `args`, quoted where needed, before process-attach callbacks. The wide text
// holds the arguments exactly; the narrow text uses the ANSI code page (RT-84). Reset CRT argv to argc=0/null:
// its startup configuration selects no arguments, the original arguments, or wildcard expansion for each width.
void set_command_line(const std::wstring& program, const std::vector<std::wstring>& args);
// Runs a mapped EXE's entry point on a new thread with `stack_bytes` of reserved stack and returns the game's
// exit code: the entry point's return value, or what it passed to exit/ExitProcess (3 for abort). Returns when
// the first of those happens; other game threads are left running (the host process ends them when it exits).
int run_entry(void* entry, std::size_t stack_bytes = 8u << 20);
} // namespace rt::process