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

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3133 into main
fuzeofficlal commented 2026-11-26 09:33:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T09:33:55Z.


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)
<!-- greyfinch-sync:Marvisak/VIctory:issue:5507495868 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3133). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T09:33:55Z. --- ### 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`)
cloph-dsp commented 2026-11-26 11:04:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T10:23:09Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7189294955 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3133#issuecomment-7189294955). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T10:23:09Z. --- 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).
Dukestar69 commented 2026-11-26 13:30:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T13:30:21Z.

Review: APPROVED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6074978371 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3133#pullrequestreview-6074978371). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T13:30:21Z. **Review:** APPROVED --- 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).
futurepro62 commented 2026-11-27 11:53:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-27T11:53:10Z.


NVIDIA full-suite run, to go with the reports above by Dukestar69, cloph-dsp, since none of them names a GPU run. Tested exact head c283299 (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 870 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: retry_coalesces_guards_separated_by_a_free_gap, retry_coalesces_adjacent_guards_and_restores_on_refusal, retry_coalesces_guards_straddling_two_mib_alignment, retry_does_not_coalesce_guards_separated_by_an_allocation.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7217587647 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3133#issuecomment-7217587647). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-27T11:53:10Z. --- NVIDIA full-suite run, to go with the reports above by Dukestar69, cloph-dsp, since none of them names a GPU run. Tested exact head `c283299` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **870 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `retry_coalesces_guards_separated_by_a_free_gap`, `retry_coalesces_adjacent_guards_and_restores_on_refusal`, `retry_coalesces_guards_straddling_two_mib_alignment`, `retry_does_not_coalesce_guards_separated_by_an_allocation`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
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/3133:upstream-pr/3133'
git switch 'upstream-pr/3133'

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