kernelx: the title's LoadLibrary of a system DLL takes its own reference #1218

Open
github-bot wants to merge 2 commits from github-bot/gh-1218 into main AGit
Owner

@⁠emirduran707 on GitHub · 2026-11-24 13:19 UTC · original
emirduran707:loadlibrary-own-reference → main · +72 −7 · 2 files · 2 commits · open

Stacked on #1151; only the top commit (kernelx: the title's LoadLibrary of a system DLL takes its own reference) belongs here.

resolver::system_module caches one host module handle per DLL name; XCL resolves imports into it and keeps it. The title's LoadLibrary of such a DLL returned that cached handle without taking a reference of its own, while the title's FreeLibrary goes to the real FreeLibrary. So a title that calls LoadLibrary("foo.dll"), then FreeLibrary on the result, dropped XCL's only reference. Windows unloaded the DLL under XCL's cached handle and under every import resolved into it, and the next call through one of them crashed. Microsoft's documentation of LoadLibrary: "Calling LoadLibrary increments the reference count."

load_library_impl (the title's LoadLibraryW/A/ExW/ExA) now takes a reference for the title, with GetModuleHandleExW(FROM_ADDRESS) on the returned module, when the handle is a real host module (not one XCL mapped, not an HLE handle) and the name is bare. Bare names come from the cache. With #1151, a name with a directory is loaded by system_module on every call, and that load is already the title's reference. Other callers of resolver::load_library (delay-load handles) are unchanged.

What is in it

  • crates/xcl/src/hle/kernelx.rs: the reference in load_library_impl; test free_library_of_a_system_dll_leaves_xcl_its_reference. It runs LoadLibraryW + FreeLibrary of dciman32.dll, a System32 DLL no other test uses, and checks that the DLL is still loaded. A second load then finds DCIOpenProvider.

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 8200da8 (this commit on top of #1151): 744 passed, 0 failed, 41 ignored; no SKIPPED GPU tests
  • the new test without the change: GetModuleHandleW("dciman32.dll") is NULL after the title's FreeLibrary (left: 0); 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: 8200da864e6576b8a63d6b33effc2ec441666ff7
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1561 (PresentX 1562)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19341 MB

Not tested

  • A title that frees a system DLL it loaded; 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

**@⁠emirduran707** on GitHub · 2026-11-24 13:19 UTC · [original](https://github.com/xcl-dev/xcl/pull/1218) `emirduran707:loadlibrary-own-reference` → `main` · +72 −7 · 2 files · 2 commits · open Stacked on #1151; only the top commit (`kernelx: the title's LoadLibrary of a system DLL takes its own reference`) belongs here. `resolver::system_module` caches one host module handle per DLL name; XCL resolves imports into it and keeps it. The title's `LoadLibrary` of such a DLL returned that cached handle without taking a reference of its own, while the title's `FreeLibrary` goes to the real `FreeLibrary`. So a title that calls `LoadLibrary("foo.dll")`, then `FreeLibrary` on the result, dropped XCL's only reference. Windows unloaded the DLL under XCL's cached handle and under every import resolved into it, and the next call through one of them crashed. Microsoft's documentation of `LoadLibrary`: "Calling LoadLibrary increments the reference count." `load_library_impl` (the title's `LoadLibraryW/A/ExW/ExA`) now takes a reference for the title, with `GetModuleHandleExW(FROM_ADDRESS)` on the returned module, when the handle is a real host module (not one XCL mapped, not an HLE handle) and the name is bare. Bare names come from the cache. With #1151, a name with a directory is loaded by `system_module` on every call, and that load is already the title's reference. Other callers of `resolver::load_library` (delay-load handles) are unchanged. **What is in it** - `crates/xcl/src/hle/kernelx.rs`: the reference in `load_library_impl`; test `free_library_of_a_system_dll_leaves_xcl_its_reference`. It runs `LoadLibraryW` + `FreeLibrary` of `dciman32.dll`, a System32 DLL no other test uses, and checks that the DLL is still loaded. A second load then finds `DCIOpenProvider`. **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 8200da8 (this commit on top of #1151): 744 passed, 0 failed, 41 ignored; no SKIPPED GPU tests - the new test without the change: `GetModuleHandleW("dciman32.dll")` is NULL after the title's `FreeLibrary` (`left: 0`); 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: 8200da864e6576b8a63d6b33effc2ec441666ff7 gpu: AMD Radeon RX 9060 XT title: retail-title seconds: 60 exit: 0x0 frames shown: 1561 (PresentX 1562) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 19341 MB ``` **Not tested** - A title that frees a system DLL it loaded; 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)
system_module cached host module handles in SYS_CACHE keyed by dll_key,
which drops the directory, and it cached failures (0) too. So after
LoadLibrary("C:\Missing\foo.dll") failed, a later LoadLibrary("foo.dll")
returned NULL without trying, and once version.dll was cached,
LoadLibrary("C:\Mods\version.dll") returned the System32 copy. Windows
loads the path it is given.

A name with a directory component is now loaded as given with
LoadLibraryW and not cached; bare names (import resolution, the HLE
modules' system_module("x.dll") calls) still use the name-keyed cache,
which now only holds successful loads.

Under Wine, a copy of version.dll is a Wine builtin and is mapped to the
module already loaded under that name whatever path is given, so the test
checks the load by path only on Windows (found in a test run under Wine).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
resolver::system_module caches one LoadLibraryW handle per system DLL,
and the title's LoadLibrary of such a DLL handed that cached handle back
without a reference of its own. Its FreeLibrary goes to the real
FreeLibrary, so LoadLibrary + FreeLibrary by the title dropped XCL's only
reference and unloaded a DLL that the cache and the imports resolved into
it still point at. On Windows every LoadLibrary takes a reference and
every FreeLibrary gives one back.

The title-facing LoadLibrary (load_library_impl) now takes a reference on
the real module it returns (GetModuleHandleExW from its address, which
names exactly that module); HLE pseudo handles and modules XCL mapped are
left alone, as FreeLibrary leaves them. Import resolution and XCL's own
callers of resolver::load_library are unchanged.

Test: LoadLibraryW + FreeLibrary of dciman32.dll (System32, used by no
other test) leaves it loaded, and a second load finds DCIOpenProvider.
Before the fix GetModuleHandleW returned NULL after the FreeLibrary.

Only a bare name takes the extra reference: those come from the resolver's
cache. A name with a directory is loaded by system_module itself on every
call, so that load is already the title's own reference.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:47:27 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 13:47 UTC · original

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

  • cargo test -p xcl --lib kernelx: 33 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** on GitHub · 2026-11-24 13:47 UTC · [original](https://github.com/xcl-dev/xcl/pull/1218#issuecomment-7133331982) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - cargo test -p xcl --lib kernelx: 33 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.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 04:05 UTC · original

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

**@⁠DavidM1911** on GitHub · 2026-11-26 04:05 UTC · [original](https://github.com/xcl-dev/xcl/pull/1218#issuecomment-7182285332) 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
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1218/head:github-bot/gh-1218'
git switch 'github-bot/gh-1218'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1218'
git switch 'github-bot/gh-1218'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1218'
git switch 'github-bot/gh-1218'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1218'
git switch 'main'
git merge --squash 'github-bot/gh-1218'
git switch 'main'
git merge --ff-only 'github-bot/gh-1218'
git switch 'main'
git merge 'github-bot/gh-1218'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.