Skip to content

xmem: roll back physical page allocation on metadata exhaustion (BUILD-3078) - #8506

Open
channa317 wants to merge 1 commit into
ItsNotRexed:mainfrom
channa317:codex/xmem-allocation-rollback
Open

channa317 wants to merge 1 commit into
ItsNotRexed:mainfrom
channa317:codex/xmem-allocation-rollback

Conversation

@channa317

Copy link
Copy Markdown

Claimed in the coordination issue as BUILD-3078. Original claim by @channa317 · Coordinator's assignment

Depends on: #7556, #7584, #7652, #8121

The coordination board also uses this line for overlapping patches. #7556 changes section ownership and allocation, #7584 and #7652 add callers that use the previous section wrapper, and #8121 edits the same include block in the physical-page trace source. This commit builds directly on main.

What and why

XMemAllocatePhysicalPages creates a Windows section for the whole batch. If allocating its shared ownership record failed, the section handle leaked. A later allocation failure while inserting page records could also leave live entries, consume page IDs and partially overwrite the caller's array.

The section now has a deleter that also runs if the shared ownership record cannot be allocated. The allocator selects recycled and fresh IDs under the existing mutex and publishes them to the caller only after all insertions succeed. A metadata allocation failure erases the inserted prefix without allocating, sets the count to zero and returns FALSE with ERROR_NOT_ENOUGH_MEMORY. The caller's page array stays intact. A failed allocation while formatting the optional first-use trace can also drop that event and let the request proceed.

Tested

  • Native Windows x64, commit a92e1ba40b on c7b44489b9. Built gta6rt (RelWithDebInfo) and xmem_test (Debug and RelWithDebInfo). Each configuration passed 20 consecutive native xmem CTest runs, with 243 checks per run.
  • The regression calls the registered export with synthetic input and fails successive C++ allocations on the calling thread. It checks the Win32 return and error, all output slots, live handles, existing pages, absence of partial records, zero further allocations during rollback, and the mixed recycled/fresh ID sequence on retry. The original implementation failed the native regression and retained an extra section handle.
  • python -I scripts/check_no_game_data.py runtime/shims/xmem.cpp runtime/test/xmem_test.cpp and git diff --check passed.
  • python -I scripts/run_semantic_tests.py -j 12 tests/semantic ran 5,957 tests across 447 files. Two test files failed: test_image_atomics_guard (47 subtest failures, tracked in test: exercise narrowed EXEC for image atomic guards #7212) and test_image_store_d16 (one subtest failure, tracked in test: cover supported d16 cube image stores #7194). Both failure sets also reproduced on unmodified upstream code.
  • The trace test injects one failed diagnostic allocation. In a separate Debug control, keeping subsequent C++ allocations failing while startup tracing was active also timed out in a program that did not link XMem.

Test environment

Component Tested configuration
OS Windows 11 Pro for Workstations, 26H2, x64, build 26300.9711
CPU and RAM Intel Core i9-14900K, 24 cores / 32 logical processors, 128 GiB installed RAM
GPU and driver NVIDIA GeForce RTX 4090 (24 GB), NVIDIA 621.14 / Windows driver 32.0.16.2114. The allocator test uses CPU and Win32 section APIs.
Native compiler Visual Studio 2022 Build Tools 17.14.39, MSVC 19.44.35228.0, v143 toolset 14.44.35207, x64 host and target, C++20
Windows SDK 10.0.26100.0
Build and test tools CMake/CTest 3.31.6-msvc6, Ninja 1.12.1, Python 3.12.10
Runtime configuration Separate Debug and RelWithDebInfo Ninja builds, optional Vulkan headers and SDL3 unavailable. Synthetic fixtures only.

AI-assisted: yes, with OpenAI Codex.

Checklist

  • No game data, keys or decryption in this contribution.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested a92e1ba on Windows 11, CPU only: pass. All 10 runs of the synthetic XMem test pass (243 checks per run). When page metadata allocation fails, the section handle count returns to its initial value. The caller gets a zero count and keeps the original output slots. A retry gets the expected recycled and fresh page IDs.

I built xmem_test in RelWithDebInfo and ran ctest -C RelWithDebInfo -R "^xmem$" --repeat until-fail:10 --output-on-failure.

For the parent control (c7b4448), I used the new test and disabled the separate trace failure injection to reach the page allocator. Three checks fail. They cover the error result, the caller outputs, and the section handle count.

AI-assisted.

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.

2 participants