diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index fc73f37f5..f42c9d995 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -1504,6 +1504,8 @@ void* Loader::load_runtime_module_w(const std::wstring& basename, uint32_t flags if (prefer_host_copy(narrow)) return nullptr; std::lock_guard lock(mutex_); + // As load_runtime_module: a run-time load, so DllMain gets a null lpvReserved. + const RuntimeLoadScope dynamic(runtime_loads_); Module* existing = find_wide_module(basename); const std::filesystem::path candidate = game_dir_ / basename; std::error_code ec; @@ -1890,6 +1892,25 @@ void Loader::initialize_tls(Module& m) { throw std::runtime_error(m.name + ": reserved TLS index is out of range"); } + // An EXE's code may read its TLS at index 0 directly (/GA); index 0 is gta6rt.exe's here (pe_loader.h, + // tls_ga_slot). Patched before any of the image's code runs, TLS callbacks included, and before the owner is + // published below: from then on a thread event (install_thread_block) reads m.tls_ga_slot + // (RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT). + if (executable && total && index != 0 && ga_tls_patch_enabled()) { + static const DWORD ga_slot = TlsAlloc(); + if (ga_slot == TLS_OUT_OF_INDEXES || ga_slot >= 64) { + std::fprintf(stderr, "[rt] loader: %s: no TlsAlloc slot below 64 for its index-0 TLS reads\n", + m.name.c_str()); + } else { + m.tls_ga_sites = patch_ga_tls_reads(m, ga_slot); + if (m.tls_ga_sites) m.tls_ga_slot = ga_slot; + std::fprintf(stderr, + "[rt] loader: %s: %zu index-0 TLS reads (gs:[58h]) now read its own TLS (TlsSlots[%lu]); " + "its module TLS index is %u\n", + m.name.c_str(), m.tls_ga_sites, static_cast(ga_slot), index); + } + } + // The fields are complete before a thread event can see the owner. ExclusiveTlsLock lock; Module* expected = nullptr; @@ -1908,26 +1929,6 @@ void Loader::initialize_tls(Module& m) { // VirtualProtect dance. if (index_rva) *reinterpret_cast(m.base + index_rva) = index; } - { - // An EXE's code may read its TLS at index 0 directly (/GA); index 0 is gta6rt.exe's here (pe_loader.h, - // tls_ga_slot). Patched before any of the image's code runs, TLS callbacks included. - const auto* dos = reinterpret_cast(m.base); - const auto* nt = reinterpret_cast(m.base + dos->e_lfanew); - if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && total && index != 0 && ga_tls_patch_enabled()) { - static const DWORD ga_slot = TlsAlloc(); - if (ga_slot == TLS_OUT_OF_INDEXES || ga_slot >= 64) { - std::fprintf(stderr, "[rt] loader: %s: no TlsAlloc slot below 64 for its index-0 TLS reads\n", - m.name.c_str()); - } else { - m.tls_ga_sites = patch_ga_tls_reads(m, ga_slot); - if (m.tls_ga_sites) m.tls_ga_slot = ga_slot; - std::fprintf(stderr, - "[rt] loader: %s: %zu index-0 TLS reads (gs:[58h]) now read its own TLS (TlsSlots[%lu]); " - "its module TLS index is %u\n", - m.name.c_str(), m.tls_ga_sites, static_cast(ga_slot), index); - } - } - } try { install_thread_block(m); } catch (...) { diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index 6930878a9..9e39551e3 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp @@ -990,6 +990,23 @@ void test_attach_reserved() { } catch (const std::exception& e) { failed("a run-time load's DllMain gets a NULL lpvReserved", e.what()); } + + // The same load through the wide path (LoadLibraryW / LoadLibraryExW). It had no RuntimeLoadScope, so its DllMain + // got the static load's non-NULL lpvReserved (RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT, @kevincommits on #1608). + reset_notifications(); + try { + write_image_as(make_lifecycle_image(), "attach_reserved_dynamic_w.dll"); + rt::Loader loader(g_dir); + bool handled = false; + void* h = loader.load_runtime_module_w(L"attach_reserved_dynamic_w.dll", 0, handled); + const char* what = "a wide run-time load's DllMain gets a NULL lpvReserved"; + if (!handled || !h) failed(what, "the run-time load failed"); + else if (!g_attach_seen) failed(what, "DllMain was not called"); + else if (g_attach_reserved) failed(what, "lpvReserved was non-NULL"); + else pass(what); + } catch (const std::exception& e) { + failed("a wide run-time load's DllMain gets a NULL lpvReserved", e.what()); + } } void test_process_detach_dependencies() { diff --git a/runtime/test/loader_tls_index0_test.cpp b/runtime/test/loader_tls_index0_test.cpp index e9d641876..3a990e07b 100644 --- a/runtime/test/loader_tls_index0_test.cpp +++ b/runtime/test/loader_tls_index0_test.cpp @@ -534,6 +534,9 @@ __declspec(noinline) int scenario_noreserve(const fs::path& exe) { "without reserve_tls_index0 the executable gets a helper index"); check(_tls_index == 0, "and this EXE keeps module TLS index 0"); check(ga_exports(m).tls_index() == m.tls_index, "the executable's _tls_index holds that helper index"); + // Set in initialize_tls before the slot owner is published (RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT). + check(m.tls_ga_slot != rt::kNoTlsIndex && m.tls_ga_sites > 0, + "its index-0 (/GA) reads are rewritten to a TlsAlloc slot, as it does not hold index 0"); return 0; }