From 126062ae4a8831a564b9e3042684657c58781bef Mon Sep 17 00:00:00 2001 From: Ayinaki Date: Sat, 21 Nov 2026 18:48:36 +0000 Subject: Runtime: initialise a loaded module's TLS and DllMain Mapping an image never ran anything. TLS callbacks and DllMain were documented as the caller's, and nothing called them, so a module that needs either could not be brought up at all. Loader::initialise() now allocates the module's TLS slot, copies the template and SizeOfZeroFill into it, writes the slot index into the image, runs the TLS callbacks with DLL_PROCESS_ATTACH, then calls the entry point of a DLL as DllMain. Every address it follows is bounded like the rest of the loader: the directory, the template, the index, the callback table and each callback, with a 1 MiB ceiling on the block. Only DLLs get DllMain; an EXE's entry point has a different signature and stays the caller's to run, and --dry-run still runs nothing. The tests build an image whose TLS callback and DllMain are real machine-code stubs that store a sentinel, so they check the code actually executed rather than merely being parsed. --- runtime/src/pe_loader.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'runtime/src/pe_loader.cpp') diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index e6a9501ab..f26527989 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -56,6 +56,8 @@ bool prefer_host_copy(const std::string& dll) { Loader::~Loader() { for (auto* m : order_) { + if (m->tls_block) VirtualFree(m->tls_block, 0, MEM_RELEASE); + if (m->has_tls_slot) TlsFree(m->tls_slot); if (m->base) VirtualFree(m->base, 0, MEM_RELEASE); delete m; } @@ -70,6 +72,88 @@ Module& Loader::load(const std::filesystem::path& image) { return m; } +void Loader::initialise() { + // order_ is dependency order: a module is pushed after the modules it imports. + for (auto* m : order_) initialise(*m); +} + +void Loader::initialise(Module& m) { + if (m.initialised) return; + auto* nt = at(m.base, reinterpret_cast(m.base)->e_lfanew); + const uint64_t base = reinterpret_cast(m.base); + + // The TLS directory holds virtual addresses, which relocations have already moved to this mapping. + auto rva_of = [&](uint64_t va, const char* what) { + if (va < base || va - base >= m.size) + throw std::runtime_error(m.name + ": " + what + " is outside the image"); + return va - base; + }; + + auto& tls_dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS]; + if (tls_dir.Size) { + require_range(m.name, tls_dir.VirtualAddress, sizeof(IMAGE_TLS_DIRECTORY64), m.size, "the TLS directory"); + auto* tls = at(m.base, tls_dir.VirtualAddress); + + uint64_t start = 0, template_size = 0; + if (tls->StartAddressOfRawData || tls->EndAddressOfRawData) { + start = rva_of(tls->StartAddressOfRawData, "the TLS template"); + uint64_t end = rva_of(tls->EndAddressOfRawData, "the end of the TLS template"); + if (end < start) throw std::runtime_error(m.name + ": the TLS template ends before it starts"); + template_size = end - start; + require_range(m.name, start, template_size, m.size, "the TLS template"); + } + m.tls_block_size = template_size + tls->SizeOfZeroFill; + if (m.tls_block_size > (1u << 20)) throw std::runtime_error(m.name + ": the TLS block is larger than 1 MiB"); + if (m.tls_block_size) { + m.tls_block = VirtualAlloc(nullptr, static_cast(m.tls_block_size), MEM_RESERVE | MEM_COMMIT, + PAGE_READWRITE); + if (!m.tls_block) throw std::runtime_error(m.name + ": cannot allocate the TLS block"); + if (template_size) std::memcpy(m.tls_block, m.base + start, static_cast(template_size)); + } + + if (tls->AddressOfIndex) { + uint64_t index_rva = rva_of(tls->AddressOfIndex, "the TLS index"); + require_range(m.name, index_rva, sizeof(uint32_t), m.size, "the TLS index"); + DWORD slot = TlsAlloc(); + if (slot == TLS_OUT_OF_INDEXES) throw std::runtime_error(m.name + ": no TLS slot is free"); + m.tls_slot = slot; + m.has_tls_slot = true; + if (m.tls_block) TlsSetValue(slot, m.tls_block); + // The index lives in a writable section, but protect_sections already ran, so lift it for the write. + auto* index = m.base + index_rva; + auto* page = reinterpret_cast(reinterpret_cast(index) & ~uintptr_t(0xFFF)); + DWORD old = 0, ignored = 0; + if (!VirtualProtect(page, 0x1000, PAGE_READWRITE, &old)) + throw std::runtime_error(m.name + ": the TLS index can't be written"); + *reinterpret_cast(index) = slot; + VirtualProtect(page, 0x1000, old, &ignored); + } + + if (tls->AddressOfCallBacks) { + uint64_t table_rva = rva_of(tls->AddressOfCallBacks, "the TLS callback table"); + for (uint64_t i = 0;; ++i) { + uint64_t entry_rva = table_rva + i * sizeof(uint64_t); + require_range(m.name, entry_rva, sizeof(uint64_t), m.size, "a TLS callback table entry"); + uint64_t fn = *at(m.base, static_cast(entry_rva)); + if (!fn) break; + auto callback = reinterpret_cast(m.base + rva_of(fn, "a TLS callback")); + callback(m.base, DLL_PROCESS_ATTACH, nullptr); + } + } + } + + // Only a DLL gets DllMain. An EXE's entry point has a different signature and is the caller's to run. + if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) { + if (m.entry_rva >= m.size) throw std::runtime_error(m.name + ": the entry point is outside the image"); + auto entry = reinterpret_cast(m.base + m.entry_rva); + if (!entry(m.base, DLL_PROCESS_ATTACH, nullptr)) { + m.initialised = true; // it ran: a second DLL_PROCESS_ATTACH would be wrong + throw std::runtime_error(m.name + ": DllMain returned FALSE"); + } + } + m.initialised = true; +} + Module& Loader::map_image(const std::filesystem::path& path) { auto file = read_file(path); const std::string name = path.filename().string(); -- cgit v1.2.3