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

Merged
Dufftwt merged 1 commit from upstream-pr/357 into main 2026-11-25 00:08:31 +00:00
Dufftwt commented 2026-11-24 02:34:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dufftwt on 2026-11-24T02:34:31Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573325204 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/357). Originally posted by [Dufftwt](https://github.com/Dufftwt) on 2026-11-24T02:34:31Z. --- ## 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.
jeremygold02 commented 2026-11-24 06:55:45 +00:00 (Migrated from github.com)

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 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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7122026579 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/357#issuecomment-7122026579). Originally posted by [jeremygold02](https://github.com/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 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)
mercuryVM commented 2026-11-24 13:01:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T12:50:45Z.

Review: APPROVED


Tested c6959ac on Windows 11, RTX 5070, MSVC: cargo test -p victory-hle 89 passed, including loading_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 existing field_reassign_with_default at xmem.rs:747.

  • Dropping MODULES before LoadLibraryExA removes the lock-order inversion with the OS loader lock that a DLL_THREAD_ATTACH or TLS callback looking up a function could hit. Re-taking the lock afterwards with entry().or_insert makes a racing double load harmless: both handles are to the same module, which stays loaded.
  • The test's .CRT$XLY TLS callback is cfg(test) and gated by ARMED, so other tests in the binary only pay an atomic load per thread start.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6039492461 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/357#pullrequestreview-6039492461). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T12:50:45Z. **Review:** APPROVED --- Tested c6959ac on Windows 11, RTX 5070, MSVC: `cargo test -p victory-hle` 89 passed, including `loading_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 existing `field_reassign_with_default` at xmem.rs:747. - Dropping `MODULES` before `LoadLibraryExA` removes the lock-order inversion with the OS loader lock that a `DLL_THREAD_ATTACH` or TLS callback looking up a function could hit. Re-taking the lock afterwards with `entry().or_insert` makes a racing double load harmless: both handles are to the same module, which stays loaded. - The test's `.CRT$XLY` TLS callback is `cfg(test)` and gated by `ARMED`, so other tests in the binary only pay an atomic load per thread start. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-25 01:03:29 +00:00
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!357
No description provided.