D3D12.X: check pointer alignment in is_adopted before dereferencing (#1130) #1130

Closed
GamingChelsea wants to merge 1 commit from upstream-pr/1139 into main
GamingChelsea commented 2026-11-24 12:24:51 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by GamingChelsea on 2026-11-24T12:24:51Z.


Fixes #1130.

d3d12x::real::is_adopted previously dereferenced *(obj as *const usize) without checking pointer alignment. Because this_first(a, b) probes both arguments of struct-returning device hooks — including GetCustomHeapProperties (slot 26, returning [u32; 5], 4-byte aligned) — passing a 4-byte-aligned return buffer caused undefined behavior and aborted debug builds with a misaligned pointer dereference panic.

What is in it

  • crates/xcl/src/hle/d3d12x/real.rs: require pointer alignment ((obj as usize) & (std::mem::align_of::<usize>() - 1) == 0) in is_adopted before readable() and the vtable read (adopted COM objects are always pointer-aligned); add unit test this_first_tests::misaligned_return_buffer_is_not_adopted_and_does_not_panic covering 4-byte-aligned stack buffers (including one containing a stale vtable copy).
  • crates/xcl/src/hle/d3d12x/device_x.rs: cache real::this_first(a, b) once in get_resource_allocation_info2_x; add GPU host-driver parity test custom_heap_properties_match_host_in_cpp_and_c_abi verifying get_custom_heap_properties_x in both C++ (this, ret, ...) and C (ret, this, ...) ABI orders with a 4-byte-aligned [u32; 5] return buffer.

Tested

  • Hardware: NVIDIA GeForce RTX 4070 (driver 32.0.16.2124, Wave32), Intel Core i7-14700K, Windows 11 (10.0.26300.0), rustc 1.100.0 (0ef1bc386 2026-11-09), debug (tests) and release (smoke).
  • cargo test --workspace -- --test-threads=1: 1437 passed, 0 failed, 47 ignored.
  • SKIPPED GPU tests on this adapter: 25 (22 wave64-only no wave64 skips + 3 #1426 f16/f32 direct-DXIL per-lane skips).

Regression run
main:

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 4070
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 858 (PresentX 858)
presents per 2 s (last 3): 58 59 58
device removals: 0
crashes: 0
peak commit: 20010 MB

PR head:

xcl-smoke v1
commit: e2ebd0c8f68be944505132e0d02c319f7611e4ad
gpu: NVIDIA GeForce RTX 4070
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1027 (PresentX 1028)
presents per 2 s (last 3): 41 41 51
device removals: 0
crashes: 0
peak commit: 19084 MB

Not tested

  • AMD / Wave64 hardware (wave64-only GPU tests were skipped on the RTX 4070).
  • stable-x86_64-pc-windows-gnullvm toolchain (tested on stable-x86_64-pc-windows-msvc).

AI use

  • Antigravity with Gemini 4 Neon: implemented the alignment check and this_first caching, wrote the unit and host-driver ABI parity tests, rebased onto b49be18, ran the workspace test suite and smoke regression, and updated this PR description.
<!-- greyfinch-sync:ivall/xcl:issue:5472095908 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1139). Originally posted by [GamingChelsea](https://github.com/GamingChelsea) on 2026-11-24T12:24:51Z. --- Fixes #1130. `d3d12x::real::is_adopted` previously dereferenced `*(obj as *const usize)` without checking pointer alignment. Because `this_first(a, b)` probes both arguments of struct-returning device hooks — including `GetCustomHeapProperties` (slot 26, returning `[u32; 5]`, 4-byte aligned) — passing a 4-byte-aligned return buffer caused undefined behavior and aborted debug builds with a misaligned pointer dereference panic. **What is in it** - `crates/xcl/src/hle/d3d12x/real.rs`: require pointer alignment (`(obj as usize) & (std::mem::align_of::<usize>() - 1) == 0`) in `is_adopted` before `readable()` and the vtable read (adopted COM objects are always pointer-aligned); add unit test `this_first_tests::misaligned_return_buffer_is_not_adopted_and_does_not_panic` covering 4-byte-aligned stack buffers (including one containing a stale vtable copy). - `crates/xcl/src/hle/d3d12x/device_x.rs`: cache `real::this_first(a, b)` once in `get_resource_allocation_info2_x`; add GPU host-driver parity test `custom_heap_properties_match_host_in_cpp_and_c_abi` verifying `get_custom_heap_properties_x` in both C++ `(this, ret, ...)` and C `(ret, this, ...)` ABI orders with a 4-byte-aligned `[u32; 5]` return buffer. **Tested** - Hardware: NVIDIA GeForce RTX 4070 (driver 32.0.16.2124, Wave32), Intel Core i7-14700K, Windows 11 (10.0.26300.0), `rustc 1.100.0 (0ef1bc386 2026-11-09)`, debug (tests) and release (smoke). - `cargo test --workspace -- --test-threads=1`: 1437 passed, 0 failed, 47 ignored. - `SKIPPED` GPU tests on this adapter: 25 (22 wave64-only `no wave64` skips + 3 `#1426` f16/f32 direct-DXIL per-lane skips). **Regression run** main: ``` xcl-smoke v1 commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7 gpu: NVIDIA GeForce RTX 4070 title: 3c290c16 seconds: 60 exit: 0x0 frames shown: 858 (PresentX 858) presents per 2 s (last 3): 58 59 58 device removals: 0 crashes: 0 peak commit: 20010 MB ``` PR head: ``` xcl-smoke v1 commit: e2ebd0c8f68be944505132e0d02c319f7611e4ad gpu: NVIDIA GeForce RTX 4070 title: 3c290c16 seconds: 60 exit: 0x0 frames shown: 1027 (PresentX 1028) presents per 2 s (last 3): 41 41 51 device removals: 0 crashes: 0 peak commit: 19084 MB ``` **Not tested** - AMD / Wave64 hardware (wave64-only GPU tests were skipped on the RTX 4070). - `stable-x86_64-pc-windows-gnullvm` toolchain (tested on `stable-x86_64-pc-windows-msvc`). **AI use** - Antigravity with Gemini 4 Neon: implemented the alignment check and `this_first` caching, wrote the unit and host-driver ABI parity tests, rebased onto `b49be18`, ran the workspace test suite and smoke regression, and updated this PR description.
towiice commented 2026-11-24 12:59:19 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by towiice on 2026-11-24T12:59:19Z.

Review: COMMENTED


Tested (as reporter of #1130):

  • Hardware: NVIDIA GeForce GTX 1650 SUPER (Turing TU116, 4 GB VRAM, wave32, driver 610.88), Windows 11 x64 (26H2 / build 26300), DXC v1.9.2609 (01b62ad4), toolchain stable-x86_64-pc-windows-gnullvm (llvm-mingw 23.1.6), debug build — the exact setup where #1130 aborted with STATUS_STACK_BUFFER_OVERRUN.
  • Test command: cargo test -p xcl --lib hle::d3d12x::real -- --test-threads=1
  • Result: 19 passed, 0 failed (1.54 s), including the new this_first_tests::misaligned_return_buffer_is_not_adopted_and_does_not_panic.

Checking pointer alignment before dereferencing cleanly fixes the misaligned-read UB in is_adopted(). Once this lands, we can also drop the #[repr(align(8))] workaround in #1132.

Not tested: full workspace suite, release builds, retail titles (smoke.ps1).

<!-- greyfinch-sync:ivall/xcl:review:6039611052 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1139#pullrequestreview-6039611052). Originally posted by [towiice](https://github.com/towiice) on 2026-11-24T12:59:19Z. **Review:** COMMENTED --- **Tested (as reporter of #1130):** - Hardware: NVIDIA GeForce GTX 1650 SUPER (Turing TU116, 4 GB VRAM, wave32, driver 610.88), Windows 11 x64 (26H2 / build 26300), DXC v1.9.2609 (`01b62ad4`), toolchain `stable-x86_64-pc-windows-gnullvm` (llvm-mingw 23.1.6), debug build — the exact setup where #1130 aborted with `STATUS_STACK_BUFFER_OVERRUN`. - Test command: `cargo test -p xcl --lib hle::d3d12x::real -- --test-threads=1` - Result: **19 passed, 0 failed** (1.54 s), including the new `this_first_tests::misaligned_return_buffer_is_not_adopted_and_does_not_panic`. Checking pointer alignment before dereferencing cleanly fixes the misaligned-read UB in `is_adopted()`. Once this lands, we can also drop the `#[repr(align(8))]` workaround in #1132. **Not tested:** full workspace suite, release builds, retail titles (`smoke.ps1`).
DavidM1911 commented 2026-11-24 13:03:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T12:56:45Z.


Independent verification on current main 384f7f7 (branch already contains it — clean).

Machine: Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug.

  • cargo test -p xcl --lib adopt: 1 passed, 0 failed — misaligned_return_buffer_is_not_adopted_and_does_not_panic covers the #1130 alignment check on the real backend path.

AI use: test runs by Devin (Cognition, SWE-2); no code changes.

<!-- greyfinch-sync:ivall/xcl:comment:7132133343 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1139#issuecomment-7132133343). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T12:56:45Z. --- Independent verification on current `main` `384f7f7` (branch already contains it — clean). **Machine:** Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug. - `cargo test -p xcl --lib adopt`: **1 passed, 0 failed** — `misaligned_return_buffer_is_not_adopted_and_does_not_panic` covers the #1130 alignment check on the real backend path. AI use: test runs by Devin (Cognition, SWE-2); no code changes.
Cecerw commented 2026-11-24 14:06:39 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Cecerw on 2026-11-24T13:45:45Z.


Tested on NVIDIA: passes

Tree Passed Failed Ignored SKIPPED
Head 20a893f (on main 47efa2e) 743 0 41 2 (no wave64)
  • RTX 3060 Ti 8 GB, driver 32.0.16.1088; i5-14600K; Windows 11 26300.9818; Rust 1.100.0, debug; DXC 1.9.2602.17
  • Head 20a893f (on main 47efa2e), no local changes: .\scripts\test.ps1 (cargo test --workspace -- --test-threads=4): 743 passed, 0 failed, 41 ignored
  • custom_heap_properties_match_host_in_cpp_and_c_abi and misaligned_return_buffer_is_not_adopted_and_does_not_panic: ok
  • SKIPPED: 2 (no wave64), the usual two on this adapter

Note: #1132 adds an integration test with the same name, custom_heap_properties_match_host_in_cpp_and_c_abi (in crates/xcl/tests/d3d12_allocation_and_heap_info.rs), covering the same C++ / C argument orders. Whichever lands second may want to drop or rename its copy.

Not tested: AMD, any title (no smoke run).

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this report.

<!-- greyfinch-sync:ivall/xcl:comment:7133298601 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1139#issuecomment-7133298601). Originally posted by [Cecerw](https://github.com/Cecerw) on 2026-11-24T13:45:45Z. --- **Tested on NVIDIA: passes** | Tree | Passed | Failed | Ignored | SKIPPED | |---|---|---|---|---| | Head `20a893f` (on `main` `47efa2e`) | 743 | 0 | 41 | 2 (no wave64) | - RTX 3060 Ti 8 GB, driver 32.0.16.1088; i5-14600K; Windows 11 26300.9818; Rust 1.100.0, debug; DXC 1.9.2602.17 - Head `20a893f` (on `main` `47efa2e`), no local changes: `.\scripts\test.ps1` (`cargo test --workspace -- --test-threads=4`): 743 passed, 0 failed, 41 ignored - `custom_heap_properties_match_host_in_cpp_and_c_abi` and `misaligned_return_buffer_is_not_adopted_and_does_not_panic`: ok - SKIPPED: 2 (no wave64), the usual two on this adapter Note: #1132 adds an integration test with the same name, `custom_heap_properties_match_host_in_cpp_and_c_abi` (in `crates/xcl/tests/d3d12_allocation_and_heap_info.rs`), covering the same C++ / C argument orders. Whichever lands second may want to drop or rename its copy. **Not tested:** AMD, any title (no smoke run). **AI use:** Claude Code (Claude Opus 5.5) ran the tests and wrote this report.
bjarngard commented 2026-11-24 14:06:42 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bjarngard on 2026-11-24T13:48:00Z.


Test report for head 20a893f (AMD)

Tested

  • AMD Radeon(TM) Graphics (Ryzen 9 9950X iGPU, RDNA2, native wave64), driver 32.0.21043.5001; Windows 11 build 26300; Rust 1.99.1 MSVC; DXC 1.9.2602.17; debug
  • XCL_ADAPTER=radeon cargo test --workspace --no-fail-fast -- --test-threads=4: 743 passed, 0 failed, 41 ignored, no SKIPPED
  • misaligned_return_buffer_is_not_adopted_and_does_not_panic, custom_heap_properties_match_host_in_cpp_and_c_abi passed when run alone on the AMD adapter

Not tested: retail titles (no smoke run), RDNA3/RDNA4.

AI use: the tests were run and this report was written by an AI agent, Claude Code (Claude Opus 5.5), on the hardware above. The numbers are copied from the actual test output.

<!-- greyfinch-sync:ivall/xcl:comment:7133354539 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1139#issuecomment-7133354539). Originally posted by [bjarngard](https://github.com/bjarngard) on 2026-11-24T13:48:00Z. --- Test report for head `20a893f` (AMD) **Tested** - AMD Radeon(TM) Graphics (Ryzen 9 9950X iGPU, RDNA2, native wave64), driver 32.0.21043.5001; Windows 11 build 26300; Rust 1.99.1 MSVC; DXC 1.9.2602.17; debug - `XCL_ADAPTER=radeon cargo test --workspace --no-fail-fast -- --test-threads=4`: 743 passed, 0 failed, 41 ignored, no SKIPPED - `misaligned_return_buffer_is_not_adopted_and_does_not_panic`, `custom_heap_properties_match_host_in_cpp_and_c_abi` passed when run alone on the AMD adapter **Not tested:** retail titles (no smoke run), RDNA3/RDNA4. **AI use:** the tests were run and this report was written by an AI agent, Claude Code (Claude Opus 5.5), on the hardware above. The numbers are copied from the actual test output.
JoaoPauluu commented 2026-11-24 15:05:50 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by JoaoPauluu on 2026-11-24T14:21:39Z.


A regression run under Wine on Linux, on a retail title.

Tested

  • RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from #257, DXC 1.9.2609.5. Release, cross-built for x86_64-pc-windows-gnu. 20a893f as it is (merge base 47efa2e) and main 47efa2e, no uncommitted changes. Retail title 1.0.2.1, scripts/linux/smoke.sh (#257) for 60 s, no input. Twelve runs in a row, main first and last, other PRs in between.
  • A regression check: I did not check whether GTA VI reaches the changed code in these 60 s.

Regression run

main (first run):

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 3060
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1384 (PresentX 1384)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 0 MB

this PR:

xcl-smoke v1
commit: 20a893f3a962ec28ebcb16e31ffe08bf69673efb
gpu: NVIDIA GeForce RTX 3060
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1384 (PresentX 1384)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 0 MB

The last main run: 1425 frames, exit: 0x0, no device removal, no crash. Under Wine, peak commit reads 0 MB, so these blocks do not check memory.

Not tested

  • the test suite on this head; native Windows; AMD.

AI use

  • Claude Code (Claude Opus 5.5) built both commits, ran the runs and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.
<!-- greyfinch-sync:ivall/xcl:comment:7134247192 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1139#issuecomment-7134247192). Originally posted by [JoaoPauluu](https://github.com/JoaoPauluu) on 2026-11-24T14:21:39Z. --- A regression run under Wine on Linux, on a retail title. **Tested** - RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from #257, DXC 1.9.2609.5. Release, cross-built for `x86_64-pc-windows-gnu`. `20a893f` as it is (merge base `47efa2e`) and `main` `47efa2e`, no uncommitted changes. Retail title 1.0.2.1, `scripts/linux/smoke.sh` (#257) for 60 s, no input. Twelve runs in a row, `main` first and last, other PRs in between. - A regression check: I did not check whether GTA VI reaches the changed code in these 60 s. **Regression run** main (first run): ``` xcl-smoke v1 commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3 gpu: NVIDIA GeForce RTX 3060 title: GTA6.Xbox.v1.0.2.1 seconds: 60 exit: 0x0 frames shown: 1384 (PresentX 1384) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 0 MB ``` this PR: ``` xcl-smoke v1 commit: 20a893f3a962ec28ebcb16e31ffe08bf69673efb gpu: NVIDIA GeForce RTX 3060 title: GTA6.Xbox.v1.0.2.1 seconds: 60 exit: 0x0 frames shown: 1384 (PresentX 1384) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 0 MB ``` The last `main` run: 1425 frames, `exit: 0x0`, no device removal, no crash. Under Wine, `peak commit` reads 0 MB, so these blocks do not check memory. **Not tested** - the test suite on this head; native Windows; AMD. **AI use** - Claude Code (Claude Opus 5.5) built both commits, ran the runs and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.
throny commented 2026-11-25 01:10:55 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-11-25T00:51:19Z.


Two contributors checked independently and found that #1522 solves the same problem (for #1130) in a different way: #1607, group D67. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint from the community triage in #1607, not a maintainer decision.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

<!-- greyfinch-sync:ivall/xcl:comment:7148955934 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1139#issuecomment-7148955934). Originally posted by [throny](https://github.com/throny) on 2026-11-25T00:51:19Z. --- <!-- xcl-1607-hint:D67 --> Two contributors checked independently and found that #1522 solves the same problem (for #1130) in a different way: #1607, group D67. You may want to compare them with the other author and agree on one, or say in the PR why both are needed. This is a hint from the community triage in #1607, not a maintainer decision. AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.
DavidM1911 commented 2026-11-26 04:07:43 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T03:41:05Z.


Re-verification on RTX 3060 Ti (Windows 11): still a merge conflict with current main on the new head — needs another rebase before it can be tested.

<!-- greyfinch-sync:ivall/xcl:comment:7181933678 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1139#issuecomment-7181933678). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T03:41:05Z. --- Re-verification on RTX 3060 Ti (Windows 11): still a **merge conflict with current `main`** on the new head — needs another rebase before it can be tested.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!1130
No description provided.