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

Closed
github-bot wants to merge 1 commit from gh-pr/2270/head into gh-pr/2270/base
Owner

@⁠Calvix2k on GitHub · 2026-11-25 09:28 UTC · original
Calvix2k:codex/hle-xmem-32bit-align-2183 → main · +37 −2 · 1 files · 1 commits · closed without merging

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.

**@⁠Calvix2k** on GitHub · 2026-11-25 09:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/2270) `Calvix2k:codex/hle-xmem-32bit-align-2183` → `main` · +37 −2 · 1 files · 1 commits · closed without merging 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.
`XMemAllocDefault`'s 32-bit branch went through `XMemVirtualAlloc`, which
takes an alignment only from the 2 MiB-page kind and otherwise leaves
`MEM_ADDRESS_REQUIREMENTS::Alignment` at 0 (the 64 KiB granularity), so a
block asked for at 128 KiB, 1 MiB or 2 MiB came back 64 KiB-aligned.

Split the body into `virtual_alloc` with an explicit alignment and pass the
attributes' alignment from the 32-bit branch, when it is at least the
allocation granularity (below it the requirements cannot express it and 0
already means exactly that). The address-bit and 4 GiB bounds are unchanged.

Test: 32-bit blocks at 128 KiB, 1 MiB and 2 MiB are aligned and below
4 GiB, and free.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:00:29 +00:00
github-bot closed this pull request 2026-11-26 05:00:29 +00:00
Author
Owner

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

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.

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

@⁠Calvix2k on GitHub · 2026-11-25 10:20 UTC · original

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.

**@⁠Calvix2k** on GitHub · 2026-11-25 10:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/2270#issuecomment-7159571693) 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

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