xmem: 32-bit XMemAllocDefault blocks get the alignment they ask for #2182

Open
BastienRodz wants to merge 1 commit from upstream-pr/2184 into main
BastienRodz commented 2026-11-25 07:08:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-25T07:08:54Z.


What's wrong

XMemAllocDefault honours the alignment field of attributes (log2, 5 bits at 24) for host-heap blocks, but its ALLOC_32BIT branch calls XMemVirtualAlloc with no alignment. That path sets MEM_ADDRESS_REQUIREMENTS::Alignment only for PAGES_2M, so a 32-bit block asked for at 128 KiB, 256 KiB, 1 MiB or 2 MiB gets the 64 KiB allocation granularity instead. The module header says alignment is one of the address constraints it honours.

What the fix does

  • Moves the body of XMemVirtualAlloc into a private virtual_alloc that takes the Alignment to request. XMemVirtualAlloc keeps its behaviour (2 MiB for PAGES_2M, else 0).
  • The 32-bit branch of XMemAllocDefault passes the asked alignment when it is above 64 KiB, and 0 otherwise: MEM_ADDRESS_REQUIREMENTS requires a nonzero alignment to be at least the allocation granularity, and 0 already gives 64 KiB.

Tests

New modules::xmem::tests::alloc_32bit_honours_alignments_above_64k: four live 32-bit blocks per alignment (16 B, 64 KiB, 128 KiB, 256 KiB, 2 MiB), each checked below 4 GiB, aligned, and writable.

Not tested

No title run. The title imports xmem.dll!XMemAlloc, but whether it asks for 32-bit blocks aligned above 64 KiB is not measured. #2022 changes the host-heap branch of the same function; the two don't overlap.

Fixes #2183

AI-assisted (Gemini 4 Neon found and wrote it, Claude Code reviewed and verified it). No game data used.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:5486845833 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2184). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-25T07:08:54Z. --- ### What's wrong `XMemAllocDefault` honours the alignment field of `attributes` (log2, 5 bits at 24) for host-heap blocks, but its `ALLOC_32BIT` branch calls `XMemVirtualAlloc` with no alignment. That path sets `MEM_ADDRESS_REQUIREMENTS::Alignment` only for `PAGES_2M`, so a 32-bit block asked for at 128 KiB, 256 KiB, 1 MiB or 2 MiB gets the 64 KiB allocation granularity instead. The module header says alignment is one of the address constraints it honours. ### What the fix does - Moves the body of `XMemVirtualAlloc` into a private `virtual_alloc` that takes the `Alignment` to request. `XMemVirtualAlloc` keeps its behaviour (2 MiB for `PAGES_2M`, else 0). - The 32-bit branch of `XMemAllocDefault` passes the asked alignment when it is above 64 KiB, and 0 otherwise: `MEM_ADDRESS_REQUIREMENTS` requires a nonzero alignment to be at least the allocation granularity, and 0 already gives 64 KiB. ### Tests New `modules::xmem::tests::alloc_32bit_honours_alignments_above_64k`: four live 32-bit blocks per alignment (16 B, 64 KiB, 128 KiB, 256 KiB, 2 MiB), each checked below 4 GiB, aligned, and writable. - On `main` (940d32f) it fails: `field 0x11: 0x10000 is not 131072-byte aligned`. - With the fix it and the other `modules::xmem::` tests pass (11 of 11, Wine 10, x86_64-pc-windows-msvc). - windows-latest run on the fork: https://github.com/BastienRodz/VIctory/actions/runs/45565810430 ### Not tested No title run. The title imports `xmem.dll!XMemAlloc`, but whether it asks for 32-bit blocks aligned above 64 KiB is not measured. #2022 changes the host-heap branch of the same function; the two don't overlap. Fixes #2183 AI-assisted (Gemini 4 Neon found and wrote it, Claude Code reviewed and verified it). No game data used. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
catminou4 commented 2026-11-25 07:17:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T07:17:39Z.

Review: COMMENTED


Static review only: victory-hle is Windows-only — nothing compiles or runs on macOS; source review of the diff.

xmem: 32-bit XMemAllocDefault blocks get their alignment — allocator alignment fix for 32-bit title heaps.

<!-- greyfinch-sync:Marvisak/VIctory:review:6053351499 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2184#pullrequestreview-6053351499). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T07:17:39Z. **Review:** COMMENTED --- Static review only: victory-hle is Windows-only — nothing compiles or runs on macOS; source review of the diff. xmem: 32-bit XMemAllocDefault blocks get their alignment — allocator alignment fix for 32-bit title heaps.
kotekompotek commented 2026-11-25 10:07:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T09:18:33Z.


Verified on Windows 10 / GTX 1050, cargo test -p victory-hle --locked xmem: 11/11 pass, including the new alloc_32bit_honours_alignments_above_64k.

Mechanics check out: PAGE is 64 KiB, so align > PAGE correctly maps "at or below the allocation granularity" to Alignment = 0, which MEM_ADDRESS_REQUIREMENTS requires (nonzero must be ≥64 KiB). XMemVirtualAlloc keeps its old contract (2 MiB only for PAGES_2M), and the four-live-blocks-per-alignment test design guards against 64 KiB slots landing on coarse boundaries by chance — it does fail on 940d32f as reported.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158156029 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2184#issuecomment-7158156029). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T09:18:33Z. --- Verified on Windows 10 / GTX 1050, `cargo test -p victory-hle --locked xmem`: 11/11 pass, including the new `alloc_32bit_honours_alignments_above_64k`. Mechanics check out: `PAGE` is 64 KiB, so `align > PAGE` correctly maps "at or below the allocation granularity" to `Alignment = 0`, which `MEM_ADDRESS_REQUIREMENTS` requires (nonzero must be ≥64 KiB). `XMemVirtualAlloc` keeps its old contract (2 MiB only for `PAGES_2M`), and the four-live-blocks-per-alignment test design guards against 64 KiB slots landing on coarse boundaries by chance — it does fail on `940d32f` as reported.
Marvisak commented 2026-11-25 13:02:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T12:38:20Z.


Automated triage for the maintainers.

Merge candidate. XMemAllocDefault with ALLOC_32BIT (crates/victory-hle/src/modules/xmem.rs) now honours the attributes' alignment field. It reads the same field as layout (attributes >> ALIGNMENT_SHIFT & 0x1f). Alignments above the 64 KiB granularity are passed as MEM_ADDRESS_REQUIREMENTS::Alignment, and anything at or below it uses 0. That is right, because VirtualAlloc2 only accepts 0 or at least the allocation granularity, and 0 already gives 64 KiB. XMemVirtualAlloc keeps its old behaviour (2 MiB only for PAGES_2M) through the new virtual_alloc helper.

alloc_32bit_honours_alignments_above_64k holds four live blocks per alignment, so a block can't pass by landing on a coarse boundary by chance. A reviewer in the thread reports that it fails on main. CI check passes, and the branch merges cleanly.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7162649504 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2184#issuecomment-7162649504). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T12:38:20Z. --- <!-- victory-triage v1 head=8954790b4fef837ed82ef7d3ddec28699f0a7eb4 verdict=merge-candidate at=2026-11-25T13:49:00Z --> Automated triage for the maintainers. **Merge candidate.** `XMemAllocDefault` with `ALLOC_32BIT` (`crates/victory-hle/src/modules/xmem.rs`) now honours the attributes' alignment field. It reads the same field as `layout` (`attributes >> ALIGNMENT_SHIFT & 0x1f`). Alignments above the 64 KiB granularity are passed as `MEM_ADDRESS_REQUIREMENTS::Alignment`, and anything at or below it uses 0. That is right, because `VirtualAlloc2` only accepts 0 or at least the allocation granularity, and 0 already gives 64 KiB. `XMemVirtualAlloc` keeps its old behaviour (2 MiB only for `PAGES_2M`) through the new `virtual_alloc` helper. `alloc_32bit_honours_alignments_above_64k` holds four live blocks per alignment, so a block can't pass by landing on a coarse boundary by chance. A reviewer in the thread reports that it fails on `main`. CI `check` passes, and the branch merges cleanly. --- _Generated by [Claude Code](https://claude.ai/code)_
jaschinski97-ops commented 2026-11-25 16:18:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jaschinski97-ops on 2026-11-25T16:18:22Z.

Review: COMMENTED


Native Windows 11 verification of unchanged head 8954790, including integration with #2022 and current main 67e8ae1.

  • All 11 XMem tests pass on the unchanged PR head, including the six requested alignment fields and four simultaneous live allocations per field.
  • Copying only the alignment regression onto both main 940d32f and current main 67e8ae1 reproduces the failure: field 0x11 can return an address with remainder 65,536 modulo 131,072. This is a real before/after check on native Windows.
  • Both original PR heads merge together and onto current main without conflict. On the combined branch, the complete HLE library passes 149/149, including an independent host-heap cost probe for #2022; all-target/all-feature Clippy with warnings denied also passes.

Reproducible evidence branch, exact heads, commands and results. Original commits and authors are preserved; no implementation edits or competing PR.

Windows 11 Pro build 26300, Rust 1.98.1 x86_64-pc-windows-gnu, LLVM-MinGW. This native GNU run complements the existing MSVC CI. Synthetic fixtures only, no title data or gameplay claim. AI-assisted verification with Codex. Claim: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7167502972.

<!-- greyfinch-sync:Marvisak/VIctory:review:6061023581 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2184#pullrequestreview-6061023581). Originally posted by [jaschinski97-ops](https://github.com/jaschinski97-ops) on 2026-11-25T16:18:22Z. **Review:** COMMENTED --- Native Windows 11 verification of unchanged head `8954790`, including integration with #2022 and current main `67e8ae1`. - All **11 XMem tests pass** on the unchanged PR head, including the six requested alignment fields and four simultaneous live allocations per field. - Copying only the alignment regression onto both main `940d32f` and current main `67e8ae1` reproduces the failure: field `0x11` can return an address with remainder 65,536 modulo 131,072. This is a real before/after check on native Windows. - Both original PR heads merge together and onto current main without conflict. On the combined branch, the complete HLE library passes **149/149**, including an independent host-heap cost probe for #2022; all-target/all-feature Clippy with warnings denied also passes. [Reproducible evidence branch, exact heads, commands and results](https://github.com/jaschinski97-ops/VIctory/blob/4360830/verification/xmem-windows.md). Original commits and authors are preserved; no implementation edits or competing PR. Windows 11 Pro build 26300, Rust 1.98.1 `x86_64-pc-windows-gnu`, LLVM-MinGW. This native GNU run complements the existing MSVC CI. Synthetic fixtures only, no title data or gameplay claim. AI-assisted verification with Codex. Claim: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7167502972.
fuzeofficlal commented 2026-11-26 19:00:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T19:00:29Z.

Review: APPROVED


Reviewed 8954790b4fef merged cleanly onto 63bcca43.

The change in crates/victory-hle/src/modules/xmem.rs:87-115 and 305-319 is minimal and exact:

  • Factoring virtual_alloc(..., alignment) preserves XMemVirtualAlloc's existing PAGES_2M (2 * MIB) / 0 behavior verbatim.
  • In XMemAllocDefault's ALLOC_32BIT path (xmem.rs:306-318), extracting 1usize << (attributes >> ALIGNMENT_SHIFT & 0x1f) and passing if align > PAGE { align } else { 0 } matches MEM_ADDRESS_REQUIREMENTS::Alignment (0 for 64 KiB granularity and powers of two above 64 KiB) while keeping XMemFreeDefault's VirtualFree(..., MEM_RELEASE) symmetry intact.

Verification (native Windows 11):

  • cargo xtask policy and cargo clippy --locked -p victory-hle --all-targets -- -D warnings (default and --all-features) pass cleanly.
  • cargo test --locked -p victory-hle xmem:: passes 15/15 (including alloc_32bit_honours_alignments_above_64k).
  • Verified an additional alignment probe covering fields 0, 1, 12, 16..=26 (64 KiB through 64 MiB) with 4 simultaneous live allocations across sizes 1 B to 256 KiB (all < 4 GiB, properly aligned, writable, freed via XMemFreeDefault/XMemFree, and re-allocated), extreme fields 30/31 (1 GiB/2 GiB with no panic or UB), and XMemVirtualAlloc with PAGES_2M (2 MiB aligned).

LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6079267989 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2184#pullrequestreview-6079267989). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T19:00:29Z. **Review:** APPROVED --- Reviewed `8954790b4fef` merged cleanly onto `63bcca43`. The change in `crates/victory-hle/src/modules/xmem.rs:87-115` and `305-319` is minimal and exact: - Factoring `virtual_alloc(..., alignment)` preserves `XMemVirtualAlloc`'s existing `PAGES_2M` (`2 * MIB`) / `0` behavior verbatim. - In `XMemAllocDefault`'s `ALLOC_32BIT` path (`xmem.rs:306-318`), extracting `1usize << (attributes >> ALIGNMENT_SHIFT & 0x1f)` and passing `if align > PAGE { align } else { 0 }` matches `MEM_ADDRESS_REQUIREMENTS::Alignment` (0 for 64 KiB granularity and powers of two above 64 KiB) while keeping `XMemFreeDefault`'s `VirtualFree(..., MEM_RELEASE)` symmetry intact. **Verification** (native Windows 11): - `cargo xtask policy` and `cargo clippy --locked -p victory-hle --all-targets -- -D warnings` (default and `--all-features`) pass cleanly. - `cargo test --locked -p victory-hle xmem::` passes 15/15 (including `alloc_32bit_honours_alignments_above_64k`). - Verified an additional alignment probe covering fields `0, 1, 12, 16..=26` (`64 KiB` through `64 MiB`) with 4 simultaneous live allocations across sizes `1 B` to `256 KiB` (all `< 4 GiB`, properly aligned, writable, freed via `XMemFreeDefault`/`XMemFree`, and re-allocated), extreme fields `30`/`31` (`1 GiB`/`2 GiB` with no panic or UB), and `XMemVirtualAlloc` with `PAGES_2M` (`2 MiB` aligned). LGTM.
ColorMeBoom commented 2026-11-27 06:28:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ColorMeBoom on 2026-11-27T06:28:32Z.


#2184 and #2022 together on current main (the check NEW-192 on #1633 asks for). Native Windows 11 26200, Rust 1.98.1 MSVC, no game data. Both are merge candidates and both edit crates/victory-hle/src/modules/xmem.rs.

  • Together: main 63bcca4 + #2184 (8954790) + #2022 (8d8eee9) merge without conflicts. cargo test -p victory-hle --lib --locked -- --test-threads=1: 154 passed, 0 failed, both new tests included. cargo clippy -p victory-hle --all-targets --locked -- -D warnings: clean.

  • Each test without its fix (each PR merged onto main, then only its non-test hunks undone):

    • #2184's alloc_32bit_honours_alignments_above_64k fails: left: 65536, right: 0 at the alignment assertion.
    • #2022's aligned_blocks_are_padded_once takes the test process down with 0xc0000374 (STATUS_HEAP_CORRUPTION).

    So both tests catch the bug they're for, and neither fix depends on the other.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7211560013 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2184#issuecomment-7211560013). Originally posted by [ColorMeBoom](https://github.com/ColorMeBoom) on 2026-11-27T06:28:32Z. --- **#2184 and #2022 together on current `main`** (the check NEW-192 on #1633 asks for). Native Windows 11 26200, Rust 1.98.1 MSVC, no game data. Both are merge candidates and both edit `crates/victory-hle/src/modules/xmem.rs`. - **Together:** `main` `63bcca4` + #2184 (`8954790`) + #2022 (`8d8eee9`) merge without conflicts. `cargo test -p victory-hle --lib --locked -- --test-threads=1`: **154 passed, 0 failed**, both new tests included. `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: clean. - **Each test without its fix** (each PR merged onto `main`, then only its non-test hunks undone): - #2184's `alloc_32bit_honours_alignments_above_64k` fails: `left: 65536, right: 0` at the alignment assertion. - #2022's `aligned_blocks_are_padded_once` takes the test process down with `0xc0000374` (`STATUS_HEAP_CORRUPTION`). So both tests catch the bug they're for, and neither fix depends on the other. AI use: Claude Code (Claude Opus 5.5).
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/2184:upstream-pr/2184'
git switch 'upstream-pr/2184'

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