xmem: an aligned XMemAllocDefault block is padded once, not twice #2020

Open
0xkn0wh3r3 wants to merge 2 commits from upstream-pr/2022 into main
0xkn0wh3r3 commented 2026-11-25 03:17:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T03:17:45Z.


The problem

An aligned block from XMemAllocDefault takes twice the alignment in extra host memory, not once. A 64 KiB-aligned block costs 192 KiB.

XMemAllocDefault asked the host allocator for size + align bytes at alignment align, to fit its header below an aligned pointer. Above 16 bytes of alignment, Rust's Windows System allocator pads an over-aligned layout by the alignment itself (HeapAlloc of size + align). So the block cost size + 2 * align:

block cost on main
64 KiB at 64 KiB alignment 192 KiB
4 KiB at 4 KiB alignment 12 KiB

The fix

  • The block is now asked for at the heap's own 16-byte alignment (HEAP_ALIGN), still size + align bytes. The host allocator does not pad that.
  • The pointer is the first aligned address past the header, at most align bytes in.
  • The header keeps the block's size and the pointer's offset in it (it kept the size and the alignment before). XMemFreeDefault uses the offset to find the block.

Alignments of 16 and below are unchanged.

Testing

  • New test modules::xmem::tests::aligned_blocks_are_padded_once (16 B, 4 KiB and 64 KiB alignment): checks, through HeapSize, that the block under an aligned pointer is the process heap's own block of size + align bytes.
  • The existing alloc_honours_alignment_and_frees still passes.
  • cargo test -p victory-hle: all pass. cargo clippy -p victory-hle --all-targets: clean.

Not covered

Not measured in a title run (no game data here). Grand Theft Auto VI routes XMemAlloc through its own hooks, so the gain there depends on how much reaches the default allocator.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5484495200 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2022). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T03:17:45Z. --- ## The problem An aligned block from `XMemAllocDefault` takes twice the alignment in extra host memory, not once. A 64 KiB-aligned block costs 192 KiB. `XMemAllocDefault` asked the host allocator for `size + align` bytes at alignment `align`, to fit its header below an aligned pointer. Above 16 bytes of alignment, Rust's Windows `System` allocator pads an over-aligned layout by the alignment itself (`HeapAlloc` of size + align). So the block cost `size + 2 * align`: | block | cost on main | |---|---| | 64 KiB at 64 KiB alignment | 192 KiB | | 4 KiB at 4 KiB alignment | 12 KiB | ## The fix - The block is now asked for at the heap's own 16-byte alignment (`HEAP_ALIGN`), still `size + align` bytes. The host allocator does not pad that. - The pointer is the first aligned address past the header, at most `align` bytes in. - The header keeps the block's size and the pointer's offset in it (it kept the size and the alignment before). `XMemFreeDefault` uses the offset to find the block. Alignments of 16 and below are unchanged. ## Testing - New test `modules::xmem::tests::aligned_blocks_are_padded_once` (16 B, 4 KiB and 64 KiB alignment): checks, through `HeapSize`, that the block under an aligned pointer is the process heap's own block of `size + align` bytes. - The existing `alloc_honours_alignment_and_frees` still passes. - `cargo test -p victory-hle`: all pass. `cargo clippy -p victory-hle --all-targets`: clean. ## Not covered Not measured in a title run (no game data here). Grand Theft Auto VI routes `XMemAlloc` through its own hooks, so the gain there depends on how much reaches the default allocator.
catminou4 commented 2026-11-25 03:33:17 +00:00 (Migrated from github.com)

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

Review: COMMENTED


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

Aligned XMemAllocDefault block padded once, not twice — double-padding fix in the XMem allocator; small, exact.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051461893 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2022#pullrequestreview-6051461893). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T03:33:17Z. **Review:** COMMENTED --- Static review only: victory-hle is Windows-only — nothing compiles or runs on macOS; source review of the diff. Aligned XMemAllocDefault block padded once, not twice — double-padding fix in the XMem allocator; small, exact.
jeremygold02 commented 2026-11-25 04:06:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-25T03:24:40Z.


Triage verdict — Allocation change looks reasonable; harden the regression test (9f24a38)

Evidence

  • The patch separates the requested user alignment from the host allocation layout, then records the actual offset for deallocation. With the host block aligned to 16 bytes and a 16-byte header, that offset is at most the requested alignment.
  • The test checks p as usize % align before reading the header via p.sub(HEADER). A null allocation passes that alignment assertion, so the test can reach the unsafe read instead of failing cleanly.

Checks and limits

  • This is source review. I found no check run on the reviewed head and did not rerun the allocator test.

Next step

  • Assert the allocation is non-null before doing address arithmetic or reading the header. Keep the HeapSize check and consider touching both ends of the requested payload before freeing so the test also verifies that the aligned address leaves the full usable span.

This verification was AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7151888239 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2022#issuecomment-7151888239). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-25T03:24:40Z. --- **Triage verdict — Allocation change looks reasonable; harden the regression test** (`9f24a38`) **Evidence** - The patch separates the requested user alignment from the host allocation layout, then records the actual offset for deallocation. With the host block aligned to 16 bytes and a 16-byte header, that offset is at most the requested alignment. - The test checks `p as usize % align` before reading the header via `p.sub(HEADER)`. A null allocation passes that alignment assertion, so the test can reach the unsafe read instead of failing cleanly. **Checks and limits** - This is source review. I found no check run on the reviewed head and did not rerun the allocator test. **Next step** - Assert the allocation is non-null before doing address arithmetic or reading the header. Keep the `HeapSize` check and consider touching both ends of the requested payload before freeing so the test also verifies that the aligned address leaves the full usable span. This verification was AI-assisted.
Calvix2k commented 2026-11-25 04:07:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T03:45:18Z.


Verified on Windows 10 19045, head 9f24a38: cargo test -p victory-hle --lib — 144 passed, 0 failed, including the xmem test for this change.

The padding rule in the diff is the one that matches the console's own allocator contract (the alignment is applied to the requested size once, not to the already-padded block), and the test asserts the size and the page count, which is the part that would have gone unnoticed otherwise.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152210608 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2022#issuecomment-7152210608). Originally posted by [Calvix2k](https://github.com/Calvix2k) on 2026-11-25T03:45:18Z. --- Verified on Windows 10 19045, head `9f24a38`: `cargo test -p victory-hle --lib` — **144 passed, 0 failed**, including the `xmem` test for this change. The padding rule in the diff is the one that matches the console's own allocator contract (the alignment is applied to the requested size once, not to the already-padded block), and the test asserts the size and the page count, which is the part that would have gone unnoticed otherwise.
0xkn0wh3r3 commented 2026-11-25 07:03:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T06:19:19Z.


Thanks @jeremygold02, done in 73a1fbf. The test now asserts the block is non-null before any address arithmetic or header read. Before freeing, it writes and reads back the first and last bytes of the 1,000-byte payload, so the aligned address is shown to leave the whole requested span usable. The HeapSize check stays. Thanks @Calvix2k for the Windows 10 run.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7154670266 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2022#issuecomment-7154670266). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T06:19:19Z. --- Thanks @jeremygold02, done in `73a1fbf`. The test now asserts the block is non-null before any address arithmetic or header read. Before freeing, it writes and reads back the first and last bytes of the 1,000-byte payload, so the aligned address is shown to leave the whole requested span usable. The `HeapSize` check stays. Thanks @Calvix2k for the Windows 10 run.
Calvix2k commented 2026-11-25 09:06:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T08:14:46Z.


Re-verified on 73a1fbf, Windows 10 19045: cargo test -p victory-hle --lib — 144 passed, 0 failed. The non-null check and the payload first/last-byte readback are exactly what the test was missing; the HeapSize assertion still shows the block is padded once.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7156789879 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2022#issuecomment-7156789879). Originally posted by [Calvix2k](https://github.com/Calvix2k) on 2026-11-25T08:14:46Z. --- Re-verified on `73a1fbf`, Windows 10 19045: `cargo test -p victory-hle --lib` — **144 passed, 0 failed**. The non-null check and the payload first/last-byte readback are exactly what the test was missing; the `HeapSize` assertion still shows the block is padded once.
Marvisak commented 2026-11-25 10:07:38 +00:00 (Migrated from github.com)

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


Automated triage for the maintainers.

Merge candidate (kept). The new head (8d8eee9) is the same two commits replayed onto current main. It merges cleanly, and the diff to crates/victory-hle/src/modules/xmem.rs is unchanged from the last review.

layout now asks for size + align at 16-byte alignment (HEAP_ALIGN). XMemAllocDefault stores the size and the offset of the aligned pointer. XMemFreeDefault frees with the same size, offset and 16-byte layout, so the two sides still match. I rechecked that the offset is at most align: the block is 16-aligned and the header is 16 bytes. The test checks for null, writes both ends of the payload and checks the host block through HeapSize.

CI: passing (check). Recommending a look, not approving.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158173397 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2022#issuecomment-7158173397). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T09:19:18Z. --- <!-- victory-triage v1 head=8d8eee9c841ec4101c08a0341d202ee73445d23c verdict=merge-candidate at=2026-11-26T01:20:30Z --> Automated triage for the maintainers. **Merge candidate (kept).** The new head (`8d8eee9`) is the same two commits replayed onto current `main`. It merges cleanly, and the diff to `crates/victory-hle/src/modules/xmem.rs` is unchanged from the last review. `layout` now asks for `size + align` at 16-byte alignment (`HEAP_ALIGN`). `XMemAllocDefault` stores the size and the offset of the aligned pointer. `XMemFreeDefault` frees with the same size, offset and 16-byte layout, so the two sides still match. I rechecked that the offset is at most `align`: the block is 16-aligned and the header is 16 bytes. The test checks for null, writes both ends of the payload and checks the host block through `HeapSize`. CI: passing (`check`). Recommending a look, not approving. --- _Generated by [Claude Code](https://claude.ai/code)_
jaschinski97-ops commented 2026-11-25 16:20:10 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Native Windows 11 check of unchanged head 73a1fbf: 12/12 XMem tests pass, including an independent native-heap cost probe. Main 67e8ae1 reproduces the double padding: the 4 KiB and 64 KiB payload/alignment cases cost 12,288 and 196,608 native bytes. With #2022, they cost 8,192 and 131,072.

Both original heads (#2022 and #2184) merge together and onto current main without conflict. The combined HLE library passes 149/149; all-target/all-feature Clippy with warnings denied passes.

Exact heads, commands, safe HeapWalk/HeapSize probe and results. Original commits/authors retained; this is an evidence branch. Windows 11 Pro build 26300, Rust 1.98.1 Windows GNU / LLVM-MinGW. Synthetic allocations only; no title data. AI-assisted verification with Codex. Claim: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7167502972.

<!-- greyfinch-sync:Marvisak/VIctory:review:6061037222 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2022#pullrequestreview-6061037222). Originally posted by [jaschinski97-ops](https://github.com/jaschinski97-ops) on 2026-11-25T16:20:10Z. **Review:** COMMENTED --- Native Windows 11 check of unchanged head `73a1fbf`: **12/12 XMem tests pass**, including an independent native-heap cost probe. Main `67e8ae1` reproduces the double padding: the 4 KiB and 64 KiB payload/alignment cases cost 12,288 and 196,608 native bytes. With #2022, they cost 8,192 and 131,072. Both original heads (#2022 and #2184) merge together and onto current main without conflict. The combined HLE library passes **149/149**; all-target/all-feature Clippy with warnings denied passes. [Exact heads, commands, safe HeapWalk/HeapSize probe and results](https://github.com/jaschinski97-ops/VIctory/blob/4360830/verification/xmem-windows.md). Original commits/authors retained; this is an evidence branch. Windows 11 Pro build 26300, Rust 1.98.1 Windows GNU / LLVM-MinGW. Synthetic allocations only; no title data. AI-assisted verification with Codex. Claim: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7167502972.
fuzeofficlal commented 2026-11-26 19:11:35 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed 8d8eee9c841e merged cleanly onto 63bcca43.

The single-padding change in crates/victory-hle/src/modules/xmem.rs:242-249 and 309-358 is clean and mathematically sound:

  • Requesting Layout::from_size_align(size.checked_add(align)?, HEAP_ALIGN) (HEAP_ALIGN = 16, align = 1 << field.max(4)) guarantees that from any 16-byte-aligned heap block b, offset = (b + HEADER).next_multiple_of(align) - b satisfies HEADER <= offset <= align, so the 16-byte header [layout.size(), offset] lies inside [0, offset) and the size-byte payload lies inside [offset, size + align) even when size == 0 (avoiding zero-sized std::alloc::alloc).
  • XMemFreeDefault (xmem.rs:345-358) restores the exact base pointer via user.sub(offset) and deallocates with HEAP_ALIGN, matching the allocation layout.

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 all tests including aligned_blocks_are_padded_once.
  • Verified an additional alignment/size matrix probe across fields 0, 1, 4, 6, 8, 12, 16, 20 (16 B through 1 MiB) and sizes 0, 1, 15, 16, 17, 1000, 65536: every block satisfied addr % align == 0, HEADER <= offset <= align, HeapSize(GetProcessHeap(), 0, base) == size + align, payload first/last-byte readback, and clean XMemFreeDefault.
  • Also verified a clean zero-conflict co-merge with #2184 (8954790b4fef) on 63bcca43 with all 16 xmem:: tests passing.

LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6079359037 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2022#pullrequestreview-6079359037). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T19:11:35Z. **Review:** APPROVED --- Reviewed `8d8eee9c841e` merged cleanly onto `63bcca43`. The single-padding change in `crates/victory-hle/src/modules/xmem.rs:242-249` and `309-358` is clean and mathematically sound: - Requesting `Layout::from_size_align(size.checked_add(align)?, HEAP_ALIGN)` (`HEAP_ALIGN = 16`, `align = 1 << field.max(4)`) guarantees that from any 16-byte-aligned heap block `b`, `offset = (b + HEADER).next_multiple_of(align) - b` satisfies `HEADER <= offset <= align`, so the 16-byte header `[layout.size(), offset]` lies inside `[0, offset)` and the `size`-byte payload lies inside `[offset, size + align)` even when `size == 0` (avoiding zero-sized `std::alloc::alloc`). - `XMemFreeDefault` (`xmem.rs:345-358`) restores the exact base pointer via `user.sub(offset)` and deallocates with `HEAP_ALIGN`, matching the allocation layout. **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 all tests including `aligned_blocks_are_padded_once`. - Verified an additional alignment/size matrix probe across fields `0, 1, 4, 6, 8, 12, 16, 20` (`16 B` through `1 MiB`) and sizes `0, 1, 15, 16, 17, 1000, 65536`: every block satisfied `addr % align == 0`, `HEADER <= offset <= align`, `HeapSize(GetProcessHeap(), 0, base) == size + align`, payload first/last-byte readback, and clean `XMemFreeDefault`. - Also verified a clean zero-conflict co-merge with `#2184` (`8954790b4fef`) on `63bcca43` with all 16 `xmem::` tests passing. LGTM.
ColorMeBoom commented 2026-11-27 06:28:33 +00:00 (Migrated from github.com)

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


#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:7211560250 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2022#issuecomment-7211560250). Originally posted by [ColorMeBoom](https://github.com/ColorMeBoom) on 2026-11-27T06:28:33Z. --- **#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/2022:upstream-pr/2022'
git switch 'upstream-pr/2022'

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