diff options
| author | DenRakEiw <89697885+DenRakEiw@users.noreply.github.com> | 2026-11-23 23:00:06 +0100 |
|---|---|---|
| committer | DenRakEiw <89697885+DenRakEiw@users.noreply.github.com> | 2026-11-23 23:00:06 +0100 |
| commit | 3afcab2a8b5776e60c6e76b5e7ff8af43cb8f4a1 (patch) | |
| tree | 9a6e279fe9999d5373d2bb16054b6452c0e6d09c /runtime/shims/d3d12_com.cpp | |
| parent | 5d74c272363f2703265d44e6aaab1a1df8ddfaf3 (diff) | |
d3d12: enforce cross-device ownership checks on queue, fence, and bundle paths
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index f25168a2a..de27a5ff4 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -254,8 +254,8 @@ public: | |||
| 254 | if (list_.closed()) return; | 254 | if (list_.closed()) return; |
| 255 | if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); | 255 | if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); |
| 256 | GraphicsCommandList* bundle = from(command_list); | 256 | GraphicsCommandList* bundle = from(command_list); |
| 257 | if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ || | 257 | if (!bundle || bundle->attached_device() != attached_device() || |
| 258 | !bundle->list_.closed()) | 258 | bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ || !bundle->list_.closed()) |
| 259 | return check(false); | 259 | return check(false); |
| 260 | // The bundle's state calls and draws play on this list through the members the application would have | 260 | // The bundle's state calls and draws play on this list through the members the application would have |
| 261 | // called, so a draw sees this list's state plus what the bundle set, and what the bundle set stays here | 261 | // called, so a draw sees this list's state plus what the bundle set, and what the bundle set stays here |
| @@ -1433,6 +1433,7 @@ public: | |||
| 1433 | 1433 | ||
| 1434 | bool ok() const { return fence_.semaphore() != VK_NULL_HANDLE; } | 1434 | bool ok() const { return fence_.semaphore() != VK_NULL_HANDLE; } |
| 1435 | VkSemaphore semaphore() const { return fence_.semaphore(); } | 1435 | VkSemaphore semaphore() const { return fence_.semaphore(); } |
| 1436 | const leonida::VulkanContext* context() const { return &context_; } | ||
| 1436 | 1437 | ||
| 1437 | // A Vulkan timeline only increases; D3D12 allows setting a fence lower. Such a signal is reported and dropped. | 1438 | // A Vulkan timeline only increases; D3D12 allows setting a fence lower. Such a signal is reported and dropped. |
| 1438 | // A queue's claim also records the value as one the GPU will signal, which a later CPU signal must not overtake. | 1439 | // A queue's claim also records the value as one the GPU will signal, which a later CPU signal must not overtake. |
| @@ -1606,6 +1607,14 @@ public: | |||
| 1606 | d3d12_trace::record(method, "failed"); | 1607 | d3d12_trace::record(method, "failed"); |
| 1607 | return; | 1608 | return; |
| 1608 | } | 1609 | } |
| 1610 | if (list->attached_device() != attached_device()) { | ||
| 1611 | std::fprintf(stderr, | ||
| 1612 | "[rt] d3d12: ExecuteCommandLists: list %u belongs to a different device; refusing batch\n", | ||
| 1613 | i); | ||
| 1614 | last_result_ = leonida::ReplayResult::unsupported; | ||
| 1615 | d3d12_trace::record(method, "failed"); | ||
| 1616 | return; | ||
| 1617 | } | ||
| 1609 | // A list runs only on a queue of its own type (the debug layer's EXECUTECOMMANDLISTS_COMMANDLISTMISMATCH, | 1618 | // A list runs only on a queue of its own type (the debug layer's EXECUTECOMMANDLISTS_COMMANDLISTMISMATCH, |
| 1610 | // message 836): a DIRECT list's draws would otherwise be recorded for a COPY queue's transfer-only | 1619 | // message 836): a DIRECT list's draws would otherwise be recorded for a COPY queue's transfer-only |
| 1611 | // Vulkan family. The whole batch is refused, as for the other checks here. | 1620 | // Vulkan family. The whole batch is refused, as for the other checks here. |
| @@ -1686,7 +1695,8 @@ public: | |||
| 1686 | HRESULT STDMETHODCALLTYPE Signal(ID3D12Fence* fence, UINT64 value) override { | 1695 | HRESULT STDMETHODCALLTYPE Signal(ID3D12Fence* fence, UINT64 value) override { |
| 1687 | constexpr auto method = "ID3D12CommandQueue::Signal"; | 1696 | constexpr auto method = "ID3D12CommandQueue::Signal"; |
| 1688 | Fence* f = Fence::from(fence); | 1697 | Fence* f = Fence::from(fence); |
| 1689 | if (!f) return d3d12_trace::result(method, E_INVALIDARG); | 1698 | if (!f || f->context() != &context_ || f->attached_device() != attached_device()) |
| 1699 | return d3d12_trace::result(method, E_INVALIDARG); | ||
| 1690 | std::lock_guard lock(mutex_); | 1700 | std::lock_guard lock(mutex_); |
| 1691 | if (!f->claim(value, true)) return d3d12_trace::result(method, S_OK); | 1701 | if (!f->claim(value, true)) return d3d12_trace::result(method, S_OK); |
| 1692 | return d3d12_trace::result(method, queue_.signal(f->semaphore(), value, fence_pin(fence)) ? S_OK : E_FAIL); | 1702 | return d3d12_trace::result(method, queue_.signal(f->semaphore(), value, fence_pin(fence)) ? S_OK : E_FAIL); |
| @@ -1695,7 +1705,8 @@ public: | |||
| 1695 | HRESULT STDMETHODCALLTYPE Wait(ID3D12Fence* fence, UINT64 value) override { | 1705 | HRESULT STDMETHODCALLTYPE Wait(ID3D12Fence* fence, UINT64 value) override { |
| 1696 | constexpr auto method = "ID3D12CommandQueue::Wait"; | 1706 | constexpr auto method = "ID3D12CommandQueue::Wait"; |
| 1697 | Fence* f = Fence::from(fence); | 1707 | Fence* f = Fence::from(fence); |
| 1698 | if (!f) return d3d12_trace::result(method, E_INVALIDARG); | 1708 | if (!f || f->context() != &context_ || f->attached_device() != attached_device()) |
| 1709 | return d3d12_trace::result(method, E_INVALIDARG); | ||
| 1699 | std::lock_guard lock(mutex_); | 1710 | std::lock_guard lock(mutex_); |
| 1700 | queue_.wait(f->semaphore(), value, fence_pin(fence)); | 1711 | queue_.wait(f->semaphore(), value, fence_pin(fence)); |
| 1701 | return d3d12_trace::result(method, S_OK); | 1712 | return d3d12_trace::result(method, S_OK); |
| @@ -1983,15 +1994,21 @@ HRESULT create_fence(leonida::VulkanContext& context, UINT64 initial_value, D3D1 | |||
| 1983 | 1994 | ||
| 1984 | HRESULT set_event_on_multiple_fence_completion(leonida::VulkanContext& context, ID3D12Fence* const* fences, | 1995 | HRESULT set_event_on_multiple_fence_completion(leonida::VulkanContext& context, ID3D12Fence* const* fences, |
| 1985 | const UINT64* values, UINT count, | 1996 | const UINT64* values, UINT count, |
| 1986 | D3D12_MULTIPLE_FENCE_WAIT_FLAGS flags, HANDLE event) { | 1997 | D3D12_MULTIPLE_FENCE_WAIT_FLAGS flags, HANDLE event, |
| 1998 | IUnknown* device) { | ||
| 1987 | if (flags != D3D12_MULTIPLE_FENCE_WAIT_FLAG_ALL && flags != D3D12_MULTIPLE_FENCE_WAIT_FLAG_ANY) | 1999 | if (flags != D3D12_MULTIPLE_FENCE_WAIT_FLAG_ALL && flags != D3D12_MULTIPLE_FENCE_WAIT_FLAG_ANY) |
| 1988 | return E_INVALIDARG; | 2000 | return E_INVALIDARG; |
| 1989 | if (!count || !fences || !values) return E_INVALIDARG; // no fences is refused, as on the host runtime | 2001 | if (!count || !fences || !values) return E_INVALIDARG; // no fences is refused, as on the host runtime |
| 1990 | std::vector<Fence*> objects(count); | 2002 | std::vector<Fence*> objects(count); |
| 1991 | for (UINT i = 0; i < count; ++i) | 2003 | for (UINT i = 0; i < count; ++i) { |
| 1992 | if (!(objects[i] = Fence::from(fences[i]))) return E_INVALIDARG; | 2004 | Fence* const f = Fence::from(fences[i]); |
| 2005 | if (!f || f->context() != &context || (device && f->attached_device() != device)) | ||
| 2006 | return E_INVALIDARG; | ||
| 2007 | objects[i] = f; | ||
| 2008 | } | ||
| 1993 | const bool any = flags == D3D12_MULTIPLE_FENCE_WAIT_FLAG_ANY; | 2009 | const bool any = flags == D3D12_MULTIPLE_FENCE_WAIT_FLAG_ANY; |
| 1994 | if (!event) { | 2010 | if (!event) { |
| 2011 | if (context.is_device_lost()) return DXGI_ERROR_DEVICE_REMOVED; | ||
| 1995 | // One vkWaitSemaphores call: VK_SEMAPHORE_WAIT_ANY_BIT is the ANY flag, no flag is ALL. | 2012 | // One vkWaitSemaphores call: VK_SEMAPHORE_WAIT_ANY_BIT is the ANY flag, no flag is ALL. |
| 1996 | std::vector<VkSemaphore> semaphores(count); | 2013 | std::vector<VkSemaphore> semaphores(count); |
| 1997 | for (UINT i = 0; i < count; ++i) semaphores[i] = objects[i]->semaphore(); | 2014 | for (UINT i = 0; i < count; ++i) semaphores[i] = objects[i]->semaphore(); |
| @@ -2003,7 +2020,10 @@ HRESULT set_event_on_multiple_fence_completion(leonida::VulkanContext& context, | |||
| 2003 | const VkResult result = context.vk.vkWaitSemaphores(context.device(), &wait, UINT64_MAX); | 2020 | const VkResult result = context.vk.vkWaitSemaphores(context.device(), &wait, UINT64_MAX); |
| 2004 | // Unlike a single fence's blocking SetEventOnCompletion, this path invokes Vulkan directly. | 2021 | // Unlike a single fence's blocking SetEventOnCompletion, this path invokes Vulkan directly. |
| 2005 | // A lost device must wake its other registered fence waiters and reject later submissions. | 2022 | // A lost device must wake its other registered fence waiters and reject later submissions. |
| 2006 | if (result == VK_ERROR_DEVICE_LOST) context.mark_device_lost(); | 2023 | if (result == VK_ERROR_DEVICE_LOST) { |
| 2024 | context.mark_device_lost(); | ||
| 2025 | return DXGI_ERROR_DEVICE_REMOVED; | ||
| 2026 | } | ||
| 2007 | return result == VK_SUCCESS ? S_OK : E_FAIL; | 2027 | return result == VK_SUCCESS ? S_OK : E_FAIL; |
| 2008 | } | 2028 | } |
| 2009 | // Each fence's own waiter thread counts the shared waiter down; no thread of its own. | 2029 | // Each fence's own waiter thread counts the shared waiter down; no thread of its own. |