PE loader: LoadLibrary waits for a game DLL another thread is loading #1208

Open
emirduran707 wants to merge 1 commit from upstream-pr/1217 into main
emirduran707 commented 2026-11-24 13:18:56 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by emirduran707 on 2026-11-24T13:18:56Z.


The loader registers a game DLL early (loader.rs, modules::register_mapped): before its imports are resolved, before its pages get their protections, and before DllMain runs. The loader's lock serialises callers of load_game_dll. But resolver::load_library returns a DLL it finds by name at once, outside that lock. So thread B calling LoadLibrary for a DLL that thread A is still loading got its base immediately, while the DLL's pages were still read-write, its import slots unresolved and its DllMain not run. If A's DllMain then failed, B held a handle to a DLL that was unloaded. Windows' LoadLibrary returns a handle only once DllMain has returned TRUE, and NULL when it returns FALSE.

A DLL that is found but not yet initialised is now loaded through load_game_dll. On another thread that waits for the loader's lock, so B gets the module once A's load has finished, or NULL if it failed. The loading thread itself is re-entrant and still gets it at once, as when a DllMain loads its own DLL.

What is in it

  • crates/xcl/src/resolver.rs: the wait in load_library.
  • crates/xcl/src/loader.rs: test load_library_waits_for_a_dll_another_thread_is_loading. It holds the loader lock and maps a DLL without running its DllMain, then calls load_library on another thread. The call must not return until init_dll has run and the lock is released.

Tested

  • AMD Radeon RX 9060 XT (driver 32.0.31044.16), Ryzen 5 5600, Windows 11 Pro 26300, Rust 1.100.0; tests debug, smoke release
  • cargo test --workspace -- --test-threads=4 on 8933d64: 742 passed, 0 failed, 41 ignored; no SKIPPED GPU tests
  • the new test without the change in resolver.rs fails (load_library returned the base before the DLL was initialised); with it it passes

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1541 (PresentX 1541)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19650 MB

this PR:

xcl-smoke v1
commit: 8933d646234246d1e64aff9291f9d2fa712dfd03
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1488 (PresentX 1488)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19677 MB

Not tested

  • A title that loads the same DLL on two threads at once; the smoke title's run is a regression check only.
  • NVIDIA and Intel GPUs.

AI use

  • Claude Code with Claude Opus 5.5: found the problem, wrote the change, the test and this description; I reviewed the diff and ran the tests and smoke runs above.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ivall/xcl:issue:5472909275 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1217). Originally posted by [emirduran707](https://github.com/emirduran707) on 2026-11-24T13:18:56Z. --- The loader registers a game DLL early (`loader.rs`, `modules::register_mapped`): before its imports are resolved, before its pages get their protections, and before `DllMain` runs. The loader's lock serialises callers of `load_game_dll`. But `resolver::load_library` returns a DLL it finds by name at once, outside that lock. So thread B calling `LoadLibrary` for a DLL that thread A is still loading got its base immediately, while the DLL's pages were still read-write, its import slots unresolved and its `DllMain` not run. If A's `DllMain` then failed, B held a handle to a DLL that was unloaded. Windows' `LoadLibrary` returns a handle only once `DllMain` has returned TRUE, and NULL when it returns FALSE. A DLL that is found but not yet initialised is now loaded through `load_game_dll`. On another thread that waits for the loader's lock, so B gets the module once A's load has finished, or NULL if it failed. The loading thread itself is re-entrant and still gets it at once, as when a `DllMain` loads its own DLL. **What is in it** - `crates/xcl/src/resolver.rs`: the wait in `load_library`. - `crates/xcl/src/loader.rs`: test `load_library_waits_for_a_dll_another_thread_is_loading`. It holds the loader lock and maps a DLL without running its `DllMain`, then calls `load_library` on another thread. The call must not return until `init_dll` has run and the lock is released. **Tested** - AMD Radeon RX 9060 XT (driver 32.0.31044.16), Ryzen 5 5600, Windows 11 Pro 26300, Rust 1.100.0; tests debug, smoke release - `cargo test --workspace -- --test-threads=4` on 8933d64: 742 passed, 0 failed, 41 ignored; no SKIPPED GPU tests - the new test without the change in `resolver.rs` fails (`load_library` returned the base before the DLL was initialised); with it it passes **Regression run** main: ``` xcl-smoke v1 commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3 gpu: AMD Radeon RX 9060 XT title: retail-title seconds: 60 exit: 0x0 frames shown: 1541 (PresentX 1541) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 19650 MB ``` this PR: ``` xcl-smoke v1 commit: 8933d646234246d1e64aff9291f9d2fa712dfd03 gpu: AMD Radeon RX 9060 XT title: retail-title seconds: 60 exit: 0x0 frames shown: 1488 (PresentX 1488) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 19677 MB ``` **Not tested** - A title that loads the same DLL on two threads at once; the smoke title's run is a regression check only. - NVIDIA and Intel GPUs. **AI use** - Claude Code with Claude Opus 5.5: found the problem, wrote the change, the test and this description; I reviewed the diff and ran the tests and smoke runs above. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
thehollowedone commented 2026-11-24 14:06:31 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by thehollowedone on 2026-11-24T13:22:44Z.


Independent Windows check of the failed-attach path on 47efa2e and 8933d64. Windows 11 build 26300, Ryzen 7 9800X3D, Rust 1.99.1 MSVC, debug.

A scratch fixture paused the first load after registering a synthetic DLL and before its DllMain returned FALSE. A second thread called LoadLibrary for the same name during that pause. On main it returned a handle before initialization finished. On the PR it waited, then returned no handle once initialization failed. The failed DLL was absent from the loaded-module registry. The probe checked the returned status only.

cargo test --offline --locked -p xcl --lib scratch_second_load_waits_then_reports_failed_attach -- --test-threads=1 failed on main and passed on the PR. The loader::tests filter passed 4/4 on the PR, including the scratch case and the stock successful-attach case. Synthetic DLLs only.

AI-assisted. Codex (GPT-6, exact variant unavailable) wrote and ran the scratch test and drafted this report.

<!-- greyfinch-sync:ivall/xcl:comment:7132744419 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1217#issuecomment-7132744419). Originally posted by [thehollowedone](https://github.com/thehollowedone) on 2026-11-24T13:22:44Z. --- Independent Windows check of the failed-attach path on [`47efa2e`](https://github.com/ivall/xcl/commit/47efa2ecea0a12934719ae69b81930cf02a4b1c3) and [`8933d64`](https://github.com/ivall/xcl/commit/8933d646234246d1e64aff9291f9d2fa712dfd03). Windows 11 build 26300, Ryzen 7 9800X3D, Rust 1.99.1 MSVC, debug. A scratch fixture paused the first load after registering a synthetic DLL and before its `DllMain` returned FALSE. A second thread called `LoadLibrary` for the same name during that pause. On main it returned a handle before initialization finished. On the PR it waited, then returned no handle once initialization failed. The failed DLL was absent from the loaded-module registry. The probe checked the returned status only. `cargo test --offline --locked -p xcl --lib scratch_second_load_waits_then_reports_failed_attach -- --test-threads=1` failed on main and passed on the PR. The `loader::tests` filter passed 4/4 on the PR, including the scratch case and the stock successful-attach case. Synthetic DLLs only. AI-assisted. Codex (GPT-6, exact variant unavailable) wrote and ran the scratch test and drafted this report.
DavidM1911 commented 2026-11-24 14:06:40 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T13:47:11Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib loader: 3 passed, 0 failed.

Note: the full d3d12x lib filter currently aborts with 0xc0000005 on unmodified main too (pre-existing, unrelated to this PR); the PR's own tests were run by name and pass.

<!-- greyfinch-sync:ivall/xcl:comment:7133334143 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1217#issuecomment-7133334143). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T13:47:11Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - cargo test -p xcl --lib loader: 3 passed, 0 failed. Note: the full `d3d12x` lib filter currently aborts with 0xc0000005 on unmodified `main` too (pre-existing, unrelated to this PR); the PR's own tests were run by name and pass.
DavidM1911 commented 2026-11-26 04:07:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T04:05:27Z.


Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

<!-- greyfinch-sync:ivall/xcl:comment:7182285042 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1217#issuecomment-7182285042). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T04:05:27Z. --- Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
github-actions[bot] commented 2026-11-30 08:42:34 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-30T08:42:34Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7273244792 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1217#issuecomment-7273244792). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-30T08:42:34Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
Foniass commented 2026-12-01 05:08:58 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:08:58Z.


Triage (#1607) – head 8933d64

No open or merged PR makes the same change; main's resolver::load_library still returns a registered but uninitialised game DLL at once. When the module is not initialised yet, this PR goes through load_game_dll, which waits on the loader lock (re-entrant on the loading thread). If that DllMain failed, the module is unregistered and gets mapped again, as on Windows. The branch conflicts with main and needs a rebase. No problems found in the diff.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7301470432 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1217#issuecomment-7301470432). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:08:58Z. --- **Triage (#1607)** – head 8933d64 No open or merged PR makes the same change; main's `resolver::load_library` still returns a registered but uninitialised game DLL at once. When the module is not initialised yet, this PR goes through `load_game_dll`, which waits on the loader lock (re-entrant on the loading thread). If that DllMain failed, the module is unregistered and gets mapped again, as on Windows. The branch conflicts with main and needs a rebase. No problems found in the diff. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1217:upstream-pr/1217'
git switch 'upstream-pr/1217'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1217'
git switch 'upstream-pr/1217'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1217'
git switch 'upstream-pr/1217'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1217'
git switch 'main'
git merge --squash 'upstream-pr/1217'
git switch 'main'
git merge --ff-only 'upstream-pr/1217'
git switch 'main'
git merge 'upstream-pr/1217'
git push origin 'main'
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
xcl-dev/xcl!1208
No description provided.