Skip to content

d3d12_xs: keep child proxies alive during queue calls - #9475

Closed
Kadamitas wants to merge 2 commits into
ProjectLeonida:mainfrom
Kadamitas:fix/live-child-queue-lifetime
Closed

Kadamitas wants to merge 2 commits into
ProjectLeonida:mainfrom
Kadamitas:fix/live-child-queue-lifetime

Conversation

@Kadamitas

@Kadamitas Kadamitas commented Nov 23, 2026 •

Copy link
Copy Markdown

Queue forwarding can reenter Release on the caller's last queue or fence reference. Keep live child-proxy identity in an allocation-free registry, and pin the queue and proxied fence until Signal/Wait's backend call returns. Proxy classification checks the live allocation and kind instead of treating a matching first word as a child object. Queue slot routing is unchanged.

Native objects use the existing vtable fast path and take no registry lock or walk. Child candidates select one lookup in a fixed 256-bucket intrusive registry, keeping the registry allocation-free.

Claimed in #9401 (comment) (child-proxy identity and queue-call lifetime; separate from #7490's console slot routing).

Checked on Windows 11, MSVC 19.36, SDK 10.0.26100: all six existing xs_proxy_mock CTest modes pass; check_no_game_data.py and git diff --check pass. CPU mocks only; no GPU or game files used. No test files changed.

These existing checks are smoke coverage. They do not establish a regression test for reentrant last-reference release or synthetic child-vtable candidates.

AI-assisted: yes.

Independent review: VibeClauding retested 95621062 on Windows / RTX 3070 and reports device_proxy, device_proxy_trace, all six xs_proxy_mock modes and queue_proxy pass. Their separate reentrancy-check commit reportedly fails on main and passes with this fix; it has been reviewed but is not included or independently run in this PR.

@Kadamitas
Kadamitas marked this pull request as ready for review November 23, 2026 21:20

@al3xar al3xar 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.

Independent Windows + GPU + Vulkan validation verification of f6586decf (AI-assisted).

  • Environment: Windows 11 x64, MSVC 19.44.35230.0 (Ninja Release), Windows SDK 10.0.26100.0, NVIDIA GeForce RTX 3060 (driver 621.24), Vulkan SDK 1.4.363.0 (LEONIDA_VK_VALIDATION=1), no game files.
  • Test runs:
    • ctest -R "^(xs_proxy_mock|device_proxy)" --output-on-failure: 8/8 PASS (all 6 xs_proxy_mock* modes + device_proxy + device_proxy_trace).
    • d3d12_multithread_test (LEONIDA_VK_VALIDATION=1): PASS (2 queues x 32 concurrent submissions + resource churn + shared-texture handoff + multi-fence ALL/ANY; 0 validation errors, exit 0).
  • Code review:
    • Allocation-free intrusive live-child registry protected by SRWLOCK avoids resurrecting zero-refcount proxies (pin_live_child CAS loop checks refs != 0), unregisters before external Release calls in destroy_child (including on wrap_child QI failure), and pins both queue and fence across Signal/Wait forwarding without skewing guest-visible AddRef/Release counts.
    • Built with 0 warnings in d3d12_xs_device.cpp.

LGTM.

@milas6669

Copy link
Copy Markdown

Checked head f6586decf merged onto main 867d922f0, clean (1 file, +83/-8). Native Windows 11 26200, MSVC 19.44 RelWithDebInfo, RTX 3070 (Ampere, 621.24), LEONIDA_VK_VALIDATION=1. ctest -R "device_proxy|xs_proxy|queue_proxy|allocator_proxy|d3d12_xs|d3d12_compute$|d3d12_bundle$": 12/12 pass.

New data, a coverage gap: the description says "No test files changed", and that matters here. With main's runtime/shims/d3d12_xs_device.cpp put back and the same 12 tests: also 12/12 pass. So none of the existing tests, with or without this change, exercises what the PR is about (a backend that re-enters Release on the caller's last queue or fence reference during Signal/Wait, and a candidate whose first word merely matches a proxy vtable). The PR's behaviour is therefore unverified by anything in the repository; the regression the description names would not be caught if the pin or the live registry were removed. A mock queue whose Signal calls Release on the proxy it was reached through, and a stack object whose first word is a child vtable passed as the fence argument, would be the two cases to add to xs_proxy_mock_test.cpp.

Merge order: it conflicts with #7290 (fence slot 12) in d3d12_xs_device.cpp (#7290 merges with main alone); it merges cleanly with #8124, #6964, #7066 and #7201.

A cost to look at, which I read from the code and did not measure: pin_live_child, is_live_child and is_child_proxy walk a singly linked list of every live child proxy under one SRWLOCK, and unwrap_object does up to three of those walks per proxied call. That is fine for tens or hundreds of allocators; with thousands of live proxies each queue call becomes thousands of pointer comparisons under a shared lock. A hash table or an intrusive per-kind range would keep it constant.

No defect found in what the tests can run. No game data. AI-assisted (Claude Code).

@VibeClauding

Copy link
Copy Markdown

Tested f6586decf merged onto main 5d74c2723 on native Windows 11 26200, MSVC 14.44, RTX 3070 (NVIDIA 610.88): device_proxy, device_proxy_trace, the six xs_proxy_mock* modes and queue_proxy (real D3D12 queue) all pass, plain and with LEONIDA_VK_VALIDATION=1. No regression.

Two review points:

1. Classification cost for non-proxies. is_child_proxy<K> was one vtable compare and is now a walk of the global live list under the SRW lock. proxy_real_object calls it once per kind, so every pointer that isn't a child proxy (a real resource, heap or list) costs three full walks. That's the common case on its hot callers: UnwrappedArray::fill runs per element of MakeResident/Evict (34/35), SetResidencyPriority (46), EnqueueMakeResident (50) and ExecuteCommandLists, and the list holds every live queue, fence and allocator. A cheap fix that keeps your safety property: proxy_real_object still reads the candidate's first word for the heap check, so it can use that word to pick the one kind whose vtable it matches, then confirm membership with a single registry walk. Non-proxies then cost zero walks, as before, and a stale pointer whose memory happens to hold a child vtable is still rejected by the registry.

2. No test for the reentrancy being fixed. "No test files changed", so nothing fails on main and passes here. xs_proxy_mock_test already fakes the real objects, so a mock queue whose Signal releases the caller's last reference to the queue (and, separately, to the fence) would show the use-after-free on main and the pin keeping both alive here. Running it under the debug CRT heap, or with page heap on, would make the main failure deterministic.

AI-assisted (Claude Code). No game files.

@Kadamitas

Copy link
Copy Markdown
Author

Addressed the classification cost: native objects now take no registry lookup, a matching child vtable selects at most one lookup, and live children are indexed in 256 allocation-free intrusive address buckets. Queue routing and pin lifetime rules are unchanged. The six existing CPU mock modes and hygiene checks still pass.

The coverage limitation is valid: those existing checks do not establish the reentrant-release regression. The PR description now states that explicitly; this change still does not add regression cases.

@VibeClauding

Copy link
Copy Markdown

@Kadamitas thanks for the bucket index. Retested 95621062d on the same RTX 3070: device_proxy, device_proxy_trace, the six xs_proxy_mock* modes and queue_proxy all pass.

For the missing regression case, here's one you can cherry-pick: 79c2e0a85 on VibeClauding:pr9475-reentry-test (one file, runtime/test/xs_proxy_mock_test.cpp, +50; it applies on your head).

  • A g_reenter hook in mock_call runs inside a forwarded call. In check_reentrant_release() it releases the title's only reference to the queue from inside Signal (slot 14), then, in a second round, the only reference to the fence. It samples the real object's count right after that release, still inside the call.
  • It expects the real object to be alive inside the call (refs >= 1) and released exactly once afterwards (refs == 0), and the device to end at zero.
  • Red/green: with this PR's d3d12_xs_device.cpp, it passes 3/3 runs, plus all six xs_proxy_mock* ctest modes. With main's, it fails at g_reenter_refs >= 1: the real queue reaches 0 while its Signal is still running, which is the use-after-free the pins prevent.

AI-assisted (Claude Code). No game files.

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.

5 participants