Loader: a game DLL that failed to load is not left registered (#106) #141

Manually merged
github merged 1 commit from gh-pr/141/head into gh-pr/141/base 2026-11-26 04:12:15 +00:00
Owner

@⁠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.rs registers mapped modules before pe.imports() / tls_install() without unregistering (or freeing on TLS error) on failure, and caches DLLs whose DllMain(PROCESS_ATTACH) returned FALSE in MAPPED so a second load_game_dll returns Ok(m)".

map_image_ex registers the module before resolving its imports, so that cyclic game-DLL imports terminate. A failure after that left it in the registry:

failure before after
import table does not parse memory freed, module still registered: the next load_game_dll returned it (use after free) unregistered, memory freed (nothing was resolved against it yet)
static TLS install fails registered, never initialised unregistered, memory kept
DllMain returns FALSE registered: the next load returned Ok without DllMain ever having succeeded unregistered, memory kept; the next load maps it again and runs DllMain again, as Windows' loader does

modules::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 or DllMain failure the memory stays mapped: code DllMain ran, 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::tests builds a PE32+ DLL in the test: one .text section, relocatable with an empty block, entry point xor eax, eax; ret or mov 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 runs DllMain again. 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=4 on main 484dd57 (RTX 5070 Ti): 562 passed, 0 failed, 39 ignored. No game run (only failure paths changed).

AI-assisted.

🤖 Generated with Claude Code

**@⁠tiagonleal** on GitHub · 2026-11-23 15:02 UTC · [original](https://github.com/xcl-dev/xcl/pull/141) `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.rs` registers mapped modules before `pe.imports()` / `tls_install()` without unregistering (or freeing on TLS error) on failure, and caches DLLs whose `DllMain(PROCESS_ATTACH)` returned `FALSE` in `MAPPED` so a second `load_game_dll` returns `Ok(m)`". `map_image_ex` registers the module before resolving its imports, so that cyclic game-DLL imports terminate. A failure after that left it in the registry: | failure | before | after | |---|---|---| | import table does not parse | memory **freed**, module still registered: the next `load_game_dll` returned it (use after free) | unregistered, memory freed (nothing was resolved against it yet) | | static TLS install fails | registered, never initialised | unregistered, memory kept | | `DllMain` returns FALSE | registered: the next load returned `Ok` without `DllMain` ever having succeeded | unregistered, memory kept; the next load maps it again and runs `DllMain` again, as Windows' loader does | `modules::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 or `DllMain` failure the memory stays mapped: code `DllMain` ran, 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::tests` builds a PE32+ DLL in the test: one `.text` section, relocatable with an empty block, entry point `xor eax, eax; ret` or `mov 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 runs `DllMain` again. 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=4` on `main` 484dd57 (RTX 5070 Ti): 562 passed, 0 failed, 39 ignored. No game run (only failure paths changed). _AI-assisted._ 🤖 Generated with [Claude Code](https://claude.com/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]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:12:14 +00:00
github manually merged commit b5380ad539 into gh-pr/141/base 2026-11-26 04:12:15 +00:00
Author
Owner

@⁠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 main eaa36b6: 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).

**@⁠ivall** on GitHub · 2026-11-23 21:08 UTC · [original](https://github.com/xcl-dev/xcl/pull/141#issuecomment-7114071278) Merged (main 6da33fa). Checked on an RX 9070 (RDNA4) with the 22 PRs merged together on main eaa36b6: `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).
Author
Owner

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

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-23 15:02 UTC · [original](https://github.com/xcl-dev/xcl/pull/141#pullrequestreview-6023943621) · **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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.