diff options
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 275 |
1 files changed, 202 insertions, 73 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 31f5cc0a5..a59f9d0b1 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | #include <intrin.h> | 4 | #include <intrin.h> |
| 5 | 5 | ||
| 6 | #include <algorithm> | 6 | #include <algorithm> |
| 7 | #include <array> | ||
| 7 | #include <atomic> | 8 | #include <atomic> |
| 8 | #include <cstdio> | 9 | #include <cstdio> |
| 9 | #include <cstdlib> | 10 | #include <cstdlib> |
| @@ -74,8 +75,11 @@ IMAGE_TLS_DIRECTORY64* tls_directory(Module& m) { | |||
| 74 | // loader-assigned per-module TLS index and is a different index space from TlsAlloc/TlsGetValue (TlsSlots). Only the OS | 75 | // loader-assigned per-module TLS index and is a different index space from TlsAlloc/TlsGetValue (TlsSlots). Only the OS |
| 75 | // loader can allocate a module index, so the runtime ships a minimal DLL whose only content is a TLS anchor | 76 | // loader can allocate a module index, so the runtime ships a minimal DLL whose only content is a TLS anchor |
| 76 | // (src/tls_host.cpp). Loading it makes the OS reserve an index and give every thread a block for it; the native loader | 77 | // (src/tls_host.cpp). Loading it makes the OS reserve an index and give every thread a block for it; the native loader |
| 77 | // reuses that index for the mapped image. One helper means one mapped TLS module at a time. | 78 | // reuses that index for the mapped image. When more than one manually mapped module uses static TLS at once, the loader |
| 79 | // clones gta6rt_tls_host.dll to a per-process temporary path on demand and loads it, so the OS assigns an independent | ||
| 80 | // module TLS index for each simultaneous module (up to kMaxMappedTlsModules). | ||
| 78 | constexpr DWORD kMaxModuleTlsSlots = 64; | 81 | constexpr DWORD kMaxModuleTlsSlots = 64; |
| 82 | constexpr size_t kMaxMappedTlsModules = 16; | ||
| 79 | void** module_tls_slots() { return reinterpret_cast<void**>(__readgsqword(0x58)); } | 83 | void** module_tls_slots() { return reinterpret_cast<void**>(__readgsqword(0x58)); } |
| 80 | 84 | ||
| 81 | void __cdecl on_host_thread_event(DWORD reason); | 85 | void __cdecl on_host_thread_event(DWORD reason); |
| @@ -85,22 +89,64 @@ struct TlsHostModule { | |||
| 85 | DWORD index = TLS_OUT_OF_INDEXES; | 89 | DWORD index = TLS_OUT_OF_INDEXES; |
| 86 | }; | 90 | }; |
| 87 | 91 | ||
| 88 | TlsHostModule load_tls_host() { | 92 | std::filesystem::path tls_host_base_path() { |
| 89 | TlsHostModule host; | ||
| 90 | // Dynamic buffer loop for paths that exceed MAX_PATH | ||
| 91 | std::wstring exe_path; | 93 | std::wstring exe_path; |
| 92 | DWORD size = MAX_PATH; | 94 | DWORD size = MAX_PATH; |
| 93 | while (true) { | 95 | while (true) { |
| 94 | exe_path.resize(size); | 96 | exe_path.resize(size); |
| 95 | DWORD len = GetModuleFileNameW(nullptr, exe_path.data(), size); | 97 | DWORD len = GetModuleFileNameW(nullptr, exe_path.data(), size); |
| 96 | if (len == 0) return host; | 98 | if (len == 0) return {}; |
| 97 | if (len < size - 1) { | 99 | if (len < size - 1) { |
| 98 | exe_path.resize(len); | 100 | exe_path.resize(len); |
| 99 | break; | 101 | break; |
| 100 | } | 102 | } |
| 101 | size *= 2; | 103 | size *= 2; |
| 102 | } | 104 | } |
| 103 | const std::filesystem::path path = std::filesystem::path(exe_path).parent_path() / L"gta6rt_tls_host.dll"; | 105 | return std::filesystem::path(exe_path).parent_path() / L"gta6rt_tls_host.dll"; |
| 106 | } | ||
| 107 | |||
| 108 | std::filesystem::path tls_host_clone_dir() { | ||
| 109 | wchar_t buf[MAX_PATH]; | ||
| 110 | DWORD n = GetTempPathW(MAX_PATH, buf); | ||
| 111 | if (n > 0 && n < MAX_PATH) return std::filesystem::path(buf); | ||
| 112 | return std::filesystem::temp_directory_path(); | ||
| 113 | } | ||
| 114 | |||
| 115 | struct TlsCloneRecord { | ||
| 116 | HMODULE module = nullptr; | ||
| 117 | std::filesystem::path path; | ||
| 118 | }; | ||
| 119 | |||
| 120 | std::mutex g_tls_alloc_mutex; | ||
| 121 | std::array<TlsHostModule, kMaxMappedTlsModules> g_tls_hosts{}; | ||
| 122 | std::array<std::atomic<Module*>, kMaxMappedTlsModules> g_tls_slot_owners{}; | ||
| 123 | std::vector<TlsCloneRecord> g_tls_clones; | ||
| 124 | |||
| 125 | void cleanup_tls_clones_at_exit() { | ||
| 126 | for (const auto& rec : g_tls_clones) { | ||
| 127 | if (rec.module) FreeLibrary(rec.module); | ||
| 128 | if (!rec.path.empty()) DeleteFileW(rec.path.c_str()); | ||
| 129 | } | ||
| 130 | g_tls_clones.clear(); | ||
| 131 | } | ||
| 132 | |||
| 133 | void sweep_stale_tls_clones(const std::filesystem::path& dir) { | ||
| 134 | WIN32_FIND_DATAW fd{}; | ||
| 135 | const std::filesystem::path pattern = dir / L"leonida_tls_host_*.dll"; | ||
| 136 | HANDLE h = FindFirstFileW(pattern.c_str(), &fd); | ||
| 137 | if (h == INVALID_HANDLE_VALUE) return; | ||
| 138 | do { | ||
| 139 | if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { | ||
| 140 | // Fails harmlessly for clones still mapped in live processes; deletes orphaned ones from exited processes. | ||
| 141 | DeleteFileW((dir / fd.cFileName).c_str()); | ||
| 142 | } | ||
| 143 | } while (FindNextFileW(h, &fd)); | ||
| 144 | FindClose(h); | ||
| 145 | } | ||
| 146 | |||
| 147 | TlsHostModule load_tls_host_image(const std::filesystem::path& path, bool hook_thread_events) { | ||
| 148 | TlsHostModule host; | ||
| 149 | if (path.empty()) return host; | ||
| 104 | // The helper is linked with the static CRT (/MT), so it needs only system DLLs. Restricting the dependency search | 150 | // The helper is linked with the static CRT (/MT), so it needs only system DLLs. Restricting the dependency search |
| 105 | // to System32 keeps a game-supplied vcruntime next to the executable from being picked up. | 151 | // to System32 keeps a game-supplied vcruntime next to the executable from being picked up. |
| 106 | host.module = LoadLibraryExW(path.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); | 152 | host.module = LoadLibraryExW(path.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); |
| @@ -122,31 +168,81 @@ TlsHostModule load_tls_host() { | |||
| 122 | return host; | 168 | return host; |
| 123 | } | 169 | } |
| 124 | host.index = *reinterpret_cast<DWORD*>(tls->AddressOfIndex); | 170 | host.index = *reinterpret_cast<DWORD*>(tls->AddressOfIndex); |
| 125 | // From here on the helper's DllMain reports every thread the process starts or ends (on_host_thread_event). | 171 | if (hook_thread_events) { |
| 126 | using SetThreadEvent = void(__cdecl*)(void(__cdecl*)(DWORD)); | 172 | // Only the primary helper reports thread attach/detach events; on_host_thread_event iterates all active slots. |
| 127 | if (auto set = reinterpret_cast<SetThreadEvent>(GetProcAddress(host.module, "gta6rt_tls_host_set_thread_event"))) | 173 | using SetThreadEvent = void(__cdecl*)(void(__cdecl*)(DWORD)); |
| 128 | set(&on_host_thread_event); | 174 | if (auto set = reinterpret_cast<SetThreadEvent>(GetProcAddress(host.module, "gta6rt_tls_host_set_thread_event"))) |
| 175 | set(&on_host_thread_event); | ||
| 176 | } | ||
| 129 | return host; | 177 | return host; |
| 130 | } | 178 | } |
| 131 | 179 | ||
| 132 | TlsHostModule& tls_host() { | 180 | // Must be called with g_tls_alloc_mutex held and WITHOUT g_tls_lock held (LoadLibraryExW takes the Windows loader lock). |
| 133 | // The helper stays loaded for the process lifetime: unloading it would release the reserved TLS index. Its own | 181 | TlsHostModule ensure_tls_host(size_t slot) { |
| 134 | // per-thread blocks are restored by uninstall_tls_for_thread, so the OS loader can free them normally. | 182 | if (slot >= kMaxMappedTlsModules) return {}; |
| 135 | static TlsHostModule host = load_tls_host(); | 183 | if (g_tls_hosts[slot].index != TLS_OUT_OF_INDEXES) return g_tls_hosts[slot]; |
| 136 | return host; | 184 | |
| 185 | const std::filesystem::path primary = tls_host_base_path(); | ||
| 186 | if (primary.empty()) return {}; | ||
| 187 | |||
| 188 | if (slot == 0) { | ||
| 189 | g_tls_hosts[0] = load_tls_host_image(primary, true); | ||
| 190 | return g_tls_hosts[0]; | ||
| 191 | } | ||
| 192 | |||
| 193 | // Ensure slot 0 is loaded first so thread attach/detach events are hooked. | ||
| 194 | if (g_tls_hosts[0].index == TLS_OUT_OF_INDEXES) { | ||
| 195 | g_tls_hosts[0] = load_tls_host_image(primary, true); | ||
| 196 | if (g_tls_hosts[0].index == TLS_OUT_OF_INDEXES) return {}; | ||
| 197 | } | ||
| 198 | |||
| 199 | // Clone gta6rt_tls_host.dll to a distinct path using CopyFileW (Wine 9 lacks CopyFile2 used by std::filesystem). | ||
| 200 | const std::filesystem::path dir = tls_host_clone_dir(); | ||
| 201 | static bool swept = false; | ||
| 202 | if (!swept) { | ||
| 203 | swept = true; | ||
| 204 | sweep_stale_tls_clones(dir); | ||
| 205 | std::atexit(&cleanup_tls_clones_at_exit); | ||
| 206 | } | ||
| 207 | const std::wstring name = | ||
| 208 | L"leonida_tls_host_" + std::to_wstring(GetCurrentProcessId()) + L"_" + std::to_wstring(slot) + L".dll"; | ||
| 209 | const std::filesystem::path clone_path = dir / name; | ||
| 210 | if (!CopyFileW(primary.c_str(), clone_path.c_str(), FALSE)) return {}; | ||
| 211 | |||
| 212 | // Hold a read-only-shared handle across LoadLibraryExW so another process cannot overwrite the clone in between. | ||
| 213 | HANDLE guard = CreateFileW(clone_path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, nullptr, | ||
| 214 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); | ||
| 215 | TlsHostModule loaded = load_tls_host_image(clone_path, false); | ||
| 216 | if (guard != INVALID_HANDLE_VALUE) CloseHandle(guard); | ||
| 217 | |||
| 218 | if (loaded.index == TLS_OUT_OF_INDEXES) { | ||
| 219 | DeleteFileW(clone_path.c_str()); | ||
| 220 | return {}; | ||
| 221 | } | ||
| 222 | for (size_t i = 0; i < kMaxMappedTlsModules; ++i) { | ||
| 223 | if (i != slot && g_tls_hosts[i].index == loaded.index) { | ||
| 224 | FreeLibrary(loaded.module); | ||
| 225 | DeleteFileW(clone_path.c_str()); | ||
| 226 | return {}; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | g_tls_clones.push_back({loaded.module, clone_path}); | ||
| 230 | g_tls_hosts[slot] = loaded; | ||
| 231 | return g_tls_hosts[slot]; | ||
| 137 | } | 232 | } |
| 138 | 233 | ||
| 139 | std::atomic<Module*> g_tls_slot_owner{nullptr}; | 234 | struct ThreadTlsSlot { |
| 235 | Module* module = nullptr; | ||
| 236 | void* saved = nullptr; | ||
| 237 | void* block = nullptr; | ||
| 238 | uint32_t index = kNoTlsIndex; | ||
| 239 | }; | ||
| 140 | 240 | ||
| 141 | // Per-thread installation state. Only one mapped TLS module can own the reserved slot, so one entry per thread is | 241 | // Per-thread installation state for each reserved TLS slot. |
| 142 | // enough; the saved pointer is the helper DLL's own block that the OS loader will free at thread exit. | 242 | thread_local std::array<ThreadTlsSlot, kMaxMappedTlsModules> g_thread_tls_slots{}; |
| 143 | thread_local Module* g_thread_tls_module = nullptr; | ||
| 144 | thread_local void* g_thread_tls_saved = nullptr; | ||
| 145 | thread_local void* g_thread_tls_block = nullptr; | ||
| 146 | thread_local uint32_t g_thread_tls_index = kNoTlsIndex; | ||
| 147 | 243 | ||
| 148 | // Orders the TLS owner's publication, its `initialised` flag and its retirement against the thread events. Shared in | 244 | // Orders the TLS owners' publication, their `initialised` flags and their retirement against the thread events. Shared |
| 149 | // on_host_thread_event, exclusive only for those short writes (no allocation and no call that takes the Windows | 245 | // in on_host_thread_event, exclusive only for those short writes (no allocation and no call that takes the Windows |
| 150 | // loader lock, which the events run under). | 246 | // loader lock, which the events run under). |
| 151 | SRWLOCK g_tls_lock = SRWLOCK_INIT; | 247 | SRWLOCK g_tls_lock = SRWLOCK_INIT; |
| 152 | struct SharedTlsLock { | 248 | struct SharedTlsLock { |
| @@ -164,30 +260,38 @@ struct ExclusiveTlsLock { | |||
| 164 | 260 | ||
| 165 | // Puts the helper DLL's own block back in this thread's slot and frees the copy that replaced it. Uses only this | 261 | // Puts the helper DLL's own block back in this thread's slot and frees the copy that replaced it. Uses only this |
| 166 | // thread's record, so it also works after the module that installed the copy is gone. | 262 | // thread's record, so it also works after the module that installed the copy is gone. |
| 167 | void release_thread_block() { | 263 | void release_thread_slot(size_t slot) { |
| 168 | if (!g_thread_tls_block) return; | 264 | if (slot >= kMaxMappedTlsModules) return; |
| 169 | void** slots = module_tls_slots(); | 265 | ThreadTlsSlot& entry = g_thread_tls_slots[slot]; |
| 170 | if (slots && g_thread_tls_index < kMaxModuleTlsSlots && slots[g_thread_tls_index] == g_thread_tls_block) | 266 | if (!entry.module && !entry.block) return; |
| 171 | slots[g_thread_tls_index] = g_thread_tls_saved; | 267 | if (entry.block) { |
| 172 | VirtualFree(g_thread_tls_block, 0, MEM_RELEASE); | 268 | void** slots = module_tls_slots(); |
| 173 | g_thread_tls_module = nullptr; | 269 | if (slots && entry.index < kMaxModuleTlsSlots && slots[entry.index] == entry.block) |
| 174 | g_thread_tls_saved = nullptr; | 270 | slots[entry.index] = entry.saved; |
| 175 | g_thread_tls_block = nullptr; | 271 | VirtualFree(entry.block, 0, MEM_RELEASE); |
| 176 | g_thread_tls_index = kNoTlsIndex; | 272 | } |
| 273 | entry = ThreadTlsSlot{}; | ||
| 177 | } | 274 | } |
| 178 | 275 | ||
| 179 | void install_thread_block(Module& m) { | 276 | void install_thread_block(Module& m) { |
| 180 | if (m.tls_index == kNoTlsIndex) return; | 277 | if (m.tls_slot >= kMaxMappedTlsModules) return; |
| 181 | if (g_thread_tls_module == &m) return; | 278 | const size_t slot = m.tls_slot; |
| 182 | if (g_thread_tls_module) { | 279 | ThreadTlsSlot& entry = g_thread_tls_slots[slot]; |
| 280 | if (entry.module == &m) return; | ||
| 281 | if (entry.module) { | ||
| 183 | // A copy left by a module whose Loader is gone is released; a live module's is a conflict. | 282 | // A copy left by a module whose Loader is gone is released; a live module's is a conflict. |
| 184 | if (g_thread_tls_module == g_tls_slot_owner.load()) | 283 | if (entry.module == g_tls_slot_owners[slot].load()) |
| 185 | throw std::runtime_error("another manually mapped module's TLS is already installed in this thread"); | 284 | throw std::runtime_error("another manually mapped module's TLS is already installed in this thread slot"); |
| 186 | release_thread_block(); | 285 | release_thread_slot(slot); |
| 187 | } | 286 | } |
| 188 | 287 | ||
| 189 | const uint64_t total = static_cast<uint64_t>(m.tls_raw_size) + m.tls_zero_fill; | 288 | const uint64_t total = static_cast<uint64_t>(m.tls_raw_size) + m.tls_zero_fill; |
| 190 | if (!total) return; | 289 | if (!total) { |
| 290 | // Callback-only TLS module (no data block): still record attachment in this slot so DLL_THREAD_DETACH runs. | ||
| 291 | entry.module = &m; | ||
| 292 | return; | ||
| 293 | } | ||
| 294 | if (m.tls_index == kNoTlsIndex) return; | ||
| 191 | void** slots = module_tls_slots(); | 295 | void** slots = module_tls_slots(); |
| 192 | if (!slots || m.tls_index >= kMaxModuleTlsSlots) | 296 | if (!slots || m.tls_index >= kMaxModuleTlsSlots) |
| 193 | throw std::runtime_error(m.name + ": cannot install TLS in this thread"); | 297 | throw std::runtime_error(m.name + ": cannot install TLS in this thread"); |
| @@ -200,19 +304,22 @@ void install_thread_block(Module& m) { | |||
| 200 | 304 | ||
| 201 | void* saved = slots[m.tls_index]; | 305 | void* saved = slots[m.tls_index]; |
| 202 | slots[m.tls_index] = block; | 306 | slots[m.tls_index] = block; |
| 203 | g_thread_tls_module = &m; | 307 | entry.module = &m; |
| 204 | g_thread_tls_saved = saved; | 308 | entry.saved = saved; |
| 205 | g_thread_tls_block = block; | 309 | entry.block = block; |
| 206 | g_thread_tls_index = m.tls_index; | 310 | entry.index = m.tls_index; |
| 207 | } | 311 | } |
| 208 | 312 | ||
| 209 | void uninstall_thread_block(Module& m) { | 313 | void uninstall_thread_block(Module& m) { |
| 210 | if (g_thread_tls_module != &m) return; | 314 | if (m.tls_slot >= kMaxMappedTlsModules) return; |
| 211 | if (m.tls_block == g_thread_tls_block) { | 315 | const size_t slot = m.tls_slot; |
| 316 | ThreadTlsSlot& entry = g_thread_tls_slots[slot]; | ||
| 317 | if (entry.module != &m) return; | ||
| 318 | if (m.tls_block == entry.block) { | ||
| 212 | m.tls_block = nullptr; | 319 | m.tls_block = nullptr; |
| 213 | m.tls_saved_block = nullptr; | 320 | m.tls_saved_block = nullptr; |
| 214 | } | 321 | } |
| 215 | release_thread_block(); | 322 | release_thread_slot(slot); |
| 216 | } | 323 | } |
| 217 | 324 | ||
| 218 | // `trace`: record each callback in the startup trace. Only the Loader's own calls (process attach and detach) pass | 325 | // `trace`: record each callback in the startup trace. Only the Loader's own calls (process attach and detach) pass |
| @@ -238,24 +345,28 @@ void run_callbacks(Module& m, uint32_t reason, bool trace = false) { | |||
| 238 | } | 345 | } |
| 239 | 346 | ||
| 240 | // Runs in gta6rt_tls_host.dll's DllMain for each thread the process starts or ends once the helper is loaded, under | 347 | // Runs in gta6rt_tls_host.dll's DllMain for each thread the process starts or ends once the helper is loaded, under |
| 241 | // the Windows loader lock, on that thread. A new thread gets its own copy of the TLS owner's template; an ending one | 348 | // the Windows loader lock, on that thread. A new thread gets its own copy of each active TLS owner's template; an |
| 242 | // gives the helper's block back before the Windows loader frees it. TLS callbacks get the thread events only after | 349 | // ending one gives each helper's block back before the Windows loader frees it. TLS callbacks get the thread events |
| 243 | // the module's DLL_PROCESS_ATTACH (initialise()), as the Windows loader orders them. Threads that existed before the | 350 | // only after the module's DLL_PROCESS_ATTACH (initialise()), as the Windows loader orders them. |
| 244 | // module was loaded get nothing: install_tls_for_thread is still the way to cover those. | ||
| 245 | void __cdecl on_host_thread_event(DWORD reason) { | 351 | void __cdecl on_host_thread_event(DWORD reason) { |
| 246 | SharedTlsLock lock; | 352 | SharedTlsLock lock; |
| 247 | Module* m = g_tls_slot_owner.load(); | ||
| 248 | try { | 353 | try { |
| 249 | if (reason == DLL_THREAD_ATTACH) { | 354 | if (reason == DLL_THREAD_ATTACH) { |
| 250 | if (!m) return; | 355 | for (size_t slot = 0; slot < kMaxMappedTlsModules; ++slot) { |
| 251 | install_thread_block(*m); | 356 | Module* m = g_tls_slot_owners[slot].load(); |
| 252 | if (m->initialised) run_callbacks(*m, DLL_THREAD_ATTACH); | 357 | if (!m) continue; |
| 358 | install_thread_block(*m); | ||
| 359 | if (m->initialised) run_callbacks(*m, DLL_THREAD_ATTACH); | ||
| 360 | } | ||
| 253 | } else if (reason == DLL_THREAD_DETACH) { | 361 | } else if (reason == DLL_THREAD_DETACH) { |
| 254 | if (m && g_thread_tls_module == m) { | 362 | for (size_t i = kMaxMappedTlsModules; i-- > 0;) { |
| 255 | if (m->initialised) run_callbacks(*m, DLL_THREAD_DETACH); | 363 | Module* m = g_tls_slot_owners[i].load(); |
| 256 | uninstall_thread_block(*m); | 364 | if (m && g_thread_tls_slots[i].module == m) { |
| 257 | } else { | 365 | if (m->initialised) run_callbacks(*m, DLL_THREAD_DETACH); |
| 258 | release_thread_block(); // installed by a module whose Loader is gone (or nothing to do) | 366 | uninstall_thread_block(*m); |
| 367 | } else { | ||
| 368 | release_thread_slot(i); // installed by a module whose Loader is gone (or nothing to do) | ||
| 369 | } | ||
| 259 | } | 370 | } |
| 260 | } | 371 | } |
| 261 | } catch (const std::exception& e) { | 372 | } catch (const std::exception& e) { |
| @@ -407,13 +518,13 @@ Loader::~Loader() { | |||
| 407 | shims::remove_module_lookup(this); | 518 | shims::remove_module_lookup(this); |
| 408 | for (auto* m : order_) { | 519 | for (auto* m : order_) { |
| 409 | // TLS state must go away before the image and its TLS block are unmapped. | 520 | // TLS state must go away before the image and its TLS block are unmapped. |
| 410 | if (m->tls_index != kNoTlsIndex) { | 521 | if (m->tls_index != kNoTlsIndex && m->tls_slot < kMaxMappedTlsModules) { |
| 411 | // Retired first, under the lock: no thread event installs it or runs its callbacks after this, and a | 522 | // Retired first, under the lock: no thread event installs it or runs its callbacks after this, and a |
| 412 | // thread that still holds a copy frees it when it ends (on_host_thread_event). | 523 | // thread that still holds a copy frees it when it ends (on_host_thread_event). |
| 413 | { | 524 | { |
| 414 | ExclusiveTlsLock lock; | 525 | ExclusiveTlsLock lock; |
| 415 | Module* owner = m; | 526 | Module* owner = m; |
| 416 | g_tls_slot_owner.compare_exchange_strong(owner, nullptr); | 527 | g_tls_slot_owners[m->tls_slot].compare_exchange_strong(owner, nullptr); |
| 417 | } | 528 | } |
| 418 | uninstall_tls_for_thread(*m); | 529 | uninstall_tls_for_thread(*m); |
| 419 | } | 530 | } |
| @@ -737,19 +848,36 @@ void Loader::initialize_tls(Module& m) { | |||
| 737 | require_range(m.name, index_rva, sizeof(uint32_t), m.size, "the TLS index"); | 848 | require_range(m.name, index_rva, sizeof(uint32_t), m.size, "the TLS index"); |
| 738 | } | 849 | } |
| 739 | 850 | ||
| 740 | DWORD index = tls_host().index; | ||
| 741 | if (index == TLS_OUT_OF_INDEXES) | ||
| 742 | throw std::runtime_error(m.name + ": gta6rt_tls_host.dll is missing or has no module TLS index"); | ||
| 743 | if (index >= kMaxModuleTlsSlots) throw std::runtime_error(m.name + ": reserved TLS index is out of range"); | ||
| 744 | if (!module_tls_slots()) throw std::runtime_error(m.name + ": this thread has no TLS array"); | 851 | if (!module_tls_slots()) throw std::runtime_error(m.name + ": this thread has no TLS array"); |
| 745 | 852 | ||
| 853 | size_t slot = kMaxMappedTlsModules; | ||
| 854 | DWORD index = TLS_OUT_OF_INDEXES; | ||
| 746 | { | 855 | { |
| 856 | std::lock_guard alloc_lock(g_tls_alloc_mutex); | ||
| 857 | for (size_t i = 0; i < kMaxMappedTlsModules; ++i) { | ||
| 858 | if (g_tls_slot_owners[i].load() == nullptr) { | ||
| 859 | slot = i; | ||
| 860 | break; | ||
| 861 | } | ||
| 862 | } | ||
| 863 | if (slot >= kMaxMappedTlsModules) | ||
| 864 | throw std::runtime_error(m.name + ": all " + std::to_string(kMaxMappedTlsModules) + | ||
| 865 | " manually mapped static TLS slots are in use"); | ||
| 866 | |||
| 867 | // Load/clone gta6rt_tls_host.dll OUTSIDE ExclusiveTlsLock so LoadLibraryExW cannot deadlock with | ||
| 868 | // on_host_thread_event (which runs under the Windows loader lock and takes SharedTlsLock). | ||
| 869 | index = ensure_tls_host(slot).index; | ||
| 870 | if (index == TLS_OUT_OF_INDEXES) | ||
| 871 | throw std::runtime_error(m.name + ": gta6rt_tls_host.dll is missing or has no module TLS index"); | ||
| 872 | if (index >= kMaxModuleTlsSlots) throw std::runtime_error(m.name + ": reserved TLS index is out of range"); | ||
| 873 | |||
| 747 | // The fields are complete before a thread event can see the owner. | 874 | // The fields are complete before a thread event can see the owner. |
| 748 | ExclusiveTlsLock lock; | 875 | ExclusiveTlsLock lock; |
| 749 | Module* expected = nullptr; | 876 | Module* expected = nullptr; |
| 750 | if (!g_tls_slot_owner.compare_exchange_strong(expected, &m)) | 877 | if (!g_tls_slot_owners[slot].compare_exchange_strong(expected, &m)) |
| 751 | throw std::runtime_error(m.name + ": only one manually mapped module may use static TLS at a time"); | 878 | throw std::runtime_error(m.name + ": static TLS slot was claimed concurrently"); |
| 752 | 879 | ||
| 880 | m.tls_slot = static_cast<uint32_t>(slot); | ||
| 753 | m.tls_index = index; | 881 | m.tls_index = index; |
| 754 | m.tls_template = raw ? m.base + raw_rva : nullptr; | 882 | m.tls_template = raw ? m.base + raw_rva : nullptr; |
| 755 | m.tls_raw_size = static_cast<uint32_t>(raw); | 883 | m.tls_raw_size = static_cast<uint32_t>(raw); |
| @@ -764,13 +892,14 @@ void Loader::initialize_tls(Module& m) { | |||
| 764 | } catch (...) { | 892 | } catch (...) { |
| 765 | ExclusiveTlsLock lock; | 893 | ExclusiveTlsLock lock; |
| 766 | m.tls_index = kNoTlsIndex; | 894 | m.tls_index = kNoTlsIndex; |
| 767 | g_tls_slot_owner = nullptr; | 895 | m.tls_slot = kNoTlsIndex; |
| 896 | g_tls_slot_owners[slot].store(nullptr); | ||
| 768 | throw; | 897 | throw; |
| 769 | } | 898 | } |
| 770 | // The loading thread's copy is the one the Module records (and ~Loader frees on that thread). | 899 | // The loading thread's copy is the one the Module records (and ~Loader frees on that thread). |
| 771 | if (g_thread_tls_module == &m) { | 900 | if (g_thread_tls_slots[slot].module == &m) { |
| 772 | m.tls_block = g_thread_tls_block; | 901 | m.tls_block = g_thread_tls_slots[slot].block; |
| 773 | m.tls_saved_block = g_thread_tls_saved; | 902 | m.tls_saved_block = g_thread_tls_slots[slot].saved; |
| 774 | } | 903 | } |
| 775 | } | 904 | } |
| 776 | 905 | ||