mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 17:18:38 +00:00
#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>
106 lines
4.0 KiB
C++
106 lines
4.0 KiB
C++
// A stand-in program for `gta6rt --run`: atexit handlers, static destructors and the ways a program ends.
|
|
// Test code, not game content. check_atexit.cmake runs it natively and through the loader with each mode below
|
|
// and compares the lines it prints.
|
|
// return main returns: handlers and static destructors run, in reverse order of registration
|
|
// exit exit(7): the same
|
|
// _exit _exit(8): no handlers, no destructors
|
|
// quick_exit quick_exit(9): no atexit handlers, no destructors
|
|
// ExitProcess ExitProcess(10): no handlers, no destructors
|
|
// cexit _cexit() runs every handler and returns; main then returns 11
|
|
// c_exit _c_exit() runs none and returns; main then returns 12, and exit runs them all
|
|
// concurrent_exit a second thread calls _exit(7) while exit(7) runs a handler: it waits for them all
|
|
// thread_local_exit a thread_local object of the main thread, then exit(13): it ends first, then the handlers
|
|
// thread_local_cexit the same object, then _cexit(): the same order, and main then returns 14
|
|
// The thread_local destructor reaches the CRT through _register_thread_local_exe_atexit_callback, which the EXE's
|
|
// startup calls only when the image has one (this program has one in every mode, constructed only in the last two).
|
|
// One handler registers another while the handlers run: it must still run, next.
|
|
#include <windows.h>
|
|
#include <process.h> // _cexit, _c_exit
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
|
|
namespace {
|
|
|
|
void say(const char* what) {
|
|
std::printf("tiny_atexit: %s\n", what);
|
|
std::fflush(stdout); // every line reaches the pipe before any way out, natively and under the loader
|
|
}
|
|
|
|
struct Tracer {
|
|
const char* name;
|
|
explicit Tracer(const char* n) : name(n) {
|
|
std::printf("tiny_atexit: ctor %s\n", name);
|
|
std::fflush(stdout);
|
|
}
|
|
~Tracer() {
|
|
std::printf("tiny_atexit: dtor %s\n", name);
|
|
std::fflush(stdout);
|
|
}
|
|
};
|
|
Tracer g_first("first");
|
|
Tracer g_second("second");
|
|
|
|
void handler_one() { say("atexit one"); }
|
|
void handler_two() { say("atexit two"); }
|
|
void handler_late() { say("atexit late"); }
|
|
void handler_registers() {
|
|
say("atexit registers late");
|
|
std::atexit(handler_late);
|
|
}
|
|
|
|
// concurrent_exit: while exit() runs this handler on the main thread, a second thread calls _exit(). The CRT lets
|
|
// it go once the handlers are done, so either call may set the exit code: both use 7.
|
|
HANDLE g_slow_started = nullptr;
|
|
void handler_slow() {
|
|
say("slow handler started");
|
|
SetEvent(g_slow_started);
|
|
Sleep(300);
|
|
say("slow handler done");
|
|
}
|
|
DWORD WINAPI quick_exit_thread(void*) {
|
|
WaitForSingleObject(g_slow_started, INFINITE);
|
|
_exit(7);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
const char* mode = argc > 1 ? argv[1] : "return";
|
|
std::atexit(handler_one);
|
|
std::atexit(handler_registers);
|
|
std::atexit(handler_two);
|
|
say("main");
|
|
if (!std::strcmp(mode, "concurrent_exit")) {
|
|
g_slow_started = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
|
std::atexit(handler_slow);
|
|
CloseHandle(CreateThread(nullptr, 0, quick_exit_thread, nullptr, 0, nullptr));
|
|
std::exit(7);
|
|
}
|
|
if (!std::strncmp(mode, "thread_local_", 13)) {
|
|
thread_local Tracer main_thread_local("thread_local"); // constructed here, on the main thread only
|
|
if (!std::strcmp(mode, "thread_local_exit")) std::exit(13);
|
|
if (!std::strcmp(mode, "thread_local_cexit")) {
|
|
_cexit();
|
|
say("after _cexit");
|
|
return 14;
|
|
}
|
|
}
|
|
if (!std::strcmp(mode, "exit")) std::exit(7);
|
|
if (!std::strcmp(mode, "_exit")) _exit(8);
|
|
if (!std::strcmp(mode, "quick_exit")) std::quick_exit(9);
|
|
if (!std::strcmp(mode, "ExitProcess")) ExitProcess(10);
|
|
if (!std::strcmp(mode, "cexit")) {
|
|
_cexit();
|
|
say("after _cexit");
|
|
return 11;
|
|
}
|
|
if (!std::strcmp(mode, "c_exit")) {
|
|
_c_exit();
|
|
say("after _c_exit");
|
|
return 12;
|
|
}
|
|
return 3;
|
|
}
|