Loader: fix thunk block overflow, capacity enforcement, flush errors, and leaks #1306

Open
geiyiliyabumo wants to merge 1 commit from upstream-pr/1306 into main
geiyiliyabumo commented 2026-11-24 17:17:49 +00:00 (Migrated from github.com)

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


Summary

Audits \crates/victory-loader/src/thunks.rs\ and fixes 4 bugs:

  • Prevents size multiplication overflow and enforces the exact requested slot count (including zero).
  • Propagates instruction-cache flush failures from \seal()\ instead of silently ignoring them.
  • Releases virtual memory reservations via \Drop\ for abandoned, empty, or failed thunk blocks while retaining process lifetime for successfully sealed stubs.
  • Adds 4 deterministic synthetic unit tests covering capacity, overflow rejection, x86_64 stub encoding/execution round-trip, and reservation release on drop.

Verification

  • \cargo test -p victory-loader --lib thunks\ ? 6 passed, 0 failed
  • \cargo clippy -p victory-loader --all-targets -- -D warnings\ ? clean
  • \cargo xtask policy\ ? 154 files ok

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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6587422177 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1306). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T17:17:49Z. --- ## Summary Audits \crates/victory-loader/src/thunks.rs\ and fixes 4 bugs: - Prevents size multiplication overflow and enforces the exact requested slot count (including zero). - Propagates instruction-cache flush failures from \seal()\ instead of silently ignoring them. - Releases virtual memory reservations via \Drop\ for abandoned, empty, or failed thunk blocks while retaining process lifetime for successfully sealed stubs. - Adds 4 deterministic synthetic unit tests covering capacity, overflow rejection, x86_64 stub encoding/execution round-trip, and reservation release on drop. ## Verification - \cargo test -p victory-loader --lib thunks\ ? 6 passed, 0 failed - \cargo clippy -p victory-loader --all-targets -- -D warnings\ ? clean - \cargo xtask policy\ ? 154 files ok *AI-assisted; synthetic tests only, no game files.*
DenRakEiw commented 2026-11-24 18:04:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T17:26:40Z.

Review: APPROVED


Tested head 29d78ba (fast-forward on main ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC):

  • cargo test -p victory-loader: 26 unit + 2 load_probe + 1 title_dlls passed, 0 failed (including capacity_matches_the_requested_slot_count, overflowing_slot_counts_fail_before_allocation, and unpublished_blocks_release_the_reservation)
  • cargo clippy -p victory-loader --all-targets: 0 warnings
  • cargo xtask policy: ok

Solid thunk-block overflow, capacity, flush, and cleanup fixes. Approving.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6044158674 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1306#pullrequestreview-6044158674). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T17:26:40Z. **Review:** APPROVED --- Tested head `29d78ba` (fast-forward on `main` `ffe310e`) on Windows 11 x64 (Rust 1.98.1 MSVC): - `cargo test -p victory-loader`: **26 unit + 2 `load_probe` + 1 `title_dlls` passed, 0 failed** (including `capacity_matches_the_requested_slot_count`, `overflowing_slot_counts_fail_before_allocation`, and `unpublished_blocks_release_the_reservation`) - `cargo clippy -p victory-loader --all-targets`: **0 warnings** - `cargo xtask policy`: ok Solid thunk-block overflow, capacity, flush, and cleanup fixes. Approving. AI-assisted (Gemini 4 Neon).
Marvisak commented 2026-11-25 03:03:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T02:34:55Z.


Automated triage for the maintainers.

The four fixes in crates/victory-loader/src/thunks.rs do not address failures that can happen on main. ThunkBlock::new is called only from LoadedImage::bind_imports in windows.rs, behind if !unresolved.is_empty(). So the count is never zero, and count * 32 cannot overflow for an import list. On main, the page-rounded capacity only allows more slots than requested, never fewer, so emit cannot overrun. The new Drop frees memory only on error paths where bind_imports returns Err and the image is never run. On x64, FlushInstructionCache failing is not a realistic case. The stub-encoding test is reasonable on its own, but it checks the bytes the code already writes.

If you have a case where one of these paths is reached, please describe it and push a change; a small PR adding only the encoding/round-trip test would also be welcome. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the triage: proposed close label. No CI ran on this head.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150908355 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1306#issuecomment-7150908355). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T02:34:55Z. --- <!-- victory-triage v1 head=29d78bae7044b6a395d0201575cad0bbafa260cf verdict=proposed-close at=2026-11-25T02:20:29Z --> Automated triage for the maintainers. The four fixes in `crates/victory-loader/src/thunks.rs` do not address failures that can happen on main. `ThunkBlock::new` is called only from `LoadedImage::bind_imports` in `windows.rs`, behind `if !unresolved.is_empty()`. So the count is never zero, and `count * 32` cannot overflow for an import list. On main, the page-rounded capacity only allows more slots than requested, never fewer, so `emit` cannot overrun. The new `Drop` frees memory only on error paths where `bind_imports` returns `Err` and the image is never run. On x64, `FlushInstructionCache` failing is not a realistic case. The stub-encoding test is reasonable on its own, but it checks the bytes the code already writes. If you have a case where one of these paths is reached, please describe it and push a change; a small PR adding only the encoding/round-trip test would also be welcome. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the `triage: proposed close` label. No CI ran on this head. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1306:upstream-pr/1306'
git switch 'upstream-pr/1306'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1306'
git switch 'upstream-pr/1306'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1306'
git switch 'upstream-pr/1306'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1306'
git switch 'main'
git merge --squash 'upstream-pr/1306'
git switch 'main'
git merge --ff-only 'upstream-pr/1306'
git switch 'main'
git merge 'upstream-pr/1306'
git push origin 'main'
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
Marvisak/VIctory!1306
No description provided.