Skip to content

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

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
Dufftwt:fix/host-dll-load-outside-cache-lock
Nov 25, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
Dufftwt:fix/host-dll-load-outside-cache-lock

Conversation

@Dufftwt

@Dufftwt Dufftwt commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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:

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.
muhammed-aksoy pushed a commit to muhammed-aksoy/VIctory that referenced this pull request Nov 24, 2026
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…loader-lock deadlock)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeremygold02

Copy link
Copy Markdown
Contributor

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

@ghost ghost mentioned this pull request Nov 24, 2026

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
…le cache's lock (loader-lock deadlock)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
@Marvisak
Marvisak merged commit a222fe3 into Marvisak:main Nov 25, 2026
Steenax6 added a commit to Steenax6/VIctory-NVID that referenced this pull request Nov 26, 2026
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 as
threads start), where it can need the cache: one thread holding the cache
and waiting for the loader lock, another holding the loader lock and waiting
for the cache hangs the process, which then cannot even exit.

The cache is looked up, unlocked for the load, and locked again to keep the
handle; a thread that loaded the same DLL meanwhile got the same module,
which stays loaded. The commit's test (a child process with a TLS callback
that looks a function up under the loader lock) comes with it.

Source: Marvisak/VIctory c6959ac (Duff), merged as PR Marvisak#357.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants