mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 23:55:52 +01:00
[PR #1881] pe_loader: fix wide dynamic load, failed-load rollback, GA TLS race, and compat forwarders (RT-PE-LOADER-DYNAMIC-ROLLBACK-AND-TLS) #1892
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#1892
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/ubiquitarianism/leonida/pull/1881
Author: @ambroseautomation
Created: 11/25/2026
Status: 🔄 Open
Base:
main← Head:fix/rt-pe-loader-dynamic-rollback-and-tls📝 Commits (1)
80e6e53pe_loader: fix wide dynamic load, failed-load rollback, GA TLS publication, and compat forwarders (RT-PE-LOADER-DYNAMIC-ROLLBACK-AND-TLS)📊 Changes
3 files changed (+298 additions, -74 deletions)
View changed files
📝
runtime/src/pe_loader.cpp(+130 -68)📝
runtime/src/pe_loader.h(+3 -0)📝
runtime/test/loader_test.cpp(+165 -6)📄 Description
Coordination item:
RT-PE-LOADER-DYNAMIC-ROLLBACK-AND-TLSProblem & Root Causes
Auditing
runtime/src/pe_loader.cppuncovered six bugs across runtime dynamic loads, failed-load rollback, static TLS publication, and compat-DLL forwarder precedence:LoadLibraryW/LoadLibraryExW) passed non-NULLlpvReservedtoDllMain:Loader::load_runtime_module()establishedRuntimeLoadScope dynamic(runtime_loads_)so newly mapped modules gotdynamic_load = true(lpvReserved == nullptr), whileLoader::load_runtime_module_w()omitted it. Every bundled DLL dynamically loaded viaload_runtime_module_w()receiveddynamic_load = falseand a non-NULLCONTEXT*lpvReserved(pretending to be a static load).initialise()poisoned subsequent runtime loads and left failed dependencies registered:initialise()threw duringLoader::load_runtime_candidate(), onlyby_name_.erase(module_key(mapped->path))ran.run_tls_callbacks()beforem.initialisedbecametrue, it stayed ininit_order_, causing every subsequent unrelated runtimeLoadLibraryto re-run it and fail.DllMainreturnedFALSE, the failed dependency stayed findable inby_name_, and the uninitialised parent remained ininit_order_, so a later unrelated runtime load silently executed the abandoned parent'sDllMain.Loader::initialise()used range-for iteration overinit_order_, which invalidates iterators if aDllMainrecursively loads another bundled module.Loader::load()leaked newly mapped modules, address ranges, and static TLS slots:map_image(),bind_imports(),protect_sections(), orregister_unwind()threw afterorder_.push_back(m)/initialize_tls(*m),Loader::load()only erasedkeyfromby_name_, leaving the failed module inorder_,ranges_, and occupying one of the 16 static TLS slots (exhausting the pool after 16 failed TLS loads)./GATLS patch publication race &VirtualSize == 0section skip:Loader::initialize_tls()publishedg_tls_slot_owners[slot] = &mand releasedExclusiveTlsLockbefore runningpatch_ga_tls_reads()and writingm.tls_ga_slot, creating a data race where concurrentDLL_THREAD_ATTACHevents sawm.tls_ga_slot == kNoTlsIndexand omitted the thread's/GAview (and shadowedntunder/W4C4456).patch_ga_tls_reads()and the CLZERO scan inprotect_sections()computedmin(Misc.VirtualSize, SizeOfRawData), skipping executable sections withMisc.VirtualSize == 0(whichmap_image()and section protection treat asSizeOfRawDataper PE/COFF).VirtualFree(nullptr)inforget_ended_thread_id():forget_ended_thread_id()calledVirtualFree(it->ga_view, 0, MEM_RELEASE)unconditionally even whenit->ga_view == nullptr.Loader::resolve_forwarder()bypassedLEONIDA_COMPAT_DIR:resolve_forwarder()checkedshims::find()directly without first checkingresolve_compat(), allowing a forwarded export to bypass a drop-in compat DLL (or fall through to a shim for a symbol missing from a compat DLL that owns the module).Summary of Changes
runtime/src/pe_loader.h,runtime/src/pe_loader.cpp:RuntimeLoadScopeintoLoader::load_runtime_candidate()so both narrow and wide runtime loads setdynamic_load = true.Loader::unmap_module()andLoader::rollback_load()(sharing teardown with~Loader()) to atomically roll backinit_order_,order_,by_name_,ranges_, unwind tables, xmem hooks (keyed per-Module*), and static TLS slots onLoader::load()failures.Loader::initialise_all(Module** failed)and updatedLoader::load_runtime_candidate()catch cleanup to unlink failed/uninitialised newly added modules fromby_name_and drop uninitialised entries frominit_order_while keeping modules whoseDllMainran (initialised == true) for~Loader()process detach./GATlsAlloc()+patch_ga_tls_reads()beforeExclusiveTlsLockpublication inLoader::initialize_tls()(also clearingm.tls_ga_slotoninstall_thread_block()catch and removing the shadowednt/W4warning).Misc.VirtualSize == 0asSizeOfRawDatainpatch_ga_tls_reads()and the CLZERO scan inprotect_sections().if (it->ga_view)inforget_ended_thread_id().resolve_compat()beforeshims::find()inLoader::resolve_forwarder().runtime/test/loader_test.cpp:load_runtime_module_wlpvReserved == nullptr, failed runtime TLS callback recovery, failed runtime dependencyDllMaincleanup, 20x failed-load TLS slot /modules()/find_address()rollback,/GApatching withVirtualSize == 0, andLEONIDA_COMPAT_DIRforwarder precedence.Verification
Environment: Windows 11 Pro 64-bit, MSVC
14.51.36231(cl.exe), LLVMclang-cl.exe. GPU: n/a (CPU-only PE loader tests).Before fix (
upstream/mainpe_loader.{h,cpp}+ new regression tests inloader_test.cpp)loader_test.exe(cl /std:c++20 /MD /W4 /permissive- /utf-8 /EHsc):101 checks, 6 failed(exit 1):FAIL a wide run-time load's DllMain gets a NULL lpvReserved: lpvReserved was non-NULLFAIL a runtime load that fails in TLS callbacks does not break later runtime loads: the later valid runtime load failed because of the abandoned moduleFAIL a runtime load whose dependency fails DllMain drops both from lookup and skips abandoned parent: the failed dependency or parent remained findable by nameFAIL failed loads after TLS setup roll back modules, address ranges, and static TLS slots: failed loads leaked static TLS slots and exhausted the 16-slot poolFAIL GA TLS patching scans executable sections whose VirtualSize is 0: the gs:[58h] read in the VirtualSize==0 section was not patchedFAIL a forwarded export honours LEONIDA_COMPAT_DIR over a shim: forwarded export to compat DLL did not bind to its Answer exportAfter fix
MSVC (
cl.exe /std:c++20 /MD /W4 /WX /permissive- /utf-8 /EHsc, 0 warnings) andclang-cl(/std:c++20 /MD /W4 /permissive- /utf-8 /EHsc, 0 warnings inpe_loader.{h,cpp}):loader_test: 101 checks, 0 failed (MSVC +clang-cl)module_names_test tiny_dll.dll: 73 checks, 0 failures (MSVC +clang-cl)loader_tls_test tiny_tls.dll: OK (118 checks) (MSVC +clang-cl)loader_tls_index0_test runs: OK (40 checks) (MSVC +clang-cl)loader_tls_index0_test threads: OK (6 checks) (MSVC +clang-cl)loader_tls_index0_test noreserve: OK (3 checks) (MSVC +clang-cl)python -I scripts/check_no_game_data.py runtime/src/pe_loader.h runtime/src/pe_loader.cpp runtime/test/loader_test.cpp: clean (exit 0)AI-assisted: yes
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.