HLE: load a host DLL outside the module cache's lock (loader-lock deadlock) #357
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/357"
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 Marvisak/VIctory. Originally posted by Dufftwt on 2026-11-24T02:34:31Z.
What's wrong
host::module(invictory-hle/src/host.rs) caches the handles of the host DLLs we pass calls through to. On a miss it callsLoadLibraryExAwhile still holding the cache's lock (MODULES).Loading a DLL that isn't loaded yet takes the OS loader lock. Title code also runs under the loader lock: a title DLL's
DllMain(DLL_THREAD_ATTACH/DETACH), and TLS callbacks, which our loader runs as every thread starts and ends. That code can needMODULES. Any first use of ahost!function does (files.rs,net.rs,xsystem.rsandmedia.rsresolve theirs lazily), and so does binding a title DLL that's loaded late. That's two locks taken in opposite orders:Neither thread can go on. The process hangs, and can't even exit, since
ExitProcessneeds the loader lock too. Microsoft's DLL best practices name this exact pattern: holding a lock while callingLoadLibrary, when code under the loader lock can take that lock, is a lock-order deadlock.What the fix does
modulenow:LoadLibraryExAwithout holding the lock;entry(..).or_insert(..)).If two threads load the same DLL at once, both get the same module handle. Whichever stores first wins, and nothing is ever freed, so the race is harmless.
How it behaves after the fix
Loading a host DLL never blocks a thread that only needs the cache. Title thread starts and ends no longer deadlock against a late DLL load. Lookups that hit the cache don't change.
Tests
loading_a_host_dll_does_not_block_thread_attach(inhost.rs) builds the exact race:.CRT$XLY), armed for one thread start, holds the loader lock like a title's thread attach. It sleeps 500 ms, then looks upkernel32.dll!GetTickCount, which is already cached, so it only needsMODULES.cabinet.dll!FDICreate, a DLL nothing has loaded yet, which needs the loader lock.I ran
cargo test --workspaceon GitHub'swindows-latestwith Rust 1.98.1 fromrust-toolchain.toml:victory-hlehas 87 passed, the other crates have 0 failed.main: the child deadlocks. The parent kills it after 30 s:deadlock: a host DLL loads under the module cache's lock.--test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (#181, #244).Not tested: the title itself starting threads during a late DLL load.
Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:26:48Z.
Tested PR #357 at head
c6959acba38304852c185bcd7bc2ba1478d6e0b2(state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged atc6959acba38304852c185bcd7bc2ba1478d6e0b2.cargo test --locked -p victory-hle loading_a_host_dll_does_not_block_thread_attach— passed (ok. 1 passed; 0 failed; 0 ignored; 0 measured; 86 filtered out; finished in 0.52s ); cases: host:👿:tests::loading_a_host_dll_does_not_block_thread_attach, host:👿:tests::loading_a_host_dll_does_not_block_thread_attach.Behavior covered: Checks loading a host DLL no longer blocks thread attach while module-cache operations are active.
Code: crates/victory-hle/src/host.rs
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T12:50:45Z.
Review: APPROVED
Tested
c6959acon Windows 11, RTX 5070, MSVC:cargo test -p victory-hle89 passed, includingloading_a_host_dll_does_not_block_thread_attach, which runs in a child process with a 30 s kill switch. Clippy's 1 warning is the existingfield_reassign_with_defaultat xmem.rs:747.MODULESbeforeLoadLibraryExAremoves the lock-order inversion with the OS loader lock that aDLL_THREAD_ATTACHor TLS callback looking up a function could hit. Re-taking the lock afterwards withentry().or_insertmakes a racing double load harmless: both handles are to the same module, which stays loaded..CRT$XLYTLS callback iscfg(test)and gated byARMED, so other tests in the binary only pay an atomic load per thread start.AI-assisted (Claude Code).