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

Open
github-bot wants to merge 1 commit from github-bot/gh-1139 into main AGit
Owner

@⁠GamingChelsea on GitHub · 2026-11-24 12:24 UTC · original
GamingChelsea:fix/is-adopted-alignment → main · +80 −3 · 2 files · 1 commits · open

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.
**@⁠GamingChelsea** on GitHub · 2026-11-24 12:24 UTC · [original](https://github.com/xcl-dev/xcl/pull/1139) `GamingChelsea:fix/is-adopted-alignment` → `main` · +80 −3 · 2 files · 1 commits · open 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.
is_adopted() previously dereferenced *(obj as *const usize) without checking
pointer alignment. When this_first(a, b) inspects both arguments of a
struct-returning device hook such as GetCustomHeapProperties (whose return
buffer D3D12_HEAP_PROPERTIES is [u32; 5], 4-byte aligned), a 4-byte-aligned
stack buffer triggers UB and aborts debug builds with a misaligned pointer
dereference panic.

Require pointer alignment in is_adopted() before readable() and the vtable
read (adopted COM objects are always pointer-aligned), cache the this_first()
result once in get_resource_allocation_info2_x(), and add unit and host-driver
ABI parity tests for 4-byte-aligned return buffers in both C++ and C calling
conventions.

Fixes #1130

Assisted-by: Antigravity (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:44:49 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 12:56 UTC · original

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.

**@⁠DavidM1911** on GitHub · 2026-11-24 12:56 UTC · [original](https://github.com/xcl-dev/xcl/pull/1139#issuecomment-7132133343) 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.
Author
Owner

@⁠Cecerw on GitHub · 2026-11-24 13:45 UTC · original

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.

**@⁠Cecerw** on GitHub · 2026-11-24 13:45 UTC · [original](https://github.com/xcl-dev/xcl/pull/1139#issuecomment-7133298601) **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.
Author
Owner

@⁠bjarngard on GitHub · 2026-11-24 13:48 UTC · original

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.

**@⁠bjarngard** on GitHub · 2026-11-24 13:48 UTC · [original](https://github.com/xcl-dev/xcl/pull/1139#issuecomment-7133354539) 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.
Author
Owner

@⁠JoaoPauluu on GitHub · 2026-11-24 14:21 UTC · original

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.
**@⁠JoaoPauluu** on GitHub · 2026-11-24 14:21 UTC · [original](https://github.com/xcl-dev/xcl/pull/1139#issuecomment-7134247192) 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.
Author
Owner

@⁠throny on GitHub · 2026-11-25 00:51 UTC · original

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.

**@⁠throny** on GitHub · 2026-11-25 00:51 UTC · [original](https://github.com/xcl-dev/xcl/pull/1139#issuecomment-7148955934) <!-- 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.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 03:41 UTC · original

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.

**@⁠DavidM1911** on GitHub · 2026-11-26 03:41 UTC · [original](https://github.com/xcl-dev/xcl/pull/1139#issuecomment-7181933678) 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.
github-bot force-pushed github-bot/gh-1139 from 63afbb8f1b to e2ebd0c8f6 2026-11-26 12:34:37 +00:00 Compare
Author
Owner

@⁠towiice on GitHub · 2026-11-24 12:59 UTC · original · reviewed

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).

**@⁠towiice** on GitHub · 2026-11-24 12:59 UTC · [original](https://github.com/xcl-dev/xcl/pull/1139#pullrequestreview-6039611052) · **reviewed** **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`).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1139/head:github-bot/gh-1139'
git switch 'github-bot/gh-1139'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1139'
git switch 'github-bot/gh-1139'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1139'
git switch 'github-bot/gh-1139'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1139'
git switch 'main'
git merge --squash 'github-bot/gh-1139'
git switch 'main'
git merge --ff-only 'github-bot/gh-1139'
git switch 'main'
git merge 'github-bot/gh-1139'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.