Loader: fix 4 bounds and cleanup bugs in windows.rs with unit tests #1047

Open
opened 2026-11-24 15:16:17 +00:00 by geiyiliyabumo · 2 comments
geiyiliyabumo commented 2026-11-24 15:16:17 +00:00 (Migrated from github.com)
Contributor

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T15:16:17Z.


Summary

Fixes 4 real bounds-checking and cleanup bugs in crates/victory-loader/src/windows.rs (previously 0 unit tests) and adds 13 deterministic synthetic unit tests.

Because external PR creation is currently restricted (collaborators_only), the CI-green branch is ready on my fork for cherry-pick / review:

  • Branch: geiyiliyabumo/VIctory@test/loader-windows
  • Commit: geiyiliyabumo/VIctory@98b761b (based on main ffe310e)

Bugs Fixed (crates/victory-loader/src/windows.rs)

  1. Unchecked IAT writes: write_slots now validates rva + 8 <= size_of_image as a batch before any write.
  2. Module leak on TLS redirect failure: load now calls FreeLibrary when tls::redirect fails after LoadLibraryExW.
  3. Out-of-bounds TLS callbacks: callbacks with RVA 0 or >= size_of_image are now logged and skipped instead of executed.
  4. Out-of-image DllMain / entry point: guarded in both notify and spawn_main.

Verification (Windows 11 x64 MSVC)

  • rustfmt --edition 2024 --check crates/victory-loader/src/windows.rs: PASS
  • cargo xtask policy: PASS (154 files ok)
  • cargo clippy -p victory-loader --all-targets --locked -- -D warnings (default + --features diagnostics): PASS (0 warnings)
  • cargo test -p victory-loader --locked -- windows::: 13 passed, 0 failed

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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6585066259 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/1047). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T15:16:17Z. --- ### Summary Fixes 4 real bounds-checking and cleanup bugs in `crates/victory-loader/src/windows.rs` (previously 0 unit tests) and adds 13 deterministic synthetic unit tests. Because external PR creation is currently restricted (`collaborators_only`), the CI-green branch is ready on my fork for cherry-pick / review: - **Branch**: `geiyiliyabumo/VIctory@test/loader-windows` - **Commit**: `geiyiliyabumo/VIctory@98b761b` (based on `main` `ffe310e`) ### Bugs Fixed (`crates/victory-loader/src/windows.rs`) 1. **Unchecked IAT writes**: `write_slots` now validates `rva + 8 <= size_of_image` as a batch before any write. 2. **Module leak on TLS redirect failure**: `load` now calls `FreeLibrary` when `tls::redirect` fails after `LoadLibraryExW`. 3. **Out-of-bounds TLS callbacks**: callbacks with RVA `0` or `>= size_of_image` are now logged and skipped instead of executed. 4. **Out-of-image `DllMain` / entry point**: guarded in both `notify` and `spawn_main`. ### Verification (Windows 11 x64 MSVC) - `rustfmt --edition 2024 --check crates/victory-loader/src/windows.rs`: **PASS** - `cargo xtask policy`: **PASS** (`154 files ok`) - `cargo clippy -p victory-loader --all-targets --locked -- -D warnings` (default + `--features diagnostics`): **PASS** (0 warnings) - `cargo test -p victory-loader --locked -- windows::`: **13 passed, 0 failed** *AI-assisted; synthetic tests only, no game files.*
Marvisak commented 2026-11-25 07:03:25 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T06:52:13Z.


Automated triage for the maintainers.

Correction to the earlier note: the TLS-redirect part of this is open as PR #1201 ("free mapped module on TLS redirect error and add windows.rs unit tests"), which links this issue.

One of the four cases holds on current main (940d32f). In load (crates/victory-loader/src/windows.rs), an error from tls::redirect returns through ? after LoadLibraryExW mapped the image. LoadedImage has no Drop, and FreeLibrary is called only in unload, so the module stays mapped. The impact is small, because that error ends the run anyway. #1201 fixes it.

The other three cases (IAT slots in write_slots, out-of-image TLS callbacks, out-of-image entry points) still read as hardening. write_slots relies on the parser yielding only slots inside the image, and nothing so far shows the parser accepting an image that breaks this. The Windows 11 run above confirms that the branch builds and its tests pass. It does not show these cases can happen. A synthetic image (no game files) that the parser accepts and that reaches one of them would settle it.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7155244262 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/1047#issuecomment-7155244262). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T06:52:13Z. --- <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-25T09:07:24Z at=2026-11-25T09:50:38Z --> Automated triage for the maintainers. Correction to the earlier note: the TLS-redirect part of this is open as PR #1201 ("free mapped module on TLS redirect error and add windows.rs unit tests"), which links this issue. One of the four cases holds on current `main` (`940d32f`). In `load` (`crates/victory-loader/src/windows.rs`), an error from `tls::redirect` returns through `?` after `LoadLibraryExW` mapped the image. `LoadedImage` has no `Drop`, and `FreeLibrary` is called only in `unload`, so the module stays mapped. The impact is small, because that error ends the run anyway. #1201 fixes it. The other three cases (IAT slots in `write_slots`, out-of-image TLS callbacks, out-of-image entry points) still read as hardening. `write_slots` relies on the parser yielding only slots inside the image, and nothing so far shows the parser accepting an image that breaks this. The Windows 11 run above confirms that the branch builds and its tests pass. It does not show these cases can happen. A synthetic image (no game files) that the parser accepts and that reaches one of them would settle it. --- _Generated by [Claude Code](https://claude.ai/code)_
StExpeditus commented 2026-11-25 10:07:29 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Marvisak/VIctory. Originally posted by StExpeditus on 2026-11-25T09:07:24Z.


Validated the fork branch on Windows 11 Pro 26200 (Intel Core i5-14600K, Rust 1.98.1 MSVC), commit 5edb22b:

  • cargo test -p victory-loader --locked -> 43 passed, 0 failed (the 13 windows:: tests included).
  • cargo clippy -p victory-loader --all-targets --locked -- -D warnings -> clean.
  • cargo fmt -p victory-loader -- --check flags crash.rs:71, which the branch inherits from its base (ffe310e); the same drift exists on current main under this toolchain, and windows.rs itself is fmt-clean.
  • The fix applies cleanly on current main (940d32f6): the only victory-loader delta is the 413-line change to windows.rs itself, no unrelated churn.

No game files used; synthetic tests only. The fix looks ready to cherry-pick.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7157905062 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/issues/1047#issuecomment-7157905062). Originally posted by [StExpeditus](https://github.com/StExpeditus) on 2026-11-25T09:07:24Z. --- Validated the fork branch on Windows 11 Pro 26200 (Intel Core i5-14600K, Rust 1.98.1 MSVC), commit `5edb22b`: - `cargo test -p victory-loader --locked` -> **43 passed, 0 failed** (the 13 `windows::` tests included). - `cargo clippy -p victory-loader --all-targets --locked -- -D warnings` -> **clean**. - `cargo fmt -p victory-loader -- --check` flags `crash.rs:71`, which the branch inherits from its base (`ffe310e`); the same drift exists on current main under this toolchain, and `windows.rs` itself is fmt-clean. - The fix applies cleanly on current main (`940d32f6`): the only `victory-loader` delta is the 413-line change to `windows.rs` itself, no unrelated churn. No game files used; synthetic tests only. The fix looks ready to cherry-pick.
Sign in to join this conversation.
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
Marvisak/VIctory#1047
No description provided.