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

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

@⁠BastienRodz on GitHub · 2026-11-25 07:08 UTC · original
BastienRodz:hle/xmem-32bit-align → main · +56 −6 · 1 files · 1 commits · open

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

**@⁠BastienRodz** on GitHub · 2026-11-25 07:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/2184) `BastienRodz:hle/xmem-32bit-align` → `main` · +56 −6 · 1 files · 1 commits · open ### 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)
XMemAllocDefault previously forwarded ALLOC_32BIT requests straight to
XMemVirtualAlloc without passing the alignment field from attributes, so
MEM_ADDRESS_REQUIREMENTS::Alignment was always 0 (64 KiB granularity)
and alignments above 64 KiB (128 KiB, 256 KiB, 2 MiB, ...) were ignored.

Extract a virtual_alloc helper taking an explicit Alignment and pass
`1 << ((attributes >> 24) & 0x1f)` when it exceeds 64 KiB (0 otherwise,
as required by MEM_ADDRESS_REQUIREMENTS).

Co-Authored-By: Gemini 4 Neon <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:57:42 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 09:18 UTC · original

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.

**@⁠kotekompotek** on GitHub · 2026-11-25 09:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2184#issuecomment-7158156029) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 12:38 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 12:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/2184#issuecomment-7162649504) <!-- 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)_
Author
Owner

@⁠catminou4 on GitHub · 2026-11-25 07:17 UTC · original · reviewed

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.

**@⁠catminou4** on GitHub · 2026-11-25 07:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/2184#pullrequestreview-6053351499) · **reviewed** 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.
Author
Owner

@⁠jaschinski97-ops on GitHub · 2026-11-25 16:18 UTC · original · reviewed

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: #1633.

**@⁠jaschinski97-ops** on GitHub · 2026-11-25 16:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2184#pullrequestreview-6061023581) · **reviewed** 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://git.gtahub.st/github/Marvisak--VIctory/issues/1633.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 19:00 UTC · original · approved these changes

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 19:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/2184#pullrequestreview-6079267989) · **approved these changes** 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.
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/2184/head:github-bot/gh-2184'
git switch 'github-bot/gh-2184'

Merge

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