D3D12.X: is_adopted() does misaligned *const usize dereference on 4-byte-aligned D3D12_HEAP_PROPERTIES return buffer #1130

Open
opened 2026-11-26 04:44:24 +00:00 by github-bot · 1 comment
Owner

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

Summary

d3d12x::real::is_adopted (crates/xcl/src/hle/d3d12x/real.rs:636) dereferences obj as *const usize without read_unaligned():

pub(super) unsafe fn is_adopted(obj: *mut c_void) -> bool {
    !obj.is_null() && crate::gpu::shader::readable(obj as usize, 8) && is_ext(*(obj as *const usize))
}

this_first(a, b) calls is_adopted on both arguments of struct-returning device hooks, including get_custom_heap_properties_x (slot 26, returning D3D12_HEAP_PROPERTIES, which is [u32; 5] — 4-byte aligned, not 8-byte aligned).

When a caller invokes GetCustomHeapProperties with a 4-byte-aligned stack buffer (e.g. 0x...b3c), *(obj as *const usize) triggers undefined behavior and aborts debug builds with a non-unwinding panic:

thread 'custom_heap_properties_match_host_in_cpp_and_c_abi' panicked at crates\xcl\src\hle\d3d12x\real.rs:636:79:
misaligned pointer dereference: address must be a multiple of 0x8 but is 0x750afeb3c
thread caused non-unwinding panic. aborting.
(exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)

Suggested Fix

Use read_unaligned() in is_adopted:

pub(super) unsafe fn is_adopted(obj: *mut c_void) -> bool {
    !obj.is_null() && crate::gpu::shader::readable(obj as usize, 8) && is_ext((obj as *const usize).read_unaligned())
}

(Alternatively, check (obj as usize) & 7 == 0 first, since real COM objects are always 8-byte aligned — a 4-byte-aligned pointer can never be an adopted COM object.)

Found while writing host-driver parity tests for GetCustomHeapProperties (Part of #480) on Windows 11 x64 (rustc 1.100.0, stable-x86_64-pc-windows-gnullvm, NVIDIA GeForce GTX 1650 SUPER).

**@⁠towiice** on GitHub · 2026-11-24 12:12 UTC · [original](https://github.com/xcl-dev/xcl/issues/1130) ## Summary `d3d12x::real::is_adopted` (`crates/xcl/src/hle/d3d12x/real.rs:636`) dereferences `obj` as `*const usize` without `read_unaligned()`: ```rust pub(super) unsafe fn is_adopted(obj: *mut c_void) -> bool { !obj.is_null() && crate::gpu::shader::readable(obj as usize, 8) && is_ext(*(obj as *const usize)) } ``` `this_first(a, b)` calls `is_adopted` on **both** arguments of struct-returning device hooks, including `get_custom_heap_properties_x` (slot 26, returning `D3D12_HEAP_PROPERTIES`, which is `[u32; 5]` — 4-byte aligned, not 8-byte aligned). When a caller invokes `GetCustomHeapProperties` with a 4-byte-aligned stack buffer (e.g. `0x...b3c`), `*(obj as *const usize)` triggers undefined behavior and aborts debug builds with a non-unwinding panic: ```text thread 'custom_heap_properties_match_host_in_cpp_and_c_abi' panicked at crates\xcl\src\hle\d3d12x\real.rs:636:79: misaligned pointer dereference: address must be a multiple of 0x8 but is 0x750afeb3c thread caused non-unwinding panic. aborting. (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN) ``` ## Suggested Fix Use `read_unaligned()` in `is_adopted`: ```rust pub(super) unsafe fn is_adopted(obj: *mut c_void) -> bool { !obj.is_null() && crate::gpu::shader::readable(obj as usize, 8) && is_ext((obj as *const usize).read_unaligned()) } ``` (Alternatively, check `(obj as usize) & 7 == 0` first, since real COM objects are always 8-byte aligned — a 4-byte-aligned pointer can never be an adopted COM object.) Found while writing host-driver parity tests for `GetCustomHeapProperties` (Part of #480) on Windows 11 x64 (`rustc 1.100.0`, `stable-x86_64-pc-windows-gnullvm`, NVIDIA GeForce GTX 1650 SUPER).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:44:24 +00:00
Author
Owner

@⁠hyperland956-lgtm on GitHub · 2026-11-24 13:50 UTC · original

I agree this is a real alignment issue in the current source snapshot.

readable(obj as usize, 8) validates that the eight bytes are mapped, but does not establish align_of::<usize>().

GetCustomHeapProperties uses a D3D12_HEAP_PROPERTIES output structure with 4-byte alignment. Consequently, this_first() can pass a non-COM return buffer to is_adopted() and trigger an unaligned *const usize dereference.

The minimal change is to use read_unaligned() for the candidate vtable word after the existing null/readability checks.

I prefer this over an alignment-only guard because is_adopted() is a classifier for arbitrary candidate arguments and should not itself have an alignment precondition.

Suggested regression test:

Construct a 4-byte-aligned D3D12_HEAP_PROPERTIES output buffer whose address is not divisible by 8. Invoke both C and C++ GetCustomHeapProperties argument orderings and verify that it is classified as the return buffer without panicking or modifying the object's vtable.

Retain the existing adopted-object/vtable test to ensure real COM pointers still classify correctly.

This is a source-level finding. The proposed patch has not yet been built or tested in my environment.

**@⁠hyperland956-lgtm** on GitHub · 2026-11-24 13:50 UTC · [original](https://github.com/xcl-dev/xcl/issues/1130#issuecomment-7133424240) I agree this is a real alignment issue in the current source snapshot. `readable(obj as usize, 8)` validates that the eight bytes are mapped, but does not establish `align_of::<usize>()`. `GetCustomHeapProperties` uses a `D3D12_HEAP_PROPERTIES` output structure with 4-byte alignment. Consequently, `this_first()` can pass a non-COM return buffer to `is_adopted()` and trigger an unaligned `*const usize` dereference. The minimal change is to use `read_unaligned()` for the candidate vtable word after the existing null/readability checks. I prefer this over an alignment-only guard because `is_adopted()` is a classifier for arbitrary candidate arguments and should not itself have an alignment precondition. **Suggested regression test:** Construct a 4-byte-aligned `D3D12_HEAP_PROPERTIES` output buffer whose address is not divisible by 8. Invoke both C and C++ `GetCustomHeapProperties` argument orderings and verify that it is classified as the return buffer without panicking or modifying the object's vtable. Retain the existing adopted-object/vtable test to ensure real COM pointers still classify correctly. This is a source-level finding. The proposed patch has not yet been built or tested in my environment.
This discussion has been locked. Commenting is limited to contributors.
No description provided.