kernelx: honour GetModuleHandleEx flags and refcounts on name lookups #1226

Open
b3IKK wants to merge 1 commit from upstream-pr/1235 into main
b3IKK commented 2026-11-24 13:30:43 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T13:30:43Z.


When GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS was not set, module_handle_ex delegated to module_handle_impl (GetModuleHandleW), ignoring flags. Real Windows DLLs were returned without incrementing or pinning their reference count, so a matching FreeLibrary prematurely unloaded the DLL. Invalid flag combinations (such as PIN | UNCHANGED_REFCOUNT or unknown bits) were also accepted.

This validates GetModuleHandleEx flags (rejecting unknown bits and PIN | UNCHANGED_REFCOUNT with ERROR_INVALID_PARAMETER) and forwards real Windows DLL lookups to ll::GetModuleHandleExW(flags, ...).

What is in it

  • crates/xcl/src/hle/kernelx.rs: add win_module_handle and module_handle_in_ex, validate flags in module_handle_ex, and forward real Windows DLL name lookups to ll::GetModuleHandleExW; add unit test get_module_handle_ex_honours_refcount_and_rejects_invalid_flags (verified failing before the fix).

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 742 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 no wave64 — gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware)
  • cargo test -p xcl --lib get_module_handle_ex_honours_refcount_and_rejects_invalid_flags: 1 passed, 0 failed (failed before the fix)

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1226 (PresentX 1226)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15341 MB

PR:

xcl-smoke v1
commit: 33f9d51f19f4992baaab7dc5e1d8e5fd9bc511a2
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1643 (PresentX 1643)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14476 MB

Not tested

  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Gemini 4 Neon: audited kernelx.rs, wrote the fix and regression test, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.
<!-- greyfinch-sync:ivall/xcl:issue:5473094064 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1235). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T13:30:43Z. --- When `GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS` was not set, `module_handle_ex` delegated to `module_handle_impl` (`GetModuleHandleW`), ignoring `flags`. Real Windows DLLs were returned without incrementing or pinning their reference count, so a matching `FreeLibrary` prematurely unloaded the DLL. Invalid flag combinations (such as `PIN | UNCHANGED_REFCOUNT` or unknown bits) were also accepted. This validates `GetModuleHandleEx` flags (rejecting unknown bits and `PIN | UNCHANGED_REFCOUNT` with `ERROR_INVALID_PARAMETER`) and forwards real Windows DLL lookups to `ll::GetModuleHandleExW(flags, ...)`. **What is in it** - `crates/xcl/src/hle/kernelx.rs`: add `win_module_handle` and `module_handle_in_ex`, validate flags in `module_handle_ex`, and forward real Windows DLL name lookups to `ll::GetModuleHandleExW`; add unit test `get_module_handle_ex_honours_refcount_and_rejects_invalid_flags` (verified failing before the fix). **Tested** - hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2609.5, debug (`scripts/test.ps1`) and release (`scripts/smoke.ps1`) - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`): 742 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 `no wave64` — `gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware`) - `cargo test -p xcl --lib get_module_handle_ex_honours_refcount_and_rejects_invalid_flags`: 1 passed, 0 failed (failed before the fix) **Regression run** main: ``` xcl-smoke v1 commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1226 (PresentX 1226) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15341 MB ``` PR: ``` xcl-smoke v1 commit: 33f9d51f19f4992baaab7dc5e1d8e5fd9bc511a2 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1643 (PresentX 1643) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14476 MB ``` **Not tested** - AMD GPUs and native wave64 hardware - other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine **AI use** - OpenCode, Gemini 4 Neon: audited `kernelx.rs`, wrote the fix and regression test, ran the test suite and `scripts/smoke.ps1` regression runs, and drafted this PR description.
DavidM1911 commented 2026-11-24 14:06:41 +00:00 (Migrated from github.com)

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


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:7133341618 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1235#issuecomment-7133341618). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T13:47:29Z. --- 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 05:05:32 +00:00 (Migrated from github.com)

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


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:7182463693 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1235#issuecomment-7182463693). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T04:17:24Z. --- 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:49 +00:00 (Migrated from github.com)

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


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

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


Triage (#1607) – head 33f9d51

Not on main: module_handle_ex in kernelx.rs still resolves names through module_handle_impl (plain GetModuleHandleW), so GetModuleHandleExW(0, name) takes no reference, and flags are never validated. #1218 fixes a related refcount problem, but in LoadLibrary (the title's own reference to a cached system DLL). It does not touch GetModuleHandleEx, so it is not a duplicate.
The flag validation (only PIN/UNCHANGED_REFCOUNT/FROM_ADDRESS, PIN together with UNCHANGED_REFCOUNT refused, *out = 0, ERROR_INVALID_PARAMETER) matches Windows. Modules XCL maps and HLE pseudo handles still take no reference, which matches FreeLibrary ignoring them. The test fails without the fix, since FreeLibrary would drop cabinet.dll's only reference, unless something else in the test process has loaded cabinet.dll.
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:7301519797 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1235#issuecomment-7301519797). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:11:55Z. --- **Triage (#1607)** – head 33f9d51 Not on main: `module_handle_ex` in kernelx.rs still resolves names through `module_handle_impl` (plain `GetModuleHandleW`), so `GetModuleHandleExW(0, name)` takes no reference, and flags are never validated. #1218 fixes a related refcount problem, but in `LoadLibrary` (the title's own reference to a cached system DLL). It does not touch GetModuleHandleEx, so it is not a duplicate. The flag validation (only PIN/UNCHANGED_REFCOUNT/FROM_ADDRESS, PIN together with UNCHANGED_REFCOUNT refused, `*out = 0`, ERROR_INVALID_PARAMETER) matches Windows. Modules XCL maps and HLE pseudo handles still take no reference, which matches `FreeLibrary` ignoring them. The test fails without the fix, since `FreeLibrary` would drop cabinet.dll's only reference, unless something else in the test process has loaded cabinet.dll. 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-02 21:46:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-02T21:46:38Z.


merge-check
base: 5ad98adbc3
head: 33f9d51f19
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3006 passed, 0 failed, 52 ignored; SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps); failing: none
review: read the whole diff (1 file, +63 -14). No problem found.

  • Compared with Windows in a scratch test (not pushed): GetModuleHandleExW of XCL and of Windows with flags 0 to 15, for a loaded DLL's name, a missing name, NULL, an address in the DLL and a bad address (40 cases). I compared the return value, the output handle, the error code and how many references the DLL has afterwards.
    • this PR: 40 of 40 the same as Windows;
    • main: 27 differ. Flags 0 and PIN take no reference, so a title's matching FreeLibrary unloads the DLL, and invalid flags are accepted or give the wrong error.
  • The new test fails when the lookup takes no reference, and when the flag check is taken out.
  • Modules that XCL maps itself, and its own stand-ins, still take no reference. FreeLibrary ignores them, so that is consistent.
  • GetModuleHandleExA shares the code; I only ran the W form. No title was run here.
  • This head shows no Build and test (Windows, no GPU) run. It needs one before a merge.
    performance: none expected, one flag check per call.
    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:7359765383 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1235#issuecomment-7359765383). Originally posted by [fromthemoonx](https://github.com/fromthemoonx) on 2026-12-02T21:46:38Z. --- merge-check base: 5ad98adbc360225ac8a57ad9661e16fe8df9d8dd head: 33f9d51f19f4992baaab7dc5e1d8e5fd9bc511a2 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3006 passed, 0 failed, 52 ignored; SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps); failing: none review: read the whole diff (1 file, +63 -14). No problem found. - Compared with Windows in a scratch test (not pushed): `GetModuleHandleExW` of XCL and of Windows with flags 0 to 15, for a loaded DLL's name, a missing name, NULL, an address in the DLL and a bad address (40 cases). I compared the return value, the output handle, the error code and how many references the DLL has afterwards. - this PR: 40 of 40 the same as Windows; - main: 27 differ. Flags 0 and PIN take no reference, so a title's matching `FreeLibrary` unloads the DLL, and invalid flags are accepted or give the wrong error. - The new test fails when the lookup takes no reference, and when the flag check is taken out. - Modules that XCL maps itself, and its own stand-ins, still take no reference. `FreeLibrary` ignores them, so that is consistent. - `GetModuleHandleExA` shares the code; I only ran the W form. No title was run here. - This head shows no `Build and test (Windows, no GPU)` run. It needs one before a merge. performance: none expected, one flag check per call. 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/1235:upstream-pr/1235'
git switch 'upstream-pr/1235'

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