Skip to content

kernelx: honour GetModuleHandleEx flags and refcounts on name lookups - #1235

Open
b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:kernelx/getmodulehandleex-refcount-and-flags
Open

b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:kernelx/getmodulehandleex-refcount-and-flags

Conversation

@b3IKK

@b3IKK b3IKK commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

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 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.

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

Assisted-by: OpenCode (Gemini 4 Neon)
@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.

@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
…dev#1235, kernelx twin)

Windows hle/kernelx.rs hunks verbatim reference-only (cfg(windows)-only):
win_module_handle helper, module_handle_impl -> module_handle_in_ex,
module_handle_in cfg(test) shim, PIN/UNCHANGED_REFCOUNT consts,
unknown-bit + PIN|UNCHANGED combo guard, FROM_ADDRESS/null/name legs
via the helper, plus the refcount/flags test.

Linux twin hle/kernelx_linux.rs gets flag-validation only (win64 ABI
untouched, upstream guard order): unknown bits or PIN|UNCHANGED_REFCOUNT
-> *out=0 + ERROR_INVALID_PARAMETER. The refcount-bump half has no Linux
hook point (resolver_linux::system_module is a stub returning None), so
only validation ports. New Linux-shaped twin test covers the invalid
combo + unknown bit (FAILED pre-fix, green post-fix) plus a valid-name
control.

Triage-close xcl-dev#864 as already landed with citation, no code: export at
hle/kernelx.rs:61, impl at :876 returns 0, test at :3077, resolver pin
at resolver.rs:503.

Gates: both cargo checks 0 errors; lib 686/0/1 (+1); diff --check clean;
title fresh-root 12 GPU-only; retail SIGSEGV +0x2237320 HOLDS.
@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 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.

@github-actions github-actions Bot removed the stale No activity for 3 days; closed 4 days later unless something happens label Dec 2, 2026
@fromthemoonx

fromthemoonx commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

merge-check
base: 5ad98ad
head: 33f9d51
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)

@github-actions github-actions Bot added the area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH label Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants