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

Merged
tiagonleal merged 1 commit from upstream-pr/141 into main 2026-11-23 21:08:36 +00:00
tiagonleal commented 2026-11-23 15:02:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by tiagonleal on 2026-11-23T15:02:38Z.


#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

<!-- greyfinch-sync:ivall/xcl:issue:6562194304 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/141). Originally posted by [tiagonleal](https://github.com/tiagonleal) on 2026-11-23T15:02:38Z. --- #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)
upstream-sync closed this pull request 2026-11-24 06:57:21 +00:00
copilot-pull-request-reviewer[bot commented 2026-11-24 06:57:21 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-23T15:02:48Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ivall/xcl:review:6023943621 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/141#pullrequestreview-6023943621). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/copilot-pull-request-reviewer%5Bbot%5D) on 2026-11-23T15:02:48Z. **Review:** COMMENTED --- 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.
ivall commented 2026-11-24 07:04:20 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T21:08:59Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7114071278 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/141#issuecomment-7114071278). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T21:08:59Z. --- 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).
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!139
No description provided.