From 10eb178253e9faf0e6be560fa0ef6b4dc866c48e Mon Sep 17 00:00:00 2001 From: Kadamitas <29363268+Kadamitas@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:12:51 -0600 Subject: [PATCH 1/3] d3d12_xs: keep child proxies alive during queue calls (cherry picked from commit f6586decf7e5e0b26c084825b3bd26a760bf6c67) --- runtime/shims/d3d12_xs_device.cpp | 104 ++++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 14 deletions(-) diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index c5f12dc050..4cf4aed3d9 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -80,8 +80,6 @@ #include #include #include -#include -#include #include #include @@ -1195,8 +1193,65 @@ struct ChildProxy { // Queues only: the gate and write fences of list memory waits and writes (d3d12_xs_list_values.h), made at the // first submission that needs them, under queue_order. std::shared_ptr values; + ChildKind kind = ChildKind::allocator; + ChildProxy* live_next = nullptr; // protected by g_live_child_lock; no identity-tracking allocation }; +SRWLOCK g_live_child_lock = SRWLOCK_INIT; +ChildProxy* g_live_children = nullptr; + +void register_live_child(ChildProxy* child) { + AcquireSRWLockExclusive(&g_live_child_lock); + child->live_next = g_live_children; + g_live_children = child; + ReleaseSRWLockExclusive(&g_live_child_lock); +} + +void unregister_live_child(ChildProxy* child) { + AcquireSRWLockExclusive(&g_live_child_lock); + for (auto** at = &g_live_children; *at; at = &(*at)->live_next) { + if (*at != child) continue; + *at = child->live_next; + child->live_next = nullptr; + break; + } + ReleaseSRWLockExclusive(&g_live_child_lock); +} + +// Identity is the live allocation and its kind, not a candidate's first word. Hold the shared lock while a +// reference is acquired so destruction cannot free the allocation; a zero count must never be resurrected. +template +ChildProxy* pin_live_child(const void* candidate) { + ChildProxy* pinned = nullptr; + AcquireSRWLockShared(&g_live_child_lock); + for (auto* child = g_live_children; child; child = child->live_next) { + if (child != candidate || child->kind != K) continue; + auto refs = child->refs.load(std::memory_order_relaxed); + while (refs && refs != (std::numeric_limits::max)()) { + if (child->refs.compare_exchange_weak(refs, refs + 1, std::memory_order_relaxed)) { + pinned = child; + break; + } + } + break; + } + ReleaseSRWLockShared(&g_live_child_lock); + return pinned; +} + +template +bool is_live_child(const void* candidate) { + bool found = false; + AcquireSRWLockShared(&g_live_child_lock); + for (const auto* child = g_live_children; child; child = child->live_next) { + if (child != candidate || child->kind != K) continue; + found = child->refs.load(std::memory_order_relaxed) != 0; + break; + } + ReleaseSRWLockShared(&g_live_child_lock); + return found; +} + template struct ChildStats { static constexpr unsigned kSlots = K == ChildKind::queue ? kQueueSlots @@ -1523,6 +1578,7 @@ std::uint64_t fence_completed_value_address(ChildProxy* self, std::uint64_t valu // reference keeps the fence alive, the event stays open (one handle per such fence) and a later completion sets // that event, not a closed or reused handle. void destroy_child(ChildProxy* self) { + unregister_live_child(self); // no foreign Release callback runs under the registry lock IUnknown* real = self->real; DeviceProxy* device = self->device; FenceMirror* mirror = self->mirror.load(std::memory_order_acquire); @@ -1551,6 +1607,12 @@ void destroy_child(ChildProxy* self) { if (device) proxy_release(device); } +unsigned long release_child_reference(ChildProxy* self) { + const unsigned long refs = self->refs.fetch_sub(1, std::memory_order_acq_rel) - 1; + if (refs == 0) destroy_child(self); + return refs; +} + template unsigned long child_add_ref(ChildProxy* self) { note_child_slot(1); @@ -1560,9 +1622,7 @@ unsigned long child_add_ref(ChildProxy* self) { template unsigned long child_release(ChildProxy* self) { note_child_slot(2); - const unsigned long refs = self->refs.fetch_sub(1, std::memory_order_acq_rel) - 1; - if (refs == 0) destroy_child(self); - return refs; + return release_child_reference(self); } // The interfaces the proxy answers: the base chain and the object's own interface. @@ -1653,7 +1713,7 @@ IUnknown* list_real_or_null(const void* object); template bool is_child_proxy(const void* object) { - return object && *reinterpret_cast(object) == reinterpret_cast(get_child_vtable()); + return is_live_child(object); } // The real object behind a heap, queue, fence or list proxy, or null when `object` is none of them. @@ -1661,9 +1721,8 @@ IUnknown* proxy_real_object(const void* object) { if (!object) return nullptr; void* const vtable = *reinterpret_cast(object); if (vtable == reinterpret_cast(get_heap_vtable())) return static_cast(object)->real; - if (vtable == reinterpret_cast(get_child_vtable()) || - vtable == reinterpret_cast(get_child_vtable()) || - vtable == reinterpret_cast(get_child_vtable())) + if (is_child_proxy(object) || is_child_proxy(object) || + is_child_proxy(object)) return static_cast(object)->real; if (IUnknown* real = list_real_or_null(object)) return real; return nullptr; @@ -2467,6 +2526,20 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_ return fence_completed_value_address(self, a1, a2, a3); } else { note_child_slot(Slot); + // A reentrant Release can drop the caller's last reference while this call is still forwarding. + // The internal references also contribute to AddRef and Release return counts until this call returns. + using Pin = std::unique_ptr; + Pin queue_pin(nullptr, &release_child_reference), fence_pin(nullptr, &release_child_reference); + if constexpr (K == ChildKind::queue) { + queue_pin.reset(pin_live_child(self)); + if (!queue_pin) return hr_to_u64(E_INVALIDARG); + if constexpr (Slot == kQueueSignal || Slot == kQueueWait || Slot == 16 || Slot == 17 || + Slot == 23 || Slot == 25) + fence_pin.reset(pin_live_child(reinterpret_cast(a1))); + } + const auto fence_argument = [&]() { + return fence_pin ? fence_pin->real : proxy_real_object(reinterpret_cast(a1)); + }; std::unique_lock queue_order; // Both queue-order fixes: only work that orders on the queue takes queue_order. The markers do not, and // neither does slot 18 - an ExecuteCommandListsX holds it for its whole replay, which can include building a @@ -2479,7 +2552,7 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_ if constexpr (K == ChildKind::queue) { bool orders = queue_slot_orders_work(Slot); if (orders && (Slot == 16 || Slot == 17)) - orders = proxy_real_object(reinterpret_cast(a1)) != nullptr; + orders = fence_argument() != nullptr; if (orders && (Slot == 19 || Slot == 20)) { const unsigned char own = self->queue_layout.load(std::memory_order_relaxed); const bool newer = own == ChildProxy::kQueueLayoutNewer || @@ -2547,7 +2620,7 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_ return own == ChildProxy::kQueueLayoutUnknown && shared == ChildProxy::kQueueLayoutNewer; }(); if (Slot == 16 || Slot == 17) { - if (IUnknown* fence = proxy_real_object(reinterpret_cast(a1))) { + if (IUnknown* fence = fence_argument()) { self->queue_layout.store(ChildProxy::kQueueLayoutNewer, std::memory_order_relaxed); self->device->console_layout.store(ChildProxy::kQueueLayoutNewer, std::memory_order_relaxed); @@ -2563,7 +2636,7 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_ self->device->console_layout.store(ChildProxy::kQueueLayoutOlder, std::memory_order_relaxed); } - } else if ((Slot == 14 || Slot == 15) && !proxy_real_object(reinterpret_cast(a1)) && + } else if ((Slot == 14 || Slot == 15) && !fence_argument() && newer) { return hr_to_u64(S_OK); // the newer layout's two inserted console methods, unmeasured } @@ -2587,7 +2660,7 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_ // alternative is the fence itself carrying the layout evidence, per the comment above; a // proven-older queue still leaves slot 25 to the console-only no-op, since its fence-taking // methods sit at 14/15/23 there. - if (IUnknown* fence = proxy_real_object(reinterpret_cast(a1))) { + if (IUnknown* fence = fence_argument()) { if (Slot == 25u && !newer) { self->queue_layout.store(ChildProxy::kQueueLayoutNewer, std::memory_order_relaxed); @@ -2687,7 +2760,8 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_ self->real, reinterpret_cast(a1))); } } - if constexpr (K == ChildKind::queue && (Slot == kQueueSignal || Slot == kQueueWait)) a1 = unwrap_object(a1); + if constexpr (K == ChildKind::queue && (Slot == kQueueSignal || Slot == kQueueWait)) + a1 = fence_pin ? reinterpret_cast(fence_pin->real) : unwrap_object(a1); UnwrappedArray unwrapped_lists; if constexpr (K == ChildKind::queue && Slot == 10) { if (!unwrapped_lists.fill(a2, a1)) return hr_to_u64(E_OUTOFMEMORY); @@ -2783,7 +2857,9 @@ HRESULT wrap_child(IUnknown* created, DeviceProxy* device, const IID* riid, void proxy->slot_count = kAllocatorPublicSlots; } proxy->refs.store(1, std::memory_order_relaxed); + proxy->kind = K; proxy_add_ref(device); // the object keeps its Xbox-ABI device alive for GetDevice + register_live_child(proxy); // before QueryInterface publishes the output pointer const HRESULT hr = child_query_interface(proxy, riid, out); if (FAILED(hr)) { From 038980a3875a34238a60cc79ac7df15f82800310 Mon Sep 17 00:00:00 2001 From: Kadamitas <29363268+Kadamitas@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:36:05 -0600 Subject: [PATCH 2/3] d3d12_xs: avoid full registry walks when unwrapping objects (cherry picked from commit 95621062dfe5a3d5337e9f7d789d25faea8b53cf) --- runtime/shims/d3d12_xs_device.cpp | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 4cf4aed3d9..26dd5e7f70 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -1198,18 +1198,25 @@ struct ChildProxy { }; SRWLOCK g_live_child_lock = SRWLOCK_INIT; -ChildProxy* g_live_children = nullptr; +constexpr std::size_t kLiveChildBuckets = 256; +ChildProxy* g_live_children[kLiveChildBuckets]{}; + +std::size_t live_child_bucket(const void* object) { + const auto address = reinterpret_cast(object); + return ((address >> 4) ^ (address >> 12)) & (kLiveChildBuckets - 1); +} void register_live_child(ChildProxy* child) { AcquireSRWLockExclusive(&g_live_child_lock); - child->live_next = g_live_children; - g_live_children = child; + auto& head = g_live_children[live_child_bucket(child)]; + child->live_next = head; + head = child; ReleaseSRWLockExclusive(&g_live_child_lock); } void unregister_live_child(ChildProxy* child) { AcquireSRWLockExclusive(&g_live_child_lock); - for (auto** at = &g_live_children; *at; at = &(*at)->live_next) { + for (auto** at = &g_live_children[live_child_bucket(child)]; *at; at = &(*at)->live_next) { if (*at != child) continue; *at = child->live_next; child->live_next = nullptr; @@ -1224,7 +1231,7 @@ template ChildProxy* pin_live_child(const void* candidate) { ChildProxy* pinned = nullptr; AcquireSRWLockShared(&g_live_child_lock); - for (auto* child = g_live_children; child; child = child->live_next) { + for (auto* child = g_live_children[live_child_bucket(candidate)]; child; child = child->live_next) { if (child != candidate || child->kind != K) continue; auto refs = child->refs.load(std::memory_order_relaxed); while (refs && refs != (std::numeric_limits::max)()) { @@ -1243,7 +1250,7 @@ template bool is_live_child(const void* candidate) { bool found = false; AcquireSRWLockShared(&g_live_child_lock); - for (const auto* child = g_live_children; child; child = child->live_next) { + for (const auto* child = g_live_children[live_child_bucket(candidate)]; child; child = child->live_next) { if (child != candidate || child->kind != K) continue; found = child->refs.load(std::memory_order_relaxed) != 0; break; @@ -1721,8 +1728,14 @@ IUnknown* proxy_real_object(const void* object) { if (!object) return nullptr; void* const vtable = *reinterpret_cast(object); if (vtable == reinterpret_cast(get_heap_vtable())) return static_cast(object)->real; - if (is_child_proxy(object) || is_child_proxy(object) || - is_child_proxy(object)) + // Native objects take no registry lock or walk. A child-vtable match only selects the one identity lookup; + // matching bytes alone still do not establish that the candidate is a live child proxy. + if ((vtable == reinterpret_cast(get_child_vtable()) && + is_child_proxy(object)) || + (vtable == reinterpret_cast(get_child_vtable()) && + is_child_proxy(object)) || + (vtable == reinterpret_cast(get_child_vtable()) && + is_child_proxy(object))) return static_cast(object)->real; if (IUnknown* real = list_real_or_null(object)) return real; return nullptr; From 53ed711b85694298f3cc3f8a8913601ceb91918f Mon Sep 17 00:00:00 2001 From: Kadamitas <29363268+Kadamitas@users.noreply.github.com> Date: Tue, 24 Nov 2026 09:37:04 -0600 Subject: [PATCH 3/3] xs proxy mock: check queue and fence ownership during forwarding --- runtime/test/xs_proxy_mock_test.cpp | 30 ++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index e808256231..7735c4112c 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -117,6 +117,9 @@ struct Mock { Mock* completes_with; // fence: once its completed value reaches completes_at, a GetCompletedValue first std::uint64_t completes_at, completes_value; // signals that fence to completes_value (GPU order across queues) bool signals_complete; // queue: a Signal (slot 14) completes the fence at once, as an idle GPU would + const void* observed_queue; // optional proxy pair to sample during a forwarded fence call + const void* observed_fence; + unsigned long observed_queue_refs, observed_fence_refs; unsigned calls[kMockSlots]; std::uint64_t args[kMockSlots][6]; std::uint64_t array[kMockSlots][4]; // device: the first entries of the array argument, as received @@ -581,6 +584,10 @@ std::uint64_t mock_call(Mock* m, unsigned slot, const std::uint64_t* words) { if (slot == 48 && mock_is_null_rtv(static_cast(a1))) m->list_error = true; // ClearRenderTargetView if (slot == 80) mock_barrier(m, a1, a2); // Barrier } + if (m->kind == Kind::queue && (slot == 14 || slot == 15) && m->observed_queue) { + m->observed_queue_refs = xs::command_object_proxy_ref_count(m->observed_queue); + m->observed_fence_refs = xs::command_object_proxy_ref_count(m->observed_fence); + } return S_OK; } @@ -3288,9 +3295,16 @@ int check_tail(bool want_guarded) { EXPECT(queue && fence); EXPECT(xs::command_object_tables_guarded() == want_guarded); Mock* const queue_mock = reinterpret_cast(xs::queue_proxy_real_queue(queue)); - // Signal (14) still reaches the real queue, with the real fence. - EXPECT(hr_of(raw(queue, 14, word(fence), 5)) == S_OK && queue_mock->calls[14] == 1 && - queue_mock->args[14][0] == word(xs::fence_proxy_real_fence(fence)) && queue_mock->args[14][1] == 5); + // Both wrappers keep a reference until the native call returns. + queue_mock->observed_queue = queue; + queue_mock->observed_fence = fence; + for (unsigned slot : {14u, 15u}) { + queue_mock->observed_queue_refs = queue_mock->observed_fence_refs = 0; + EXPECT(hr_of(raw(queue, slot, word(fence), 5)) == S_OK && queue_mock->calls[slot] == 1 && + queue_mock->args[slot][0] == word(xs::fence_proxy_real_fence(fence)) && queue_mock->args[slot][1] == 5); + EXPECT(queue_mock->observed_queue_refs == 2 && queue_mock->observed_fence_refs == 2); + EXPECT(xs::command_object_proxy_ref_count(queue) == 1 && xs::command_object_proxy_ref_count(fence) == 1); + } // Console-only queue methods (the tail past the PC chain, SignalX, PresentX) report S_OK instead of // refusing: frame pacing flows through them. Nothing is forwarded — the real queue never sees them. for (unsigned slot = 19; slot < xs::queue_proxy_table_size(); ++slot) { @@ -3299,6 +3313,16 @@ int check_tail(bool want_guarded) { } EXPECT(raw(fence, 12) != 0); // the completed value's address (check_fence_completed_value) for (unsigned slot = 13; slot < xs::fence_proxy_table_size(); ++slot) EXPECT(hr_of(raw(fence, slot)) == E_NOTIMPL); + for (unsigned slot : {16u, 17u, 25u}) { + const unsigned native_slot = slot == 17u ? 15u : 14u; + const unsigned expected_calls = queue_mock->calls[native_slot] + 1; + queue_mock->observed_queue_refs = queue_mock->observed_fence_refs = 0; + EXPECT(hr_of(raw(queue, slot, word(fence), 6)) == S_OK && queue_mock->calls[native_slot] == expected_calls && + queue_mock->args[native_slot][0] == word(xs::fence_proxy_real_fence(fence)) && + queue_mock->args[native_slot][1] == 6); + EXPECT(queue_mock->observed_queue_refs == 2 && queue_mock->observed_fence_refs == 2); + EXPECT(xs::command_object_proxy_ref_count(queue) == 1 && xs::command_object_proxy_ref_count(fence) == 1); + } EXPECT(xs::queue_table_overruns() == 0 && xs::fence_table_overruns() == 0); EXPECT(release(queue) == 0 && release(fence) == 0 && release(device) == 0 && mock->refs == 0); return 0;