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

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

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


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

<!-- greyfinch-sync:ivall/xcl:issue:5472911718 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1218). Originally posted by [emirduran707](https://github.com/emirduran707) on 2026-11-24T13:19:06Z. --- 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)
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:05Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7133331982 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1218#issuecomment-7133331982). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T13:47:05Z. --- 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 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:29Z.


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:7182285332 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1218#issuecomment-7182285332). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T04:05:29Z. --- 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:36 +00:00 (Migrated from github.com)

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


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:7273245511 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1218#issuecomment-7273245511). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-30T08:42:36Z. --- 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:09:14 +00:00 (Migrated from github.com)

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


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.

<!-- greyfinch-sync:ivall/xcl:comment:7301474769 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1218#issuecomment-7301474769). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:09:14Z. --- **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.
fromthemoonx commented 2026-12-03 09:03:28 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-03T09:03:28Z.


merge-check
base: 16c2fd082a
head: 8200da864e
merge: clean (the first commit, #1151, is on main already; only the top one is new)
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3013 passed, 0 failed, 56 ignored (base: 3012 passed, 0 failed, 56 ignored, counted on 395405e4; only docs and scripts changed since); SKIPPED: 25, the same as base (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps); failing: none
review: read the whole diff as it merges (1 file, +25). No finding.

  1. The bug is real on main's code. With the new reference switched off, the PR's test fails at "dciman32.dll unloaded by the title's FreeLibrary".
  2. The references balance. A scratch test with DLLs that were not loaded before:
calls without the new reference with it
loadperf.dll by name, 3 loads: still loaded after each of 3 frees? no, no, no yes, yes, yes
one free too many after that not loaded not loaded
pdhui.dll by path, 2 loads: still loaded after each of 2 frees? yes, no yes, no

So a load by name leaves XCL its own reference, and a load by path does not gain an extra one. A title that frees more often than it loads can still take XCL's reference, as before.
3) The condition for "cached" in load_library_impl is the same as in resolver::system_module (a name with no slash, backslash or colon). Modules XCL mapped and its own modules are left out, and FreeLibrary leaves out the same ones.
4) Not tested: a retail title (none on this machine), AMD.
performance: none expected: one GetModuleHandleExW for each LoadLibrary of a system DLL by name.
verdict: safe to merge
hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)

<!-- greyfinch-sync:ivall/xcl:comment:7373112817 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1218#issuecomment-7373112817). Originally posted by [fromthemoonx](https://github.com/fromthemoonx) on 2026-12-03T09:03:28Z. --- merge-check base: 16c2fd082ae2e96967a2ab553bc2e1032e491f70 head: 8200da864e6576b8a63d6b33effc2ec441666ff7 merge: clean (the first commit, #1151, is on main already; only the top one is new) build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3013 passed, 0 failed, 56 ignored (base: 3012 passed, 0 failed, 56 ignored, counted on 395405e4; only docs and scripts changed since); SKIPPED: 25, the same as base (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps); failing: none review: read the whole diff as it merges (1 file, +25). No finding. 1) The bug is real on main's code. With the new reference switched off, the PR's test fails at "dciman32.dll unloaded by the title's FreeLibrary". 2) The references balance. A scratch test with DLLs that were not loaded before: | calls | without the new reference | with it | |---|---|---| | `loadperf.dll` by name, 3 loads: still loaded after each of 3 frees? | no, no, no | yes, yes, yes | | one free too many after that | not loaded | not loaded | | `pdhui.dll` by path, 2 loads: still loaded after each of 2 frees? | yes, no | yes, no | So a load by name leaves XCL its own reference, and a load by path does not gain an extra one. A title that frees more often than it loads can still take XCL's reference, as before. 3) The condition for "cached" in `load_library_impl` is the same as in `resolver::system_module` (a name with no slash, backslash or colon). Modules XCL mapped and its own modules are left out, and `FreeLibrary` leaves out the same ones. 4) Not tested: a retail title (none on this machine), AMD. performance: none expected: one `GetModuleHandleExW` for each `LoadLibrary` of a system DLL by name. verdict: safe to merge hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
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/1218:upstream-pr/1218'
git switch 'upstream-pr/1218'

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/1218'
git switch 'upstream-pr/1218'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1218'
git switch 'upstream-pr/1218'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1218'
git switch 'main'
git merge --squash 'upstream-pr/1218'
git switch 'main'
git merge --ff-only 'upstream-pr/1218'
git switch 'main'
git merge 'upstream-pr/1218'
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!1209
No description provided.