diff options
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 396d362f8..d495bb53f 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -424,6 +424,8 @@ struct ThreadTlsSlot { | |||
| 424 | void* saved = nullptr; | 424 | void* saved = nullptr; |
| 425 | void* block = nullptr; | 425 | void* block = nullptr; |
| 426 | uint32_t index = kNoTlsIndex; | 426 | uint32_t index = kNoTlsIndex; |
| 427 | void** ga_view = nullptr; // the thread's slot-0 view for a /GA EXE (Module::tls_ga_slot) | ||
| 428 | uint32_t ga_slot = kNoTlsIndex; | ||
| 427 | }; | 429 | }; |
| 428 | 430 | ||
| 429 | // Per-thread installation state for each reserved TLS slot. | 431 | // Per-thread installation state for each reserved TLS slot. |
| @@ -627,6 +629,10 @@ void release_thread_slot(size_t slot) { | |||
| 627 | if (slot >= kTlsSlotCount) return; | 629 | if (slot >= kTlsSlotCount) return; |
| 628 | ThreadTlsSlot& entry = g_thread_tls_slots[slot]; | 630 | ThreadTlsSlot& entry = g_thread_tls_slots[slot]; |
| 629 | if (!entry.module && !entry.block) return; | 631 | if (!entry.module && !entry.block) return; |
| 632 | if (entry.ga_view) { | ||
| 633 | if (TlsGetValue(entry.ga_slot) == entry.ga_view) TlsSetValue(entry.ga_slot, nullptr); | ||
| 634 | VirtualFree(entry.ga_view, 0, MEM_RELEASE); | ||
| 635 | } | ||
| 630 | if (entry.block) { | 636 | if (entry.block) { |
| 631 | void** slots = module_tls_slots(); | 637 | void** slots = module_tls_slots(); |
| 632 | if (slots && entry.index < kMaxModuleTlsSlots && slots[entry.index] == entry.block) | 638 | if (slots && entry.index < kMaxModuleTlsSlots && slots[entry.index] == entry.block) |
| @@ -708,6 +714,20 @@ void install_thread_block(Module& m) { | |||
| 708 | if (m.tls_zero_fill) | 714 | if (m.tls_zero_fill) |
| 709 | std::memset(static_cast<uint8_t*>(block) + m.tls_raw_size, 0, m.tls_zero_fill); | 715 | std::memset(static_cast<uint8_t*>(block) + m.tls_raw_size, 0, m.tls_zero_fill); |
| 710 | 716 | ||
| 717 | void** view = nullptr; | ||
| 718 | if (m.tls_ga_slot != kNoTlsIndex) { | ||
| 719 | // The /GA view: [0] and [tls_index] are this thread's block, the rest stays null (the image reads no other). | ||
| 720 | view = static_cast<void**>( | ||
| 721 | VirtualAlloc(nullptr, kMaxModuleTlsSlots * sizeof(void*), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | ||
| 722 | if (!view) { | ||
| 723 | VirtualFree(block, 0, MEM_RELEASE); | ||
| 724 | throw std::runtime_error(m.name + ": TLS view allocation failed"); | ||
| 725 | } | ||
| 726 | view[0] = block; | ||
| 727 | view[m.tls_index] = block; | ||
| 728 | TlsSetValue(m.tls_ga_slot, view); | ||
| 729 | } | ||
| 730 | |||
| 711 | void* saved = slots[m.tls_index]; | 731 | void* saved = slots[m.tls_index]; |
| 712 | slots[m.tls_index] = block; | 732 | slots[m.tls_index] = block; |
| 713 | entry.module = &m; | 733 | entry.module = &m; |
| @@ -715,6 +735,45 @@ void install_thread_block(Module& m) { | |||
| 715 | entry.saved = saved; | 735 | entry.saved = saved; |
| 716 | entry.block = block; | 736 | entry.block = block; |
| 717 | entry.index = m.tls_index; | 737 | entry.index = m.tls_index; |
| 738 | entry.ga_view = view; | ||
| 739 | entry.ga_slot = m.tls_ga_slot; | ||
| 740 | } | ||
| 741 | |||
| 742 | // Rewrites a /GA EXE's slot-0 TLS reads (Module::tls_ga_slot): every `mov r64, gs:[58h]` (65 REX.W 8B /r with | ||
| 743 | // mod=00, r/m=100, SIB=25, disp32=58h; REX 48 or 4C for r8-r15) in an executable section becomes the same | ||
| 744 | // instruction with disp32 = 1480h + 8 * slot, TEB->TlsSlots[slot]. Same length, so nothing else moves. The 9-byte | ||
| 745 | // form, with its segment prefix, SIB 25 and that displacement, is not something code or tables produce by accident. | ||
| 746 | // Must run before protect_sections() and before any of the image's code (its TLS callbacks included). | ||
| 747 | size_t patch_ga_tls_reads(Module& m, uint32_t slot) { | ||
| 748 | const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(m.base); | ||
| 749 | const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(m.base + dos->e_lfanew); | ||
| 750 | const IMAGE_SECTION_HEADER* sec = IMAGE_FIRST_SECTION(nt); | ||
| 751 | const uint32_t disp = 0x1480u + 8u * slot; | ||
| 752 | size_t sites = 0; | ||
| 753 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { | ||
| 754 | if (!(sec->Characteristics & IMAGE_SCN_MEM_EXECUTE)) continue; | ||
| 755 | const uint32_t size = (std::min)(sec->Misc.VirtualSize, sec->SizeOfRawData); | ||
| 756 | if (sec->VirtualAddress > m.size || size > m.size - sec->VirtualAddress || size < 9) continue; | ||
| 757 | uint8_t* p = m.base + sec->VirtualAddress; | ||
| 758 | for (uint32_t k = 0; k + 9 <= size; ++k) { | ||
| 759 | if (p[k] != 0x65 || (p[k + 1] != 0x48 && p[k + 1] != 0x4C) || p[k + 2] != 0x8B) continue; | ||
| 760 | const uint8_t modrm = p[k + 3]; | ||
| 761 | if ((modrm & 0xC7) != 0x04 || p[k + 4] != 0x25) continue; | ||
| 762 | uint32_t d; | ||
| 763 | std::memcpy(&d, p + k + 5, 4); | ||
| 764 | if (d != 0x58) continue; | ||
| 765 | std::memcpy(p + k + 5, &disp, 4); | ||
| 766 | ++sites; | ||
| 767 | k += 8; | ||
| 768 | } | ||
| 769 | } | ||
| 770 | return sites; | ||
| 771 | } | ||
| 772 | |||
| 773 | bool ga_tls_patch_enabled() { | ||
| 774 | char v[8] = {}; | ||
| 775 | size_t n = 0; | ||
| 776 | return !(getenv_s(&n, v, "LEONIDA_TLS_GA_PATCH") == 0 && n == 2 && v[0] == '0'); | ||
| 718 | } | 777 | } |
| 719 | 778 | ||
| 720 | void uninstall_thread_block(Module& m) { | 779 | void uninstall_thread_block(Module& m) { |
| @@ -1771,6 +1830,26 @@ void Loader::initialize_tls(Module& m) { | |||
| 1771 | // VirtualProtect dance. | 1830 | // VirtualProtect dance. |
| 1772 | if (index_rva) *reinterpret_cast<uint32_t*>(m.base + index_rva) = index; | 1831 | if (index_rva) *reinterpret_cast<uint32_t*>(m.base + index_rva) = index; |
| 1773 | } | 1832 | } |
| 1833 | { | ||
| 1834 | // An EXE's code may read its TLS at index 0 directly (/GA); index 0 is gta6rt.exe's here (pe_loader.h, | ||
| 1835 | // tls_ga_slot). Patched before any of the image's code runs, TLS callbacks included. | ||
| 1836 | const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(m.base); | ||
| 1837 | const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(m.base + dos->e_lfanew); | ||
| 1838 | if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && total && index != 0 && ga_tls_patch_enabled()) { | ||
| 1839 | static const DWORD ga_slot = TlsAlloc(); | ||
| 1840 | if (ga_slot == TLS_OUT_OF_INDEXES || ga_slot >= 64) { | ||
| 1841 | std::fprintf(stderr, "[rt] loader: %s: no TlsAlloc slot below 64 for its index-0 TLS reads\n", | ||
| 1842 | m.name.c_str()); | ||
| 1843 | } else { | ||
| 1844 | m.tls_ga_sites = patch_ga_tls_reads(m, ga_slot); | ||
| 1845 | if (m.tls_ga_sites) m.tls_ga_slot = ga_slot; | ||
| 1846 | std::fprintf(stderr, | ||
| 1847 | "[rt] loader: %s: %zu index-0 TLS reads (gs:[58h]) now read its own TLS (TlsSlots[%lu]); " | ||
| 1848 | "its module TLS index is %u\n", | ||
| 1849 | m.name.c_str(), m.tls_ga_sites, static_cast<unsigned long>(ga_slot), index); | ||
| 1850 | } | ||
| 1851 | } | ||
| 1852 | } | ||
| 1774 | try { | 1853 | try { |
| 1775 | install_thread_block(m); | 1854 | install_thread_block(m); |
| 1776 | } catch (...) { | 1855 | } catch (...) { |