Skip to content

fix(gpu-d3d12): don't sentinel-0 a slot whose command list was already submitted - #16

Merged
mledour merged 1 commit into
mainfrom
fix/d3d12-signal-uaf
Jun 8, 2026
Merged

mledour merged 1 commit into
mainfrom
fix/d3d12-signal-uaf

Conversation

@mledour

@mledour mledour commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Finding #4 (code review): D3D12 failed-Signal use-after-free

RecordTimestamp runs ExecuteCommandLists (work now in flight on the GPU), then on a failed Signal stored the "never submitted" sentinel (slot.fenceValue = 0) and returned. The next ring wrap's pre-check —

if (slot.fenceValue > 0 && m_fence->GetCompletedValue() < slot.fenceValue) { /* skip, GPU busy */ }

— treats fenceValue == 0 as free, so it Reset()s the slot's command allocator while the GPU may still be executing that command list. That's a use-after-free the D3D12 debug layer flags and release builds can TDR the host over. (Trigger is narrow: a transient Signal failure — E_OUTOFMEMORY — without device removal, then a wrap before the GPU drains.)

Fix

Tag the slot with nextFenceValue unconditionally and gate reuse on the fence (the queue is in-order):

Case Outcome
Transient Signal failure a later frame's Signal advances the fence past nextFenceValueGetCompletedValue() >= nextFenceValue proves the work is done → safe reuse, and PollResolved can still resolve the already-written timestamp
Device removed (TDR) GetCompletedValue() returns UINT64_MAX → reuse allowed, allocator Reset fails harmlessly on the dead device
No later Signal ever lands slot stays Pending, counted as a drop when the ring overwrites it — never a Reset() of in-flight work

This is the same invariant Reset() already relies on (it deliberately keeps non-zero fence values when its bounded drain times out — lines 577-586). The other slot.fenceValue = 0 sites stay correct: init, the pre-submit Reset/Close failure paths, the drain-succeeded branch of Reset(), and shutdown.

Verification

  • The D3D12 backend has no unit test (it needs a real device); covered by the CI build + review, like the rest of gpu_timer.cpp.
  • No lines >120 cols; diff is one file (+27/−13).

🤖 Generated with Claude Code

…y submitted

RecordTimestamp ran ExecuteCommandLists, then on a failed Signal stored the
"never submitted" sentinel (slot.fenceValue = 0) and returned. But the
command list was already in flight on the GPU. The next ring wrap's
pre-check (slot.fenceValue > 0 && GetCompletedValue() < fenceValue) then
treated the slot as free and Reset() its command allocator while the GPU was
still executing it -- a use-after-free the D3D12 debug layer flags and that
release builds can TDR the host over.

Tag the slot with nextFenceValue unconditionally and gate reuse on the fence
(the queue is in-order):
  * transient Signal failure: a later frame's Signal advances the fence past
    nextFenceValue, so GetCompletedValue() >= nextFenceValue proves the work
    is done; the slot reuses safely and PollResolved can still resolve the
    already-written timestamp;
  * device removed (TDR): GetCompletedValue() returns UINT64_MAX, so reuse is
    allowed and the allocator Reset just fails harmlessly on the dead device;
  * no later Signal ever lands: the slot stays Pending and is counted as a
    drop when the ring overwrites it -- never a Reset() of in-flight work.

This is the same invariant Reset() already relies on (it keeps non-zero fence
values when its bounded drain times out). The other slot.fenceValue = 0 sites
remain correct: init, the pre-submit Reset/Close failure paths, the
drain-succeeded branch of Reset(), and shutdown.

The D3D12 backend has no unit test (it needs a real device); covered by the
CI build + review, like the rest of gpu_timer.cpp.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@mledour
mledour merged commit 5599f6d into main Jun 8, 2026
3 checks passed
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