loader: a run-time LoadLibrary takes the LEONIDA_COMPAT_DIR module too (RT-COMPAT-DIR-RUNTIME-LOADS) #1565
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1565"
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?
Imported from ubiquitarianism/leonida. Originally posted by Mike7581 on 2026-11-25T04:42:17Z.
Claimed in the coordination issue as: RT-COMPAT-DIR-RUNTIME-LOADS (#1562, since #14 is at the 2,500-comment cap)
What and why
LEONIDA_COMPAT_DIRonly applied to static imports (runtime/README.md: "a run-timeLoadLibraryof the same name does not yet"). A mapped image'sLoadLibraryof a module the folder holds got the shim's pseudo-handle instead of the drop-in DLL, andGetProcAddresson the compat DLL's real handle looked the name up in the shim of the same module name first. One console module could then be served half by the drop-in and half by the shim, which the static step exists to prevent.Now
shims::BundledModuleOpsgainscompat_module/owns_compat_handle, answered byrt::Loaderfrom the samecompat_handle()its static step (resolve_compat) uses, so both get oneHMODULEper module. Inshims/library_loader.cpp:LoadLibrary*checks the compat folder before the shim registry and the bundled package DLLs;GetModuleHandle*answers a compat module's handle once loaded and NULL before (never the shim's pseudo-handle);GetProcAddresson a compat handle gives only that DLL's exports;FreeLibraryon it returns TRUE and keeps it loaded (it is the Loader's reference, static imports may be bound through it; compat modules already stay for the process lifetime).A compat file Windows refuses fails that
LoadLibrarywith Windows' error (logged) instead of throwing into the title.runtime/README.mdis updated.Tested
loader_testnow linksshims/library_loader.cppand gainstest_compat_runtime_loads, eight cases with a synthetic drop-in DLL and the test shim module: without the folder the shim still answers; with it,GetModuleHandleis NULL before the load,LoadLibraryWreturns the compat DLL,GetModuleHandle(with and without.dll) returns it after,GetProcAddressgivesAnswer(42) and not the shim'sF, a static import in the same Loader binds to the same export,FreeLibrarykeeps it loaded, and a file Windows refuses (error 193) fails the load without the shim or a throw.loader: 103 ok, 0 FAIL.ctest: 376/377; the one failure isws2_offlineunder-j 4(loopback bind failures; it failed the same way here on a run without this change), which passes 3/3 run alone.GPUs tested: n/a (CPU only). Built with MSVC (VS 2022 Build Tools 17.14) + Ninja via
scripts\build_runtime.ps1, Windows 11 Pro 10.0.26200.AI-assisted: yes (Claude Code)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-25T08:35:51Z.
Review: CHANGES_REQUESTED
The new path works:
loader_test, including the newtest_compat_runtime_loads, passes. A run-time load of a compat module now returns the same handle a static import uses, and its exports are its own.Two cases still differ from what Windows does:
LoadLibraryExflags, and compat DLLs that Windows loaded as a dependency. There are also a few nits. Line numbers refer to the PR head582e14d.Should-fix: the compat branch ignores
LoadLibraryExflagsload_shim_or_bundled(library_loader.cpp:152) getsflags, butcompat_module→compat_handle(pe_loader.cpp:2074) always does a normalLoadLibraryExW. We tested with a synthetic DLL whose initializer refuses normal attachment, loaded withDONT_RESOLVE_DLL_REFERENCES:ERROR_DLL_INIT_FAILEDThe bundled branch already refuses this flag (
pe_loader.cpp:1530).Suggested fix: pass the flags through. Once the compat folder claims the name, refuse the flag the same way, before loading or returning a cached handle.
Test it through both
LoadLibraryExAandLoadLibraryExW.Should-fix: a compat DLL that Windows loaded as a dependency is not recognized
LoadLibraryExW(..., LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)lets one compat DLL pull in another one from the same folder. The dependency is then live, but it is not incompat_modules_. This causes two problems:GetModuleHandleon it returns NULL withERROR_MOD_NOT_FOUND. We reproduced this with a native preload.GetProcAddresson its handle does not count it as a compat module (library_loader.cpp:298). A name the DLL lacks is then filled in from the shim of the same module name, which is the mix the static path forbids.Suggested fix: when the cache misses, look the module up by its full compat path, and record it.
Add a test with a compat DLL that imports a second compat DLL lacking an export the shim provides.
Nits
library_loader.cpp:150,:221): the compat lookup strips the directory. In a synthetic collision, an explicit path gave the DLL exporting 43 on Windows, but the compat DLL exporting 42 through the shim. The shim path already matches by basename onmain, so this extends existing behaviour. Consider limiting compat substitution to bare names, and to a full path that is the compat file itself.library_loader.cpp:192,:210,:250): these go straight to the bundled lookup and skip the compat folder. Run the compat lookup before the ASCII/wide split, with the wide basename parser.pe_loader.cpp:2064,:2098): withLEONIDA_COMPAT_DIRset, every run-timeLoadLibrary/GetModuleHandleof any name that misses the cache callsstd::filesystem::is_regular_fileunder the loader's mutex. Snapshot the folder's file names once perLoader, or cache the misses.What was checked
main: clean.loader(with the new compat run-time cases),loader_tls*,run_tiny_bundled_loadandrun_tiny_gdk_load. The 4 failures (root_sig,xmem,xmem_no_write_watch,root_signature_oracle) are the same on the other branches tested and are in code this PR does not touch.DONT_RESOLVE_DLL_REFERENCESReview done with AI assistance (Claude Code and GPT-6.2 Sol, cross-checked against each other). Every finding above was reproduced with a test probe.
Imported from ubiquitarianism/leonida. Originally posted by Mike7581 on 2026-11-25T18:18:09Z.
Thanks for the probes. All points are addressed in
2cca47b:LoadLibraryExflags reach the compat branch;DONT_RESOLVE_DLL_REFERENCESon a compat module fails withERROR_NOT_SUPPORTEDbefore loading or returning a loaded handle, as the bundled branch does (A and W tested).compat_handlefinds it by its full path (GetModuleHandleExW, reference kept), andowns_compat_handleby its folder (read outsidemutex_, sinceGetModuleFileNameWcan take the loader lock).GetModuleHandle/LoadLibraryreturn it, andGetProcAddressno longer fills in a missing export from the shim.main); the compat lookup runs before the ASCII/wide split, so non-ASCII basenames reach it; the folder's file names are listed once perLoader, so lookups no longer touch the disk.test_compat_runtime_edgesadds six cases, including a compat DLL importing a second one that lacks the shim's export; all six fail on582e14d. Full build and CTest pass here (377/377, RTX 4050).Written with AI assistance (Claude Code, Claude Opus 5.5) and reviewed with GPT-6 Sol High; every change above was checked against the code and the tests were run locally.
Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-28T03:48:15Z.
Review: APPROVED
Re-checked at
2cca47b5cmerged cleanly onto currentmain(02f71ccfe, Windows 10 x64, MSVC 14.44 RelWithDebInfo, Ninja).All should-fix items and nits from the previous review are addressed cleanly:
LoadLibraryExA/Wflags reach the compat path and refuseDONT_RESOLVE_DLL_REFERENCESwithERROR_NOT_SUPPORTEDbefore loading or returning a cached handle.GetModuleHandleExWincompat_handleand folder lookup outsidemutex_inowns_compat_handle, soGetModuleHandle/LoadLibraryreturn them andGetProcAddressdoes not fall back to a shim.std::filesystem::equivalent, non-ASCII basenames are checked before the ASCII/wide split, andcompat_files_is snapshotted once perLoader.Local verification:
python -I scripts/check_no_game_data.pyon all 8 changed files: exit 0.loader_testcleanly (zero new warnings).ctest -R '^loader$': 114 checks, 0 failed (21.39 s), covering all newtest_compat_runtime_loadsandtest_compat_runtime_edgescases.(Minor non-blocking nit for a future cleanup: when
LEONIDA_COMPAT_DIRis set,owns_compat_module_handleinget_proc_addressruns before savingGetLastError().)Looks ready to merge.