Loader: unload already-mapped title DLLs when a dependency fails #3054

Open
github-bot wants to merge 1 commit from github-bot/gh-3054 into main AGit
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 05:37 UTC · original
Dukestar69:fix/loader-unload-dlls-on-error → main · +112 −3 · 2 files · 1 commits · open

Summary

Unloads already-mapped title DLLs (calling LoadedImage::unload, which removes them from threads::IMAGES and frees their HMODULE) when loading a transitive or sibling dependency fails in map_dll, load_title_dll, or load_title_dlls.

Why

LoadedImage intentionally has no Drop implementation because mapped images stay live after being handed to the runtime. However:

  1. In map_dll, load(&prepared, dirs.1) maps the DLL and registers it in threads::IMAGES before recursively calling map_imports(&image.info, ...). If any transitive dependency fails to read, prepare, or load, ? dropped image without unloading it.
  2. In load_title_dll and load_title_dlls, if a later sibling or transitive dependency fails after earlier DLLs were already pushed into loaded, ? dropped loaded without unloading them.

In both cases, those HMODULEs remained permanently mapped in the process (keeping their cached .dll files locked on disk) and permanently registered in threads::IMAGES, which is reachable at runtime via LateDlls::load if a dynamically loaded title DLL has a broken dependency.

(Scoped to map_dll / load_title_dll / load_title_dlls only, with a separate integration test file — zero overlap with #1201, which handles load() TLS redirect failure.)

Verification

  • Added synthetic integration tests in crates/victory-loader/tests/dll_unload_on_error.rs:
    • load_title_dll_unloads_parent_when_dependency_fails (failed on 63bcca4, passes with fix)
    • load_title_dlls_unloads_earlier_siblings_when_later_dll_fails (failed on 63bcca4, passes with fix)
  • cargo test -p victory-loader -- --test-threads=1: all 41 tests passed (35 unit + 6 integration).
  • cargo clippy -p victory-loader --all-targets -- -D warnings: clean.
  • rustfmt --edition 2024 --check: clean.
  • cargo xtask policy: 192 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 05:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/3054) `Dukestar69:fix/loader-unload-dlls-on-error` → `main` · +112 −3 · 2 files · 1 commits · open ## Summary Unloads already-mapped title DLLs (calling `LoadedImage::unload`, which removes them from `threads::IMAGES` and frees their `HMODULE`) when loading a transitive or sibling dependency fails in `map_dll`, `load_title_dll`, or `load_title_dlls`. ### Why `LoadedImage` intentionally has no `Drop` implementation because mapped images stay live after being handed to the runtime. However: 1. In `map_dll`, `load(&prepared, dirs.1)` maps the DLL and registers it in `threads::IMAGES` before recursively calling `map_imports(&image.info, ...)`. If any transitive dependency fails to read, prepare, or load, `?` dropped `image` without unloading it. 2. In `load_title_dll` and `load_title_dlls`, if a later sibling or transitive dependency fails after earlier DLLs were already pushed into `loaded`, `?` dropped `loaded` without unloading them. In both cases, those `HMODULE`s remained permanently mapped in the process (keeping their cached `.dll` files locked on disk) and permanently registered in `threads::IMAGES`, which is reachable at runtime via `LateDlls::load` if a dynamically loaded title DLL has a broken dependency. *(Scoped to `map_dll` / `load_title_dll` / `load_title_dlls` only, with a separate integration test file — zero overlap with #1201, which handles `load()` TLS redirect failure.)* ### Verification - Added synthetic integration tests in `crates/victory-loader/tests/dll_unload_on_error.rs`: - `load_title_dll_unloads_parent_when_dependency_fails` (failed on `63bcca4`, passes with fix) - `load_title_dlls_unloads_earlier_siblings_when_later_dll_fails` (failed on `63bcca4`, passes with fix) - `cargo test -p victory-loader -- --test-threads=1`: all 41 tests passed (35 unit + 6 integration). - `cargo clippy -p victory-loader --all-targets -- -D warnings`: clean. - `rustfmt --edition 2024 --check`: clean. - `cargo xtask policy`: 192 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:27:39 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 06:18 UTC · original

Automated triage for the maintainers.

Verdict: merge candidate. In crates/victory-loader/src/windows.rs, map_dll now unloads its own image when a dependency fails. load_title_dll and load_title_dlls unload, in reverse order, whatever was already pushed to loaded. Each image is unloaded exactly once on any error path: the parent unloads itself before returning, and earlier dependencies are in loaded. On these paths nothing has been bound or attached yet, because LateDlls::load in apps/victory/src/run.rs binds and attaches only after load_title_dll returns Ok, and load() maps prepared images that have no entry point. So LoadedImage::unload's safety requirement holds. The runtime path that matters is LateDlls::load (a title LoadLibrary with a broken dependency). The two new integration tests show the leak through the locked cache file.

CI: no check runs reported for this head (not run). It should run before merging.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 06:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/3054#issuecomment-7184305784) <!-- victory-triage v1 head=8d6eddef6a139eecfa973a93ec803ebc30fd9f0d verdict=merge-candidate at=2026-11-26T06:21:30Z --> Automated triage for the maintainers. **Verdict: merge candidate.** In `crates/victory-loader/src/windows.rs`, `map_dll` now unloads its own image when a dependency fails. `load_title_dll` and `load_title_dlls` unload, in reverse order, whatever was already pushed to `loaded`. Each image is unloaded exactly once on any error path: the parent unloads itself before returning, and earlier dependencies are in `loaded`. On these paths nothing has been bound or attached yet, because `LateDlls::load` in `apps/victory/src/run.rs` binds and attaches only after `load_title_dll` returns `Ok`, and `load()` maps prepared images that have no entry point. So `LoadedImage::unload`'s safety requirement holds. The runtime path that matters is `LateDlls::load` (a title `LoadLibrary` with a broken dependency). The two new integration tests show the leak through the locked cache file. CI: no check runs reported for this head (not run). It should run before merging. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-26 06:01 UTC · original · approved these changes

Verified: the failure path genuinely leaked — a transitive dep failing left the parent mapped for process life, and a later sibling failing leaked all earlier ones. The unload safety argument holds (imports are bound and entry points run only after full load); reverse order unloads dependents first. The synthetic-DLL tests are cleverly minimal and the cached-file deletion check is a real probe on Windows — a leaked mapping keeps the file undeletable.

**@⁠kotekompotek** on GitHub · 2026-11-26 06:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/3054#pullrequestreview-6069251137) · **approved these changes** Verified: the failure path genuinely leaked — a transitive dep failing left the parent mapped for process life, and a later sibling failing leaked all earlier ones. The unload safety argument holds (imports are bound and entry points run only after full load); reverse order unloads dependents first. The synthetic-DLL tests are cleverly minimal and the cached-file deletion check is a real probe on Windows — a leaked mapping keeps the file undeletable.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 06:19 UTC · original · approved these changes

Verified at 8d6eddef on Windows 11 x64 (Rust 1.98.1 MSVC):

  • Checked map_dll and load_title_dll/load_title_dlls unwind paths: if map_imports fails after load() maps a parent DLL, map_dll unloads the parent before pushing it to loaded, and the top-level entry points unwind loaded in reverse order (unload_all), avoiding both HMODULE leaks and double-unloads. LoadedImage::unload also calls crate::threads::unmapped(self.base()), so no stale thread-notification entry remains.
  • cargo test -p victory-loader --locked: 41 passed, 0 failed (including both new dll_unload_on_error tests).
  • cargo clippy -p victory-loader --locked --all-targets -- -D warnings: clean.

Looks good.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 06:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/3054#pullrequestreview-6069431887) · **approved these changes** Verified at `8d6eddef` on Windows 11 x64 (Rust 1.98.1 MSVC): - Checked `map_dll` and `load_title_dll`/`load_title_dlls` unwind paths: if `map_imports` fails after `load()` maps a parent DLL, `map_dll` unloads the parent before pushing it to `loaded`, and the top-level entry points unwind `loaded` in reverse order (`unload_all`), avoiding both `HMODULE` leaks and double-unloads. `LoadedImage::unload` also calls `crate::threads::unmapped(self.base())`, so no stale thread-notification entry remains. - `cargo test -p victory-loader --locked`: 41 passed, 0 failed (including both new `dll_unload_on_error` tests). - `cargo clippy -p victory-loader --locked --all-targets -- -D warnings`: clean. Looks good.
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-26 19:41 UTC · original

NVIDIA full-suite run, to go with the reports above by fuzeofficlal, since none of them names a GPU run. Tested exact head 8d6edde (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 868 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: load_title_dll_unloads_parent_when_dependency_fails, load_title_dlls_unloads_earlier_siblings_when_later_dll_fails.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-26 19:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/3054#issuecomment-7201865135) NVIDIA full-suite run, to go with the reports above by fuzeofficlal, since none of them names a GPU run. Tested exact head `8d6edde` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **868 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `load_title_dll_unloads_parent_when_dependency_fails`, `load_title_dlls_unloads_earlier_siblings_when_later_dll_fails`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3054/head:github-bot/gh-3054'
git switch 'github-bot/gh-3054'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3054'
git switch 'github-bot/gh-3054'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3054'
git switch 'github-bot/gh-3054'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3054'
git switch 'main'
git merge --squash 'github-bot/gh-3054'
git switch 'main'
git merge --ff-only 'github-bot/gh-3054'
git switch 'main'
git merge 'github-bot/gh-3054'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3054
No description provided.