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

Open
github-bot wants to merge 2 commits from github-bot/gh-2022 into main AGit
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 03:17 UTC · original
0xkn0wh3r3:hle/xmem-alloc-pads-once → main · +59 −14 · 1 files · 2 commits · open

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.

**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 03:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/2022) `0xkn0wh3r3:hle/xmem-alloc-pads-once` → `main` · +59 −14 · 1 files · 2 commits · open ## 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.
`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`: 192 KiB for a 64 KiB-aligned one, 12 KiB
for a 4 KiB block at 4 KiB.

The block is now asked for at the heap's own 16-byte alignment, `size +
align` bytes, and 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, which `XMemFreeDefault` uses. Alignments of
16 and below are unchanged.

The new test checks, through `HeapSize`, that the block under an aligned
pointer is the process heap's own block of `size + align` bytes.
Review (#2022): the test checked the pointer's alignment before reading
the header below it, and a null pointer passes an alignment check, so a
failed allocation would reach the unsafe read instead of failing cleanly.
It now asserts the block is allocated first, and writes and reads back
the payload's first and last bytes before freeing, so the aligned
address is shown to leave the whole requested span usable.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:51:53 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-25 03:24 UTC · original

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.

**@⁠jeremygold02** on GitHub · 2026-11-25 03:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/2022#issuecomment-7151888239) **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.
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-25 03:45 UTC · original

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.

**@⁠Calvix2k** on GitHub · 2026-11-25 03:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/2022#issuecomment-7152210608) 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.
Author
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 06:19 UTC · original

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.

**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 06:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2022#issuecomment-7154670266) 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.
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-25 08:14 UTC · original

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.

**@⁠Calvix2k** on GitHub · 2026-11-25 08:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/2022#issuecomment-7156789879) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 09:19 UTC · original · edited 2026-11-26 01:20 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 09:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2022#issuecomment-7158173397) · edited 2026-11-26 01:20 UTC <!-- 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)_
Author
Owner

@⁠catminou4 on GitHub · 2026-11-25 03:33 UTC · original · reviewed

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.

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

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

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

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

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

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.

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

Merge

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