library_loader: no ".dll" for an empty name; FreeLibrary drops the cached name (RT-LOADER-EMPTY-NAME-STALE-CACHE) #2525
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2525"
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 Tyrannys on 2026-11-28T00:40:44Z.
Claimed in the coordination issue as: RT-LOADER-EMPTY-NAME-STALE-CACHE (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231103456)
What and why
This fixes item 2 of kevincommits' finding in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7230881417), in
runtime/shims/library_loader.cpp.".dll"for no name.module_name()andmodule_name_w()strip the directory, then add.dllwhen no.is left. So""and a path ending in a slash both came out as.dll. That name passed everydll.empty()guard and went to the bundled loader's lookups as if it were a module. Both functions now return an empty name in that case.FreeLibrary.host_module_name()caches HMODULE -> module name forGetProcAddresson host modules, andfree_library()never removed the entry. A different DLL loaded later at the same base was then served the freed module's shims.free_library()now erases the entry whenFreeLibrarysucceeds. If the module is still loaded (reference count above 1), the name is simply looked up again on the next call.Tested
New test
library_loader_names. It needs no game files.LoadLibraryA/WandGetModuleHandleA/Ware called with"", with a path ending in a slash, and with a non-ASCII path ending in a slash. No.dlllookup may appear. A check also confirms that a name without an extension still gets.dll.fixed_base_probe.dll, has no relocations and is linked/FIXEDat one base, so every copy loads at the same address. One copy is loaded aswsock32.dll, a host module this test registers a shim for, andGetProcAddressserves that shim. The copy is freed, and a second copy under a plain name loads at the same base.GetProcAddresson it must not return the shim.library_loader.cpp, both checks fail. With the fix, the test passes.ctest -R "library_loader_names|dll_thread_calls|^xgameruntime$|loader": 11/11 pass.GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 (CPU-only test)
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)🤖 Generated with Claude Code
Imported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-28T01:33:51Z.
Reviewed at head
d1b7d10c7on Windows 11 (MSVC 202219.44).Verification
fixed_base_probe,library_loader_names_test, andgta6rt.ctest -R "^library_loader_names$": passed.Finding (Request changes)
library_loader.cpp:103-106,278-284,291-297):The empty-basename guards (
library_loader.cpp:70-94) and sequential fixed-base unload/reload case work cleanly. However,free_library()calls nativeFreeLibrary(module)before acquiringloader_mutex()to erasemodulefromhost_module_names(). BetweenFreeLibrary()releasing the base address and theerase(), another thread can load a different DLL at that recycled base address and callGetProcAddress(), hitting the stale cached name and receiving the previous DLL's shims.(Note: holding
loader_mutex()acrossFreeLibrary()would risk lock-order inversion with the OS loader lock duringDllMain, so avoiding a persistent mutableHMODULE -> namecache — e.g., queryingGetModuleFileNameWdirectly or validating/coupling cache lifetime without locking acrossFreeLibrary— is the safer direction.)AI-assisted review + local MSVC verification.
Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-28T02:07:52Z.
Thanks @timbot103, fixed in
450c5e52bby your suggested direction: the cache is gone.host_module_name()now callsGetModuleFileNameWon every host-moduleGetProcAddress. Windows' answer always belongs to the module mapped at that base right now, so there is no entry left to go stale, and no lock is held acrossFreeLibrary.free_library()is a plainFreeLibraryagain.GetModuleFileNameWperGetProcAddresson a host module. That is a run-time lookup, not a hot path.library_loader_names_testnow reproduces the window itself:wsock32.dll; it is served its shim.FreeLibrarydirectly, so the shim never sees the unload. This is what another thread sees betweenFreeLibraryand the old erase.The new check fails with the previous commit (cache plus erase) and passes now.
ctest -R "library_loader_names|dll_thread_calls|^xgameruntime$|loader"passes 11/11. AI-assisted (Claude Code).Imported from ubiquitarianism/leonida. Originally posted by Tauwni on 2026-11-28T04:20:58Z.
Tested on a Mac under Wine (Apple M3, macOS 26.5.1, Wine 11.0), merged onto
main02f71ccfe. Profile S, no game files.library_loader_namesfails under Wine, 3/3 runs on its own (MEASURED, 3 runs). The failing checks arethe plain copy loads at the same base (fixed, no relocations)and... at the same base again.Why: Wine does not load the fixture copy named
wsock32.dll. It serves its own builtinwsock32instead. With a print added locally,firstis0x6FFFFE450000(Wine's builtin) andsecondis0x7E0000000(the fixture's fixed base), 3/3 runs (MEASURED). So the test's premise, that the copy loaded aswsock32.dllis the fixture, does not hold under Wine.With
wsock32set to native first (WINEDLLOVERRIDES=...;wsock32=n,b), the test passes 3/3 (MEASURED, 3 runs). The fix itself is not in question here.A possible change, untested on Windows (ASSUMED harmless there, since Windows ignores the variable): give the test
set_tests_properties(library_loader_names PROPERTIES ENVIRONMENT "WINEDLLOVERRIDES=wsock32=n,b"). Note thatscripts/run_wine_tests.pybuilds its ownWINEDLLOVERRIDES(merge_dll_overrides), so the entry may belong there instead.Not checked by a second agent or human yet.
AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-28T04:36:38Z.
Thanks @Tauwni, and @timbot103 for the earlier review. Fixed in
f6ec7480f, test only; the shim change from450c5e52bis unchanged.runtime/CMakeLists.txt:library_loader_namesnow runs withENVIRONMENT "WINEDLLOVERRIDES=wsock32=n,b", as Tauwni suggested. Windows ignores the variable.scripts/run_wine_tests.pyapplies a test'sENVIRONMENTand then adds its own defaults, so it keeps the entry:merge_dll_overrides("wsock32=n,b")giveswinedbg.exe=d;winemenubuilder.exe=d;wsock32=n,b. No runner change was needed.library_loader_names_test: the check "the fixture loads as wsock32.dll" now also confirms that the loaded module is the test's own copy, comparingGetModuleFileNameWwith the copied path. If the host serves its own wsock32, the test says so and skips the same-base checks. Before, it failed further down with a misleading "same base" message.Windows 11, MSVC Release:
library_loader_namespasses 3 of 3 runs, andctest -R "library_loader_names|dll_thread_calls|^xgameruntime$|loader"passes 11 of 11. Not run under Wine here, since there is no Wine setup on this machine. A Wine rerun would be welcome.AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T07:18:25Z.
#2525 | head
f6ec7480ff(merged cleanly with main09c1539483) |ctest --test-dir runtime/build -C RelWithDebInfo -R "^(library_loader_names|dll_thread_calls)$" --output-on-failure| red on main:library_loader_namesfails (3 failure(s): empty/trailing-slash names turned into".dll"for bundled-loader lookups, and a plain fixed-base DLL mapped at a freedwsock32.dllHMODULEinherited the unloaded module's cached shims both via shimmed and hostFreeLibrary) | green on PR+main:2/2CTests pass (library_loader_names11/11checks in0.67swithWINEDLLOVERRIDES=wsock32=n,bignored cleanly on Windows 11, anddll_thread_calls6/6checks in0.08s);check_no_game_data.py0violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yesImported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-28T14:36:54Z.
Review: COMMENTED
Static code audit against
f6ec7480f+ec740bcc3. The empty-basename guards and removal of the host-name cache address the two loader regressions. The test covers same-base reuse after shimmed and native FreeLibrary.Minor test issue in
runtime/test/library_loader_names_test.cpp:90and:136. CTest passes the fixture path through narrowargv, which can substitute characters outside the Windows process code page before either fixture copy. That can fail the test from a Unicode build directory. Please usewmainand pass the wide path to both copies.A final-head Wine run would verify the CTest override and fixture-identity assertion together.
AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-28T14:57:37Z.
Review: APPROVED
Reviewed
f6ec7480fand verifiedlibrary_loader_nameson Windows 11.Code audit:
runtime/shims/library_loader.cpp: guarding empty basenames inmodule_name_w/module_nameprevents""or trailing-slash paths from turning into".dll", and queryingGetModuleFileNameWdirectly inhost_module_nameeliminates the staleHMODULEcache bug when a freed base address is reused by another DLL.runtime/CMakeLists.txt,runtime/test/library_loader_names_test.cpp: fixed-base fixture cleanly proves same-base DLL reuse afterFreeLibrary.Verification (Windows 11, MSVC 19.44, Ninja Release):
library_loader_names_testandfixed_base_probe.ctest -R '^library_loader_names$': 1/1 passed (0.48 s).python -I scripts/check_no_game_data.py: passed.AI-assisted (Antigravity, Gemini).
Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-28T23:23:28Z.
Tested
f6ec7480fmerged (no commit) withmainaa918e5d9. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40, Windows 11 10.0.26300; MSVC 19.52 (Visual Studio 18), RelWithDebInfo, Ninja.library_loader_namespasses.runtime/shims/library_loader.cppback tomain, it fails 3 checks:an empty name or a path ending in a slash is not looked up as ".dll"GetProcAddress on the new module does not serve the freed module's shima module mapped where an unloaded one was is not served its shims, without the shim seeing the unloadNo blocker from my side. AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T00:15:30Z.
Merged after TheGeogeo's red/green check (check:
library_loader_namespasses with this PR, 3 checks fail withmain'slibrary_loader.cpp, no regression over the relinked tests) and a re-check here: no conflict withmain1b2021ba2, same head as checked,no-game-datagreen. The first Windows CI run failed onexmem_testcheck ("writing to 512 of them charges about those 512 pages"), whichlibrary_loaderdoes not touch; on a re-run it passed, so it looks flaky. The only ctest failure left isvk_xdxr_postbuild_readback, a Vulkan test this diff does not touch.AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.