Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions runtime/shims/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
// shares with gta6rt. exit() runs it newest first, as the CRT does, before the run ends; _exit(), _Exit(),
// quick_exit(), ExitProcess() and abort() do not. _cexit() runs it and returns, _c_exit() returns. A second
// thread that calls exit(), _exit(), _Exit() or quick_exit() while the first is leaving waits, as with the
// CRT's exit lock. The thread-local destructor callback is kept here too, one per thread, and run by the
// loader when that thread ends. Not covered yet: at_quick_exit() handlers (quick_exit() runs none).
// CRT's exit lock. Not covered yet: at_quick_exit() handlers (quick_exit() runs none).
// - the EXE's thread-local destructor callback (_register_thread_local_exe_atexit_callback) is kept here too:
// exit() and _cexit() call it before the atexit handlers, as the UCRT does (see game_exit below).
Expand Down Expand Up @@ -136,6 +138,22 @@ void run_atexit_handlers() {
int __cdecl game_crt_atexit(AtexitFn fn) {
return fn && register_atexit(fn) ? 0 : -1;
}

// UCRT: _tls_callback_fn _register_thread_local_exe_atexit_callback(_tls_callback_fn), the previous one back. The
// UCRT keeps one callback per thread and runs it when that thread ends, for the thread_local destructors of an EXE;
// it is also where a title that a shim has to answer reaches ucrtbase!terminate, whose UCRT-side consistency check
// fails in this process (ucrtbase!abort, FAST_FAIL_FATAL_APP_EXIT, before any D3D12 call: measured on 1.0.2.1 with
// the 14.29 runtime the package ships against this host's UCRT). Keeping the registration here, per thread, leaves
// the CRT out of it.
using TlsCallbackFn = void(__cdecl*)();
thread_local TlsCallbackFn g_thread_local_exe_atexit_callback = nullptr;

TlsCallbackFn __cdecl game_register_thread_local_exe_atexit_callback(TlsCallbackFn callback) {
TlsCallbackFn previous = g_thread_local_exe_atexit_callback;
g_thread_local_exe_atexit_callback = callback;
return previous;
}

// msvcrt.dll (MinGW): int atexit(void (*)(void)) and _onexit_t _onexit(_onexit_t).
int __cdecl game_msvcrt_atexit(AtexitFn fn) { return game_crt_atexit(fn); }
OnexitFn __cdecl game_onexit(OnexitFn fn) {
Expand Down Expand Up @@ -509,6 +527,14 @@ int run_entry(void* entry, size_t stack_bytes) {
return g_exit_code.load();
}

// Outside the anonymous namespace: the loader calls it when a thread ends (process_shim.h).
void run_thread_local_exe_atexit_callback() {
if (TlsCallbackFn callback = g_thread_local_exe_atexit_callback) {
g_thread_local_exe_atexit_callback = nullptr; // once, even if it throws
callback();
}
}

} // namespace rt::process

namespace {
Expand All @@ -518,6 +544,7 @@ using namespace rt::process;
{dll, "_Exit", reinterpret_cast<void*>(&game_quick_exit)}, \
{dll, "quick_exit", reinterpret_cast<void*>(&game_quick_exit)}, \
{dll, "_crt_atexit", reinterpret_cast<void*>(&game_crt_atexit)}, \
{dll, "_register_thread_local_exe_atexit_callback", reinterpret_cast<void*>(&game_register_thread_local_exe_atexit_callback)}, \
{dll, "_cexit", reinterpret_cast<void*>(&game_cexit)}, {dll, "_c_exit", reinterpret_cast<void*>(&game_c_exit)}, \
{dll, "abort", reinterpret_cast<void*>(&game_abort)}, {dll, "__p___argc", reinterpret_cast<void*>(&game_p_argc)}, \
{dll, "_register_thread_local_exe_atexit_callback", \
Expand Down
5 changes: 5 additions & 0 deletions runtime/shims/process_shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ void set_command_line(const std::wstring& program, const std::vector<std::wstrin
// 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);

// The thread-local destructor callback the game registered with _register_thread_local_exe_atexit_callback, run on
// the thread that registered it and at most once. The loader calls it for a thread that ends; the game's own exit
// does not run it, as with the UCRT, whose thread events only fire when the thread ends.
void run_thread_local_exe_atexit_callback();

} // namespace rt::process
12 changes: 10 additions & 2 deletions runtime/src/pe_loader.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "pe_loader.h"
#include "pe_loader.h"

#include <windows.h>
#include <intrin.h>
Expand All @@ -16,6 +16,7 @@
#include <system_error>
#include <vector>

#include "../shims/process_shim.h"
#include "../shims/shims.h"
#include "../shims/startup_trace.h"
#include "base_reservation.h"
Expand Down Expand Up @@ -1205,6 +1206,13 @@ void __cdecl on_host_thread_event(DWORD reason) {
release_thread_slot(i); // installed by a module whose Loader is gone (or nothing to do)
}
}
// The executable's thread-local destructors, which its CRT would run at this point: after the DLL slots'
// callbacks, on the thread that is ending. A throw from one must not leave the Windows loader lock held.
try {
rt::process::run_thread_local_exe_atexit_callback();
} catch (const std::exception& e) {
std::fprintf(stderr, "[rt] loader: thread detach, thread-local exe-atexit callback: %s\n", e.what());
}
}
}

Expand Down Expand Up @@ -1450,7 +1458,7 @@ Module* Loader::find_narrow_module(const std::string& dll_lower) const {
for (const auto& entry : by_name_)
if (entry.second->narrow_key == dll_lower) return entry.second;
// The key is ASCII case-folded only. A name with letters outside ASCII (Cyrillic in code page 1251, say)
// compares case-insensitively through the code page, as GetModuleHandleA does: GUEST_ДА.EXE finds guest_да.exe.
// compares case-insensitively through the code page, as GetModuleHandleA does: GUEST_ДА.EXE finds guest_да.exe.
// Widening the folded key is safe here: rt::lower() folds ASCII letters only, and no ANSI code page's multibyte
// sequences contain ASCII letter bytes, so both sides widen to the same characters they were spelt with.
if (const std::wstring wide = acp_to_wide(dll_lower); !wide.empty())
Expand Down
49 changes: 49 additions & 0 deletions runtime/test/process_cmdline_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,55 @@ int main() {
SetProcessAffinityMask(GetCurrentProcess(), process_mask);
}

// The thread-local destructor callback a title registers: the shim keeps it per thread instead of letting the
// UCRT hold it, registering returns the previous one and runs nothing yet, and the runner runs the current one
// once (the loader calls the runner when a thread ends).
{
using TlsFn = void(__cdecl*)();
using RegisterFn = TlsFn(__cdecl*)(TlsFn);
const auto reg =
reinterpret_cast<RegisterFn>(rt::shims::find("ucrtbase.dll", "_register_thread_local_exe_atexit_callback"));
if (!reg) {
std::fprintf(stderr, "FAIL missing _register_thread_local_exe_atexit_callback shim\n");
++failures;
} else {
static int ran = 0;
TlsFn first = +[] { ++ran; };
if (reg(first) != nullptr) {
std::fprintf(stderr, "FAIL the first registration did not return no previous callback\n");
++failures;
}
if (ran != 0) {
std::fprintf(stderr, "FAIL the callback ran before the thread ended\n");
++failures;
}
rt::process::run_thread_local_exe_atexit_callback();
if (ran != 1) {
std::fprintf(stderr, "FAIL the runner ran the callback %d time(s), not once\n", ran);
++failures;
}
rt::process::run_thread_local_exe_atexit_callback();
if (ran != 1) {
std::fprintf(stderr, "FAIL the runner ran the callback again (%d)\n", ran);
++failures;
}
TlsFn second = +[] { };
if (reg(second) != nullptr) {
std::fprintf(stderr, "FAIL a registration after the run did not return no previous callback\n");
++failures;
}
if (reg(first) != second) {
std::fprintf(stderr, "FAIL a registration did not return the callback it replaces\n");
++failures;
}
rt::process::run_thread_local_exe_atexit_callback();
if (ran != 2) {
std::fprintf(stderr, "FAIL the runner ran the replacing callback %d time(s), not once\n", ran);
++failures;
}
}
}

if (failures) return 1;
std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact (code page %u); "
"the WinMain line and the kernelbase/API-set names are the game's\n",
Expand Down