Loader: a game DLL that failed to load is not left registered (#106) #141
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!141
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/141/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@tiagonleal on GitHub · 2026-11-23 15:02 UTC · original
tiagonleal:fix/loader-failed-dll-unregistered→main· +133 −4 · 2 files · 1 commits · merged 2026-11-23 21:08 UTC#106 item 3, the loader part: "
loader.rsregisters mapped modules beforepe.imports()/tls_install()without unregistering (or freeing on TLS error) on failure, and caches DLLs whoseDllMain(PROCESS_ATTACH)returnedFALSEinMAPPEDso a secondload_game_dllreturnsOk(m)".map_image_exregisters the module before resolving its imports, so that cyclic game-DLL imports terminate. A failure after that left it in the registry:load_game_dllreturned it (use after free)DllMainreturns FALSEOkwithoutDllMainever having succeededDllMainagain, as Windows' loader doesmodules::unregister_mapped(base)takes the module out of the name / base / address lookups (it also clears its slot in the lock-free range table). After a TLS orDllMainfailure the memory stays mapped: codeDllMainran, or game DLLs loaded while its imports were resolved, may point into it. A leak on a failure path is safer than a dangling pointer.Tests
loader::testsbuilds a PE32+ DLL in the test: one.textsection, relocatable with an empty block, entry pointxor eax, eax; retormov eax, 1; ret.a_dll_whose_dllmain_fails_is_not_loaded: both loads fail with "returned FALSE" and nothing is left registered. The second load is relocated, since the first copy's memory stays, and runsDllMainagain. The TRUE variant loads, and a second load returns the same module.a_dll_with_a_broken_import_table_is_not_left_registered: an import descriptor whose name RVA lies outside the image. Both loads fail with "import table" and nothing is left registered.Both tests failed without the change (
attempt 0: still registered).cargo test --workspace --no-fail-fast -- --test-threads=4onmain484dd57(RTX 5070 Ti): 562 passed, 0 failed, 39 ignored. No game run (only failure paths changed).AI-assisted.
🤖 Generated with Claude Code
map_image_ex registers the module before resolving its imports (so cyclic game-DLL imports terminate), but a failure after that left it registered: an import table that did not parse freed its memory with the module still in the registry (a later load_game_dll returned it – freed memory), a static TLS failure kept it registered uninitialised, and a DllMain returning FALSE left it there too, so the next load returned Ok without DllMain ever having succeeded. modules::unregister_mapped takes such a module out (by name, base and address lookups). After an import table failure its memory goes as before; after a TLS or DllMain failure it stays (code it ran, or DLLs loaded while its imports were resolved, may point into it). A later load maps the DLL again and runs DllMain again, as Windows' loader does. Tests (a PE32+ DLL built in the test): DllMain returning FALSE fails both loads and leaves nothing registered (the second one relocated and ran DllMain again); a broken import table likewise; returning TRUE loads once. Both failed before the change ("still registered"). Co-Authored-By: Claude Opus 5.5 <[email protected]>@ivall on GitHub · 2026-11-23 21:08 UTC · original
Merged (main
6da33fa). Checked on an RX 9070 (RDNA4) with the 22 PRs merged together on maineaa36b6:cargo build --workspace && cargo test --workspace -- --test-threads=4- 582 unit tests passed, 0 failed (20 ignored), the integration and e2e tests passed too; and the retail game still reaches its first menu (646 frames in 30 s, no device removal).@copilot-pull-request-reviewer[bot] on GitHub · 2026-11-23 15:02 UTC · original · reviewed
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.