upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/process.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/shims/process.cpp')
-rw-r--r--runtime/shims/process.cpp101
1 files changed, 80 insertions, 21 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp
index 5f96e9f96..a692a6224 100644
--- a/runtime/shims/process.cpp
+++ b/runtime/shims/process.cpp
@@ -11,7 +11,12 @@
11// shares with gta6rt. exit() runs it newest first, as the CRT does, before the run ends; _exit(), _Exit(), 11// shares with gta6rt. exit() runs it newest first, as the CRT does, before the run ends; _exit(), _Exit(),
12// quick_exit(), ExitProcess() and abort() do not. _cexit() runs it and returns, _c_exit() returns. A second 12// quick_exit(), ExitProcess() and abort() do not. _cexit() runs it and returns, _c_exit() returns. A second
13// thread that calls exit(), _exit(), _Exit() or quick_exit() while the first is leaving waits, as with the 13// thread that calls exit(), _exit(), _Exit() or quick_exit() while the first is leaving waits, as with the
14// CRT's exit lock; 14// CRT's exit lock. Registration and the cleanup of exit(), quick_exit() and _cexit() (the thread-local
15// callback, the table's drain and, for _cexit(), the stream flush) hold one lock of the run's own, as the UCRT's
16// exit lock serializes them: a second thread in _cexit(), or registering while a drain runs, waits for that
17// cleanup; the exiting thread may register from a handler (the lock is recursive). The lock is released before
18// the run ends. A handler that lets a C++ exception escape terminates the run as the UCRT's terminate() would:
19// abort(), exit code 3;
15// - the game's at_quick_exit registrations (_crt_at_quick_exit) go to a second table of the run's own, which 20// - the game's at_quick_exit registrations (_crt_at_quick_exit) go to a second table of the run's own, which
16// quick_exit() runs newest first; exit(), _exit() and _Exit() do not; 21// quick_exit() runs newest first; exit(), _exit() and _Exit() do not;
17// - the EXE's thread-local destructor callback (_register_thread_local_exe_atexit_callback, which the EXE's CRT 22// - the EXE's thread-local destructor callback (_register_thread_local_exe_atexit_callback, which the EXE's CRT
@@ -88,7 +93,12 @@ std::string narrow(const std::wstring& w) {
88 return s; 93 return s;
89} 94}
90 95
96void release_cleanup_lock();
97
91[[noreturn]] void end_run(int code) { 98[[noreturn]] void end_run(int code) {
99 // A handler may end the run from inside a drain: the cleanup lock it holds is released before the thread parks,
100 // so the host (and a later run_entry) can take it.
101 release_cleanup_lock();
92 // Read before signalling: once g_done is set, run_entry can return and clear g_running while the host is still 102 // Read before signalling: once g_done is set, run_entry can return and clear g_running while the host is still
93 // finishing the run (main.cpp's "game exited" line and cleanup), which this thread must not cut short. 103 // finishing the run (main.cpp's "game exited" line and cleanup), which this thread must not cut short.
94 const bool running = g_running; 104 const bool running = g_running;
@@ -109,14 +119,38 @@ std::string narrow(const std::wstring& w) {
109using AtexitFn = void(__cdecl*)(); 119using AtexitFn = void(__cdecl*)();
110using OnexitFn = int(__cdecl*)(); 120using OnexitFn = int(__cdecl*)();
111using TlsCallback = void(NTAPI*)(PVOID, DWORD, PVOID); // _tls_callback_type 121using TlsCallback = void(NTAPI*)(PVOID, DWORD, PVOID); // _tls_callback_type
112std::mutex g_atexit_lock; 122// The run's cleanup lock, as the UCRT's exit lock: registration takes it, and a cleanup (exit, quick_exit, _cexit)
123// holds it from before the thread-local callback through the table's drain, so two threads never clean up at once
124// and a registration from another thread waits for the drain. Recursive, so the exiting thread may register from a
125// handler. t_cleanup_held counts this thread's holds: end_run releases them all before the thread parks.
126std::recursive_mutex g_cleanup_lock;
127thread_local unsigned t_cleanup_held = 0;
113std::vector<AtexitFn> g_atexit; 128std::vector<AtexitFn> g_atexit;
114std::vector<AtexitFn> g_at_quick_exit; 129std::vector<AtexitFn> g_at_quick_exit;
115std::atomic<TlsCallback> g_thread_local_exit{nullptr}; 130std::atomic<TlsCallback> g_thread_local_exit{nullptr};
116std::atomic<DWORD> g_exiting_thread{0}; 131std::atomic<DWORD> g_exiting_thread{0};
117 132
133struct CleanupLock {
134 CleanupLock() {
135 g_cleanup_lock.lock();
136 ++t_cleanup_held;
137 }
138 ~CleanupLock() {
139 --t_cleanup_held;
140 g_cleanup_lock.unlock();
141 }
142 CleanupLock(const CleanupLock&) = delete;
143 CleanupLock& operator=(const CleanupLock&) = delete;
144};
145void release_cleanup_lock() {
146 while (t_cleanup_held) {
147 --t_cleanup_held;
148 g_cleanup_lock.unlock();
149 }
150}
151
118bool register_in(std::vector<AtexitFn>& table, AtexitFn fn) { 152bool register_in(std::vector<AtexitFn>& table, AtexitFn fn) {
119 std::lock_guard<std::mutex> lock(g_atexit_lock); 153 CleanupLock lock;
120 try { 154 try {
121 table.push_back(fn); 155 table.push_back(fn);
122 } catch (const std::bad_alloc&) { 156 } catch (const std::bad_alloc&) {
@@ -126,25 +160,39 @@ bool register_in(std::vector<AtexitFn>& table, AtexitFn fn) {
126} 160}
127bool register_atexit(AtexitFn fn) { return register_in(g_atexit, fn); } 161bool register_atexit(AtexitFn fn) { return register_in(g_atexit, fn); }
128 162
129// Newest first. A handler registered while the handlers run goes on the end and so runs next, as in the UCRT. 163void __cdecl game_abort();
130void run_handlers(std::vector<AtexitFn>& table) { 164
165// A guest cleanup callback. A C++ exception it lets escape would unwind into the runtime; the UCRT's exit calls
166// terminate() if one throws, and its default handler aborts: exit code 3, and the remaining handlers do not run.
167// A thread_local destructor is noexcept, so one that throws reaches the game's own terminate() before this catch;
168// that abort() is the game's import, game_abort, and ends the run with the same code.
169template <typename Call>
170void guarded(const char* what, Call&& call) {
171 try {
172 call();
173 } catch (...) {
174 std::fprintf(stderr, "[rt] process: a %s threw a C++ exception; terminating as the UCRT does\n", what);
175 game_abort();
176 }
177}
178
179// Newest first, with the cleanup lock held throughout (the caller took it). A handler registered while the handlers
180// run goes on the end and so runs next, as in the UCRT.
181void run_handlers(std::vector<AtexitFn>& table, const char* what) {
131 for (;;) { 182 for (;;) {
132 AtexitFn fn; 183 if (table.empty()) return;
133 { 184 const AtexitFn fn = table.back();
134 std::lock_guard<std::mutex> lock(g_atexit_lock); 185 table.pop_back();
135 if (table.empty()) return; 186 guarded(what, fn);
136 fn = table.back();
137 table.pop_back();
138 }
139 fn();
140 } 187 }
141} 188}
142// exit() and _cexit(): the exiting thread's thread_local destructors first (the UCRT calls the registered callback 189// exit() and _cexit(): the exiting thread's thread_local destructors first (the UCRT calls the registered callback
143// with DLL_PROCESS_DETACH; a second call finds the thread's list empty), then the atexit handlers and static 190// with DLL_PROCESS_DETACH; a second call finds the thread's list empty), then the atexit handlers and static
144// destructors. 191// destructors. The caller holds the cleanup lock.
145void run_atexit_handlers() { 192void run_atexit_handlers() {
146 if (const TlsCallback callback = g_thread_local_exit.load()) callback(nullptr, DLL_PROCESS_DETACH, nullptr); 193 if (const TlsCallback callback = g_thread_local_exit.load())
147 run_handlers(g_atexit); 194 guarded("thread-local destructor", [&] { callback(nullptr, DLL_PROCESS_DETACH, nullptr); });
195 run_handlers(g_atexit, "atexit handler");
148} 196}
149 197
150// UCRT: int _crt_atexit(_PVFV), 0 on success. MSVC's atexit() and the static destructors of an EXE reach it. 198// UCRT: int _crt_atexit(_PVFV), 0 on success. MSVC's atexit() and the static destructors of an EXE reach it.
@@ -171,13 +219,18 @@ bool take_exit_lock() {
171 DWORD owner = 0; 219 DWORD owner = 0;
172 if (g_exiting_thread.compare_exchange_strong(owner, self)) return true; 220 if (g_exiting_thread.compare_exchange_strong(owner, self)) return true;
173 if (owner != self) { 221 if (owner != self) {
222 release_cleanup_lock(); // parked for good: a drain it was in must not keep the owner waiting
174 for (;;) Sleep(INFINITE); 223 for (;;) Sleep(INFINITE);
175 } 224 }
176 return false; 225 return false;
177} 226}
178// exit(): the game's handlers and static destructors, then the end of the run. 227// exit(): the game's handlers and static destructors, then the end of the run. The cleanup lock is taken after the
228// exit lock (a thread parked by the latter holds nothing) and released by end_run.
179void __cdecl game_exit(int code) { 229void __cdecl game_exit(int code) {
180 if (take_exit_lock()) run_atexit_handlers(); 230 if (take_exit_lock()) {
231 CleanupLock lock;
232 run_atexit_handlers();
233 }
181 end_run(code); 234 end_run(code);
182} 235}
183// _exit(), _Exit(): the end of the run without any handler. 236// _exit(), _Exit(): the end of the run without any handler.
@@ -187,11 +240,17 @@ void __cdecl game_underscore_exit(int code) {
187} 240}
188// quick_exit(): the at_quick_exit handlers, newest first, then the end of the run without the atexit handlers. 241// quick_exit(): the at_quick_exit handlers, newest first, then the end of the run without the atexit handlers.
189void __cdecl game_quick_exit(int code) { 242void __cdecl game_quick_exit(int code) {
190 if (take_exit_lock()) run_handlers(g_at_quick_exit); 243 if (take_exit_lock()) {
244 CleanupLock lock;
245 run_handlers(g_at_quick_exit, "quick_exit handler");
246 }
191 end_run(code); 247 end_run(code);
192} 248}
193// _cexit() runs the handlers and flushes the streams, then returns; _c_exit() only returns. 249// _cexit() runs the handlers and flushes the streams, then returns; _c_exit() only returns. The flush is inside the
250// cleanup lock, as the UCRT's _cexit flushes under its exit lock. A second thread's _cexit() waits for the first's
251// cleanup, then finds the table empty.
194void __cdecl game_cexit() { 252void __cdecl game_cexit() {
253 CleanupLock lock;
195 run_atexit_handlers(); 254 run_atexit_handlers();
196 std::fflush(nullptr); 255 std::fflush(nullptr);
197} 256}
@@ -484,7 +543,7 @@ void set_command_line(const std::wstring& program, const std::vector<std::wstrin
484 543
485int run_entry(void* entry, size_t stack_bytes) { 544int run_entry(void* entry, size_t stack_bytes) {
486 { 545 {
487 std::lock_guard<std::mutex> lock(g_atexit_lock); 546 CleanupLock lock;
488 g_atexit.clear(); // a run never sees the handlers of an earlier one 547 g_atexit.clear(); // a run never sees the handlers of an earlier one
489 g_at_quick_exit.clear(); 548 g_at_quick_exit.clear();
490 } 549 }