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

Merged
fuzeofficlal merged 1 commit from upstream-pr/2447 into main 2026-11-30 14:12:02 +00:00
fuzeofficlal commented 2026-11-29 13:07:48 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by fuzeofficlal on 2026-11-29T13:07:48Z.


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, GetModuleFileNameA/W, and image_containing query IMAGE_PATHS without acquiring LoaderLock, another thread 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 / reading unmapped headers 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, image_containing, 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

  • Added clean-room synthetic tests using copies of Windows' System32\version.dll with unique trailing overlay tags and Barrier synchronization in apps/victory/src/run.rs (late_dll_*) and crates/victory-hle/src/runtime.rs (staged_images_are_thread_private_until_published_or_forgotten).
  • Verified locally on VIctory-NVIDIA:
    • 5 consecutive runs of cargo test --locked -p victory late_dll_
    • cargo test --locked -p victory
    • cargo test --locked -p victory-hle -- --test-threads=1
    • cargo clippy --locked -p victory -p victory-hle --all-targets (no new warnings)
    • cargo run --quiet --locked -p xtask -- policy --staged
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5551006928 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2447). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-29T13:07:48Z. --- ## 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`, `GetModuleFileNameA/W`, and `image_containing` query `IMAGE_PATHS` without acquiring `LoaderLock`, another thread 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 / reading unmapped headers 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`, `image_containing`, 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 - Added clean-room synthetic tests using copies of Windows' `System32\version.dll` with unique trailing overlay tags and `Barrier` synchronization in `apps/victory/src/run.rs` (`late_dll_*`) and `crates/victory-hle/src/runtime.rs` (`staged_images_are_thread_private_until_published_or_forgotten`). - Verified locally on `VIctory-NVIDIA`: - 5 consecutive runs of `cargo test --locked -p victory late_dll_` - `cargo test --locked -p victory` - `cargo test --locked -p victory-hle -- --test-threads=1` - `cargo clippy --locked -p victory -p victory-hle --all-targets` (no new warnings) - `cargo run --quiet --locked -p xtask -- policy --staged`
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
trustno1-coder/VIctory-NVIDIA!2447
No description provided.