Skip to content

PE loader: an executable loaded as a library does not run its entry point - #1682

Merged
Prime1997 merged 1 commit into
xcl-dev:mainfrom
emirduran707:loader-exe-as-library-no-entry
Nov 30, 2026
Merged

Prime1997 merged 1 commit into
xcl-dev:mainfrom
emirduran707:loader-exe-as-library-no-entry

Conversation

@emirduran707

Copy link
Copy Markdown
Contributor

When a title loads an executable from its folder as a library (LoadLibrary of a .exe that is not the main image), load_game_dll logged that it is not a DLL. init_dll then still called the image's entry point with DllMain's arguments, so its mainCRTStartup ran on the calling thread. That either started a second copy of the program inside the process, or the load failed with "DllMain returned FALSE".

Windows does not call that entry point. LoadLibraryW: "If the specified module is an executable module, static imports are not loaded; instead, the module is loaded as if by LoadLibraryEx with the DONT_RESOLVE_DLL_REFERENCES flag", and that flag means DllMain is not called. XCL's thread attach and detach already call the entry point only for a DLL (m.is_dll); process attach now does the same.

What is in it

  • crates/xcl/src/loader.rs: init_dll calls the entry point only when the image is a DLL. Test an_exe_loaded_as_a_library_does_not_run_its_entry_point: an image without the DLL flag whose entry point returns 0 loads, and loads once.

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 c8a4266: 859 passed, 0 failed, 42 ignored; SKIPPED: heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows)
  • the new test without the change: an EXE loads as a library: "DllMain(xcl_exe_as_library.exe) returned FALSE"

Regression run

main:

xcl-smoke v1
commit: c160d86ef2efe3a07e4dfb473e4fa72eafb413c0
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1321 (PresentX 1323)
presents per 2 s (last 3): 44 22 24
device removals: 0
crashes: 0
peak commit: 47832 MB

this PR:

xcl-smoke v1
commit: c8a426675198493f21015fcd06567ff61a7847d6
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1385 (PresentX 1388)
presents per 2 s (last 3): 60 54 50
device removals: 0
crashes: 0
peak commit: 49270 MB

Not tested

  • A title that loads an executable as a library; the smoke title's run is a regression check only.
  • XCL still resolves such an image's static imports and runs its TLS callbacks with PROCESS_ATTACH. Windows does not load the imports; the documentation says nothing about TLS callbacks. Both are left as they were.
  • 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

…oint

load_game_dll noticed that the image is not a DLL but init_dll still
called its entry point with DllMain's arguments, so the program's
mainCRTStartup ran on the loading thread (or the load failed when it
returned 0). LoadLibrary loads an executable as if with
DONT_RESOLVE_DLL_REFERENCES, which does not call DllMain; thread
attach/detach already call the entry point only for a DLL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mantavisual

Copy link
Copy Markdown

Test report for c8a426675198 on a machine with an NVIDIA GPU, which the PR lists as not tested.

Tested

  • RTX 4060 8 GB (driver 616.92, wave32), i5-13600K, 32 GB, Windows 11 Pro 22631, Rust 1.100.0 MSVC, DXC 1.9.2602.17 (SDK 10.0.26100), debug.
  • cargo test --workspace --locked --no-fail-fast -- --test-threads=4: 859 passed, 0 failed, 42 ignored. The new test ran.
  • Merged locally with main 4178e88: 892 passed, 0 failed, 42 ignored.
  • SKIPPED at the head: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).

Do the tests notice a broken fix?

  • Fix reverted: the new test fails with DllMain(xcl_exe_as_library.exe) returned FALSE.
  • The entry point never called for anything, DLLs included: an existing loader::tests test fails.

Not tested: AMD, retail title, a real executable loaded as a library.

AI use: Claude Code (Claude Opus 5.5) ran this on my machine, wrote the report and posted it for me.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

  • cargo test -p xcl --lib: all passed, 0 failed, ~22 ignored. (Intermittent single-test flake under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

@MiharaxD

Copy link
Copy Markdown

Test report on an NVIDIA card, head c8a4266 (on main c160d86), which covers the "NVIDIA" line.

Tested

  • RTX 3070 Ti 8 GB (driver 621.01), Intel i5-14400F, 32 GB RAM, Windows 11 Pro 10.0.26300, Rust 1.100.0, DXC 1.9.2602.17.
  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4 (debug) on c8a4266: 859 passed, 0 failed, 42 ignored (lib tests 810 / 0 / 22).
  • SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask (no wave64), gpu_wave32_program_in_wave64_hardware (no wave64).
  • an_exe_loaded_as_a_library_does_not_run_its_entry_point passes.

Regression run
scripts/smoke.ps1 -Game <game folder> -Seconds 60 (release builds, start screen, no input). main is c160d86:

main:

xcl-smoke v1
commit: c160d86ef2efe3a07e4dfb473e4fa72eafb413c0
gpu: NVIDIA GeForce RTX 3070 Ti
title: game
seconds: 60
exit: 0x0
frames shown: 1586 (PresentX 1586)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15055 MB

this PR's head:

xcl-smoke v1
commit: c8a426675198493f21015fcd06567ff61a7847d6
gpu: NVIDIA GeForce RTX 3070 Ti
title: game
seconds: 60
exit: 0x0
frames shown: 1602 (PresentX 1602)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15420 MB

Not tested

  • A title that loads an executable as a library: the smoke run is a regression check only.
  • AMD / Intel, gameplay past the start screen.

AI use: Claude Code (Claude Opus 5.5) built the PR, ran the tests and both smoke runs, read the logs and wrote this report; I checked it before posting.

@Decodere

Copy link
Copy Markdown
Contributor

Independent desktop NVIDIA verification of PR #1682 at head c8a426675198493f21015fcd06567ff61a7847d6 (1 commit; exact parent & merge-base: c160d86ef2efe3a07e4dfb473e4fa72eafb413c0, pre-#2555 ancestor of main).

Environment

  • GPU: NVIDIA GeForce RTX 4060 (8 GB / 7956 MiB reported by D3D12, Wave32-only Ada), driver 32.0.16.2114 (NVIDIA 621.14, WDDM 3.2)
  • CPU / RAM / OS: Intel Core i5-11400F (6C / 12T), 32 GB RAM (31.9 GiB usable), Windows 11 Home 26200 (Developer Mode off)
  • Toolchain: rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC, DXC 1.9.2609.5, --release

Targeted tests & build (c8a42667)

Command Result
cargo test --release -p xcl --lib -- --nocapture an_exe_loaded_as_a_library_does_not_run_its_entry_point (3 consecutive runs) 1 passed; 0 failed; 0 ignored; 831 filtered out × 3 (0.00s, 0.00s, 0.00s, 0 skipped)
cargo test --release -p xcl --lib -- --nocapture loader:: (3 consecutive runs) 3 passed; 0 failed; 0 ignored; 829 filtered out × 3 (0.00s, 0.00s, 0.00s, 0 skipped)
cargo build --release -p xcl Clean build (1m 23s, exit 0, 0 new warnings in loader.rs)

Paired quiet 60 s retail smoke (title: 29640639)

(Note: scripts/smoke.ps1 at c160d86e predates 9683e7a6 and printed the local folder basename on title:; replaced below with the standard 9683e7a6 title hash 29640639, with all measured fields verbatim.)

Exact parent & merge-base (c160d86ef2efe3a07e4dfb473e4fa72eafb413c0, cached baseline on the same machine):

xcl-smoke v1
commit: c160d86ef2efe3a07e4dfb473e4fa72eafb413c0
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1533 (PresentX 1533)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14721 MB

PR #1682 (c8a426675198493f21015fcd06567ff61a7847d6):

xcl-smoke v1
commit: c8a426675198493f21015fcd06567ff61a7847d6
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1560 (PresentX 1560)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14703 MB

Limits of coverage

  • an_exe_loaded_as_a_library_does_not_run_its_entry_point and the full loader:: suite (3/3, 0 skipped) passed 3/3, directly verifying that init_dll skips calling the entry point when m.is_dll is false (so loading a non-DLL executable as a library maps cleanly without executing entry_rva as DllMain) while preserving DLL DllMain behavior.
  • The 60 s retail smoke confirms live PE loading and runtime stability (0 device removals, 0 crashes, steady 60 Hz) without testing a real title loading a secondary .exe via LoadLibrary; single-run frame/commit delta vs. the cached parent reflects normal startup variance.

AI disclosure: test execution and report drafting assisted by Google Antigravity (Gemini 4 Neon).

@throny

throny commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

merge-check claim
head: c8a4266

@throny

throny commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: b937fba
head: c8a4266
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2141 passed, 1 failed, 48 ignored (26 SKIPPED lines). The one failure is gpu::dxil::msaa_tests::direct_msaa_loads_precede_get_dimensions, which fails on main without this PR too (reported on #3093) and in every tree of this batch; the nine failures main had since #2959 are gone at b937fba. I did not run b937fba alone: the last base-only run is e13d625 (2130 passed, 10 failed, 48 ignored).
review: none blocking. Before, the entry point of any loaded image was called as a DllMain, so an executable loaded as a library ran its startup routine with DllMain arguments and a return of 0 failed the load; now such an image is mapped and its entry point is not run. The flag is the right one (the DLL bit of the file header), thread attach and detach already check it, and the changed function has one caller. Minor, not fixed: TLS callbacks still run and imports are still resolved for such an executable, which Windows does not do in that mode.
performance: none.
verdict: safe to merge
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).

@fromthemoonx

fromthemoonx commented Nov 30, 2026 •

Copy link
Copy Markdown

merge-check
base: 53e85fb
head: c8a4266
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2792 passed, 2 failed, 49 ignored; SKIPPED: 28 (mostly "no wave64"); failing: hle::d3d12x::standin::tests::concurrent_shell_miss_reuses_winner_and_releases_loser_pipeline fails on the base 53e85fb alone too (#2369's test against late_release, see #2369); hle::d3d12x::list_x::tests::mid_list_write64_wait64_split_avoids_cycle_and_reuses_spares hit E_OUTOFMEMORY (0x8007000E) in the parallel run and passes 3/3 alone on both trees. Neither is from this PR.
review: none blocking, after reading the whole diff (loader.rs). init_dll is called only from load_game_dll (the title's LoadLibrary), so the title's own executable is not affected, and an EXE loaded as a library still runs its TLS callbacks. The new test passes 3/3; with the m.is_dll condition removed (scratch) it fails with "DllMain(xcl_exe_as_library.exe) returned FALSE". Side note, from before this PR and not blocking: tls_install treats every !is_dll image as the title's executable and tries take_slot_zero, so an EXE loaded as a library would also try to take TLS slot 0. Also seen once: in my first suite run, e2e::incomplete_discovered_corpus_blocks_headless_title_entry hung, with its xcl child spinning for 40 minutes. Afterwards it passed 3/3 alone on this merge and on main, and the whole e2e binary passed 5/5 on both, so I count it as a one-off rather than this PR.
performance: none (one more condition at library load).
verdict: safe to merge
hardware / tool and model: RTX 3060 12 GB (driver 610.88), Ryzen 7 5700X, Windows 11 Pro 26300, Rust 1.100.0 MSVC, debug / Claude Code (Claude Opus 5.5)

@github-actions github-actions Bot added area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) labels Nov 30, 2026
@Prime1997
Prime1997 merged commit 686c7db into xcl-dev:main Nov 30, 2026
2 checks passed
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 ready-to-merge Has 2 independent safe merge-checks on current head (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants