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

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

@⁠divyeshganta on GitHub · 2026-11-24 15:48 UTC · original
divyeshganta:fix/unaligned-iat-binding → main · +219 −15 · 2 files · 1 commits · open

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).

**@⁠divyeshganta** on GitHub · 2026-11-24 15:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/1111) `divyeshganta:fix/unaligned-iat-binding` → `main` · +219 −15 · 2 files · 1 commits · open 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).
Use an unaligned store for parser-accepted IAT slots. Protect and restore each crossed memory region independently, including cleanup after partial setup failure. Add synthetic CPU-only Windows regressions. AI-assisted.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:20:21 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-11-24 16:31 UTC · original

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.

**@⁠Kal0s-design** on GitHub · 2026-11-24 16:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/1111#issuecomment-7137809083) 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.
Author
Owner

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

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).

**@⁠Zipettte** on GitHub · 2026-11-24 18:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/1111#issuecomment-7140053509) 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)._
Author
Owner

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

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

**@⁠Marvisak** on GitHub · 2026-11-25 01:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1111#issuecomment-7149543483) <!-- 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 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/1111/head:github-bot/gh-1111'
git switch 'github-bot/gh-1111'

Merge

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