From 85087eab072bd1a578a4ce92254c844ed1762cc1 Mon Sep 17 00:00:00 2001 From: The Void Date: Thu, 26 Nov 2026 08:39:37 +0100 Subject: [PATCH] startup: keep the object receiver for 8-byte struct returns when rdx holds a wrapper small_result chose the (return buffer, object) form whenever its second argument was a live wrapper. A title-ABI call to descriptor heap slot 9 (GetCPUDescriptorHandleForHeapStart) passes only `this`, so the second register holds whatever the caller left there. When that was another live wrapper, the shim wrote the handle over the heap object's vtable and returned the heap's address as the handle. Case 9 of startup_resource_retire_title (#2719) nests the slot 9 call in a slot 20 call; at /O2 rdx still held the resource wrapper, so the RTV went to an untracked slot and the heap's next Release jumped into data. All six variants segfaulted in Release since #2719. small_result now takes its object from receiver(), the rule the larger struct-returning slots already use: a wrapper first is the object, and only when both look like wrappers does the one on this thread's stack become the result buffer. Case 10 calls slot 9 with a live wrapper in the second argument and through a return buffer; it fails in Debug and Release without the shim change. Co-Authored-By: Claude Opus 5.5 --- src/startup_stubs/d3d12_xs_device.cpp | 17 +++++----- tests/startup_resource_retire_title.cpp | 43 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index a8146d032c..b2113c9871 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -4270,18 +4270,19 @@ void* __stdcall descriptor_heap_get_desc(void* a, void* b) return result; } // The title returns structures of at most 8 bytes in rax (this in rcx), larger ones through a hidden -// pointer passed first. If the second argument is a wrapper, the hidden-pointer form is used instead. +// pointer passed first. receiver() decides which argument is the object: a wrapper first is the object +// even when the caller left another live wrapper in the second register, which a call passing only +// `this` does not set. Only the (return buffer, object) form writes through the first argument. template std::uint64_t small_result(void* a, void* b, const char* method, Get get) { - if (Wrapper* w = as_wrapper(b)) { - const T value = get(w); - *static_cast(a) = value; - return reinterpret_cast(a); - } - Wrapper* w = as_wrapper(a); - if (!w) fail(method, a, b); + void* result = nullptr; + Wrapper* w = receiver(a, b, &result, method); const T value = get(w); + if (w != a) { + *static_cast(result) = value; + return reinterpret_cast(result); + } std::uint64_t bits = 0; static_assert(sizeof(T) <= sizeof(bits)); std::memcpy(&bits, &value, sizeof(T)); diff --git a/tests/startup_resource_retire_title.cpp b/tests/startup_resource_retire_title.cpp index 526db906e3..4e02039b3a 100644 --- a/tests/startup_resource_retire_title.cpp +++ b/tests/startup_resource_retire_title.cpp @@ -27,6 +27,10 @@ // tracked heap slot holds one ref per slot; the same view written again adds nothing, and overwrite, descriptor // copy and heap destruction release exactly what was added. Read through the retire probe (case runs with the // switch on; the retire class still excludes non-SRV view textures, so the budget column cannot show these refs). +// 10 struct-return ABI: descriptor heap slot 9 (GetCPUDescriptorHandleForHeapStart) returns its 8-byte handle in rax +// with only `this` in rcx. A live wrapper left in the second register must not select the (return buffer, +// object) form, which wrote the handle over the heap object; the hidden-pointer form still fills its buffer. +// Release builds of case 9 left a resource wrapper there and crashed at the heap's next Release. // 9 slot 19-21 fallback (Slice 3a): the same view call in a tracked slot leaves a class texture in the retire class // (a refused view adds no ref, so the released texture retires), while a descriptor in a heap the shim does not // track keeps the retire_leave_class fallback and the texture is not freed. Runs in both modes; without the switch @@ -1172,6 +1176,43 @@ bool case9_view_slot_fallback(Context& c, bool on, void* tracked_memory, void* u "the untracked-descriptor fallback did not keep the texture retained"); } +// 10: descriptor heap slot 9 returns an 8-byte structure. The title calls it with `this` in rcx and reads rax, so +// the second register holds whatever the caller left there; an optimized caller computing the next call's arguments +// can leave another live object there. That must not select the (return buffer, object) form, which wrote the +// handle over the heap object's vtable and returned the heap's address. The hidden-pointer form, a stack buffer +// first and the object second, still fills the buffer and returns it. +bool case10_heap_start_abi(Context& c) +{ + ComPtr heap = c.make_descriptor_heap("abi heap", D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1); + ComPtr other = c.make_descriptor_heap("abi second heap", D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1); + const UINT64 want = invoke(heap.Get(), 9, static_cast(nullptr)); + const UINT64 got = invoke(heap.Get(), 9, static_cast(other.Get())); + if (!want || got != want) { + std::printf("FAIL: heap start with a live object in the second argument: got %#llx, expected %#llx (heap %p)\n", + static_cast(got), static_cast(want), + static_cast(heap.Get())); + heap.Detach(); // its vtable slot may now hold a descriptor handle; never call through it again + return false; + } + using HiddenForm = D3D12_CPU_DESCRIPTOR_HANDLE*(WINAPI*)(D3D12_CPU_DESCRIPTOR_HANDLE*, IUnknown*); + const auto hidden = reinterpret_cast((*reinterpret_cast(heap.Get()))[9]); + D3D12_CPU_DESCRIPTOR_HANDLE buffer{}; + const D3D12_CPU_DESCRIPTOR_HANDLE* returned = hidden(&buffer, heap.Get()); + if (returned != &buffer || buffer.ptr != want) { + std::printf("FAIL: heap start through a return buffer: returned %p (buffer %p) holding %#llx, expected %#llx\n", + static_cast(returned), static_cast(&buffer), + static_cast(buffer.ptr), static_cast(want)); + return false; + } + const UINT64 again = invoke(heap.Get(), 9, static_cast(nullptr)); + if (again != want) { + std::printf("FAIL: heap start changed after the calls above: got %#llx, expected %#llx\n", + static_cast(again), static_cast(want)); + return false; + } + return true; +} + } // namespace int main(int argc, char** argv) @@ -1228,6 +1269,8 @@ int main(int argc, char** argv) Context c{guest.Get(), device.Get(), debug.Get()}; try { + if (!case10_heap_start_abi(c)) return 5; + std::puts("10 heap start ABI: a live object in the second register keeps the rax form; a return buffer is filled"); c.desc = {D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, side, side, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE}; c.texture_bytes = device->GetResourceAllocationInfo(0, 1, &c.desc).SizeInBytes;