Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions runtime/src/pe_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<unsigned long>(ga_slot), index);
}
}

// The fields are complete before a thread event can see the owner.
ExclusiveTlsLock lock;
Module* expected = nullptr;
Expand All @@ -1908,26 +1929,6 @@ void Loader::initialize_tls(Module& m) {
// VirtualProtect dance.
if (index_rva) *reinterpret_cast<uint32_t*>(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<const IMAGE_DOS_HEADER*>(m.base);
const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(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<unsigned long>(ga_slot), index);
}
}
}
try {
install_thread_block(m);
} catch (...) {
Expand Down
17 changes: 17 additions & 0 deletions runtime/test/loader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions runtime/test/loader_tls_index0_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading