From 3b96b261989720161a2817db218b594dcd18f650 Mon Sep 17 00:00:00 2001 From: gmanka Date: Thu, 26 Nov 2026 10:50:38 +0500 Subject: [PATCH] GPU: bound constant-buffer views before recording copies A descriptor can name more bytes than the host's 4,096 16-byte registers can address. The native runtime kept the title's full length, so a dynamic table could allocate and copy it before sending an invalid CBV to the host. Aligning a near-u32::MAX size could also wrap. Cap the recorded range at 64 KiB so the dynamic scratch copy and the mirrored host view use the same bounded range. Keep the original size for smaller and zero-byte descriptors. Cover the public creation path with a regression for unaligned, boundary and overflowing sizes. Based-on: https://github.com/fxceless/victory-fork/pull/1206 Based-on: https://github.com/fxceless/victory-fork/pull/1260 Tested-on: Fedora Linux 45, Xeon E5-2670 v3, native VKD3D-Proton with Lavapipe (44 tests) and AMD Radeon RX 6800 with RADV (clear readback) Assisted-by: gpt-6.2 sol xhigh --- crates/victory-gpu/src/descriptor.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index 80026f9f5b..5f843991bf 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -413,8 +413,13 @@ fn words(desc: *const [u32; N]) -> Option<[u32; N]> { /// Device method. pub(crate) extern "win64" fn create_cbv(_: This, desc: *const [u32; 4], handle: u64) { + use windows::Win32::Graphics::Direct3D12::D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT; + if let Some([lo, hi, size, _]) = words(desc) { let address = u64::from(lo) | u64::from(hi) << 32; + // A host constant view can address 4,096 16-byte registers. Bound the + // range before it is copied to scratch or rounded up for the host. + let size = size.min(D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * 16); record(handle, View::Constant(address, size)); } } @@ -683,6 +688,22 @@ mod tests { assert_eq!(create(bad, none, &mut h), E_INVALIDARG); } + #[test] + fn constant_views_keep_only_the_bytes_the_host_can_address() { + let mut slot = [0u64; 4]; + let handle = slot.as_mut_ptr() as u64; + for (size, expected) in [ + (0, 0), + (17, 17), + (65_536, 65_536), + (65_537, 65_536), + (u32::MAX, 65_536), + ] { + create_cbv(std::ptr::null(), &[0x1000, 1, size, 0], handle); + assert_eq!(view(handle), Some(View::Constant(0x1_0000_1000, expected))); + } + } + #[test] fn views_survive_copies() { let null = std::ptr::null();