xmem: coalesce low-4 GiB guards on retry when no single guard fits #3133

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

@⁠fuzeofficlal on GitHub · 2026-11-26 09:33 UTC · original
fuzeofficlal:fix/xmem-low-guards-coalesce → main · +429 −5 · 1 files · 1 commits · open

Problem

low_guards::retry in crates/victory-hle/src/modules/xmem.rs releases one guard at a time, invokes allocate(), and immediately re-guards that range on failure before trying the next guard.

When a 32-bit XMemVirtualAlloc request needs a contiguous span that crosses two or more guards, every single-guard attempt fails and returns ERROR_NOT_ENOUGH_MEMORY even though the combined address range is unused:

  1. Guards split by a freed allocation: an earlier 32-bit allocation carves the middle out of a guard, producing two guards separated by the allocation; once the title frees that allocation, the gap between the two guards is MEM_FREE, yet neither guard alone exposes the combined contiguous span.
  2. Adjacent guards: a request larger than either individual guard cannot span their shared boundary.
  3. 2 MiB alignment (PAGES_2M) straddling a guard boundary: a 2 MiB-aligned VirtualAlloc2 window whose start falls in one guard and whose end falls in the next cannot succeed while either guard stays reserved.

Fix

  • Keep the existing single-guard fast path unchanged first.
  • If no single guard satisfies the request, sort live guards by base address and partition them into clusters whose consecutive guards are either adjacent or separated solely by MEM_FREE space (VirtualQuery across each gap). Single-guard clusters (and guards separated by an active host/title allocation) are skipped.
  • Try multi-guard clusters ordered by total span (smallest fitting span first, then smaller spans descending), releasing the whole cluster before calling allocate().
  • Re-guard only the remaining MEM_FREE portions inside our released guard intervals, merging strictly adjacent released intervals without claiming unguarded free gaps between non-adjacent guards, and preserve GetLastError across queries and re-guarding.

Verification

Added four targeted regression tests in modules::xmem::low_guards::tests (confirmed failing on main 63bcca43 before the fix and passing after):

  • retry_coalesces_guards_separated_by_a_free_gap (also verifies a refused retry leaves the unguarded MEM_FREE gap free and both guards restored)
  • retry_coalesces_adjacent_guards_and_restores_on_refusal (also verifies GetLastError preservation)
  • retry_coalesces_guards_straddling_two_mib_alignment (using VirtualAlloc2 with Alignment = 2 * MIB)
  • retry_does_not_coalesce_guards_separated_by_an_allocation

Full validation passed:

  • cargo test -p victory-hle --locked and VICTORY_D3D12_WARP=1 cargo test -p victory-hle --locked --features diagnostics (156 unit + 2 integration tests passed)
  • cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features (0 warnings)
  • rustfmt --edition 2024 --check (0 diffs on all modified/added lines)
  • cargo test -p xtask --locked policy::tests::repository_passes_policy (asset policy: 191 files ok)
**@⁠fuzeofficlal** on GitHub · 2026-11-26 09:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/3133) `fuzeofficlal:fix/xmem-low-guards-coalesce` → `main` · +429 −5 · 1 files · 1 commits · open ### Problem `low_guards::retry` in `crates/victory-hle/src/modules/xmem.rs` releases one guard at a time, invokes `allocate()`, and immediately re-guards that range on failure before trying the next guard. When a 32-bit `XMemVirtualAlloc` request needs a contiguous span that crosses two or more guards, every single-guard attempt fails and returns `ERROR_NOT_ENOUGH_MEMORY` even though the combined address range is unused: 1. **Guards split by a freed allocation**: an earlier 32-bit allocation carves the middle out of a guard, producing two guards separated by the allocation; once the title frees that allocation, the gap between the two guards is `MEM_FREE`, yet neither guard alone exposes the combined contiguous span. 2. **Adjacent guards**: a request larger than either individual guard cannot span their shared boundary. 3. **2 MiB alignment (`PAGES_2M`) straddling a guard boundary**: a 2 MiB-aligned `VirtualAlloc2` window whose start falls in one guard and whose end falls in the next cannot succeed while either guard stays reserved. ### Fix - Keep the existing single-guard fast path unchanged first. - If no single guard satisfies the request, sort live guards by base address and partition them into clusters whose consecutive guards are either adjacent or separated solely by `MEM_FREE` space (`VirtualQuery` across each gap). Single-guard clusters (and guards separated by an active host/title allocation) are skipped. - Try multi-guard clusters ordered by total span (smallest fitting span first, then smaller spans descending), releasing the whole cluster before calling `allocate()`. - Re-guard only the remaining `MEM_FREE` portions inside our released guard intervals, merging strictly adjacent released intervals without claiming unguarded free gaps between non-adjacent guards, and preserve `GetLastError` across queries and re-guarding. ### Verification Added four targeted regression tests in `modules::xmem::low_guards::tests` (confirmed failing on `main` `63bcca43` before the fix and passing after): - `retry_coalesces_guards_separated_by_a_free_gap` (also verifies a refused retry leaves the unguarded `MEM_FREE` gap free and both guards restored) - `retry_coalesces_adjacent_guards_and_restores_on_refusal` (also verifies `GetLastError` preservation) - `retry_coalesces_guards_straddling_two_mib_alignment` (using `VirtualAlloc2` with `Alignment = 2 * MIB`) - `retry_does_not_coalesce_guards_separated_by_an_allocation` Full validation passed: - `cargo test -p victory-hle --locked` and `VICTORY_D3D12_WARP=1 cargo test -p victory-hle --locked --features diagnostics` (156 unit + 2 integration tests passed) - `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features` (0 warnings) - `rustfmt --edition 2024 --check` (0 diffs on all modified/added lines) - `cargo test -p xtask --locked policy::tests::repository_passes_policy` (`asset policy: 191 files ok`)
low_guards::retry previously released only one guard at a time and re-guarded it immediately on failure before trying the next guard. When a 32-bit XMemVirtualAlloc request needs a contiguous span crossing two or more guards — such as guards split by an earlier allocation that was later freed, strictly adjacent guards, or a 2 MiB-aligned (PAGES_2M) window straddling a guard boundary — every single-guard attempt fails and returns ERROR_NOT_ENOUGH_MEMORY even though the combined span is free.

Preserve the single-guard fast path first, then fall back to releasing each cluster of base-sorted guards bridged exclusively by MEM_FREE space simultaneously. After the allocation attempt, re-guard only the remaining free portions of the released guard intervals (coalescing strictly adjacent guard intervals while never claiming unguarded free gaps between non-adjacent guards), and preserve the allocation error across VirtualQuery and re-guard calls.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:08:14 +00:00
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 10:23 UTC · original

Independent local verification of head c283299 on Windows 11, Rust 1.100.0/MSVC:

  • cargo test --locked -p victory-hle --lib modules::xmem::low_guards::tests — 6 passed, 0 failed (including all four new retry_coalesces_* tests).
  • git diff --check origin/main HEAD — clean.

One deprecation warning appears during compilation, but it is pre-existing in victory-gpu/src/object.rs (Atomic::fetch_update), unrelated to this diff. Synthetic VirtualAlloc2/VirtualQuery fixtures only; no game assets or DRM material used. AI-assisted (Hy4 flash).

**@⁠cloph-dsp** on GitHub · 2026-11-26 10:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/3133#issuecomment-7189294955) Independent local verification of head `c283299` on Windows 11, Rust 1.100.0/MSVC: - `cargo test --locked -p victory-hle --lib modules::xmem::low_guards::tests` — 6 passed, 0 failed (including all four new `retry_coalesces_*` tests). - `git diff --check origin/main HEAD` — clean. One deprecation warning appears during compilation, but it is pre-existing in `victory-gpu/src/object.rs` (`Atomic::fetch_update`), unrelated to this diff. Synthetic `VirtualAlloc2`/`VirtualQuery` fixtures only; no game assets or DRM material used. AI-assisted (Hy4 flash).
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 13:30 UTC · original · approved these changes

Verified against origin/main (63bcca4):

  • cargo test -p victory-hle --lib modules::xmem:: -- --test-threads=1: 18/18 passed (including retry_coalesces_adjacent_guards_and_restores_on_refusal, retry_coalesces_guards_separated_by_a_free_gap, retry_coalesces_guards_straddling_two_mib_alignment, and retry_does_not_coalesce_guards_separated_by_an_allocation).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly coalesces low-4 GiB guards on retry when no single guard fits while preserving intervening allocations.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 13:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/3133#pullrequestreview-6074978371) · **approved these changes** Verified against `origin/main` (`63bcca4`): - `cargo test -p victory-hle --lib modules::xmem:: -- --test-threads=1`: 18/18 passed (including `retry_coalesces_adjacent_guards_and_restores_on_refusal`, `retry_coalesces_guards_separated_by_a_free_gap`, `retry_coalesces_guards_straddling_two_mib_alignment`, and `retry_does_not_coalesce_guards_separated_by_an_allocation`). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly coalesces low-4 GiB guards on retry when no single guard fits while preserving intervening allocations. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request can be merged automatically.
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/3133/head:github-bot/gh-3133'
git switch 'github-bot/gh-3133'

Merge

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