xmem: an aligned XMemAllocDefault block is padded once, not twice #2022
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2022
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2022"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@0xkn0wh3r3 on GitHub · 2026-11-25 03:17 UTC · original
0xkn0wh3r3:hle/xmem-alloc-pads-once→main· +59 −14 · 1 files · 2 commits · openThe problem
An aligned block from
XMemAllocDefaulttakes twice the alignment in extra host memory, not once. A 64 KiB-aligned block costs 192 KiB.XMemAllocDefaultasked the host allocator forsize + alignbytes at alignmentalign, to fit its header below an aligned pointer. Above 16 bytes of alignment, Rust's WindowsSystemallocator pads an over-aligned layout by the alignment itself (HeapAllocof size + align). So the block costsize + 2 * align:The fix
HEAP_ALIGN), stillsize + alignbytes. The host allocator does not pad that.alignbytes in.XMemFreeDefaultuses the offset to find the block.Alignments of 16 and below are unchanged.
Testing
modules::xmem::tests::aligned_blocks_are_padded_once(16 B, 4 KiB and 64 KiB alignment): checks, throughHeapSize, that the block under an aligned pointer is the process heap's own block ofsize + alignbytes.alloc_honours_alignment_and_freesstill 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
XMemAllocthrough its own hooks, so the gain there depends on how much reaches the default allocator.@jeremygold02 on GitHub · 2026-11-25 03:24 UTC · original
Triage verdict — Allocation change looks reasonable; harden the regression test (
9f24a38)Evidence
p as usize % alignbefore reading the header viap.sub(HEADER). A null allocation passes that alignment assertion, so the test can reach the unsafe read instead of failing cleanly.Checks and limits
Next step
HeapSizecheck 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 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 thexmemtest 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 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. TheHeapSizecheck stays. Thanks @Calvix2k for the Windows 10 run.@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; theHeapSizeassertion still shows the block is padded once.@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 currentmain. It merges cleanly, and the diff tocrates/victory-hle/src/modules/xmem.rsis unchanged from the last review.layoutnow asks forsize + alignat 16-byte alignment (HEAP_ALIGN).XMemAllocDefaultstores the size and the offset of the aligned pointer.XMemFreeDefaultfrees with the same size, offset and 16-byte layout, so the two sides still match. I rechecked that the offset is at mostalign: 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 throughHeapSize.CI: passing (
check). Recommending a look, not approving.Generated by Claude Code
@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.
@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. Main67e8ae1reproduces 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.
@fuzeofficlal on GitHub · 2026-11-26 19:11 UTC · original · approved these changes
Reviewed
8d8eee9c841emerged cleanly onto63bcca43.The single-padding change in
crates/victory-hle/src/modules/xmem.rs:242-249and309-358is clean and mathematically sound: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 blockb,offset = (b + HEADER).next_multiple_of(align) - bsatisfiesHEADER <= offset <= align, so the 16-byte header[layout.size(), offset]lies inside[0, offset)and thesize-byte payload lies inside[offset, size + align)even whensize == 0(avoiding zero-sizedstd::alloc::alloc).XMemFreeDefault(xmem.rs:345-358) restores the exact base pointer viauser.sub(offset)and deallocates withHEAP_ALIGN, matching the allocation layout.Verification (native Windows 11):
cargo xtask policyandcargo 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 includingaligned_blocks_are_padded_once.0, 1, 4, 6, 8, 12, 16, 20(16 Bthrough1 MiB) and sizes0, 1, 15, 16, 17, 1000, 65536: every block satisfiedaddr % align == 0,HEADER <= offset <= align,HeapSize(GetProcessHeap(), 0, base) == size + align, payload first/last-byte readback, and cleanXMemFreeDefault.#2184(8954790b4fef) on63bcca43with all 16xmem::tests passing.LGTM.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.