HLE: load a host DLL outside the module cache's lock (loader-lock deadlock) #357
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!357
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/357/head"
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?
@Dufftwt on GitHub · 2026-11-24 02:34 UTC · original
Dufftwt:fix/host-dll-load-outside-cache-lock→main· +96 −4 · 1 files · 1 commits · merged 2026-11-25 00:08 UTCWhat'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.
@jeremygold02 on GitHub · 2026-11-24 04:26 UTC · original
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