Skip to content

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

Open
emirduran707 wants to merge 2 commits into
xcl-dev:mainfrom
emirduran707:loadlibrary-own-reference
Open

emirduran707 wants to merge 2 commits into
xcl-dev:mainfrom
emirduran707:loadlibrary-own-reference

Conversation

@emirduran707

@emirduran707 emirduran707 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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 PE loader: load system DLLs by path as given and do not cache failures #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

@DavidM1911

Copy link
Copy Markdown
Contributor

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.

emirduran707 and others added 2 commits November 24, 2026 17:04
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
@DavidM1911

Copy link
Copy Markdown
Contributor

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

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…pstream xcl-dev#1218 + xcl-dev#1151, resolver verbatim reference-only)

Windows resolver.rs hunk verbatim reference-only (cfg(windows)-only):
system_module by-path early-load + name-keyed cache + failures-never-cached
rewrite + module_path helper + 2 tests. The xcl-dev#1218 resolver hunk is
byte-identical to xcl-dev#1151 (git diff pr1151head pr1218head -- resolver.rs
empty) — landed once, xcl-dev#1151 triage-closes as CONTAINED.

Windows hle/kernelx.rs hunks verbatim reference-only (cfg(windows)-only):
load_library_impl title-owned GetModuleHandleExW(FROM_ADDRESS)
self-reference block + free_library_of_a_system_dll test.

NO Linux twin change: resolver_linux::system_module (resolver_linux.rs)
is a stub returning None always — no LoadLibrary twin on Linux, same
category as xcl-dev#1667 no-hook-point. Both live heads re-verified pre-commit
(pr1218head=8200da86 +72/-7 2 files, pr1151head=5157a51c +47/-7 1 file,
both still OPEN, merge-base false, same hashes as plan pin, no force-push).
@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale No activity for 3 days; closed 4 days later unless something happens label Nov 30, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Triage (#1607) – head 8200da8

The first commit on the branch (5157a51) is already on main through #1151. Only the kernelx.rs commit is new, and main does not have it. kernelx.rs:load_library_impl takes an extra reference (GetModuleHandleExW without UNCHANGED_REFCOUNT) for a bare-name load of a real system module whose handle system_module caches, so the title's FreeLibrary no longer drops XCL's only reference. Related: #1235 (GetModuleHandleEx refcounts on name lookups) is a different change. 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.

@github-actions github-actions Bot removed the stale No activity for 3 days; closed 4 days later unless something happens label Dec 3, 2026
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.

3 participants