Loader: free mapped module on TLS redirect error and add windows.rs unit tests #1201

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

@⁠geiyiliyabumo on GitHub · 2026-11-24 16:24 UTC · original
geiyiliyabumo:test/loader-windows → main · +413 −1 · 1 files · 1 commits · open

Summary

Closes #1047

Full verification details and change notes are documented in #1047 (commit 98b761b, based on main ffe310e).

 crates/victory-loader/src/windows.rs | 596 ++++++++++++++++++++++++++++++++++-
 1 file changed, 592 insertions(+), 4 deletions(-)

Verification (Windows 11 x64 MSVC)

  • rustfmt --edition 2024 --check on edited files: PASS
  • cargo xtask policy: PASS (zero game files/data)
  • cargo clippy --all-targets --locked -- -D warnings (default + --features diagnostics where applicable): PASS
  • Targeted & crate unit/GPU tests: PASS

AI-assisted; synthetic tests only, no game files.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 16:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/1201) `geiyiliyabumo:test/loader-windows` → `main` · +413 −1 · 1 files · 1 commits · open ### Summary Closes #1047 Full verification details and change notes are documented in #1047 (commit `98b761b`, based on `main` `ffe310e`). ```text crates/victory-loader/src/windows.rs | 596 ++++++++++++++++++++++++++++++++++- 1 file changed, 592 insertions(+), 4 deletions(-) ``` ### Verification (Windows 11 x64 MSVC) - `rustfmt --edition 2024 --check` on edited files: **PASS** - `cargo xtask policy`: **PASS** (zero game files/data) - `cargo clippy --all-targets --locked -- -D warnings` (default + `--features diagnostics` where applicable): **PASS** - Targeted & crate unit/GPU tests: **PASS** *AI-assisted; synthetic tests only, no game files.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:23:26 +00:00
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 18:00 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 98b761b: cargo test -p victory-loader passes, 38 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 18:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/1201#issuecomment-7140015711) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 98b761b: `cargo test -p victory-loader` passes, 38 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:26 UTC · original · edited 2026-11-25 02:29 UTC

Automated triage for the maintainers.

Merge candidate. Recommended for a maintainer's look. Thanks for trimming this as asked. The range guards and their tests are gone. What remains is the FreeLibrary(module) in load (crates/victory-loader/src/windows.rs) when tls::redirect fails, so a load that fails after mapping no longer leaves the copy mapped. LoadedImage has no Drop, and threads::mapped has not run yet at that point, so freeing it there is safe.

The added tests cover existing behaviour: provenance-based binding, protection restore, unresolved-import stubs, export lookup, title DLL mapping, and the new unmap-on-failure path. That last test forces the failure through tls::near's 1 GiB size refusal. The PR body still describes the old 596-line version and points to #1047. Updating it would help the reviewer.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1201#issuecomment-7149550231) · edited 2026-11-25 02:29 UTC <!-- victory-triage v1 head=5edb22b53a17d3025a43ddac4d407aad60c250d7 verdict=merge-candidate at=2026-11-25T02:20:29Z --> Automated triage for the maintainers. **Merge candidate.** Recommended for a maintainer's look. Thanks for trimming this as asked. The range guards and their tests are gone. What remains is the `FreeLibrary(module)` in `load` (`crates/victory-loader/src/windows.rs`) when `tls::redirect` fails, so a load that fails after mapping no longer leaves the copy mapped. `LoadedImage` has no `Drop`, and `threads::mapped` has not run yet at that point, so freeing it there is safe. The added tests cover existing behaviour: provenance-based binding, protection restore, unresolved-import stubs, export lookup, title DLL mapping, and the new unmap-on-failure path. That last test forces the failure through `tls::near`'s 1 GiB size refusal. The PR body still describes the old 596-line version and points to #1047. Updating it would help the reviewer. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-25 01:39 UTC · original

Updated per triage feedback: dropped the three range guards (IAT bound, TLS callback range, and DLL/EXE entry-point range checks) along with their tests, keeping only the \FreeLibrary\ cleanup on \ ls::redirect\ failure and the existing-behavior unit tests. Rebased onto \main; \cargo test -p victory-loader --locked\ (47 passed, 0 failed), \cargo clippy -p victory-loader --all-targets --locked -- -D warnings, and \cargo xtask policy\ all pass.

**@⁠geiyiliyabumo** on GitHub · 2026-11-25 01:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/1201#issuecomment-7149811622) Updated per triage feedback: dropped the three range guards (IAT bound, TLS callback range, and DLL/EXE entry-point range checks) along with their tests, keeping only the \FreeLibrary\ cleanup on \ ls::redirect\ failure and the existing-behavior unit tests. Rebased onto \main\; \cargo test -p victory-loader --locked\ (47 passed, 0 failed), \cargo clippy -p victory-loader --all-targets --locked -- -D warnings\, and \cargo xtask policy\ all pass.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 01:14 UTC · original

Code review and Windows 11 verification at head 5edb22b53a17 merged cleanly onto main (63bcca43c6a0):

  • Code review (crates/victory-loader/src/windows.rs:85-92, 436-839):
    • Calling FreeLibrary(module) before returning on tls::redirect failure cleanly eliminates the leaked HMODULE (since LoadedImage has no Drop impl). Runs strictly before threads::mapped(&loaded) and after prepare() strips entrypoints/imports, so there is no double-free or dangling registry pointer.
    • All 9 new windows.rs unit tests thoroughly cover load/unload, write_if_changed, bind_imports, section protections, unresolved stubs, a_failed_tls_redirect_unmaps_the_image (verified via GetModuleHandleW), export, and load_title_dlls.
  • Verified (Windows 11, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy -p victory-loader --all-targets -- -D warnings: 0 warnings
    • victory-loader: 44/44 unit + 4/4 integration (crash_log, load_probe, title_dlls) passed.

LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 01:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/1201#issuecomment-7179572449) Code review and Windows 11 verification at head `5edb22b53a17` merged cleanly onto `main` (`63bcca43c6a0`): - **Code review (`crates/victory-loader/src/windows.rs:85-92, 436-839`)**: - Calling `FreeLibrary(module)` before returning on `tls::redirect` failure cleanly eliminates the leaked `HMODULE` (since `LoadedImage` has no `Drop` impl). Runs strictly before `threads::mapped(&loaded)` and after `prepare()` strips entrypoints/imports, so there is no double-free or dangling registry pointer. - All 9 new `windows.rs` unit tests thoroughly cover `load`/`unload`, `write_if_changed`, `bind_imports`, section protections, unresolved stubs, `a_failed_tls_redirect_unmaps_the_image` (verified via `GetModuleHandleW`), `export`, and `load_title_dlls`. - **Verified (Windows 11, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy -p victory-loader --all-targets -- -D warnings`: 0 warnings - `victory-loader`: **44/44 unit + 4/4 integration (`crash_log`, `load_probe`, `title_dlls`) passed**. LGTM.
Author
Owner

@⁠potatoe0100 on GitHub · 2026-11-26 14:21 UTC · original

Heads-up for the merge batch: this PR and #1059 are both merge candidates and each merges cleanly onto main (63bcca4), but together they don't compile. #1059 changes victory_loader::windows::load to take a third original_name: &str argument, and the two new tests here call load(&prepared, dir.path()) (windows.rs, around lines 594 and 769 after both merge). I checked main + #1059 + #1201: cargo test -p victory-loader --no-run fails with E0061 twice. Whichever lands second needs the extra argument.

AI-assisted (Claude).

**@⁠potatoe0100** on GitHub · 2026-11-26 14:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/1201#issuecomment-7194686710) Heads-up for the merge batch: this PR and #1059 are both merge candidates and each merges cleanly onto `main` (`63bcca4`), but together they don't compile. #1059 changes `victory_loader::windows::load` to take a third `original_name: &str` argument, and the two new tests here call `load(&prepared, dir.path())` (`windows.rs`, around lines 594 and 769 after both merge). I checked `main` + #1059 + #1201: `cargo test -p victory-loader --no-run` fails with E0061 twice. Whichever lands second needs the extra argument. AI-assisted (Claude).
Author
Owner

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

Reviewed 5edb22b53a17d3025a43ddac4d407aad60c250d7 merged cleanly onto main (63bcca43c6a0) and verified on Windows 11 x64 (Rust 1.98.1, debug and --release).

Code Review

  • Leak fix (crates/victory-loader/src/windows.rs:85-92): Calling unsafe { FreeLibrary(module) } on tls::redirect failure cleanly unmaps the module (along with its OS unwind table and static TLS slot) before returning the error. Because prepare() strips entry points and imports, no title code runs during LoadLibraryExW; threads::mapped(&loaded) runs after tls::redirect succeeds (so no dangling module registration exists), and LoadedImage has no Drop impl (so there is no double-free risk).
  • Test coverage (crates/victory-loader/src/windows.rs:577-839): The 9 new unit tests thoroughly exercise load/unload, write_if_changed on unchanged read-only files, provenance-based import/delay-import binding, per-section memory protection restoration, callable stubs for unresolved imports, export lookups, deduplicated lowercase title DLL loading, and a_failed_tls_redirect_unmaps_the_image (verifying GetModuleHandleW == 0 after the forced tls::near failure).
  • Merge-batch note: As noted earlier in the thread, if #1059 (which adds a third original_name: &str parameter to windows::load) lands before this PR, the two new load(&prepared, dir.path()) test calls here will need the extra argument.

Verification (Windows 11 x64, Rust 1.98.1)

  • cargo run --locked -p xtask -- policy: 191 files ok.
  • cargo clippy --locked -p victory-loader -p victory-pe --all-targets -- -D warnings (default and --all-features): 0 warnings.
  • cargo test --locked -p victory-loader -p victory-pe -- --test-threads=1 (both dev and --release): 84/84 passed in each profile (victory-loader: 44 unit + 1 crash_log + 2 load_probe + 1 title_dlls; victory-pe: 36 unit).

LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 14:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/1201#pullrequestreview-6075994357) · **approved these changes** Reviewed `5edb22b53a17d3025a43ddac4d407aad60c250d7` merged cleanly onto `main` (`63bcca43c6a0`) and verified on Windows 11 x64 (Rust `1.98.1`, debug and `--release`). ### Code Review - **Leak fix (`crates/victory-loader/src/windows.rs:85-92`)**: Calling `unsafe { FreeLibrary(module) }` on `tls::redirect` failure cleanly unmaps the module (along with its OS unwind table and static TLS slot) before returning the error. Because `prepare()` strips entry points and imports, no title code runs during `LoadLibraryExW`; `threads::mapped(&loaded)` runs after `tls::redirect` succeeds (so no dangling module registration exists), and `LoadedImage` has no `Drop` impl (so there is no double-free risk). - **Test coverage (`crates/victory-loader/src/windows.rs:577-839`)**: The 9 new unit tests thoroughly exercise `load`/`unload`, `write_if_changed` on unchanged read-only files, provenance-based import/delay-import binding, per-section memory protection restoration, callable stubs for unresolved imports, export lookups, deduplicated lowercase title DLL loading, and `a_failed_tls_redirect_unmaps_the_image` (verifying `GetModuleHandleW == 0` after the forced `tls::near` failure). - **Merge-batch note**: As noted earlier in the thread, if `#1059` (which adds a third `original_name: &str` parameter to `windows::load`) lands before this PR, the two new `load(&prepared, dir.path())` test calls here will need the extra argument. ### Verification (Windows 11 x64, Rust `1.98.1`) - `cargo run --locked -p xtask -- policy`: `191 files ok`. - `cargo clippy --locked -p victory-loader -p victory-pe --all-targets -- -D warnings` (default and `--all-features`): 0 warnings. - `cargo test --locked -p victory-loader -p victory-pe -- --test-threads=1` (both `dev` and `--release`): **84/84 passed** in each profile (`victory-loader`: 44 unit + 1 `crash_log` + 2 `load_probe` + 1 `title_dlls`; `victory-pe`: 36 unit). LGTM.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/1201/head:github-bot/gh-1201'
git switch 'github-bot/gh-1201'

Merge

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