Skip to content

d3d12: fix barrier null-pointer crashes and validate barrier arguments - #5814

Open
DenRakEiw wants to merge 2 commits into
ItsNotRexed:mainfrom
DenRakEiw:fix/d3d12-barrier-validation-parity
Open

DenRakEiw wants to merge 2 commits into
ItsNotRexed:mainfrom
DenRakEiw:fix/d3d12-barrier-validation-parity

Conversation

@DenRakEiw

Copy link
Copy Markdown
Contributor

Claim: #5581 (comment)

Summary

Fixes null-pointer dereference crashes and tightens D3D12 argument validation parity in ID3D12GraphicsCommandList::ResourceBarrier and ID3D12GraphicsCommandList7::Barrier (
untime/shims/d3d12_com.cpp,
untime/src/command_list.h), verified against native Windows D3D12 on NVIDIA GeForce RTX 5090 and WARP. Does not touch COPY/COMPUTE list-type restrictions (#5252) or �ulkan_replay.h.

Changes

  • Null-pointer crash fixes (
    untime/shims/d3d12_com.cpp)
    :
    • Reject ResourceBarrier(count > 0, nullptr) and Barrier(groups > 0, nullptr) via check(false) instead of dereferencing null pointers; keep count == 0, nullptr / groups == 0, nullptr as valid no-ops on open lists (and reject on closed lists).
    • Inside Barrier, validate D3D12_BARRIER_TYPE before NumBarriers == 0 (matching Windows D3D12, which rejects unknown types even when NumBarriers == 0), and reject NumBarriers > 0 with null pGlobalBarriers / pBufferBarriers / pTextureBarriers.
    • Reject D3D12_BUFFER_BARRIER with non-zero Offset.
  • Barrier argument validation (
    untime/src/command_list.h)
    :
    • TransitionBarrier: reject StateBefore == StateAfter, unknown D3D12_RESOURCE_STATES bits, and write-state bits combined with any other state bit.
    • Enhanced GlobalBarrier / BufferBarrier / TextureBarrier: reject unknown D3D12_BARRIER_SYNC and D3D12_BARRIER_ACCESS bits, NO_ACCESS combined with other access bits, SYNC_NONE with Access != NO_ACCESS, incompatible SYNC/ACCESS pairings, and unknown D3D12_BARRIER_LAYOUT values.
  • Tests (
    untime/test/d3d12_barrier_validation_test.cpp,
    untime/test/command_list_test.cpp,
    untime/test/d3d12_close_parity_test.cpp,
    untime/CMakeLists.txt)
    :
    • Added d3d12_barrier_validation_test covering 10 layer-only null-pointer regression cases, 33 host (RTX 5090) + WARP Close() parity cases, and valid legacy + enhanced barrier GPU replay.
    • Updated 3 previously Known::layer_looser transition barrier rows in d3d12_close_parity_test.cpp to Known::match.

Verification (Windows 11, NVIDIA GeForce RTX 5090, LEONIDA_VK_VALIDATION=1)

All tests built and passed with 0 failures and 0 Vulkan validation errors:

  • d3d12_barrier_validation_test: 10/10 null-pointer cases, 33/33 HW (RTX 5090) parity, 33/33 WARP parity, GPU replay passed (0 validation errors)
  • command_list_test: passed
  • d3d12_state_map_test: passed
  • d3d12_gcl8_test: passed
  • d3d12_com_test: passed (0 validation errors)
  • d3d12_vulkan_device_test: passed (0 validation errors)
  • d3d12_close_parity_test: 100 cases, 68 match WARP (+3), 0 failures (0 validation errors)
  • ctest: 6/6 passed (command_list, d3d12_com, d3d12_barrier_validation, d3d12_gcl8, d3d12_vulkan_device, d3d12_close_parity), git diff --check clean.

AI-assisted (Gemini 4 Neon).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 0b8b354 on NVIDIA GeForce RTX 4080, driver 617.42: d3d12_barrier_validation_test exits 0 with "layer null-pointer regression: 10 cases, 0 failures", "parity matrix: 33 cases (HW compared=33, WARP compared=33), 0 failures" and "GPU replay (legacy + enhanced barriers): passed". d3d12_close_parity_test exits 0 with "close parity: 100 cases, 68 match WARP, 20 known layer-stricter, 12 known layer-looser, 0 not run, 0 where WARP differs from the measured column, 0 failures". d3d12_com_test and d3d12_state_map_test exit 0 with their summary lines, and command_list_test and d3d12_vulkan_device_test exit 0 with no summary. No validation layer was loaded and d3d12_gcl8_test was not run.

AI-assisted.

@SirKingg

Copy link
Copy Markdown
Contributor

Independent synthetic check of head 0b8b35405a4d094c3da995103d459ca07513ab81 on Windows 11 Pro 26300, RTX 4070 Ti, NVIDIA 610.88, MSVC 19.44, Vulkan SDK 1.4.363.0.

Distinct from the RTX 4080 report: I submitted seven individual valid barrier-only command lists to each of native NVIDIA D3D12, WARP and the Vulkan-backed layer, each in a fresh bounded process. All 21 closed successfully and their fences completed: legacy COMMON -> COPY_DEST, legacy COMMON -> COPY_SOURCE|PIXEL_SHADER_RESOURCE, enhanced global NONE/NO_ACCESS -> ALL/COMMON, buffer COPY/COPY_DEST -> COMPUTE/UAV, discarded texture UNDEFINED -> RENDER_TARGET, global ALL_SHADING/(VERTEX_BUFFER|CONSTANT_BUFFER) -> DRAW/INDEX_BUFFER, and global RESOLVE/RESOLVE_DEST -> COPY/COPY_SOURCE. These controls did not find a rejected valid combination; this is not exhaustive.

On the layer, LEONIDA_VK_VALIDATION=1 enabled VK_LAYER_KHRONOS_validation (loader confirmed it was inserted, including synchronization validation); no validation errors were reported for the seven submissions or the PR's separately submitted valid replay. The PR's layer-only null-pointer cases passed 10/10; command_list_test also passed. No game files.

Limits: the Windows D3D12 debug layer was unavailable, so native/WARP acceptance is not debug-layer validation. The lists only contain barriers, not data readback or functional rendering. I deliberately did not run the full 33-case host matrix or submit invalid native calls, to avoid potentially unsafe driver calls; these results do not independently establish invalid-argument parity. No cases in the seven-case submitted set were skipped; no timeouts or discrepancies observed.

AI-assisted.

@Flyander

Flyander commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

I found a remaining invalid texture-barrier combination in the current head. Source-level repro after list.reset() (using the existing y, a, l, and all test aliases):

CHECK(list.barrier(TextureBarrier{y::all, y::render_target, a::common, a::render_target,
                                  l::undefined, l::render_target, 9, all, texture_barrier_discard}));

The current predicates accept and append it: valid_access(y::all, a::common) and valid_access(y::render_target, a::render_target) both pass, both layouts are recognized, and the discard flag is allowed. But the D3D12 Enhanced Barriers spec requires the access matching a sole UNDEFINED layout to be NO_ACCESS; here AccessBefore is COMMON. The existing valid UNDEFINED -> RENDER_TARGET test uses SYNC_NONE/NO_ACCESS, so this invalid pairing is uncovered. Please reject the mismatch and add a regression assertion.

I could not run the C++ target locally because CMake, Ninja, and MSVC/clang-cl are unavailable.

@Flyander

Flyander commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Separate compatibility regression from the UNDEFINED/access pairing above: valid_layout() caps values at video_queue_common (30), but value 31 is the valid D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_GENERIC_READ_COMPUTE_QUEUE_ACCESSIBLE enumerant. The spec defines it as D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_COPY_DEST + 2 and says it is usable on both direct and compute queues for generic-read access: https://microsoft.github.io/DirectX-Specs/d3d/D3D12EnhancedBarriers.html#d3d12_barrier_layout_direct_queue_generic_read_compute_queue_accessible

Repro with the current test aliases after list.reset():

CHECK(list.barrier(TextureBarrier{y::all, y::all, a::shader_resource, a::shader_resource,
                                  31u, 31u, 9, all, 0}));

This returns false solely because valid_layout(31) compares it against 30; the pre-PR command list accepted the barrier and replay maps unhandled read layouts to VK_IMAGE_LAYOUT_GENERAL. Please include the valid layout in the whitelist and add this acceptance case.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 0b8b354 on Intel UHD Graphics 770, driver 32.0.101.7088, MSVC 19.44, with VK_LAYER_KHRONOS_validation loaded (the loader prints "adding layers VK_LAYER_KHRONOS_validation"): command_list_test, d3d12_com_test, d3d12_barrier_validation_test, d3d12_close_parity_test, d3d12_state_map_test, d3d12_gcl8_test and d3d12_vulkan_device_test build and exit 0, with 0 "Validation Error" lines in their output. d3d12_barrier_validation_test prints "layer null-pointer regression: 10 cases, 0 failures" and "parity matrix: 33 cases (HW compared=14, WARP compared=33), 0 failures", so the hardware leg compares 14 of 33 on this GPU. d3d12_close_parity_test prints "close parity: 100 cases, 68 match WARP, 20 known layer-stricter, 12 known layer-looser, 0 not run, 0 where WARP differs from the measured column, 0 failures". command_list_test, d3d12_state_map_test and d3d12_gcl8_test print no device line, so for them the exit code is the only signal. d3d12_vulkan_device_test prints "[rt] d3d12: ExecuteCommandLists failed: invalid query; nothing was submitted" on stderr and still exits 0; I did not run main, so I cannot say whether that line is new. ctest was not run.

AI-assisted.

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

Reviewed at 0b8b35405a on Windows 11, RTX 5070 Ti, VS2022/Ninja, Vulkan 1.4.363.

  • Built with -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTING=ON -DLEONIDA_BUILD_WINDOWS_RUNTIME=ON, ran with LEONIDA_VK_DEVICE='RTX 5070 Ti' and LEONIDA_VK_VALIDATION=1.
  • d3d12_barrier_validation_test.exe: exit 0. 10/10 null-pointer cases, 33/33 hardware parity, 33/33 WARP parity, GPU replay passed; 0 failures.
  • ctest -R '^(command_list|d3d12_com|d3d12_barrier_validation|d3d12_gcl8|d3d12_vulkan_device|d3d12_close_parity)$': 6/6 passed. close parity: 100 cases, 68 match WARP (+3), 0 failures.
  • Negative control: the same test built against main 561b65a83 faults with 0xC0000005 (access violation) on the first ResourceBarrier(1, nullptr) case; this branch returns the D3D12 error instead.
  • Reviewed the new state/access/layout checks: bit sets match the SDK enums and the measured matrix.

Verdict: LGTM. It fixes the crash and the hardware/WARP parity holds on this GPU.

@DenRakEiw

Copy link
Copy Markdown
Contributor Author

Thanks @Flyander for both catches! Addressed in commit fba9bb7f2:

  1. UNDEFINED layout requires NO_ACCESS: CommandList::barrier(const TextureBarrier&) now rejects layout_before == undefined && layout_after != undefined && access_before != no_access (while still allowing UNDEFINED -> UNDEFINED no-layout-change barriers, matching both native Windows D3D12 HW and WARP). Added your exact regression assertion in runtime/test/command_list_test.cpp and HW/WARP parity cases in runtime/test/d3d12_barrier_validation_test.cpp.
  2. Layout 31u (D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_GENERIC_READ_COMPUTE_QUEUE_ACCESSIBLE): Added d3d12_layout::direct_queue_generic_read_compute_queue_accessible = 31 and updated valid_layout() to accept it, with your acceptance assertion in runtime/test/command_list_test.cpp.

Verification (Windows 11, NVIDIA GeForce RTX 5090, LEONIDA_VK_VALIDATION=1, 0 Vulkan validation errors)

  • command_list_test: PASS (exit 0)
  • d3d12_barrier_validation_test: PASS (exit 0; 10/10 null-pointer regression, 35/35 HW + WARP parity cases, GPU replay passed)
  • d3d12_close_parity_test: PASS (exit 0)
  • d3d12_com_test: PASS (exit 0)
  • d3d12_gcl8_test: PASS (exit 0)
  • d3d12_vulkan_device_test: PASS (exit 0)

AI-assisted (Gemini 4 Neon).

@CryingLaughing

Copy link
Copy Markdown
Contributor

Independent barrier retest at fba9bb7f on Windows 11 build 26200, RTX 4080, NVIDIA 621.01, MSVC 19.44 RelWithDebInfo and Khronos validation 1.4.357. Seven CTests passed: command_list, d3d12_barrier_validation, d3d12_close_parity, d3d12_com, d3d12_gcl8, d3d12_vulkan_device, and d3d12_state_map. All 10 null-pointer regression cases passed. The 35-case barrier matrix compared all 35 with native hardware and all 35 with WARP, with zero failures. Both legacy and enhanced barrier replay passed on Vulkan. The separate Close() parity suite ran all 100 cases with zero unexpected failures. No Vulkan validation errors. Synthetic inputs only. AI-assisted with Codex.

@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested fba9bb7 on Windows 11, AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32029.2010: pass

  • The head moved during my run from 0b8b354; the result is for fba9bb7 (the later fixes are in).
  • d3d12_barrier_validation_test: "layer null-pointer regression: 10 cases, 0 failures", "parity matrix: 35 cases (HW compared=35, WARP compared=35), 0 failures", "GPU replay (legacy + enhanced barriers): passed". The host comparison ran in full on this GPU (35 of 35).
  • Also pass with validation on: command_list_test, d3d12_com_test, d3d12_close_parity_test (100 cases, 68 match WARP, 20 stricter, 12 looser, 0 failures), d3d12_state_map_test, d3d12_gcl8_test, d3d12_vulkan_device_test.
  • Negative control: command_list.h and d3d12_com.cpp at main on top of the PR head. d3d12_barrier_validation_test crashes (exit -1073741819, 0xC0000005) and d3d12_close_parity_test reports 65 match and 3 failures. So the new tests do catch the old behaviour.
  • Not done: no other barrier types than the matrix covers; the 12 "looser" close cases were not read one by one.

AI-assisted (Claude Code).

@sennecools

Copy link
Copy Markdown

Adding to @Jadiac5's RX 9070 XT run at fba9bb7f: with the Vulkan layer on the 9800X3D iGPU (RDNA2 gfx1036, AMD 32.0.21045) and native D3D12 still on the 9070 XT, d3d12_barrier_validation_test gives the same result: 10/10 null-pointer cases, parity 35/35 HW and 35/35 WARP, GPU replay passed, no validation messages.

AI-assisted.

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.

8 participants