Run, HLE: keep late-loaded title DLLs private until DllMain succeeds and unload on bind failure #3308

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

@⁠fuzeofficlal on GitHub · 2026-11-26 16:55 UTC · original
fuzeofficlal:fix/late-dll-publish-and-bind-rollback → main · +654 −30 · 2 files · 1 commits · open

Summary

Fixes two lifecycle and concurrency bugs in late title DLL loading (LateDlls::load in apps/victory/src/run.rs and IMAGE_PATHS in crates/victory-hle/src/runtime.rs):

  1. Prevent cross-thread observation and use-after-free of uninitialized/rolling-back DLLs:
    Previously, LateDlls::load registered every newly mapped DLL globally in IMAGE_PATHS via set_image_path before running DllMain(DLL_PROCESS_ATTACH). Because GetModuleHandleA/W and GetModuleFileNameA/W query IMAGE_PATHS without acquiring LoaderLock, another thread running the standard fast-path (GetModuleHandle before LoadLibrary) could observe an HMODULE while DllMain was still executing on the loading thread—either invoking exports before initialization finished, or holding a dangling module handle when DllMain returned FALSE and LateDlls::load detached and unmapped the batch via dll.unload().

    victory_hle::runtime now supports thread-staged image paths (stage_image_path / publish_images). Staged images remain visible to the loading thread (so DllMain, GetModuleFileName, GetModuleHandle, FreeLibrary, and re-entrant LoadLibrary continue to work during both attach and rollback detach), stay hidden from all other threads until every DLL in the batch finishes attach(), and are published atomically as a batch before LoaderLock is released.

  2. Roll back mapped DLLs on import-binding failure:
    Previously, if bind_imports failed on a later DLL in a newly mapped batch, early ? exit leaked every mapped LoadedImage in host virtual memory and victory_loader::threads::IMAGES while leaving earlier bound DLLs permanently registered in IMAGE_PATHS without ever running their DllMain or adding them to LateDlls::mapped. LateDlls::load now completes import binding for the entire batch before staging paths or updating mapped, and unloads every newly mapped image in reverse order if binding fails.

Verification

All tests are clean-room synthetic tests using copies of the host's System32\version.dll with unique trailing overlay tags (no game data) and deterministic std::sync::Barrier synchronization (no sleeps):

  • Baseline RED on 63bcca43: cargo test -p victory --locked late_dll_ failed 4/4 deterministically in 0.04s (cross-thread visibility during attach and leaked IMAGE_PATHS entry after bind failure).
  • After fix:
    • rustfmt --check --edition 2024: 0 diffs in apps/victory/src/run.rs and 0 diffs on touched hunks in crates/victory-hle/src/runtime.rs.
    • Targeted tests + 20-round stress loop: victory (late_dll_, 4/4) and victory-hle (runtime::tests::staged_images, 1/1) passed 20/20 consecutive runs.
    • Full crate tests: cargo test -p victory --locked (22/22), cargo test -p victory --all-features --locked (22/22), cargo test -p victory-hle --locked (155/155), cargo test -p victory-hle --all-features --locked (155/155).
    • cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features: clean.
    • cargo test -p xtask --locked policy::tests::repository_passes_policy: passed.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 16:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/3308) `fuzeofficlal:fix/late-dll-publish-and-bind-rollback` → `main` · +654 −30 · 2 files · 1 commits · open ## Summary Fixes two lifecycle and concurrency bugs in late title DLL loading (`LateDlls::load` in `apps/victory/src/run.rs` and `IMAGE_PATHS` in `crates/victory-hle/src/runtime.rs`): 1. **Prevent cross-thread observation and use-after-free of uninitialized/rolling-back DLLs**: Previously, `LateDlls::load` registered every newly mapped DLL globally in `IMAGE_PATHS` via `set_image_path` before running `DllMain(DLL_PROCESS_ATTACH)`. Because `GetModuleHandleA/W` and `GetModuleFileNameA/W` query `IMAGE_PATHS` without acquiring `LoaderLock`, another thread running the standard fast-path (`GetModuleHandle` before `LoadLibrary`) could observe an `HMODULE` while `DllMain` was still executing on the loading thread—either invoking exports before initialization finished, or holding a dangling module handle when `DllMain` returned `FALSE` and `LateDlls::load` detached and unmapped the batch via `dll.unload()`. `victory_hle::runtime` now supports thread-staged image paths (`stage_image_path` / `publish_images`). Staged images remain visible to the loading thread (so `DllMain`, `GetModuleFileName`, `GetModuleHandle`, `FreeLibrary`, and re-entrant `LoadLibrary` continue to work during both attach and rollback detach), stay hidden from all other threads until every DLL in the batch finishes `attach()`, and are published atomically as a batch before `LoaderLock` is released. 2. **Roll back mapped DLLs on import-binding failure**: Previously, if `bind_imports` failed on a later DLL in a newly mapped batch, early `?` exit leaked every mapped `LoadedImage` in host virtual memory and `victory_loader::threads::IMAGES` while leaving earlier bound DLLs permanently registered in `IMAGE_PATHS` without ever running their `DllMain` or adding them to `LateDlls::mapped`. `LateDlls::load` now completes import binding for the entire batch before staging paths or updating `mapped`, and unloads every newly mapped image in reverse order if binding fails. ## Verification All tests are clean-room synthetic tests using copies of the host's `System32\version.dll` with unique trailing overlay tags (no game data) and deterministic `std::sync::Barrier` synchronization (no sleeps): - **Baseline RED on `63bcca43`**: `cargo test -p victory --locked late_dll_` failed 4/4 deterministically in 0.04s (cross-thread visibility during attach and leaked `IMAGE_PATHS` entry after bind failure). - **After fix**: - `rustfmt --check --edition 2024`: 0 diffs in `apps/victory/src/run.rs` and 0 diffs on touched hunks in `crates/victory-hle/src/runtime.rs`. - Targeted tests + 20-round stress loop: `victory` (`late_dll_`, 4/4) and `victory-hle` (`runtime::tests::staged_images`, 1/1) passed 20/20 consecutive runs. - Full crate tests: `cargo test -p victory --locked` (22/22), `cargo test -p victory --all-features --locked` (22/22), `cargo test -p victory-hle --locked` (155/155), `cargo test -p victory-hle --all-features --locked` (155/155). - `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features`: clean. - `cargo test -p xtask --locked policy::tests::repository_passes_policy`: passed.
Stage late-loaded title DLL paths on the loading thread while their DllMain(DLL_PROCESS_ATTACH) callbacks run and publish the batch atomically only after every DLL in the load succeeds, so concurrent GetModuleHandle/GetModuleFileName callers on other threads cannot observe uninitialized or about-to-be-unloaded HMODULEs. Also unload every newly mapped DLL if bind_imports fails instead of leaking mappings and leaving unattached entries in IMAGE_PATHS.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 16:57:41 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 17:08 UTC · original · approved these changes

Reviewed the diff and verified locally on Windows 11 x64:

  • Staging late-loaded title DLL paths as thread-private (stage_image_path) until DllMain succeeds (publish_images) and unloading/forgetting all mapped DLLs on bind_imports or attach failure cleanly prevents cross-thread observation of uninitialized DLLs and mapped-image leaks.
  • cargo test -p victory-hle --lib runtime:: -- --test-threads=1: 5 passed, 0 failed (including staged_images_are_thread_private_until_published_or_forgotten).
  • cargo test -p victory -- --test-threads=1: 18 unit + 4 integration tests passed, 0 failed (including late_dll_bind_failure_unloads_all_mapped_dlls_and_leaves_no_stale_paths, late_dll_publishes_to_other_threads_only_after_attach_succeeds, late_dll_reentrant_load_keeps_outer_hidden_and_inner_published_independently, and late_dll_stays_hidden_from_other_threads_during_attach_and_rolls_back_on_failure).
  • cargo clippy -p victory -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 17:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/3308#pullrequestreview-6078093199) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64: - Staging late-loaded title DLL paths as thread-private (`stage_image_path`) until `DllMain` succeeds (`publish_images`) and unloading/forgetting all mapped DLLs on `bind_imports` or `attach` failure cleanly prevents cross-thread observation of uninitialized DLLs and mapped-image leaks. - `cargo test -p victory-hle --lib runtime:: -- --test-threads=1`: 5 passed, 0 failed (including `staged_images_are_thread_private_until_published_or_forgotten`). - `cargo test -p victory -- --test-threads=1`: 18 unit + 4 integration tests passed, 0 failed (including `late_dll_bind_failure_unloads_all_mapped_dlls_and_leaves_no_stale_paths`, `late_dll_publishes_to_other_threads_only_after_attach_succeeds`, `late_dll_reentrant_load_keeps_outer_hidden_and_inner_published_independently`, and `late_dll_stays_hidden_from_other_threads_during_attach_and_rolls_back_on_failure`). - `cargo clippy -p victory -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 17:32 UTC · original · edited 2026-11-26 17:34 UTC

Independent local verification of PR #3308 on exact head 56b85362bb384b452be5912f4adf154504783416. Control: origin/main 63bcca43c6a00a758510398fa43096ce7e4f7df0.

Environment: Windows 11 x64, Rust 1.98.1 MSVC, CPU-only (no GPU used), no game files, dumps or shaders; the tests use copies of the host's System32\version.dll.

Head results (--locked, -j 4):

  • cargo test -p victory late_dll_: 4/4 passed. Stress loop of 20 consecutive runs: 20/20 passed (4/4 each time).
  • cargo test -p victory-hle --lib staged_images: 1/1 passed, 20/20 consecutive runs.
  • cargo test -p victory (--no-fail-fast): all passed (18 + 2 + 1 + 1 tests across its targets, 0 failed).
  • cargo test -p victory-hle --lib: 152 passed, 1 failed in 3 of 4 full runs, 153/153 in 1 of 4 runs. The failure is modules::dstorage::tests::a_page_list_filled_after_its_request_starts (assertion left == right). It does not involve the code this PR changes.

Control on origin/main: the same dstorage test fails in 2 of 3 full victory-hle --lib runs (151 passed, 1 failed; one run 152/152), so that flake predates this PR. The PR's new tests cannot be built on main because load_with does not exist there.

Negative control on head: I replaced stage_image_path(...) with an immediately-global set_image_path(...) in LateDlls::load_with and kept the PR's tests. 3 of 4 late_dll_ tests then failed: ..._publishes_to_other_threads_only_after_attach_succeeds, ..._reentrant_load_keeps_outer_hidden_... and ..._stays_hidden_from_other_threads_.... Only ..._bind_failure_unloads_... still passed, since that one covers the rollback path, which this mutation does not touch. The mutation was discarded and the tree restored.

AI use: Claude Code (Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-26 17:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/3308#issuecomment-7199195253) · edited 2026-11-26 17:34 UTC Independent local verification of PR #3308 on exact head `56b85362bb384b452be5912f4adf154504783416`. Control: `origin/main` `63bcca43c6a00a758510398fa43096ce7e4f7df0`. Environment: Windows 11 x64, Rust 1.98.1 MSVC, CPU-only (no GPU used), no game files, dumps or shaders; the tests use copies of the host's `System32\version.dll`. Head results (`--locked`, `-j 4`): - `cargo test -p victory late_dll_`: 4/4 passed. Stress loop of 20 consecutive runs: 20/20 passed (4/4 each time). - `cargo test -p victory-hle --lib staged_images`: 1/1 passed, 20/20 consecutive runs. - `cargo test -p victory` (`--no-fail-fast`): all passed (18 + 2 + 1 + 1 tests across its targets, 0 failed). - `cargo test -p victory-hle --lib`: 152 passed, 1 failed in 3 of 4 full runs, 153/153 in 1 of 4 runs. The failure is `modules::dstorage::tests::a_page_list_filled_after_its_request_starts` (assertion `left == right`). It does not involve the code this PR changes. Control on `origin/main`: the same dstorage test fails in 2 of 3 full `victory-hle --lib` runs (151 passed, 1 failed; one run 152/152), so that flake predates this PR. The PR's new tests cannot be built on main because `load_with` does not exist there. Negative control on head: I replaced `stage_image_path(...)` with an immediately-global `set_image_path(...)` in `LateDlls::load_with` and kept the PR's tests. 3 of 4 `late_dll_` tests then failed: `..._publishes_to_other_threads_only_after_attach_succeeds`, `..._reentrant_load_keeps_outer_hidden_...` and `..._stays_hidden_from_other_threads_...`. Only `..._bind_failure_unloads_...` still passed, since that one covers the rollback path, which this mutation does not touch. The mutation was discarded and the tree restored. AI use: Claude Code (Claude Sonnet 5.5).
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/3308/head:github-bot/gh-3308'
git switch 'github-bot/gh-3308'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3308'
git switch 'github-bot/gh-3308'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3308'
git switch 'github-bot/gh-3308'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3308'
git switch 'main'
git merge --squash 'github-bot/gh-3308'
git switch 'main'
git merge --ff-only 'github-bot/gh-3308'
git switch 'main'
git merge 'github-bot/gh-3308'
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!3308
No description provided.