xmem: 32-bit blocks keep the alignment their attributes ask for #2268

Closed
Calvix2k wants to merge 1 commit from upstream-pr/2270 into main
Calvix2k commented 2026-11-25 09:28:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T09:28:51Z.


Fixes #2183.

Problem

XMemAllocDefault's 32-bit branch called XMemVirtualAlloc, whose address requirements take an alignment only from the PAGES_2M kind; otherwise MEM_ADDRESS_REQUIREMENTS::Alignment is 0, i.e. the 64 KiB allocation granularity. A 32-bit block asked for at 128 KiB, 1 MiB or 2 MiB therefore came back 64 KiB-aligned, although the module's header promises that what constrains addresses — alignment included — is honoured.

Change

The body of XMemVirtualAlloc is now virtual_alloc(..., alignment), with the public entry passing 0 (kind-derived, as before). XMemAllocDefault's 32-bit branch passes the attributes' alignment (log2 in 5 bits at 24, at least 16) when it is at least the 64 KiB granularity — below that the address requirements cannot express it and 0 already means the granularity. The 32-bit address range (ADDRESS_32BIT, below 4 GiB) and the write-watch/commit accounting are untouched.

Verification

New aligned_32_bit_blocks_get_their_alignment on this machine (Windows 10 19045): blocks of 4096 bytes at 128 KiB, 1 MiB and 2 MiB come back aligned to their request and below 4 GiB, then free cleanly.

On unmodified main the same test fails at the 1 MiB request (the returned address was 128 KiB-aligned); the 128 KiB case passed there only because the allocator happened to satisfy it. With the change all three pass.

cargo test -p victory-hle: 144 lib tests pass; clippy -D warnings clean; cargo xtask policy 1 file ok.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6601354021 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2270). Originally posted by [Calvix2k](https://github.com/Calvix2k) on 2026-11-25T09:28:51Z. --- Fixes #2183. ## Problem `XMemAllocDefault`'s 32-bit branch called `XMemVirtualAlloc`, whose address requirements take an alignment only from the `PAGES_2M` kind; otherwise `MEM_ADDRESS_REQUIREMENTS::Alignment` is 0, i.e. the 64 KiB allocation granularity. A 32-bit block asked for at 128 KiB, 1 MiB or 2 MiB therefore came back 64 KiB-aligned, although the module's header promises that what constrains addresses — alignment included — is honoured. ## Change The body of `XMemVirtualAlloc` is now `virtual_alloc(..., alignment)`, with the public entry passing 0 (kind-derived, as before). `XMemAllocDefault`'s 32-bit branch passes the attributes' alignment (log2 in 5 bits at 24, at least 16) when it is at least the 64 KiB granularity — below that the address requirements cannot express it and 0 already means the granularity. The 32-bit address range (`ADDRESS_32BIT`, below 4 GiB) and the write-watch/commit accounting are untouched. ## Verification New `aligned_32_bit_blocks_get_their_alignment` on this machine (Windows 10 19045): blocks of 4096 bytes at 128 KiB, 1 MiB and 2 MiB come back aligned to their request and below 4 GiB, then free cleanly. On unmodified `main` the same test fails at the 1 MiB request (the returned address was 128 KiB-aligned); the 128 KiB case passed there only because the allocator happened to satisfy it. With the change all three pass. `cargo test -p victory-hle`: 144 lib tests pass; clippy `-D warnings` clean; `cargo xtask policy` 1 file ok.
catminou4 commented 2026-11-25 10:07:16 +00:00 (Migrated from github.com)

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

Review: COMMENTED


xmem: 32-bit blocks keep the alignment their attributes ask for (alignment parameter threaded through virtual_alloc). Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1.

<!-- greyfinch-sync:Marvisak/VIctory:review:6055074318 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2270#pullrequestreview-6055074318). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T09:35:39Z. **Review:** COMMENTED --- xmem: 32-bit blocks keep the alignment their attributes ask for (alignment parameter threaded through virtual_alloc). Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1.
kotekompotek commented 2026-11-25 10:07:49 +00:00 (Migrated from github.com)

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


Triage note: this is the same fix as #2184 for #2183 — both extract a virtual_alloc taking an explicit alignment and forward the attributes' alignment field on the ALLOC_32BIT path, zeroing it at/below the 64 KiB granularity (this PR uses .max(4) + >= 64*1024, #2184 uses the raw field + > PAGE; equivalent). Only real difference: here XMemVirtualAlloc passes 0 so virtual_alloc still owns the PAGES_2M → 2 MiB rule, while #2184 has XMemVirtualAlloc compute that alignment itself. Same behavior either way. I verified #2184's test suite passes (11/11 xmem tests); whichever lands, the other is redundant.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158744367 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2270#issuecomment-7158744367). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T09:44:49Z. --- Triage note: this is the same fix as #2184 for #2183 — both extract a `virtual_alloc` taking an explicit alignment and forward the attributes' alignment field on the `ALLOC_32BIT` path, zeroing it at/below the 64 KiB granularity (this PR uses `.max(4)` + `>= 64*1024`, #2184 uses the raw field + `> PAGE`; equivalent). Only real difference: here `XMemVirtualAlloc` passes `0` so `virtual_alloc` still owns the `PAGES_2M` → 2 MiB rule, while #2184 has `XMemVirtualAlloc` compute that alignment itself. Same behavior either way. I verified #2184's test suite passes (11/11 xmem tests); whichever lands, the other is redundant.
upstream-sync closed this pull request 2026-11-25 11:04:31 +00:00
Calvix2k commented 2026-11-25 11:05:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T10:20:56Z.


Closing as the duplicate of #2184 (@BastienRodz) that @kotekompotek spotted: same virtual_alloc(..., alignment) extraction and the same 32-bit branch passing the attribute's alignment, and #2184 has its own regression test (alloc_32bit_honours_alignments_above_64k). Mine adds nothing beyond it; one PR is better.

For the record, the check that made me keep mine this long was on hardware: on this Windows 10 19045 machine the new test passes at 128 KiB, 1 MiB and 2 MiB, and on unmodified main it fails at the 1 MiB request (the address comes back 128 KiB-aligned). #2184's test covers the same ground.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7159571693 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2270#issuecomment-7159571693). Originally posted by [Calvix2k](https://github.com/Calvix2k) on 2026-11-25T10:20:56Z. --- Closing as the duplicate of #2184 (@BastienRodz) that @kotekompotek spotted: same `virtual_alloc(..., alignment)` extraction and the same 32-bit branch passing the attribute's alignment, and #2184 has its own regression test (`alloc_32bit_honours_alignments_above_64k`). Mine adds nothing beyond it; one PR is better. For the record, the check that made me keep mine this long was on hardware: on this Windows 10 19045 machine the new test passes at 128 KiB, 1 MiB and 2 MiB, and on unmodified `main` it fails at the 1 MiB request (the address comes back 128 KiB-aligned). #2184's test covers the same ground.

Pull request closed

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!2268
No description provided.