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 e5becb472..d85c1fe5f 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -342,6 +342,8 @@ struct ThreadTlsSlot { | |||
| 342 | void* saved = nullptr; | 342 | void* saved = nullptr; |
| 343 | void* block = nullptr; | 343 | void* block = nullptr; |
| 344 | uint32_t index = kNoTlsIndex; | 344 | uint32_t index = kNoTlsIndex; |
| 345 | void** ga_view = nullptr; // the thread's slot-0 view for a /GA EXE (Module::tls_ga_slot) | ||
| 346 | uint32_t ga_slot = kNoTlsIndex; | ||
| 345 | }; | 347 | }; |
| 346 | 348 | ||
| 347 | // Per-thread installation state for each reserved TLS slot. | 349 | // Per-thread installation state for each reserved TLS slot. |
| @@ -370,6 +372,10 @@ void release_thread_slot(size_t slot) { | |||
| 370 | if (slot >= kMaxMappedTlsModules) return; | 372 | if (slot >= kMaxMappedTlsModules) return; |
| 371 | ThreadTlsSlot& entry = g_thread_tls_slots[slot]; | 373 | ThreadTlsSlot& entry = g_thread_tls_slots[slot]; |
| 372 | if (!entry.module && !entry.block) return; | 374 | if (!entry.module && !entry.block) return; |
| 375 | if (entry.ga_view) { | ||
| 376 | if (TlsGetValue(entry.ga_slot) == entry.ga_view) TlsSetValue(entry.ga_slot, nullptr); | ||
| 377 | VirtualFree(entry.ga_view, 0, MEM_RELEASE); | ||
| 378 | } | ||
| 373 | if (entry.block) { | 379 | if (entry.block) { |
| 374 | void** slots = module_tls_slots(); | 380 | void** slots = module_tls_slots(); |
| 375 | if (slots && entry.index < kMaxModuleTlsSlots && slots[entry.index] == entry.block) | 381 | if (slots && entry.index < kMaxModuleTlsSlots && slots[entry.index] == entry.block) |
| @@ -451,6 +457,20 @@ void install_thread_block(Module& m) { | |||
| 451 | if (m.tls_zero_fill) | 457 | if (m.tls_zero_fill) |
| 452 | std::memset(static_cast<uint8_t*>(block) + m.tls_raw_size, 0, m.tls_zero_fill); | 458 | std::memset(static_cast<uint8_t*>(block) + m.tls_raw_size, 0, m.tls_zero_fill); |
| 453 | 459 | ||
| 460 | void** view = nullptr; | ||
| 461 | if (m.tls_ga_slot != kNoTlsIndex) { | ||
| 462 | // The /GA view: [0] and [tls_index] are this thread's block, the rest stays null (the image reads no other). | ||
| 463 | view = static_cast<void**>( | ||
| 464 | VirtualAlloc(nullptr, kMaxModuleTlsSlots * sizeof(void*), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | ||
| 465 | if (!view) { | ||
| 466 | VirtualFree(block, 0, MEM_RELEASE); | ||
| 467 | throw std::runtime_error(m.name + ": TLS view allocation failed"); | ||
| 468 | } | ||
| 469 | view[0] = block; | ||
| 470 | view[m.tls_index] = block; | ||
| 471 | TlsSetValue(m.tls_ga_slot, view); | ||
| 472 | } | ||
| 473 | |||
| 454 | void* saved = slots[m.tls_index]; | 474 | void* saved = slots[m.tls_index]; |
| 455 | slots[m.tls_index] = block; | 475 | slots[m.tls_index] = block; |
| 456 | entry.module = &m; | 476 | entry.module = &m; |
| @@ -458,6 +478,45 @@ void install_thread_block(Module& m) { | |||
| 458 | entry.saved = saved; | 478 | entry.saved = saved; |
| 459 | entry.block = block; | 479 | entry.block = block; |
| 460 | entry.index = m.tls_index; | 480 | entry.index = m.tls_index; |
| 481 | entry.ga_view = view; | ||
| 482 | entry.ga_slot = m.tls_ga_slot; | ||
| 483 | } | ||
| 484 | |||
| 485 | // Rewrites a /GA EXE's slot-0 TLS reads (Module::tls_ga_slot): every `mov r64, gs:[58h]` (65 REX.W 8B /r with | ||
| 486 | // mod=00, r/m=100, SIB=25, disp32=58h; REX 48 or 4C for r8-r15) in an executable section becomes the same | ||
| 487 | // instruction with disp32 = 1480h + 8 * slot, TEB->TlsSlots[slot]. Same length, so nothing else moves. The 9-byte | ||
| 488 | // form, with its segment prefix, SIB 25 and that displacement, is not something code or tables produce by accident. | ||
| 489 | // Must run before protect_sections() and before any of the image's code (its TLS callbacks included). | ||
| 490 | size_t patch_ga_tls_reads(Module& m, uint32_t slot) { | ||
| 491 | const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(m.base); | ||
| 492 | const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(m.base + dos->e_lfanew); | ||
| 493 | const IMAGE_SECTION_HEADER* sec = IMAGE_FIRST_SECTION(nt); | ||
| 494 | const uint32_t disp = 0x1480u + 8u * slot; | ||
| 495 | size_t sites = 0; | ||
| 496 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { | ||
| 497 | if (!(sec->Characteristics & IMAGE_SCN_MEM_EXECUTE)) continue; | ||
| 498 | const uint32_t size = (std::min)(sec->Misc.VirtualSize, sec->SizeOfRawData); | ||
| 499 | if (sec->VirtualAddress > m.size || size > m.size - sec->VirtualAddress || size < 9) continue; | ||
| 500 | uint8_t* p = m.base + sec->VirtualAddress; | ||
| 501 | for (uint32_t k = 0; k + 9 <= size; ++k) { | ||
| 502 | if (p[k] != 0x65 || (p[k + 1] != 0x48 && p[k + 1] != 0x4C) || p[k + 2] != 0x8B) continue; | ||
| 503 | const uint8_t modrm = p[k + 3]; | ||
| 504 | if ((modrm & 0xC7) != 0x04 || p[k + 4] != 0x25) continue; | ||
| 505 | uint32_t d; | ||
| 506 | std::memcpy(&d, p + k + 5, 4); | ||
| 507 | if (d != 0x58) continue; | ||
| 508 | std::memcpy(p + k + 5, &disp, 4); | ||
| 509 | ++sites; | ||
| 510 | k += 8; | ||
| 511 | } | ||
| 512 | } | ||
| 513 | return sites; | ||
| 514 | } | ||
| 515 | |||
| 516 | bool ga_tls_patch_enabled() { | ||
| 517 | char v[8] = {}; | ||
| 518 | size_t n = 0; | ||
| 519 | return !(getenv_s(&n, v, "LEONIDA_TLS_GA_PATCH") == 0 && n == 2 && v[0] == '0'); | ||
| 461 | } | 520 | } |
| 462 | 521 | ||
| 463 | void uninstall_thread_block(Module& m) { | 522 | void uninstall_thread_block(Module& m) { |
| @@ -1445,6 +1504,26 @@ void Loader::initialize_tls(Module& m) { | |||
| 1445 | // VirtualProtect dance. | 1504 | // VirtualProtect dance. |
| 1446 | if (index_rva) *reinterpret_cast<uint32_t*>(m.base + index_rva) = index; | 1505 | if (index_rva) *reinterpret_cast<uint32_t*>(m.base + index_rva) = index; |
| 1447 | } | 1506 | } |
| 1507 | { | ||
| 1508 | // An EXE's code may read its TLS at index 0 directly (/GA); index 0 is gta6rt.exe's here (pe_loader.h, | ||
| 1509 | // tls_ga_slot). Patched before any of the image's code runs, TLS callbacks included. | ||
| 1510 | const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(m.base); | ||
| 1511 | const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(m.base + dos->e_lfanew); | ||
| 1512 | if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && total && index != 0 && ga_tls_patch_enabled()) { | ||
| 1513 | static const DWORD ga_slot = TlsAlloc(); | ||
| 1514 | if (ga_slot == TLS_OUT_OF_INDEXES || ga_slot >= 64) { | ||
| 1515 | std::fprintf(stderr, "[rt] loader: %s: no TlsAlloc slot below 64 for its index-0 TLS reads\n", | ||
| 1516 | m.name.c_str()); | ||
| 1517 | } else { | ||
| 1518 | m.tls_ga_sites = patch_ga_tls_reads(m, ga_slot); | ||
| 1519 | if (m.tls_ga_sites) m.tls_ga_slot = ga_slot; | ||
| 1520 | std::fprintf(stderr, | ||
| 1521 | "[rt] loader: %s: %zu index-0 TLS reads (gs:[58h]) now read its own TLS (TlsSlots[%lu]); " | ||
| 1522 | "its module TLS index is %u\n", | ||
| 1523 | m.name.c_str(), m.tls_ga_sites, static_cast<unsigned long>(ga_slot), index); | ||
| 1524 | } | ||
| 1525 | } | ||
| 1526 | } | ||
| 1448 | try { | 1527 | try { |
| 1449 | install_thread_block(m); | 1528 | install_thread_block(m); |
| 1450 | } catch (...) { | 1529 | } catch (...) { |