fix(loader): preserve protection regions when binding unaligned imports #1111

Open
divyeshganta wants to merge 1 commit from upstream-pr/1111 into main
divyeshganta commented 2026-11-24 15:48:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by divyeshganta on 2026-11-24T15:48:38Z.


Related to #923. Import binding now supports unaligned IAT slots and restores each crossed memory region’s protection, including cleanup after partial setup failure. Adds synthetic Windows regressions. Patch by DocBlack89 (5f8800dd93).

<!-- greyfinch-sync:Marvisak/VIctory:issue:6585738908 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1111). Originally posted by [divyeshganta](https://github.com/divyeshganta) on 2026-11-24T15:48:38Z. --- Related to #923. Import binding now supports unaligned IAT slots and restores each crossed memory region’s protection, including cleanup after partial setup failure. Adds synthetic Windows regressions. Patch by DocBlack89 (5f8800dd93d4464aa627e679cd5ae0e91352eba4).
DenRakEiw commented 2026-11-24 16:04:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T16:01:20Z.

Review: APPROVED


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

  • cargo test -p victory-loader: 25 unit + 2 load_probe + 1 title_dlls passed, 0 failed (including iat_binding_preserves_each_crossed_page_protection, iat_slot_writes_accept_every_byte_alignment, and iat_binding_failure_restores_earlier_pages_without_writing)
  • cargo clippy -p victory-loader --all-targets: 0 warnings
  • cargo xtask policy: ok

Solid loader safety fix preserving page protections across unaligned/cross-page IAT slot writes. Approving.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6042811263 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1111#pullrequestreview-6042811263). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T16:01:20Z. **Review:** APPROVED --- Tested head `5f8800d` (fast-forward on `main` `ffe310e`) on Windows 11 x64 (Rust 1.98.1 MSVC): - `cargo test -p victory-loader`: **25 unit + 2 `load_probe` + 1 `title_dlls` passed, 0 failed** (including `iat_binding_preserves_each_crossed_page_protection`, `iat_slot_writes_accept_every_byte_alignment`, and `iat_binding_failure_restores_earlier_pages_without_writing`) - `cargo clippy -p victory-loader --all-targets`: **0 warnings** - `cargo xtask policy`: ok Solid loader safety fix preserving page protections across unaligned/cross-page IAT slot writes. Approving. AI-assisted (Gemini 4 Neon).
Kal0s-design commented 2026-11-24 17:04:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Kal0s-design on 2026-11-24T16:31:50Z.


Independent check of 5f8800d, merged locally onto main ffe310e (fast-forward). Windows 11, MSVC, Rust 1.98.1 as pinned. It adds to DenRakEiw's run on the same head with a repeat count and a check of the old behaviour.

  • cargo test -p victory-loader --locked: 25 unit + 2 load_probe + 1 title_dlls passed, 0 failed. The lib suite was run 10 more times: 10/10 green.
  • cargo clippy -p victory-loader --all-targets --locked -- -D warnings, with and without --all-features: clean. --locked holds with the new Win32_System_SystemInformation feature.
  • Old behaviour: the new tests call the new helpers, so they cannot be compiled against main. Instead I replayed main's write_slots sequence in a scratch test that is not part of the PR: VirtualProtect(slot, 8, PAGE_READWRITE), a write, then VirtualProtect(slot, 8, old), with the slot straddling a PAGE_READONLY page and a PAGE_EXECUTE_READ page. After the sequence the second page reads back as PAGE_READONLY, not PAGE_EXECUTE_READ, 3 runs out of 3: the single old value restores only the first page's protection. This is what the per-region restore in this PR fixes.

Not covered: that case needs a slot that is not 8-aligned, which a normal linker output does not produce, so this is hardening against malformed images; nothing was run against a real title.

AI-assisted: yes. Every number above is from my own runs.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7137809083 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1111#issuecomment-7137809083). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-11-24T16:31:50Z. --- Independent check of `5f8800d`, merged locally onto `main` `ffe310e` (fast-forward). Windows 11, MSVC, Rust 1.98.1 as pinned. It adds to DenRakEiw's run on the same head with a repeat count and a check of the old behaviour. - `cargo test -p victory-loader --locked`: 25 unit + 2 `load_probe` + 1 `title_dlls` passed, 0 failed. The lib suite was run 10 more times: 10/10 green. - `cargo clippy -p victory-loader --all-targets --locked -- -D warnings`, with and without `--all-features`: clean. `--locked` holds with the new `Win32_System_SystemInformation` feature. - Old behaviour: the new tests call the new helpers, so they cannot be compiled against `main`. Instead I replayed `main`'s `write_slots` sequence in a scratch test that is not part of the PR: `VirtualProtect(slot, 8, PAGE_READWRITE)`, a write, then `VirtualProtect(slot, 8, old)`, with the slot straddling a PAGE_READONLY page and a PAGE_EXECUTE_READ page. After the sequence the second page reads back as PAGE_READONLY, not PAGE_EXECUTE_READ, 3 runs out of 3: the single `old` value restores only the first page's protection. This is what the per-region restore in this PR fixes. Not covered: that case needs a slot that is not 8-aligned, which a normal linker output does not produce, so this is hardening against malformed images; nothing was run against a real title. AI-assisted: yes. Every number above is from my own runs.
Zipettte commented 2026-11-24 18:06:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T18:01:46Z.


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

Tested with AI assistance (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140053509 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1111#issuecomment-7140053509). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T18:01:46Z. --- Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 5f8800d: `cargo test -p victory-loader` passes, 28 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Marvisak commented 2026-11-25 02:06:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:26:10Z.


Automated triage for the maintainers.

Needs work. LoadedImage::write_slots (crates/victory-loader/src/windows.rs) now writes IAT slots with write_unaligned. The new write_protected_iat_slot walks each memory region the slot crosses with VirtualQuery, makes each one writable, and restores all of them, including after a partial failure. The code reads correctly and the tests check it, but it handles a case real images don't produce. An 8-byte-aligned slot can't cross a page boundary, so only an unaligned slot can span two regions, and linker-built PE32+ images align their import tables. The bug shown in the comments needs a straddling slot, which the tests build by hand.

What would make this mergeable: a short change that checks the one real assumption. The loader or victory-pe parser could reject (or report) an IAT slot that isn't 8-byte aligned, and the comment "8-byte aligned by the PE format" would then be enforced instead of assumed. If you know of a real image with unaligned IAT slots, please name it. That would justify the multi-region handling as it is.

The Win32_System_SystemInformation feature is only used by the tests.

CI: not run.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149543483 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1111#issuecomment-7149543483). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:26:10Z. --- <!-- victory-triage v1 head=5f8800dd93d4464aa627e679cd5ae0e91352eba4 verdict=needs-work at=2026-11-25T01:26:08Z --> Automated triage for the maintainers. **Needs work.** `LoadedImage::write_slots` (`crates/victory-loader/src/windows.rs`) now writes IAT slots with `write_unaligned`. The new `write_protected_iat_slot` walks each memory region the slot crosses with `VirtualQuery`, makes each one writable, and restores all of them, including after a partial failure. The code reads correctly and the tests check it, but it handles a case real images don't produce. An 8-byte-aligned slot can't cross a page boundary, so only an unaligned slot can span two regions, and linker-built PE32+ images align their import tables. The bug shown in the comments needs a straddling slot, which the tests build by hand. What would make this mergeable: a short change that checks the one real assumption. The loader or `victory-pe` parser could reject (or report) an IAT slot that isn't 8-byte aligned, and the comment "8-byte aligned by the PE format" would then be enforced instead of assumed. If you know of a real image with unaligned IAT slots, please name it. That would justify the multi-region handling as it is. The `Win32_System_SystemInformation` feature is only used by the tests. CI: not run. --- _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/1111:upstream-pr/1111'
git switch 'upstream-pr/1111'

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