HLE: load a host DLL outside the module cache's lock (loader-lock deadlock) #357

Manually merged
github merged 1 commit from gh-pr/357/head into gh-pr/357/base 2026-11-26 04:00:36 +00:00
Owner

@⁠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 UTC

What's wrong

host::module (in victory-hle/src/host.rs) caches the handles of the host DLLs we pass calls through to. On a miss it calls LoadLibraryExA while 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 need MODULES. Any first use of a host! function does (files.rs, net.rs, xsystem.rs and media.rs resolve theirs lazily), and so does binding a title DLL that's loaded late. That's two locks taken in opposite orders:

thread A: host::module("x.dll")    holds MODULES      -> LoadLibraryExA waits for the loader lock
thread B: title thread attach      holds loader lock  -> host!(...) on first use waits for MODULES

Neither thread can go on. The process hangs, and can't even exit, since ExitProcess needs the loader lock too. Microsoft's DLL best practices name this exact pattern: holding a lock while calling LoadLibrary, when code under the loader lock can take that lock, is a lock-order deadlock.

What the fix does

module now:

  1. looks the DLL up in the cache and releases the lock;
  2. calls LoadLibraryExA without holding the lock;
  3. takes the lock again only to store the handle (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 (in host.rs) builds the exact race:

  • A test TLS callback (in .CRT$XLY), armed for one thread start, holds the loader lock like a title's thread attach. It sleeps 500 ms, then looks up kernel32.dll!GetTickCount, which is already cached, so it only needs MODULES.
  • Meanwhile another thread looks up cabinet.dll!FDICreate, a DLL nothing has loaded yet, which needs the loader lock.
  • The test runs in a child process. A loader deadlock keeps a process from exiting, so the parent kills the child after 30 s and fails.

I ran cargo test --workspace on GitHub's windows-latest with Rust 1.98.1 from rust-toolchain.toml:

  • This PR: everything passes. victory-hle has 87 passed, the other crates have 0 failed.
  • The new test alone on main: the child deadlocks. The parent kills it after 30 s: deadlock: a host DLL loads under the module cache's lock.
  • I ran with --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.

**@⁠Dufftwt** on GitHub · 2026-11-24 02:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/357) `Dufftwt:fix/host-dll-load-outside-cache-lock` → `main` · +96 −4 · 1 files · 1 commits · merged 2026-11-25 00:08 UTC ## What's wrong `host::module` (in `victory-hle/src/host.rs`) caches the handles of the host DLLs we pass calls through to. On a miss it calls `LoadLibraryExA` **while 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 need `MODULES`. Any first use of a `host!` function does (`files.rs`, `net.rs`, `xsystem.rs` and `media.rs` resolve theirs lazily), and so does binding a title DLL that's loaded late. That's two locks taken in opposite orders: ``` thread A: host::module("x.dll") holds MODULES -> LoadLibraryExA waits for the loader lock thread B: title thread attach holds loader lock -> host!(...) on first use waits for MODULES ``` Neither thread can go on. The process hangs, and can't even exit, since `ExitProcess` needs the loader lock too. Microsoft's [DLL best practices](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices) name this exact pattern: holding a lock while calling `LoadLibrary`, when code under the loader lock can take that lock, is a lock-order deadlock. ## What the fix does `module` now: 1. looks the DLL up in the cache and releases the lock; 2. calls `LoadLibraryExA` without holding the lock; 3. takes the lock again only to store the handle (`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` (in `host.rs`) builds the exact race: - A test TLS callback (in `.CRT$XLY`), armed for one thread start, holds the loader lock like a title's thread attach. It sleeps 500 ms, then looks up `kernel32.dll!GetTickCount`, which is already cached, so it only needs `MODULES`. - Meanwhile another thread looks up `cabinet.dll!FDICreate`, a DLL nothing has loaded yet, which needs the loader lock. - The test runs in a child process. A loader deadlock keeps a process from exiting, so the parent kills the child after 30 s and fails. I ran `cargo test --workspace` on GitHub's `windows-latest` with Rust 1.98.1 from `rust-toolchain.toml`: - **This PR:** everything passes. `victory-hle` has 87 passed, the other crates have 0 failed. - **The new test alone on `main`:** the child deadlocks. The parent kills it after 30 s: `deadlock: a host DLL loads under the module cache's lock`. - I ran with `--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.
host::module held the module cache's lock while it called
LoadLibraryExA. Loading a DLL that is not loaded yet takes the OS
loader lock, and title code runs under that lock too (a title DLL's
thread attach, TLS callbacks run as threads start), where it can need
the cache: any first use of a host! function, or a late-bound title
DLL. One thread holding the cache and waiting for the loader lock,
another holding the loader lock and waiting for the cache: the process
hangs, and cannot even exit, as ExitProcess needs the loader lock too.

The cache is now looked up, then unlocked for the load, then locked
again to keep the handle; a thread that loaded the same DLL meanwhile
got the same module, which stays loaded.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:00:35 +00:00
github manually merged commit c6959acba3 into gh-pr/357/base 2026-11-26 04:00:36 +00:00
Author
Owner

@⁠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 at c6959acba38304852c185bcd7bc2ba1478d6e0b2.

  • 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

**@⁠jeremygold02** on GitHub · 2026-11-24 04:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/357#issuecomment-7122026579) Tested PR #357 at head `c6959acba38304852c185bcd7bc2ba1478d6e0b2` (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at `c6959acba38304852c185bcd7bc2ba1478d6e0b2`. - `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::imp::tests::loading_a_host_dll_does_not_block_thread_attach, host::imp::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](https://github.com/Marvisak/VIctory/blob/c6959acba38304852c185bcd7bc2ba1478d6e0b2/crates/victory-hle/src/host.rs)
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!357
No description provided.