From 8403c4c2bb1fb09fba0cc4aa80d7e3fcefdbec92 Mon Sep 17 00:00:00 2001 From: dhillonjagga <162934508+dhillonjagga@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:06:15 -0600 Subject: [PATCH 1/2] acphal: SHAPE_XMA_CONTEXT and SHAPE_PCM_CONTEXT are the 64 and 32 bytes their checks name Since #9515 main does not compile with MSVC (14.51, at f48adad76): acphal.cpp(146): error C2338: static assertion failed: 'SHAPE_XMA_CONTEXT is 64 bytes per GDK' acphal.cpp(167): error C2338: static assertion failed: 'SHAPE_PCM_CONTEXT is 32 bytes per GDK' The bit-fields of SHAPE_XMA_CONTEXT fill five 32-bit words and those of SHAPE_PCM_CONTEXT six, so with reserved[10] and reserved[1] the structures were 60 and 28 bytes. The shim uses only their size, as the stride of the context arrays it allocates, so the reserved tails now cover the rest: reserved[11] and reserved[2]. The field offsets are not checked against a console here. Tested: MSVC 14.51, Windows 11: the file compiles; ctest acphal passes; python -I tests/test_acphal_abi.py OK. Co-Authored-By: Claude Fable 5.2 --- runtime/shims/acphal.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/runtime/shims/acphal.cpp b/runtime/shims/acphal.cpp index 553f57f207..6014fab980 100644 --- a/runtime/shims/acphal.cpp +++ b/runtime/shims/acphal.cpp @@ -141,7 +141,9 @@ struct SHAPE_XMA_CONTEXT { uint32_t reserved1 : 6; uint32_t loopStartOffset : 26; uint32_t loopEndOffset : 6; - uint32_t reserved[10]; + // The bit-fields above fill five words (20 bytes); the rest of the GDK's 64 bytes is reserved here. Only the + // size is used (the context array's stride); the field offsets are not checked against a console. + uint32_t reserved[11]; }; static_assert(sizeof(SHAPE_XMA_CONTEXT) == 64, "SHAPE_XMA_CONTEXT is 64 bytes per GDK"); @@ -162,7 +164,9 @@ struct SHAPE_PCM_CONTEXT { uint32_t reserved6 : 15; uint32_t format : 2; uint32_t reserved7 : 14; - uint32_t reserved[1]; + // The bit-fields above fill six words (24 bytes); the rest of the GDK's 32 bytes is reserved here. Only the + // size is used (the context array's stride). + uint32_t reserved[2]; }; static_assert(sizeof(SHAPE_PCM_CONTEXT) == 32, "SHAPE_PCM_CONTEXT is 32 bytes per GDK"); From a5165cb263bf97a56751a683bae34fb7dfdc8ab6 Mon Sep 17 00:00:00 2001 From: dhillonjagga <162934508+dhillonjagga@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:06:15 -0600 Subject: [PATCH 2/2] d3d12: slot 14's console descriptor heap compiles, through a handler the layer registers on request Since #9577 main does not compile with MSVC (14.51, at f48adad76): d3d12_xs_device.cpp(1606): error C3083: 'd3d12': the symbol to the left of a '::' must be a type d3d12_vulkan_device.cpp(9995): error C2665: 'DescriptorHeap::DescriptorHeap': cannot convert argument 1 from 'const DeviceState' to 'DeviceState &' d3d12_vulkan_device.cpp(9996): error C2248: 'Device::identity': cannot access private member d3d12_console_descriptor_heap_test.cpp(45): error C2819: 'Ref' does not have an overloaded member 'operator ->' d3d12_console_descriptor_heap_test.cpp(68): error C2660: 'create_device_proxy': function does not take 2 arguments The proxy (d3d12_xs_device.cpp) called rt::d3d12::create_console_descriptor_heap by name. It does not include d3d12_com.h, and it is built into programs without the Vulkan layer, where the name would not link either. Slot 14 now asks a handler, as slot 133 does (set_placed_at_va_handler). The heap is made by a Device member, which has the state and identity the free function could not reach. With that compiled in and the handler always registered, 21 of 294 runtime tests fail on this machine, 9 of them with an access violation (d3d12_descriptor, d3d12_present, d3d12_draw, xbox_present and others): the proxy takes the 16 bytes after the description as address and size whenever the first is not zero, a caller that passes the 16-byte PC description has unrelated bytes there, and the heap writes its descriptors to that address. So the layer registers the handler only with LEONIDA_CONSOLE_DESCRIPTOR_HEAP=1 (docs/boot.md), which is what a retail boot sets to pass stop 13 of docs/retail_boot_chain.md; without it slot 14 creates the heap from the 16 PC bytes, as before #9577. A rule that tells the two descriptions apart would make the switch unnecessary; none is measured here. The test: Ref gets operator->, create_device_proxy its three arguments and a device reference of its own, and the heap proxy's start is read from the heap behind it (slots 9 and 10 of a heap proxy return the handle in RAX, so a PC call reads a result buffer the proxy never writes). It checks slot 14 with and without the handler. Tested on an RTX 3080 Ti (driver 617.42, Windows 11, no Resizable BAR), MSVC 14.51, Vulkan SDK 1.4.363, at f48adad76 with the acphal commit before this one: scripts\build_runtime.ps1 builds all 748 targets; ctest 290/294, d3d12_console_descriptor_heap among the passes. The four failures do not involve this code: d3d12_com (line 345, a Close() result), d3d12_vulkan_device, d3d12_depth_resolve and d3d12_indirect_rt. python -I tests/test_docs_env_vars.py reports only LEONIDA_RNG_SEED (bcrypt_rng.cpp:80), which is not from this change. Not run: a retail boot up to slot 14 (on this main it stops earlier, at DStorageGetFactory). Co-Authored-By: Claude Fable 5.2 --- docs/boot.md | 4 +++ runtime/shims/d3d12_vulkan_device.cpp | 34 +++++++++++++----- runtime/shims/d3d12_xs_device.cpp | 17 ++++++--- runtime/shims/d3d12_xs_device.h | 11 ++++++ .../d3d12_console_descriptor_heap_test.cpp | 35 ++++++++++++++++--- 5 files changed, 85 insertions(+), 16 deletions(-) diff --git a/docs/boot.md b/docs/boot.md index 7ec69f0314..f012a910fa 100644 --- a/docs/boot.md +++ b/docs/boot.md @@ -86,6 +86,10 @@ Useful switches: implicit layers add swapchain usage that shows up as validation errors ([testing.md](testing.md), "Known problems"). * `--overlay DIR` sends the guest's writes to a private folder instead of the package. +* `LEONIDA_CONSOLE_DESCRIPTOR_HEAP=1` (Vulkan backend) makes device slot 14 read a console address and size after + the 16-byte heap description and report that address as the heap's start, which the retail executable asserts + on ([retail_boot_chain.md](retail_boot_chain.md), stop 13). Off by default: a caller that passes only the PC + description has unrelated bytes there, and the descriptors would be written to whatever address they spell. ## 3. Triage order diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index fc4b966c24..da6375e9c1 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -5012,6 +5012,17 @@ class Device final // standalone device gta6rt --run uses both come through here (found in review: registering only in // create_vulkan_device left the retail path without it). rt::d3d12_xs::set_placed_at_va_handler(&create_buffer_at_host_address); + // Slot 14's console-extended description reaches create_console_descriptor_heap through a handler too: the + // proxy is also built into programs without this layer, so it cannot call it by name. It is registered on + // request (LEONIDA_CONSOLE_DESCRIPTOR_HEAP=1), not always: the proxy tells a console description from a PC + // one by the 16 bytes after it, and after a PC caller's 16-byte description those are whatever follows it + // in memory. Always on, 21 of 294 runtime tests fail (9 crash) writing descriptors to such an address. + { + char text[8] = {}; + size_t length = 0; + if (getenv_s(&length, text, "LEONIDA_CONSOLE_DESCRIPTOR_HEAP") == 0 && length > 1 && text[0] != '0') + rt::d3d12_xs::set_console_descriptor_heap_handler(&rt::d3d12::create_console_descriptor_heap); + } state_.info = info; queue_users_.resize(info.command_queues.size(), 0); compute_users_.resize(info.compute_queues.size(), 0); @@ -5969,6 +5980,20 @@ class Device final return d3d12_trace::result(method, hand_out(heap, riid, out)); } + // The heap of create_console_descriptor_heap (Xbox device slot 14): it reports `console_address` as its start. + // A member because the heap takes the device's state and identity, as CreateDescriptorHeap's does; the caller + // has applied CreateDescriptorHeap's rules to `desc`. + HRESULT console_descriptor_heap(const D3D12_DESCRIPTOR_HEAP_DESC& desc, std::uint64_t console_address, + std::uint64_t console_size, REFIID riid, void** out) { + auto* heap = new DescriptorHeap(state_, desc, console_address, console_size); + heap->attach_device(identity()); + if (!heap->ok()) { + heap->Release(); + return E_OUTOFMEMORY; + } + return hand_out(heap, riid, out); + } + HRESULT STDMETHODCALLTYPE CreateQueryHeap(const D3D12_QUERY_HEAP_DESC* desc, REFIID riid, void** out) override { if (out) *out = nullptr; // The host runtime's order (compare_invalid_arguments, "query heap:"): the description first, refused with @@ -9991,14 +10016,7 @@ HRESULT create_console_descriptor_heap(ID3D12Device* device, const D3D12_DESCRIP ID3D12Device* real = rt::d3d12_xs::device_proxy_real_device(device); auto* d = Device::from(real ? real : device); if (!d) return E_INVALIDARG; - - auto* heap = new DescriptorHeap(d->state(), *desc, console_address, console_size); - heap->attach_device(d->identity()); - if (!heap->ok()) { - heap->Release(); - return E_OUTOFMEMORY; - } - return hand_out(heap, riid, out); + return d->console_descriptor_heap(*desc, console_address, console_size, riid, out); } } // namespace rt::d3d12 diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 187406e3d8..e866e5cb4d 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -1579,6 +1579,9 @@ static bool readable_range(const void* ptr, std::size_t bytes) { return true; } +// The handler slot 14 calls for a console-extended description (set_console_descriptor_heap_handler). +std::atomic g_console_descriptor_heap{nullptr}; + HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_HEAP_DESC* desc, const IID* riid, void** out) { note_slot(14); @@ -1601,11 +1604,13 @@ HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_H std::memcpy(&size, bytes + sizeof(D3D12_DESCRIPTOR_HEAP_DESC) + sizeof(address), sizeof(size)); // Console-extended slot 14 (issue #313): address/size follow the PC desc. Only take this path when a // non-zero console VA is present; normal PC 16-byte descs never enter it. - if (address != 0) { + // The heap at a console address is made by the device behind the proxy, through the handler it + // registered (set_console_descriptor_heap_handler); without one the PC path below creates the heap. + const ConsoleDescriptorHeapHandler handler = g_console_descriptor_heap.load(std::memory_order_acquire); + if (address != 0 && handler) { console_extended = true; - hr = rt::d3d12::create_console_descriptor_heap( - self->real, desc, static_cast(address), size, - __uuidof(ID3D12DescriptorHeap), &created); + hr = handler(self->real, desc, static_cast(address), size, + __uuidof(ID3D12DescriptorHeap), &created); } } } @@ -2041,6 +2046,10 @@ void set_placed_at_va_pages_hook(PlacedAtVaPagesHook hook) { g_placed_at_va_pages.store(hook, std::memory_order_release); } +void set_console_descriptor_heap_handler(ConsoleDescriptorHeapHandler handler) { + g_console_descriptor_heap.store(handler, std::memory_order_release); +} + ID3D12Device* device_proxy_real_device(const void* device) { if (!is_device_proxy(device)) return nullptr; return static_cast(device)->real; diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index ac0437215d..86fee802ee 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -108,6 +108,17 @@ void set_placed_at_va_handler(PlacedAtVaHandler handler); using PlacedAtVaPagesHook = bool (*)(const void* va, std::size_t bytes); void set_placed_at_va_pages_hook(PlacedAtVaPagesHook hook); +// Device slot 14 with the console-extended description (a console address and a size after the 16 PC bytes) makes +// the heap at that address. The proxy does not create it itself: the device behind it does, through this handler, +// which the Vulkan layer registers on request (rt::d3d12::create_console_descriptor_heap, with +// LEONIDA_CONSOLE_DESCRIPTOR_HEAP=1). This file is also built into programs without that layer, so it cannot call +// the layer by name. With none registered the slot creates the heap as the PC CreateDescriptorHeap does, from the +// 16 PC bytes alone: the bytes after a PC caller's description are not an address. +using ConsoleDescriptorHeapHandler = HRESULT (*)(ID3D12Device* real, const D3D12_DESCRIPTOR_HEAP_DESC* desc, + D3D12_GPU_VIRTUAL_ADDRESS console_address, + std::uint64_t console_size, REFIID riid, void** out); +void set_console_descriptor_heap_handler(ConsoleDescriptorHeapHandler handler); + // True for slots the proxy handles itself rather than forwarding verbatim: IUnknown (0-2), 8 and 75 (queue // proxies), 9 (allocator proxies), 14, 31/34/35/45/46/50 (proxies unwrapped), 32 and 36 (fence proxies), 48/73 and // 78/79/80 (the Xbox slots above, at their configured indices), 62, 67 and 76. Slot 10 is intentionally false: the diff --git a/runtime/test/d3d12_console_descriptor_heap_test.cpp b/runtime/test/d3d12_console_descriptor_heap_test.cpp index 5f694b5a2a..17e1498a1b 100644 --- a/runtime/test/d3d12_console_descriptor_heap_test.cpp +++ b/runtime/test/d3d12_console_descriptor_heap_test.cpp @@ -24,6 +24,7 @@ struct Ref { return &p; } T* get() const { return p; } + T* operator->() const { return p; } }; bool check_console_addressed_heap(ID3D12Device* device) { @@ -63,9 +64,13 @@ bool check_console_addressed_heap(ID3D12Device* device) { return true; } -bool check_xs_proxy_slot14(ID3D12Device* real_device) { +// `console_path`: whether the layer's handler is registered, so slot 14 takes the address after the description. +bool check_xs_proxy_slot14(ID3D12Device* real_device, bool console_path) { + // create_device_proxy takes over one reference to the device on success; the caller keeps its own. + real_device->AddRef(); void* proxy_raw = nullptr; - if (FAILED(rt::d3d12_xs::create_device_proxy(real_device, IID_PPV_ARGS(&proxy_raw))) || !proxy_raw) { + if (FAILED(rt::d3d12_xs::create_device_proxy(real_device, __uuidof(ID3D12Device), &proxy_raw)) || !proxy_raw) { + real_device->Release(); std::fprintf(stderr, "create_device_proxy failed\n"); return false; } @@ -90,7 +95,22 @@ bool check_xs_proxy_slot14(ID3D12Device* real_device) { static_cast(hr)); return false; } - const auto cpu = heap->GetCPUDescriptorHandleForHeapStart(); + // Slot 14 hands out a heap proxy, whose slots 9 and 10 return the handle in RAX as the console does: a PC call + // of them would read a result buffer the proxy never writes. The heap behind it is read instead. + if (!rt::d3d12_xs::is_heap_proxy(heap.get())) { + std::fprintf(stderr, "proxy CreateDescriptorHeap(console) did not return a heap proxy\n"); + return false; + } + const auto cpu = rt::d3d12_xs::heap_proxy_real_heap(heap.get())->GetCPUDescriptorHandleForHeapStart(); + if (!console_path) { + // No handler: the 16 PC bytes make an ordinary heap, with storage of its own. + if (cpu.ptr == 0 || cpu.ptr == static_cast(console.address)) { + std::fprintf(stderr, "proxy heap without the handler starts at 0x%llx\n", + static_cast(cpu.ptr)); + return false; + } + return true; + } if (cpu.ptr != static_cast(console.address)) { std::fprintf(stderr, "proxy CPU heap start mismatch: got 0x%llx want 0x%llx\n", static_cast(cpu.ptr), @@ -142,7 +162,14 @@ int main() { if (!check_normal_heap_unchanged(device.get())) return 1; if (!check_console_addressed_heap(device.get())) return 1; - if (!check_xs_proxy_slot14(device.get())) return 1; + // The proxy takes the console path only through the handler the layer registers on request + // (LEONIDA_CONSOLE_DESCRIPTOR_HEAP=1). Both states are checked here, whatever the environment holds. + rt::d3d12_xs::set_console_descriptor_heap_handler(nullptr); + if (!check_xs_proxy_slot14(device.get(), false)) return 1; + rt::d3d12_xs::set_console_descriptor_heap_handler(&rt::d3d12::create_console_descriptor_heap); + const bool console_ok = check_xs_proxy_slot14(device.get(), true); + rt::d3d12_xs::set_console_descriptor_heap_handler(nullptr); + if (!console_ok) return 1; std::fprintf(stderr, "ok\n"); return 0; }