From 573c9184ec1c0295a4985277d9919dc1c09b0f5f Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Wed, 25 Nov 2026 03:25:22 -0600 Subject: [PATCH 1/2] tests: d3d12test::Ref releases on `= {}`; slot 133 teardown test (RT-SLOT133-STANDIN-LEAK) The 57-63 Vulkan objects reported at vkDestroyDevice (VUID-vkDestroyDevice-device-05137) after a slot 133 placement were the test's, not the runtime's. d3d12test::Ref had no assignment operator of its own, so `placed = {};` (PR #1558's check_d16s8_refused_uav) copied a null pointer over the reference without Release: the placed texture, and through its device reference the whole D3D12 device, outlived the test. Ref now has a move assignment that releases what it held, a move constructor, and deleted copies (two Refs on one reference release it twice). Nothing on main copies a Ref or assigns `{}` to one. xbox_slot133_teardown places, through slot 133 on the Vulkan device behind the proxy, a 1280x720 format-118 depth target without UAV, a 256x256 RGBA8 texture, an RGBA8 render target and a buffer; drops each with `= {}`, checks the proxy's reference count is back where it was, releases the device and reads the validation error count only after vkDestroyDevice. With the old Ref it fails ("keeps 3 after release"); with this one it passes, plain and with LEONIDA_VK_VALIDATION=1. Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 8 ++ runtime/test/d3d12_test_shaders.h | 16 +++ runtime/test/xbox_slot133_teardown_test.cpp | 132 ++++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 runtime/test/xbox_slot133_teardown_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 2763e9d1b0..10e2c06fbe 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2919,6 +2919,14 @@ if(TARGET Vulkan::Headers) add_test(NAME xbox_d16s8_surface COMMAND xbox_d16s8_surface_test) set_tests_properties(xbox_d16s8_surface PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 20 ENVIRONMENT "LEONIDA_CONSOLE_D16S8=native") + # Slot 133 textures leave nothing of the device behind once released; validation errors are counted through + # vkDestroyDevice, where a device object left alive is reported (run it with LEONIDA_VK_VALIDATION=1). + add_executable(xbox_slot133_teardown_test test/xbox_slot133_teardown_test.cpp shims/d3d12_xs_device.cpp + shims/d3d12_xs_pso_stream.cpp) + target_link_libraries(xbox_slot133_teardown_test PRIVATE gta6rt_d3d12_com) + target_compile_definitions(xbox_slot133_teardown_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX LEONIDA_VULKAN_DEVICE) + add_test(NAME xbox_slot133_teardown COMMAND xbox_slot133_teardown_test) + set_tests_properties(xbox_slot133_teardown PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 30) add_executable(xbox_gpu_label_test test/xbox_gpu_label_test.cpp shims/d3d12_xs_device.cpp shims/d3d12_xs_pso_stream.cpp) target_link_libraries(xbox_gpu_label_test PRIVATE gta6rt_d3d12_com) target_compile_definitions(xbox_gpu_label_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX LEONIDA_VULKAN_DEVICE) diff --git a/runtime/test/d3d12_test_shaders.h b/runtime/test/d3d12_test_shaders.h index a43c141240..c814e68ddf 100644 --- a/runtime/test/d3d12_test_shaders.h +++ b/runtime/test/d3d12_test_shaders.h @@ -166,9 +166,25 @@ inline D3D12_RESOURCE_BARRIER transition(ID3D12Resource* resource, D3D12_RESOURC return barrier; } +// Owns one COM reference. `ref = {}` releases it like reset() does: the assignment a test writes to drop an object +// mid-test. Without this move assignment that line copied the null pointer over `p` and never called Release, and an +// object the test meant to free kept the device alive to the end (the slot 133 "leak" of RT-SLOT133-STANDIN-LEAK: +// VUID-vkDestroyDevice-device-05137 with ~60 objects). Copies are refused: two Refs on one reference release it twice. template struct Ref { T* p = nullptr; + Ref() = default; + Ref(const Ref&) = delete; + Ref& operator=(const Ref&) = delete; + Ref(Ref&& other) noexcept : p(other.p) { other.p = nullptr; } + Ref& operator=(Ref&& other) noexcept { + if (this != &other) { + reset(); + p = other.p; + other.p = nullptr; + } + return *this; + } ~Ref() { reset(); } void reset() { if (p) p->Release(); diff --git a/runtime/test/xbox_slot133_teardown_test.cpp b/runtime/test/xbox_slot133_teardown_test.cpp new file mode 100644 index 0000000000..ce31a4a887 --- /dev/null +++ b/runtime/test/xbox_slot133_teardown_test.cpp @@ -0,0 +1,132 @@ +// Xbox device slot 133 (CreatePlacedResourceX) textures and device teardown (RT-SLOT133-STANDIN-LEAK). +// +// A texture placed through slot 133 is a committed texture whose GPU VA is the title's address +// (create_buffer_at_host_address, docs/d3d12_vulkan.md). Once it is released and then the device, nothing of the +// device may stay behind: under LEONIDA_VK_VALIDATION=1 a device that never went leaves its Vulkan objects at +// vkDestroyDevice (VUID-vkDestroyDevice-device-05137, an error the validation callback counts). So the error count +// is read only after the Vulkan device is gone, which the other slot 133 tests do not do. +// +// The ~60 objects reported for this path were the test's, not the layer's: `placed = {}` on a d3d12test::Ref copied a +// null pointer over the reference without releasing it, so the texture, and through it the device, lived on. Ref now +// releases on that assignment, and this test drops its objects the same way. +// +// The shapes are the ones the finding named: a 1280x720 format-118 (console D16S8) depth target without unordered +// access and a plain RGBA8 texture, plus an RGBA8 render target and a buffer through the same slot. No game files. +#include "vk_test_device.h" +#include "d3d12_test_shaders.h" +#include "../shims/d3d12_com.h" +#include "../shims/d3d12_xs_device.h" +#include +#include + +using namespace d3d12test; +#define VERIFY(expr) do { if (!(expr)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #expr); return 1; } } while (0) + +namespace { +using Place = HRESULT(WINAPI*)(void*, UINT64, const D3D12_RESOURCE_DESC*, UINT, const D3D12_CLEAR_VALUE*, const IID*, + void**); + +// One slot 133 placement on fresh title pages, released again. `gpu_va` is what the texture must report. +int place(ID3D12Device* xbox, const D3D12_RESOURCE_DESC& desc, const D3D12_CLEAR_VALUE* clear, const char* what) { + constexpr SIZE_T pages = 16u << 20; // past each shape's size on any GPU + void* va = VirtualAlloc(nullptr, pages, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + VERIFY(va); + const unsigned long before = rt::d3d12_xs::device_proxy_ref_count(xbox); + Ref placed; + const HRESULT hr = reinterpret_cast(slot(xbox, 133))(xbox, reinterpret_cast(va), &desc, + 0xFFFFFFFFu, clear, &__uuidof(ID3D12Resource), + reinterpret_cast(&placed.p)); + const bool made = SUCCEEDED(hr) && placed.p; + if (!made) std::fprintf(stderr, "slot 133 %s: hr=0x%08x\n", what, unsigned(hr)); + // The resource's GetDevice answers the proxy, so it holds a reference to it while it lives, and gives it back. + Ref parent; + const bool device_ok = made && SUCCEEDED(placed->GetDevice(IID_PPV_ARGS(&parent.p))) && parent.p == xbox; + const unsigned long during = rt::d3d12_xs::device_proxy_ref_count(xbox); + parent = {}; + // `= {}` is how a test drops an object mid-test: it must release it (d3d12test::Ref's move assignment). It used + // to copy a null pointer over the reference, and the texture, and through it the device, outlived the test. + placed = {}; + VERIFY(!placed.p && !parent.p); + const unsigned long after = rt::d3d12_xs::device_proxy_ref_count(xbox); + VERIFY(!made || during == before + 2); // the texture's reference and GetDevice's + VirtualFree(va, 0, MEM_RELEASE); + VERIFY(made); + VERIFY(device_ok); + if (after != before) + std::fprintf(stderr, "slot 133 %s: the proxy had %lu references before and keeps %lu after release\n", what, + before, after); + VERIFY(after == before); + std::printf("slot 133 %s: placed and released, device references %lu -> %lu\n", what, before, after); + return 0; +} + +int run(vktest::Device& gpu) { + rt::d3d12::VulkanDeviceInfo info{}; + info.bindless = gpu.bindless; + info.bindless_partially_bound = gpu.partially_bound; + info.get_instance_proc = gpu.instance_proc; + info.instance = gpu.instance; + info.physical = gpu.physical; + info.device = gpu.device; + info.queue = gpu.queue; + info.queue_family = gpu.family; + info.buffer_device_address = true; + ID3D12Device* dev{}; + VERIFY(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&dev)))); + ID3D12Device* proxy{}; + VERIFY(SUCCEEDED(rt::d3d12_xs::create_device_proxy(dev, __uuidof(ID3D12Device), reinterpret_cast(&proxy), + true))); + rt::d3d12::set_device_outer(dev, proxy); // the proxy owns `dev` from here + VERIFY(rt::d3d12_xs::device_proxy_ref_count(proxy) == 1); + + D3D12_RESOURCE_DESC depth{}; + depth.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + depth.Width = 1280; + depth.Height = 720; + depth.DepthOrArraySize = depth.MipLevels = 1; + depth.SampleDesc.Count = 1; + depth.Format = static_cast(118); // the console's D16S8 + depth.Layout = static_cast(0x101); + depth.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + D3D12_CLEAR_VALUE depth_clear{}; + depth_clear.Format = depth.Format; + depth_clear.DepthStencil.Depth = 1; + VERIFY(place(proxy, depth, &depth_clear, "1280x720 format-118 depth target without UAV") == 0); + + D3D12_RESOURCE_DESC color = depth; + color.Width = color.Height = 256; + color.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + color.Flags = D3D12_RESOURCE_FLAG_NONE; + VERIFY(place(proxy, color, nullptr, "256x256 RGBA8 texture") == 0); + + color.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + D3D12_CLEAR_VALUE color_clear{}; + color_clear.Format = color.Format; + VERIFY(place(proxy, color, &color_clear, "256x256 RGBA8 render target") == 0); + + D3D12_RESOURCE_DESC buffer = buffer_desc(65536); + buffer.Layout = static_cast(1); // the Xbox row-major word + VERIFY(place(proxy, buffer, nullptr, "64 KiB buffer") == 0); + + VERIFY(rt::d3d12_xs::device_proxy_ref_count(proxy) == 1); + VERIFY(proxy->Release() == 0); + return 0; +} +} // namespace + +int main() { + { + auto gpu = std::make_unique(); + const auto status = gpu->initialize(); + if (status == vktest::DeviceStatus::unavailable) return 77; + VERIFY(status == vktest::DeviceStatus::ready); + VERIFY(run(*gpu) == 0); + } // vkDestroyDevice: a device object still alive is reported here, under validation + if (vktest::validation_error_count() != 0) { + std::fprintf(stderr, "%u validation errors, counted through vkDestroyDevice\n", + vktest::validation_error_count()); + return 1; + } + std::printf("slot 133 placements leave nothing of the device behind at vkDestroyDevice\n"); + return 0; +} From 3819f0a0fcd280158bc8f34cd3965bb663e407f6 Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Thu, 26 Nov 2026 05:20:18 -0600 Subject: [PATCH 2/2] tests: leave the Ref change to #1758 (TEST-REF-MOVE-ONLY) #1758 makes the same d3d12test::Ref change (move-only, `ref = {}` releases). Keep this pull request to the slot 133 teardown test, which depends on that change. AI-assisted (Claude Code). --- runtime/test/d3d12_test_shaders.h | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/runtime/test/d3d12_test_shaders.h b/runtime/test/d3d12_test_shaders.h index c814e68ddf..a43c141240 100644 --- a/runtime/test/d3d12_test_shaders.h +++ b/runtime/test/d3d12_test_shaders.h @@ -166,25 +166,9 @@ inline D3D12_RESOURCE_BARRIER transition(ID3D12Resource* resource, D3D12_RESOURC return barrier; } -// Owns one COM reference. `ref = {}` releases it like reset() does: the assignment a test writes to drop an object -// mid-test. Without this move assignment that line copied the null pointer over `p` and never called Release, and an -// object the test meant to free kept the device alive to the end (the slot 133 "leak" of RT-SLOT133-STANDIN-LEAK: -// VUID-vkDestroyDevice-device-05137 with ~60 objects). Copies are refused: two Refs on one reference release it twice. template struct Ref { T* p = nullptr; - Ref() = default; - Ref(const Ref&) = delete; - Ref& operator=(const Ref&) = delete; - Ref(Ref&& other) noexcept : p(other.p) { other.p = nullptr; } - Ref& operator=(Ref&& other) noexcept { - if (this != &other) { - reset(); - p = other.p; - other.p = nullptr; - } - return *this; - } ~Ref() { reset(); } void reset() { if (p) p->Release();