Skip to content

xgameruntime: a closed XGameSave container stays expired when its address is reused - #6312

Open
cynbun wants to merge 1 commit into
ItsNotRexed:mainfrom
cynbun:gamesave-stale-container-handle
Open

cynbun wants to merge 1 commit into
ItsNotRexed:mainfrom
cynbun:gamesave-stale-container-handle

Conversation

@cynbun

@cynbun cynbun commented Nov 23, 2026

Copy link
Copy Markdown

Claim and finding: #5934 (comment)

Cause

runtime/shims/xgameruntime_gamesave.cpp checks a handle by its address only (live(), the g_containers set), and XGameSaveCloseContainer (:682 on main 561b65a8) frees the container. The heap may give that address to the next XGameSaveCreateContainer. Two things keep a container pointer after the call that gave it:

  • an update (XGameSaveUpdate::container, :197, set in save_create_update :761). submit_update (:434-436) checks only that the address is a live container. After the address is reused, XGameSaveSubmitUpdate on an update of the closed container commits its blobs into the new container's folder;
  • an async job (Job::container, :447). XGameSaveReadBlobDataAsync checks the container when its work runs (:484). A read started on a container that is closed before the work runs, with the address reused, completes S_OK with the other container's blobs.

The quota, delete and submit jobs keep a provider or update pointer the same way.

xgameruntime_user.cpp:85-88 notes the same reuse for XUserHandle, where it does no harm because every handle names the one user. Here each container is a different save, so it is not harmless: data goes into, or is loaded from, the wrong save slot, where the shim means to return E_GS_HANDLE_EXPIRED.

Fix

Every provider, container and update gets a serial when it is made, and no two get the same one. An update keeps its container's serial, and each async job keeps the serial of the handle it names (0, which no handle has, when that handle was not live at the call). The checks become "address is live and its serial matches" (live_as). A closed handle now stays expired after its address is reused, and nothing else changes.

Tests

xgameruntime_gamesave_test gets a new section. It creates container "first", makes an update with a blob, starts a ReadBlobDataAsync on a manual queue and closes "first". Then it creates containers until one lands at the same address (it prints SKIP if the heap never reuses it) and writes a blob into that new container "second". Three checks follow:

  • SubmitUpdate of the stale update returns E_GS_HANDLE_EXPIRED;
  • it wrote nothing into second;
  • the stale read completes with E_GS_HANDLE_EXPIRED and no result.

Under Wine 11.20 the address comes back on the first try. On main all three checks fail: the update returns S_OK and writes local-user\second\mine, and the read returns the other container's blobs. With the fix, all 56 checks pass.

Platform: Windows runtime cross-built with clang-cl 22 + xwin on Linux and run under Wine 11.20 (not native Windows). CPU only, no GPU, no game files. Full ctest -j1 --timeout 300 before and after: main 561b65a8 230/246 passed, 16 failed (gameinput_host, device_proxy, device_proxy_trace, file_system, run_tiny_game_abort_exit_codes, vk_pipeline_check_cli, d3d12_noop_copy, d3d12_indirect_dispatch_count, d3d12_clear, d3d12_typeless_depth, d3d12_residency, d3d12_heap_properties, d3d12_present, dxgi_swapchain, d3d12_indirect_rt, d3d12_clear_formats). With the change the same 16 failed, and xbox_present also failed: [rt] d3d12: present failed (VkResult -11) on this desktop's live swapchain. xbox_present_test does not build either changed file (it links gta6rt_d3d12_com), so this is the display environment, not this change. xgameruntime_gamesave passes.

Overlap: #5113, #5189, #5190 and #5203 also edit this file, but in other places (commit staging, read packing, name encoding, long paths). None of them changes the handle checks. If one of them lands first, a rebase should be mechanical.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…ress is reused

XGameSaveUpdate and the async jobs keep a raw container (or provider, or
update) pointer, and the handle checks look the address up in the live set
only. XGameSaveCloseContainer frees the container, the heap hands the same
address to the next XGameSaveCreateContainer, and the stale pointer passes
as live again: SubmitUpdate of an update of the closed container commits its
blobs into the new container's folder, and a ReadBlobDataAsync started on it
completes with the other container's blobs.

Every handle now gets a serial when it is made; an update keeps its
container's serial and each async job the serial of the handle it names,
and the checks compare address and serial. xgameruntime_gamesave_test
reproduces the reuse (under Wine the address comes back at once): three
checks fail without the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant