From f6584ed80c3a9157c588151602735ed2c8411263 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 24 Nov 2026 20:12:29 +0100 Subject: [PATCH] d3d12_xs: slot 14 does not take an address in the first 64 KiB as a console heap (D3D-SLOT14-TAIL-NULL-PAGE) d3d12_draw SEGFAULTed with MSVC RelWithDebInfo: its one-entry RTV desc was followed on the stack by the words 1 and 32, which console_heap_range_plausible accepted (32 == 1 * 32), so the RTV heap started at 0x1 and CreateRenderTargetView wrote through it. Windows never maps the first 64 KiB and no console heap lives there; reject such addresses. d3d12_console_descriptor_heap_test gets the stray-words case. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/d3d12_xs_device.cpp | 10 +++++--- .../d3d12_console_descriptor_heap_test.cpp | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 85aece0bd2..be74cbd060 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -1714,12 +1714,16 @@ HRESULT proxy_unwrapping_call(DeviceProxy* self, std::uint64_t a1, std::uint64_t } // A console descriptor heap carries a size as well as an address, and that size is 32 bytes per descriptor. A PC -// desc's trailing words are whatever the caller holds next; they do not encode NumDescriptors * 32, so this -// separates the two layouts without guessing anything about the address itself. +// desc's trailing words are whatever the caller holds next; they rarely encode NumDescriptors * 32, but for a +// one-descriptor heap a small count of 32 next to it is enough. MSVC's RelWithDebInfo d3d12_draw_test leaves the +// words 1 and 32 after its one-entry RTV desc, which passed the size check and turned the RTV handle into 0x1. So +// the address also has to lie above the first 64 KiB, which Windows never maps (the lowest user-mode address is +// 0x10000) and no console heap address can be in either. static bool console_heap_range_plausible(const D3D12_DESCRIPTOR_HEAP_DESC* desc, std::uint64_t address, std::uint64_t size) { constexpr std::uint64_t kConsoleDescriptorSize = 32; - if (address == 0 || size == 0) return false; + constexpr std::uint64_t kLowestMappedAddress = 0x10000; + if (address < kLowestMappedAddress || size == 0) return false; if (desc->NumDescriptors == 0 || desc->NumDescriptors > (0xFFFFFFFFull / kConsoleDescriptorSize)) return false; if (size != static_cast(desc->NumDescriptors) * kConsoleDescriptorSize) return false; return true; diff --git a/runtime/test/d3d12_console_descriptor_heap_test.cpp b/runtime/test/d3d12_console_descriptor_heap_test.cpp index ed2d901f40..f49d04aeb5 100644 --- a/runtime/test/d3d12_console_descriptor_heap_test.cpp +++ b/runtime/test/d3d12_console_descriptor_heap_test.cpp @@ -105,6 +105,30 @@ bool check_xs_proxy_slot14(ID3D12Device* real_device) { static_cast(console.address)); return false; } + + // An ordinary one-descriptor desc followed by the words 1 and 32 (what d3d12_draw_test's RTV desc had after it + // on the stack under MSVC RelWithDebInfo): the size matches 1 * 32, but 0x1 is no heap address, so slot 14 has + // to create an ordinary heap instead of handing out 0x1 as the heap start. + ConsoleDescriptorHeapDesc stray{}; + stray.desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; + stray.desc.NumDescriptors = 1; + stray.address = 1; + stray.size = 32; + Ref stray_heap; + const HRESULT stray_hr = proxy->CreateDescriptorHeap(&stray.desc, IID_PPV_ARGS(stray_heap.put())); + ID3D12DescriptorHeap* stray_real = SUCCEEDED(stray_hr) && stray_heap.get() + ? rt::d3d12_xs::heap_proxy_real_heap(stray_heap.p) : nullptr; + if (!stray_real) { + std::fprintf(stderr, "proxy CreateDescriptorHeap(stray trailing words) failed: 0x%08lx\n", + static_cast(stray_hr)); + return false; + } + const auto stray_cpu = stray_real->GetCPUDescriptorHandleForHeapStart(); + if (stray_cpu.ptr == 0 || stray_cpu.ptr == static_cast(stray.address)) { + std::fprintf(stderr, "stray trailing words were taken as a console heap: CPU start 0x%llx\n", + static_cast(stray_cpu.ptr)); + return false; + } return true; }