Skip to content

Runtime: xmem honours the attribute word's alignment and reports the memory kind (RT-202) - #3601

Open
kotorvr wants to merge 2 commits into
ItsNotRexed:mainfrom
kotorvr:claude/cmp-os-shims-d71994
Open

kotorvr wants to merge 2 commits into
ItsNotRexed:mainfrom
kotorvr:claude/cmp-os-shims-d71994

Conversation

@kotorvr

@kotorvr kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

RT-202 (claim): xmem.dll answers the shim left as unknown. The claim listed three; the third (XMemGetWorkingSetStatistics +0) is not in this PR, see the end.

AI-assisted (Claude Code). I ran everything under "Tested" on Windows.

Problem

runtime/shims/xmem.cpp answered two things with a placeholder:

  1. XMemAlloc ignored its attribute word and aligned every block to 64 bytes ("ASSUMPTION: the attribute word is not decoded"). The package's amd_fidelityfx_upscaler_xbox.dll passes 0xa8004000, which asks for 256 bytes on 64 KiB pages, and got a 64-byte-aligned block.
  2. XMemVirtualQuery zero-filled everything after MEMORY_BASIC_INFORMATION. All four call sites in XboxBvhBuilder.dll test bits 0-1 of the byte at +0x30 and record an error for the pointer when they are clear, so every pointer it checked was reported as unusable.

Where the facts come from

Each was read in other layers and then checked against the package's own call sites with llvm-objdump (nothing was run; only the instructions around the calls were read). Facts only: no code, table, struct or identifier is taken from any of them. xcl is GPL-2.0-only and TuxBox has no licence, and I read their source while writing this, so please check the provenance against the files below. xcl and xbox-emulator share lineage, so where they agree it is one reading, not two; the package column is what each fact stands on.

Fact Other layers In the 1.0.2.1 package
Attribute word: log2 alignment in bits 24-28, page-size code in bits 14-15 (1 = 64 KiB) ivall/xcl crates/xcl/src/hle/kernelx.rs (cf08a50), omonkeyz/xbox-emulator crates/compat/xmem/src/alloc.rs (0fc7295); brcly/TuxBox runtime/xmem/README.md (a067ba9) measured 0x0480009e → 16 bytes and 0x30804000 → 64 KiB on retail titles amd_fidelityfx_upscaler_xbox.dll loads 0xa8004000 from a global at its 1 alloc and 2 free sites and rounds the size up to 256 itself (docs/fspx.md already had the word); Bink2Scarlett.dll passes 0x5e50000 (32 bytes) and aligns to 64 on its own
XMemVirtualQuery: a memory-kind word at +48, 1 for ordinary CPU memory xcl (same file), xbox-emulator crates/compat/xmem/src/virt.rs 4 of 4 call sites in XboxBvhBuilder.dll (RVAs 0x19c69, 0x19ccb, 0x19d9d, 0x19df9) require bits 0-1 of the byte at +0x30, MEM_COMMIT in State, and at the first site PAGE_READONLY or PAGE_READWRITE

Change

  • XMemAlloc aligns to the alignment field, and to 64 KiB when the page code is 1, never to less than the 64 bytes it used before. XMemFree is unchanged.
  • XMemVirtualQuery writes 1 at +48 for committed memory and leaves it zero otherwise. Bytes 52-63 stay zero.
  • docs/runtime_imports.md gets the table above; runtime/README.md and the comments in xmem.cpp no longer say the fields are unknown.

Two cases are deliberately not acted on. Each logs once and gives the answer main gives today:

  • Page codes 2 and 3. Only xbox-emulator reads them (as 2 MiB). main's XMemVirtualAlloc refuses its 2 MiB page mode "until their host mapping is known", and this follows the same line: the block is aligned by its alignment field only.
  • An alignment field above 21 (2 MiB). The field can say 2 GiB, and honouring that from the heap would commit the alignment for every block. The largest measured field is 16. Above 21 the block keeps the 64-byte floor.

So no caller gets a worse answer than today: the alignment is the same or stricter, and a block costs more only where the title asked for a coarser alignment that is honoured.

Cost. A block is still carved from the heap, not given pages of its own, so each block with page code 1 or a 64 KiB alignment pays up to 64 KiB of padding, and one with a 2 MiB alignment field up to 2 MiB. The FSR DLL makes one such allocation per pipeline.

Beside XMemVirtualAlloc. That function (from #2199) takes its page mode from the allocation-type bits and fails on any nonzero attribute word. This PR does not touch it; it decodes the attribute word for XMemAlloc only, where failing is not an option because the package's DLLs already pass nonzero words. Whether XMemVirtualAlloc should accept the same word is left for when a caller shows one.

How much this matters today: the BVH builder's check only writes a diagnostic (the export's HRESULT comes from a checksum compare), and I have not shown that the FSR DLL depends on the 256-byte alignment. These are wrong defaults the executable would meet at boot, not a crash I reproduced.

Left open in the doc: the memory-type field (bits 29-32) is not interpreted; whether a block is zero-filled and what a zero-size request returns differ between the other layers, so both stay as they were.

Tested

Windows 11 (build 26200), MSVC 19.44.35222, Visual Studio's CMake and ctest 3.31.6 with Ninja, RelWithDebInfo, built by scripts/build_runtime.ps1. Base f847748c (main). NVIDIA GeForce RTX 4090, driver 32.0.16.2114, for the Vulkan tests in the full run.

  • xmem_test with the new cases against main's xmem.cpp: 9 failures of 61 checks (seven alignment cases and the two virtual-query checks; a 64-byte-aligned block is sometimes aligned to 128, 256 or 4096 bytes by luck, so the cases up to 4 KiB do not fail on every run of the old shim, while those of 64 KiB and more always do). With the change: OK, 61 checks.
  • ctest --test-dir runtime/build -C RelWithDebInfo with LEONIDA_GAME_DIR set to my own package: 140 run, 134 passed, 6 skipped, 0 failed (gameinput is disabled, no SDL3 here). bvh_builder and fsr_dispatch, which load the real XboxBvhBuilder.dll and amd_fidelityfx_upscaler_xbox.dll through these shims, pass.
  • No game data: the test uses attribute words, not package bytes; the docs quote call-site RVAs and two attribute words, as docs/fspx.md and xmem.cpp already did.

#2401 (trace of each distinct attribute word) edits the same function. It is still wanted: it will show the words the executable passes. Whichever lands second needs a small rebase.

Not in this PR: XMemGetWorkingSetStatistics writes 0 at +0, where xcl and xbox-emulator put the memory the title may use. The package's one caller reads only +8, so nothing here confirms it; I posted it as a finding instead (comment); it became RT-220, which #3401 and #3431 now cover. They edit XMemGetWorkingSetStatistics, a different function of the same file.

🤖 Generated with Claude Code

…memory kind

XMemAlloc ignored its attribute word and aligned every block to 64 bytes. The word
holds log2 of the alignment in bits 24-28 and a page-size code in bits 14-15; the
package's FSR DLL passes 0xa8004000 (256 bytes, 64 KiB pages). Blocks now follow the
alignment field up to 2 MiB and the 64 KiB page code, and are never aligned to less
than 64 bytes. Page codes 2 and 3 and larger alignment fields are not measured: they
are logged once and leave the block as it was before.

XMemVirtualQuery zero-filled the word after MEMORY_BASIC_INFORMATION. All four call
sites in XboxBvhBuilder.dll test bits 0-1 of it and record an error when they are
clear. Committed memory now reports kind 1 there.

Both facts were read in other layers (ivall/xcl and omonkeyz/xbox-emulator, which
share lineage; brcly/TuxBox's measured words; facts only, no code) and checked
against the package's call sites. docs/runtime_imports.md has the table; xmem_test
covers both.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@Desamodeth Desamodeth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the loader path on Windows x64 with the same synthetic EXE on main (f847748) and this PR (92e7470). It imports XMemAlloc, XMemVirtualQuery and XMemFree from xmem.dll and holds three live blocks. On main, neither allocation requesting 64 KiB alignment (0xa8004000) met it, and querying the last byte of each block returned kind 0. On the PR both addresses were 64 KiB aligned, the default block kept its 64-byte alignment, and all three queries reported committed read/write memory with kind 1. All three blocks retained their endpoint bytes and were freed. The native xmem_test also passed all 61 checks. Synthetic CPU test only; I did not run the game. AI-assisted.

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Native Windows x64 MSVC at 92e7470: 61/61 synthetic xmem checks passed, including default and zero-size allocation, requested alignments through 2 MiB, and committed/reserved memory-kind queries. An independent comparison to the retail DLL was unavailable; fallback codes were checked against shim documentation only. AI-assisted CPU review.

…k TuxBox's file

The xmem table names three outside projects, so docs/related_projects.md gets an
entry for each with its licence and what is used. TuxBox's file is a link, not a
backticked path: tests/test_docs_repo_paths.py read runtime/xmem/README.md as a path
of this repository.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up commit bd335ac (docs only): tests/test_docs_repo_paths.py read the backticked TuxBox path in the new table as a path of this repository and failed, which I had not run before opening. TuxBox's file is now a link, and docs/related_projects.md gets entries for the three projects the table names (xcl and xbox-emulator as one shared-lineage entry, TuxBox as no-licence, facts only). python -I on test_docs_links.py, test_docs_repo_paths.py, test_docs_list_items.py, test_docs_design_rows.py and test_i18n_index.py: OK. No code changed since the test run in the description.

@Kam471

Kam471 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 92e74706da064d5a761bfbcdb2c253e7b8007ea6 on Windows 11, MSVC 19.44.35228 (cl 14.44.35207) + Ninja, RelWithDebInfo.

  • xmem_test.exe -> 61 checks, 61 PASS, 0 FAIL, exit 0.
  • ctest -R xmem --output-on-failure -> 1/1 passed, 0.01 s.

Private build dir; runtime/build untouched. No game files.

@shdann

shdann commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Review note about memory use. An AI tool helped me to read the diff. I did not build or run the change.

Possible problem

XMemAlloc now sends alignments of a maximum of 2 MiB to _aligned_malloc. The CRT gets approximately size + alignment + sizeof(void*) bytes from the heap for each block. Thus each block uses the full alignment as extra memory.

  • The FSR word 0xa8004000 sets 64 KiB pages for a block of 256 bytes. Each such block uses approximately 64 KiB. This is approximately 250 times the necessary size.
  • A block with alignment field 21 uses more than 2 MiB. The block size has no effect.

If a title makes many such blocks, the working set increases much. The CRT heap also becomes fragmented.

Suggestions

  1. Use VirtualAlloc for alignments of 64 KiB and larger. Its allocation granularity is 64 KiB, thus a 64 KiB alignment needs no extra memory. For a larger alignment, reserve size + alignment. Then release the reservation and reserve again at the aligned address. VirtualAlloc2 with MEM_ADDRESS_REQUIREMENTS is an alternative. XMemFree must then know the type of each block. A small table of the large blocks can do this.
  2. Or use slabs for the 64 KiB page blocks. Reserve a sequence of 64 KiB units one time. Give the units to the callers.

Test

In xmem_test, allocate 64 blocks of 256 bytes with the FSR word. Compare the commit charge of the process before and after the allocations.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested bd335ac on Windows 11, MSVC (VS 2022 Build Tools) RelWithDebInfo, with RTX 5080 (driver 616.92) and AMD Radeon iGPU gfx1036 (driver 26.10.1) each selected in turn (LEONIDA_VK_DEVICE, VK_LOADER_LAYERS_DISABLE=~implicit~; xmem itself needs no GPU): ctest -R "^xmem$" — pass, 1/1 per run, xmem_test: OK (61 checks). tests/test_docs_links.py and tests/test_docs_repo_paths.py OK.

Not run: bvh_builder and fsr_dispatch (no game files here). No negative control run.

AI-assisted.

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

@shdann Thanks, your arithmetic is right: _aligned_malloc pays the alignment in full, so the FSR word costs about 64 KiB of heap for a 256-byte block and an alignment-field-21 block a little over 2 MiB. The description states that cost under "Cost", but it does not avoid it.

What it means for the package today: the FSR DLL's one allocation site with that word sits in its pipeline-creation path, so it runs per pipeline, not per frame or per object. The trade this PR makes is a correct alignment at that padding cost, against main's wrong alignment at no cost.

Your VirtualAlloc route for 64 KiB and up is the better design (XMemFree then needs to tell the two kinds apart, as you say), and the commit-charge check you describe is the right test for it. I am not going to add it to this PR: the work in this lane is being wound down, and I would rather not leave a half-reviewed allocator change behind. If a maintainer prefers the PR to wait for it, closing this one in favour of a version that does it properly is fine with me. AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants