diff options
| author | omardev29 <154835571+omardev29@users.noreply.github.com> | 2026-11-22 05:27:50 +0100 |
|---|---|---|
| committer | omardev29 <154835571+omardev29@users.noreply.github.com> | 2026-11-22 12:29:51 +0100 |
| commit | a19602633b7f0e6ff5db7725c89a1bd11c244686 (patch) | |
| tree | ed6e11577e082ae74baed2c3c3ca72f67dc9c997 /runtime/src/tls_host.cpp | |
| parent | bb2e18421dac527374eb837c88b5063c5d96f81f (diff) | |
tls_host: report thread attach and detach to a callback the loader sets
The Windows loader calls gta6rt_tls_host.dll's DllMain with DLL_THREAD_ATTACH
on every new thread, after it has built the thread's TLS array, and with
DLL_THREAD_DETACH before it frees it. A new export,
gta6rt_tls_host_set_thread_event, lets the loader receive those events, so it
can give each thread the mapped image's TLS. DllMain still touches no TLS (the
helper's own slot is the one the loader swaps); the callback pointer is a plain
global. Nothing registers it yet.
Diffstat (limited to 'runtime/src/tls_host.cpp')
| -rw-r--r-- | runtime/src/tls_host.cpp | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/runtime/src/tls_host.cpp b/runtime/src/tls_host.cpp index 54322be4f..ab234cbd9 100644 --- a/runtime/src/tls_host.cpp +++ b/runtime/src/tls_host.cpp | |||
| @@ -4,12 +4,38 @@ | |||
| 4 | // every thread's TEB. Loader::initialize_tls reuses that reserved index for the manually mapped image, so | 4 | // every thread's TEB. Loader::initialize_tls reuses that reserved index for the manually mapped image, so |
| 5 | // __declspec(thread) access in mapped code reads a valid TEB slot. The anchor itself is never used; the loader saves | 5 | // __declspec(thread) access in mapped code reads a valid TEB slot. The anchor itself is never used; the loader saves |
| 6 | // and restores the helper's own block, so the helper's CRT TLS state is not disturbed. | 6 | // and restores the helper's own block, so the helper's CRT TLS state is not disturbed. |
| 7 | // | ||
| 8 | // The Windows loader also calls this DLL's DllMain with DLL_THREAD_ATTACH on every new thread (after it has built that | ||
| 9 | // thread's TLS array) and with DLL_THREAD_DETACH when a thread exits (before it frees the array). The loader registers | ||
| 10 | // a callback here so it can give each new thread its own copy of the mapped image's TLS, and put this DLL's block back | ||
| 11 | // before the thread exits. DllMain itself must not touch TLS: this DLL's slot is the one being swapped. | ||
| 7 | #include <windows.h> | 12 | #include <windows.h> |
| 8 | 13 | ||
| 9 | __declspec(thread) unsigned char g_tls_host_anchor = 0; | 14 | __declspec(thread) unsigned char g_tls_host_anchor = 0; |
| 10 | 15 | ||
| 16 | namespace { | ||
| 17 | |||
| 18 | using ThreadEventFn = void(__cdecl*)(DWORD reason); | ||
| 19 | |||
| 20 | // A plain global, not TLS (see above). Written once by the loader, read on every thread event. | ||
| 21 | void* volatile g_thread_event = nullptr; | ||
| 22 | |||
| 23 | } // namespace | ||
| 24 | |||
| 11 | // Never called. The export keeps the TLS variable (and therefore the image's TLS directory) from being dropped by | 25 | // Never called. The export keeps the TLS variable (and therefore the image's TLS directory) from being dropped by |
| 12 | // the linker without making DllMain touch TLS during thread attach/detach. | 26 | // the linker without making DllMain touch TLS during thread attach/detach. |
| 13 | extern "C" __declspec(dllexport) void gta6rt_tls_host_keep() { g_tls_host_anchor = 1; } | 27 | extern "C" __declspec(dllexport) void gta6rt_tls_host_keep() { g_tls_host_anchor = 1; } |
| 14 | 28 | ||
| 15 | BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID) { return TRUE; } | 29 | // Called by the loader. `fn` runs inside DllMain (under the Windows loader lock) for DLL_THREAD_ATTACH and |
| 30 | // DLL_THREAD_DETACH; nullptr stops the calls. | ||
| 31 | extern "C" __declspec(dllexport) void gta6rt_tls_host_set_thread_event(ThreadEventFn fn) { | ||
| 32 | InterlockedExchangePointer(&g_thread_event, reinterpret_cast<void*>(fn)); | ||
| 33 | } | ||
| 34 | |||
| 35 | BOOL WINAPI DllMain(HINSTANCE, DWORD reason, LPVOID) { | ||
| 36 | if (reason == DLL_THREAD_ATTACH || reason == DLL_THREAD_DETACH) { | ||
| 37 | void* fn = InterlockedCompareExchangePointer(&g_thread_event, nullptr, nullptr); | ||
| 38 | if (fn) reinterpret_cast<ThreadEventFn>(fn)(reason); | ||
| 39 | } | ||
| 40 | return TRUE; | ||
| 41 | } | ||