diff options
| author | Tyrannys <80750971+Tyrannys@users.noreply.github.com> | 2026-11-27 13:40:23 -0600 |
|---|---|---|
| committer | Tyrannys <80750971+Tyrannys@users.noreply.github.com> | 2026-11-27 13:40:23 -0600 |
| commit | 1a49f137576cc2051a0ee49f3f3731506054e1ce (patch) | |
| tree | 6ceb8f45f05c236dadc04e6df6e1c3645a8ca6ba /runtime/src/pe_loader.cpp | |
| parent | 43a9d562d355e7c5c428e486071b456353de8b05 (diff) | |
loader: wide run-time loads are dynamic; set tls_ga_slot before publishing (RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT)
Loader::load_runtime_module_w (LoadLibraryW / LoadLibraryExW of a
bundled DLL) did not open a RuntimeLoadScope, unlike the narrow path and
resolve_module_export. The module was mapped as a static load, so its
DllMain(DLL_PROCESS_ATTACH) got a non-NULL lpvReserved. It now opens the
scope too.
Loader::initialize_tls patched an executable's /GA TLS reads and wrote
m.tls_ga_slot after it had published the slot owner and released
ExclusiveTlsLock, while a thread starting then reads m.tls_ga_slot in
install_thread_block under SharedTlsLock. The patch and the write now
happen before the owner is published.
loader_test checks the wide path's lpvReserved (fails on main);
loader_tls_index0_test checks that the noreserve executable gets its
/GA slot.
Found by kevincommits on #1608.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 41 |
1 files changed, 21 insertions, 20 deletions
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 | |||
| 1504 | if (prefer_host_copy(narrow)) return nullptr; | 1504 | if (prefer_host_copy(narrow)) return nullptr; |
| 1505 | 1505 | ||
| 1506 | std::lock_guard lock(mutex_); | 1506 | std::lock_guard lock(mutex_); |
| 1507 | // As load_runtime_module: a run-time load, so DllMain gets a null lpvReserved. | ||
| 1508 | const RuntimeLoadScope dynamic(runtime_loads_); | ||
| 1507 | Module* existing = find_wide_module(basename); | 1509 | Module* existing = find_wide_module(basename); |
| 1508 | const std::filesystem::path candidate = game_dir_ / basename; | 1510 | const std::filesystem::path candidate = game_dir_ / basename; |
| 1509 | std::error_code ec; | 1511 | std::error_code ec; |
| @@ -1890,6 +1892,25 @@ void Loader::initialize_tls(Module& m) { | |||
| 1890 | throw std::runtime_error(m.name + ": reserved TLS index is out of range"); | 1892 | throw std::runtime_error(m.name + ": reserved TLS index is out of range"); |
| 1891 | } | 1893 | } |
| 1892 | 1894 | ||
| 1895 | // An EXE's code may read its TLS at index 0 directly (/GA); index 0 is gta6rt.exe's here (pe_loader.h, | ||
| 1896 | // tls_ga_slot). Patched before any of the image's code runs, TLS callbacks included, and before the owner is | ||
| 1897 | // published below: from then on a thread event (install_thread_block) reads m.tls_ga_slot | ||
| 1898 | // (RT-LOADER-RUNTIME-SCOPE-AND-TLS-SLOT). | ||
| 1899 | if (executable && total && index != 0 && ga_tls_patch_enabled()) { | ||
| 1900 | static const DWORD ga_slot = TlsAlloc(); | ||
| 1901 | if (ga_slot == TLS_OUT_OF_INDEXES || ga_slot >= 64) { | ||
| 1902 | std::fprintf(stderr, "[rt] loader: %s: no TlsAlloc slot below 64 for its index-0 TLS reads\n", | ||
| 1903 | m.name.c_str()); | ||
| 1904 | } else { | ||
| 1905 | m.tls_ga_sites = patch_ga_tls_reads(m, ga_slot); | ||
| 1906 | if (m.tls_ga_sites) m.tls_ga_slot = ga_slot; | ||
| 1907 | std::fprintf(stderr, | ||
| 1908 | "[rt] loader: %s: %zu index-0 TLS reads (gs:[58h]) now read its own TLS (TlsSlots[%lu]); " | ||
| 1909 | "its module TLS index is %u\n", | ||
| 1910 | m.name.c_str(), m.tls_ga_sites, static_cast<unsigned long>(ga_slot), index); | ||
| 1911 | } | ||
| 1912 | } | ||
| 1913 | |||
| 1893 | // The fields are complete before a thread event can see the owner. | 1914 | // The fields are complete before a thread event can see the owner. |
| 1894 | ExclusiveTlsLock lock; | 1915 | ExclusiveTlsLock lock; |
| 1895 | Module* expected = nullptr; | 1916 | Module* expected = nullptr; |
| @@ -1908,26 +1929,6 @@ void Loader::initialize_tls(Module& m) { | |||
| 1908 | // VirtualProtect dance. | 1929 | // VirtualProtect dance. |
| 1909 | if (index_rva) *reinterpret_cast<uint32_t*>(m.base + index_rva) = index; | 1930 | if (index_rva) *reinterpret_cast<uint32_t*>(m.base + index_rva) = index; |
| 1910 | } | 1931 | } |
| 1911 | { | ||
| 1912 | // An EXE's code may read its TLS at index 0 directly (/GA); index 0 is gta6rt.exe's here (pe_loader.h, | ||
| 1913 | // tls_ga_slot). Patched before any of the image's code runs, TLS callbacks included. | ||
| 1914 | const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(m.base); | ||
| 1915 | const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(m.base + dos->e_lfanew); | ||
| 1916 | if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && total && index != 0 && ga_tls_patch_enabled()) { | ||
| 1917 | static const DWORD ga_slot = TlsAlloc(); | ||
| 1918 | if (ga_slot == TLS_OUT_OF_INDEXES || ga_slot >= 64) { | ||
| 1919 | std::fprintf(stderr, "[rt] loader: %s: no TlsAlloc slot below 64 for its index-0 TLS reads\n", | ||
| 1920 | m.name.c_str()); | ||
| 1921 | } else { | ||
| 1922 | m.tls_ga_sites = patch_ga_tls_reads(m, ga_slot); | ||
| 1923 | if (m.tls_ga_sites) m.tls_ga_slot = ga_slot; | ||
| 1924 | std::fprintf(stderr, | ||
| 1925 | "[rt] loader: %s: %zu index-0 TLS reads (gs:[58h]) now read its own TLS (TlsSlots[%lu]); " | ||
| 1926 | "its module TLS index is %u\n", | ||
| 1927 | m.name.c_str(), m.tls_ga_sites, static_cast<unsigned long>(ga_slot), index); | ||
| 1928 | } | ||
| 1929 | } | ||
| 1930 | } | ||
| 1931 | try { | 1932 | try { |
| 1932 | install_thread_block(m); | 1933 | install_thread_block(m); |
| 1933 | } catch (...) { | 1934 | } catch (...) { |