From 599fe9f2212e1004f93b33a199e19146cf53d7d8 Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Tue, 24 Nov 2026 22:03:09 -0500 Subject: [PATCH 1/2] d3d12: free a declined slot-133 import at once create_host_buffer returned E_NOTIMPL for an import the driver put below the arena with its memory still allocated. The resource's destructor then handed that memory to the deferred queue, so it outlived the next submission while the shadow buffer made instead over the same address guarded its page read-only. On RADV a read-only page under a live VK_EXT_external_memory_host import makes the kernel reject that submission and every later one ("The CS has been rejected", then VK_ERROR_DEVICE_LOST). Every return after vkCreateBuffer that declines the import now destroys the buffer and frees the memory before it returns; nothing was submitted with them, since the resource is registered only on success. A counter of live slot-133 imports (rt::d3d12::title_host_imports) lets a test see it. The finding and the design are NateWeav's (coordination issue #14, comments 7141859164 and 7142359977); this implements their description. Test: d3d12_title_va_test in its imports-on mode (d3d12_geometry_views) places two buffers, at a page start and 4 bytes into a page, submits with each alive and checks that no import memory outlives a declined import. On the RDNA2 iGPU under Wine both land below the arena, the path fixed here. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/d3d12_com.h | 3 ++ runtime/shims/d3d12_vulkan_device.cpp | 48 +++++++++++++++++++++------ runtime/test/d3d12_title_va_test.cpp | 45 +++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 10 deletions(-) diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index 1a0a36c6b..66226383b 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -274,6 +274,9 @@ VkBuffer vulkan_buffer(ID3D12Resource* resource); const VulkanDeviceInfo* vulkan_device_info(ID3D12Device* device); // True when a slot-133 buffer is a shadow copied from the title's pages, false when they were imported. For tests. bool title_shadow_buffer(ID3D12Resource* resource); +// Slot-133 imports (VK_EXT_external_memory_host) whose memory `device` holds: an import declined after its memory +// was allocated is freed before create_buffer_at_host_address returns. For tests. +std::uint64_t title_host_imports(ID3D12Device* device); // VirtualQuery calls made by the title-buffer shadows in their latest refresh. For tests. std::uint64_t title_shadow_refresh_queries(ID3D12Device* device); // The trackers behind the title-buffer shadows and title windows (copies whose pages overlap or touch share one). diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index c272e03e9..9572f6d21 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -1936,6 +1936,8 @@ struct DeviceState { PFN_vkTransitionImageLayoutEXT transition_image_layout = nullptr; // VK_EXT_external_memory_host; null unless info.host_pointer_alignment is set and the entry point loaded. PFN_vkGetMemoryHostPointerPropertiesEXT get_host_pointer_properties = nullptr; + // Slot-133 imports whose memory is allocated (create_host_buffer), for title_host_imports. + std::atomic host_imports{0}; // The live shadow buffers of Xbox device slot 133 (Resource::create_shadow_buffer): title-placed buffers whose // pages the driver would not import. Each is brought up to date from the title's pages before every submission @@ -4563,7 +4565,10 @@ class Resource final device_.context->wait_retire_ticket(device_.context->submitted_retire_ticket()); auto& vk = device_.context->vk; if (buffer_) vk.vkDestroyBuffer(device_.info.device, buffer_, nullptr); - if (memory_) vk.vkFreeMemory(device_.info.device, memory_, nullptr); + if (memory_) { + vk.vkFreeMemory(device_.info.device, memory_, nullptr); + device_.host_imports.fetch_sub(1, std::memory_order_relaxed); + } buffer_ = VK_NULL_HANDLE; memory_ = VK_NULL_HANDLE; } @@ -4849,16 +4854,33 @@ class Resource final if (need_bda) info.usage |= VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; device_.share(info); if (vk.vkCreateBuffer(device_.info.device, &info, nullptr, &buffer_) != VK_SUCCESS) return E_OUTOFMEMORY; + // An import not used is destroyed here, not left to the destructor's deferred queue: there it outlived the + // next submission while the shadow the caller makes instead over the same pages guarded them, and RADV + // rejects every submission once a page under a live import is read-only (the CS is rejected, then + // VK_ERROR_DEVICE_LOST: found and measured by NateWeav, #14). Nothing was submitted with it: the resource is + // registered only on success. + const auto decline = [&](HRESULT hr) { + if (buffer_) vk.vkDestroyBuffer(device_.info.device, buffer_, nullptr); + if (memory_) { + vk.vkFreeMemory(device_.info.device, memory_, nullptr); + device_.host_imports.fetch_sub(1, std::memory_order_relaxed); + } + buffer_ = VK_NULL_HANDLE; + memory_ = VK_NULL_HANDLE; + buffer_va_ = gpu_va_ = 0; + return hr; + }; VkMemoryRequirements requirements{}; vk.vkGetBufferMemoryRequirements(device_.info.device, buffer_, &requirements); VkMemoryHostPointerPropertiesEXT host{VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT}; if (device_.get_host_pointer_properties(device_.info.device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, base_address, &host) != VK_SUCCESS) - return E_NOTIMPL; + return decline(E_NOTIMPL); const std::uint32_t types = host.memoryTypeBits & requirements.memoryTypeBits; // An offset off the buffer's own alignment cannot be bound: the fallback answers. - if (!types || (offset % requirements.alignment) || requirements.size > bytes - offset) return E_NOTIMPL; + if (!types || (offset % requirements.alignment) || requirements.size > bytes - offset) + return decline(E_NOTIMPL); VkImportMemoryHostPointerInfoEXT import{VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT}; import.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT; import.pHostPointer = base_address; @@ -4876,15 +4898,18 @@ class Resource final allocate.allocationSize = bytes; allocate.memoryTypeIndex = i; std::lock_guard serial(DeviceState::memory_mutex()); - if (vk.vkAllocateMemory(device_.info.device, &allocate, nullptr, &memory_) == VK_SUCCESS) + if (vk.vkAllocateMemory(device_.info.device, &allocate, nullptr, &memory_) == VK_SUCCESS) { memory_flags_ = device_.memory.memoryTypes[i].propertyFlags; - else + device_.host_imports.fetch_add(1, std::memory_order_relaxed); + } else { memory_ = VK_NULL_HANDLE; + } } if (memory_) break; } - if (!memory_) return E_OUTOFMEMORY; - if (vk.vkBindBufferMemory(device_.info.device, buffer_, memory_, offset) != VK_SUCCESS) return E_OUTOFMEMORY; + if (!memory_) return decline(E_OUTOFMEMORY); + if (vk.vkBindBufferMemory(device_.info.device, buffer_, memory_, offset) != VK_SUCCESS) + return decline(E_OUTOFMEMORY); if (need_bda && vk.vkGetBufferDeviceAddress) { VkBufferDeviceAddressInfo bda{VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO}; bda.buffer = buffer_; @@ -4910,9 +4935,7 @@ class Resource final address, static_cast(desc_.Width), static_cast(buffer_va_), static_cast(device_.arena.address)); - buffer_va_ = 0; - gpu_va_ = 0; - return E_NOTIMPL; + return decline(E_NOTIMPL); } mapped_ = address; host_import_ = true; @@ -15350,6 +15373,11 @@ std::uint64_t title_shadow_refresh_queries(ID3D12Device* device) { return queries; } +std::uint64_t title_host_imports(ID3D12Device* device) { + auto* d = Device::from(device); + return d ? d->state().host_imports.load(std::memory_order_relaxed) : 0; +} + std::uint64_t title_shadow_trackers(ID3D12Device* device) { auto* d = Device::from(device); if (!d) return 0; diff --git a/runtime/test/d3d12_title_va_test.cpp b/runtime/test/d3d12_title_va_test.cpp index 0adb72d25..ff7b05c01 100644 --- a/runtime/test/d3d12_title_va_test.cpp +++ b/runtime/test/d3d12_title_va_test.cpp @@ -298,6 +298,50 @@ int check_kind(Context& c, const char* kind, DWORD protect, int& checks) { return 0; } +// With imports on: a slot-133 buffer the layer does not import holds no import memory once +// create_buffer_at_host_address has returned, whichever step declined it. The memory an import declined after its +// allocation (one the driver put below the arena) used to wait in the deferred queue past the next submission, while +// the shadow made instead over the same pages made them read-only: RADV then rejects every submission (found and +// measured by NateWeav, #14). Two placements: at a page start (imported where the driver takes it) and 4 bytes into +// a page (declined after the buffer exists wherever the buffer's alignment is above 4). Then a submission over the +// shadow's pages, which must still succeed. +int check_declined_imports(Context& c) { + const std::uint64_t before = rt::d3d12::title_host_imports(c.inner); + void* pages = VirtualAlloc(nullptr, kPages, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + EXPECT(pages); + const D3D12_RESOURCE_DESC desc = buffer_desc(kPages / 2); + int shadows = 0; + for (const std::size_t at : {std::size_t(0), std::size_t(4)}) { + Ref buffer; + EXPECT(SUCCEEDED(rt::d3d12::create_buffer_at_host_address(c.inner, static_cast(pages) + at, + desc, 0xFFFFFFFFu, IID_PPV_ARGS(&buffer.p)))); + const bool shadow = rt::d3d12::title_shadow_buffer(buffer.p); + shadows += shadow; + const std::uint64_t live = rt::d3d12::title_host_imports(c.inner); + std::printf("slot-133 buffer at page offset %zu: %s, %llu import(s) live\n", at, + shadow ? "declined (shadow)" : "imported", static_cast(live - before)); + EXPECT(live == before + (shadow ? 0 : 1)); + // A submission with the buffer alive: the shadow's tracker guards its pages here. + Ref allocator; + Ref list; + EXPECT(SUCCEEDED(c.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, + IID_PPV_ARGS(&allocator.p)))); + EXPECT(SUCCEEDED(c.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, + IID_PPV_ARGS(&list.p)))); + EXPECT(list->Close() == S_OK); + ID3D12CommandList* lists[] = {list.p}; + c.queue->ExecuteCommandLists(1, lists); + EXPECT(rt::d3d12::last_execute_result(c.queue) == leonida::ReplayResult::success); + EXPECT(SUCCEEDED(c.queue->Signal(c.fence, ++c.fence_value))); + EXPECT(SUCCEEDED(c.fence->SetEventOnCompletion(c.fence_value, nullptr))); + buffer.reset(); + EXPECT(rt::d3d12::title_host_imports(c.inner) == before); + } + VirtualFree(pages, 0, MEM_RELEASE); + std::printf("declined imports: %d of 2 placements took the shadow, none left import memory behind\n", shadows); + return 0; +} + // The title's own memory outside any slot-133 buffer: a VirtualAlloc range standing for its per-frame constant ring // (#8862 section 3.5). Three ways a shader is given an address in it, each read back through an arena buffer: // - a buffer V# the CPU writes into a shader-visible heap itself, as the title does (rewritten before the submission); @@ -794,6 +838,7 @@ int main(int argc, char** argv) { EXPECT(d.validation_errors() == 0); return 0; } + if (geometry && check_declined_imports(c)) return 1; if (check_kind(c, "read-write", PAGE_READWRITE, checks)) return 1; if (check_kind(c, "write-combined", PAGE_READWRITE | PAGE_WRITECOMBINE, checks)) return 1; if (check_title_memory(c, checks, geometry)) return 1; From 9f82f136f68a84bafa745ff61eb6eebb01128654 Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Tue, 24 Nov 2026 22:03:36 -0500 Subject: [PATCH 2/2] d3d12: pin the pages of a live host import RADV checks the pages of a VK_EXT_external_memory_host import again at every submission, and once one of them is read-only the kernel rejects that submission and every later one. The slot-133 shadow trackers and the geometry watch make copied title pages read-only to see the next write, and a page can be both under a shadow and under a live import. A new leonida::kernel_writes::hold pins a range until release() and opens it. Unlike begin_write it records the pin when no opener is set yet (no tracker exists), so a tracker made later passes over the pages too; ShadowPages and the geometry watch already pass over pinned pages. The slot-133 import (create_host_buffer) and the console descriptor heap import take the pin before vkAllocateMemory, so no guard lands between the writability check and the import, and end it on every path that frees the memory: a declined or failed import and the release. The finding and the design are NateWeav's (coordination issue #14, comments 7141859164 and 7142359977); this implements their description. As in their tree the pin is unconditional; a driver gate is not done. Tests: kernel_writes_test checks hold and release on the pin set (no opener, with an opener, alongside a file read's pin). d3d12_title_va_test in its imports-on mode makes a shadow over pages, checks that its tracker guards them, then imports the same pages and checks that two submissions leave them writable, that the pin ends with the import and that the tracker guards them again afterwards. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/d3d12_vulkan_device.cpp | 11 ++++ runtime/src/title_page_watch.h | 25 +++++++++ runtime/test/d3d12_title_va_test.cpp | 80 +++++++++++++++++++++++++++ runtime/test/kernel_writes_test.cpp | 55 +++++++++++++++++- 4 files changed, 169 insertions(+), 2 deletions(-) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 9572f6d21..25bb7d33b 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -2452,6 +2452,7 @@ struct DeviceState { // import VkBuffer buffer = VK_NULL_HANDLE; VkDeviceMemory memory = VK_NULL_HANDLE; + std::uint64_t pin = 0; // the imported pages' leonida::kernel_writes pin while `memory` lives std::size_t scan_at = 0; // the next slot the T# scan reads // copy bool identity = false; @@ -2666,6 +2667,7 @@ struct DeviceState { const auto fail = [&] { if (heap.buffer) vk.vkDestroyBuffer(info.device, heap.buffer, nullptr); if (heap.memory) vk.vkFreeMemory(info.device, heap.memory, nullptr); + leonida::kernel_writes::release(std::exchange(heap.pin, 0)); heap.buffer = VK_NULL_HANDLE; heap.memory = VK_NULL_HANDLE; return false; @@ -2683,6 +2685,7 @@ struct DeviceState { import.pHostPointer = const_cast(pages); VkMemoryAllocateFlagsInfo flags{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO, &import}; flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; + heap.pin = leonida::kernel_writes::hold(pages, std::size_t(heap.bytes)); // as create_host_buffer's for (const VkMemoryPropertyFlags want : {VkMemoryPropertyFlags(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT), VkMemoryPropertyFlags(0)}) { @@ -2805,6 +2808,7 @@ struct DeviceState { context->wait_retire_ticket(context->submitted_retire_ticket()); context->vk.vkDestroyBuffer(info.device, heap->buffer, nullptr); context->vk.vkFreeMemory(info.device, heap->memory, nullptr); + leonida::kernel_writes::release(std::exchange(heap->pin, 0)); } else { arena_release(heap->shader_va, heap->bytes, arena_descriptor_heap); } @@ -4569,6 +4573,7 @@ class Resource final vk.vkFreeMemory(device_.info.device, memory_, nullptr); device_.host_imports.fetch_sub(1, std::memory_order_relaxed); } + leonida::kernel_writes::release(std::exchange(host_pin_, 0)); buffer_ = VK_NULL_HANDLE; memory_ = VK_NULL_HANDLE; } @@ -4865,6 +4870,7 @@ class Resource final vk.vkFreeMemory(device_.info.device, memory_, nullptr); device_.host_imports.fetch_sub(1, std::memory_order_relaxed); } + leonida::kernel_writes::release(std::exchange(host_pin_, 0)); buffer_ = VK_NULL_HANDLE; memory_ = VK_NULL_HANDLE; buffer_va_ = gpu_va_ = 0; @@ -4886,6 +4892,10 @@ class Resource final import.pHostPointer = base_address; VkMemoryAllocateFlagsInfo flags{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO, &import}; flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; + // The imported pages are pinned before the import and until its memory is freed, so the shadow trackers + // and the geometry watch never make them read-only meanwhile (leonida::kernel_writes::hold). Taken first, + // so no guard lands between the writability check above and the import. + host_pin_ = leonida::kernel_writes::hold(base_address, std::size_t(bytes)); // Host-visible and coherent first, as the caller's pages are; then any type both the import and the // buffer take. for (const VkMemoryPropertyFlags want : @@ -6154,6 +6164,7 @@ class Resource final std::uint64_t buffer_va_ = 0; std::uint64_t gpu_va_ = 0; bool host_import_ = false; // memory_ is the caller's pages (create_host_buffer): freed at once, not deferred + std::uint64_t host_pin_ = 0; // create_host_buffer: the pages' leonida::kernel_writes pin while memory_ lives std::uint8_t* shadow_ = nullptr; // create_shadow_buffer: memory_ mapped, in DeviceState::shadows bool aliased_ = false; // va_ is in leonida::title_va_aliases(), reached at title_alias_ std::uint64_t title_alias_ = 0; diff --git a/runtime/src/title_page_watch.h b/runtime/src/title_page_watch.h index b83e71b7b..580edf622 100644 --- a/runtime/src/title_page_watch.h +++ b/runtime/src/title_page_watch.h @@ -636,6 +636,31 @@ inline void end_write(std::uint64_t id, bool pending) { s.count.store(s.pins.size(), std::memory_order_release); } +// For as long as the GPU may use [buffer, buffer + bytes) as host memory (the D3D12 layer's VK_EXT_external_memory_host +// imports): pins the range until release() and opens it. Unlike begin_write it records the pin before any tracker +// exists, so a tracker made later passes over the pages too. RADV checks an import's pages again at every submission, +// and once one of them is read-only the kernel rejects that submission and every later one ("The CS has been +// rejected", then VK_ERROR_DEVICE_LOST; found and measured by NateWeav, #14), so no guard may land on them. Returns +// the pin's id, 0 for an empty range. +inline std::uint64_t hold(const void* buffer, std::size_t bytes) { + if (!buffer || !bytes) return 0; + State& s = state(); + const auto begin = reinterpret_cast(buffer); + std::uint64_t id = 0; + { + std::lock_guard lock(s.mutex); + id = s.next_id++; + s.pins.push_back({begin, begin + bytes, nullptr, id}); + s.count.store(s.pins.size(), std::memory_order_release); + } + // After the pin: a guard a refresh made before it is lifted, and no refresh from here on makes one. + if (const Opener opener = s.opener.load(std::memory_order_acquire)) opener(begin, begin + bytes); + return id; +} + +// The import is freed: its pin ends. +inline void release(std::uint64_t id) { end_write(id, false); } + // Readies [buffer, buffer + bytes) for a retry after ERROR_NOACCESS: commits xmem's pages that wait for their first // touch (the kernel's write does not fault them in) and opens watched pages again. false: neither side is set. inline bool reopen(const void* buffer, std::size_t bytes) { diff --git a/runtime/test/d3d12_title_va_test.cpp b/runtime/test/d3d12_title_va_test.cpp index ff7b05c01..2aac6c680 100644 --- a/runtime/test/d3d12_title_va_test.cpp +++ b/runtime/test/d3d12_title_va_test.cpp @@ -342,6 +342,85 @@ int check_declined_imports(Context& c) { return 0; } +// With imports on: a page a shadow tracker watches and a live import shares stays writable across submissions. RADV +// rejects every submission once a page under a live import is read-only (found and measured by NateWeav, #14), so an +// import pins its pages (leonida::kernel_writes::hold) and the tracker passes over them. The shadow comes first: its +// pages are read-only when the buffer is made, so the layer does not import them. Made writable again, the tracker +// guards them at the next submission (the control); written, they are open again; then an import over the same pages, +// and two submissions that must leave them writable. +int check_import_over_shadow(Context& c) { + auto* pages = static_cast( + VirtualAlloc(nullptr, kPages, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + EXPECT(pages); + const auto protection = [&] { + MEMORY_BASIC_INFORMATION region{}; + return VirtualQuery(pages, ®ion, sizeof(region)) ? region.Protect & 0xFF : 0; + }; + const auto submit = [&]() -> int { + Ref allocator; + Ref list; + EXPECT(SUCCEEDED(c.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, + IID_PPV_ARGS(&allocator.p)))); + EXPECT(SUCCEEDED(c.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, + IID_PPV_ARGS(&list.p)))); + EXPECT(list->Close() == S_OK); + ID3D12CommandList* lists[] = {list.p}; + c.queue->ExecuteCommandLists(1, lists); + EXPECT(rt::d3d12::last_execute_result(c.queue) == leonida::ReplayResult::success); + EXPECT(SUCCEEDED(c.queue->Signal(c.fence, ++c.fence_value))); + EXPECT(SUCCEEDED(c.fence->SetEventOnCompletion(c.fence_value, nullptr))); + return 0; + }; + const D3D12_RESOURCE_DESC desc = buffer_desc(kPages); + DWORD old = 0; + EXPECT(VirtualProtect(pages, kPages, PAGE_READONLY, &old)); + Ref shadow, import; + EXPECT(SUCCEEDED(rt::d3d12::create_buffer_at_host_address(c.inner, pages, desc, 0xFFFFFFFFu, + IID_PPV_ARGS(&shadow.p)))); + EXPECT(rt::d3d12::title_shadow_buffer(shadow.p)); + EXPECT(VirtualProtect(pages, kPages, PAGE_READWRITE, &old)); + if (submit()) return 1; + const bool guarded = protection() == PAGE_READONLY; + pages[0] = 1; // the tracker's fault handler opens the page again + EXPECT(protection() == PAGE_READWRITE); + const std::uint64_t imports = rt::d3d12::title_host_imports(c.inner); + EXPECT(SUCCEEDED(rt::d3d12::create_buffer_at_host_address(c.inner, pages, desc, 0xFFFFFFFFu, + IID_PPV_ARGS(&import.p)))); + const bool imported = !rt::d3d12::title_shadow_buffer(import.p); + std::size_t pinned = 0; + const auto begin = reinterpret_cast(pages); + leonida::kernel_writes::for_each_pin(begin, begin + kPages, + [&](std::uintptr_t from, std::uintptr_t to) { pinned += to - from; }); + std::printf("import over a shadow's pages: the tracker %s them alone; the second buffer %s, %zu KiB pinned\n", + guarded ? "guarded" : "did not guard", imported ? "imported" : "declined (shadow)", pinned >> 10); + EXPECT(guarded); // the control: without a pin the tracker makes these pages read-only + if (!imported) { + EXPECT(pinned == 0 && rt::d3d12::title_host_imports(c.inner) == imports); + import.reset(); + shadow.reset(); + VirtualFree(pages, 0, MEM_RELEASE); + return 0; + } + EXPECT(pinned >= kPages && rt::d3d12::title_host_imports(c.inner) == imports + 1); + pages[1] = 2; // written again: the tracker would guard these pages at the next refresh + for (int i = 0; i < 2; ++i) { + if (submit()) return 1; + EXPECT(protection() == PAGE_READWRITE); + } + import.reset(); + pinned = 0; + leonida::kernel_writes::for_each_pin(begin, begin + kPages, + [&](std::uintptr_t from, std::uintptr_t to) { pinned += to - from; }); + EXPECT(pinned == 0); // the import's memory is freed: its pin ended + pages[2] = 3; + if (submit()) return 1; + EXPECT(protection() == PAGE_READONLY); // and the tracker guards the pages again + shadow.reset(); + EXPECT(protection() == PAGE_READWRITE); + VirtualFree(pages, 0, MEM_RELEASE); + return 0; +} + // The title's own memory outside any slot-133 buffer: a VirtualAlloc range standing for its per-frame constant ring // (#8862 section 3.5). Three ways a shader is given an address in it, each read back through an arena buffer: // - a buffer V# the CPU writes into a shader-visible heap itself, as the title does (rewritten before the submission); @@ -839,6 +918,7 @@ int main(int argc, char** argv) { return 0; } if (geometry && check_declined_imports(c)) return 1; + if (geometry && check_import_over_shadow(c)) return 1; if (check_kind(c, "read-write", PAGE_READWRITE, checks)) return 1; if (check_kind(c, "write-combined", PAGE_READWRITE | PAGE_WRITECOMBINE, checks)) return 1; if (check_title_memory(c, checks, geometry)) return 1; diff --git a/runtime/test/kernel_writes_test.cpp b/runtime/test/kernel_writes_test.cpp index 20b347748..070ae3488 100644 --- a/runtime/test/kernel_writes_test.cpp +++ b/runtime/test/kernel_writes_test.cpp @@ -1,7 +1,8 @@ // leonida::kernel_writes::read_file (runtime/src/title_page_watch.h) into memory that is reserved until its first // touch, as xmem maps the title's physical pages (SEC_RESERVE section views committed by a fault handler): the kernel's -// write does not fault such a page in, so the read fails with ERROR_NOACCESS unless the committer runs first. No game -// data. +// write does not fault such a page in, so the read fails with ERROR_NOACCESS unless the committer runs first. Then +// kernel_writes::hold, the pin a host import keeps over its pages: recorded with no opener set (no tracker yet), seen +// by for_each_pin until release(), untouched by a file read's pin ending over the same pages. No game data. #include #include @@ -27,6 +28,55 @@ constexpr DWORD kFileBytes = 256 * 1024; std::uint8_t* g_view = nullptr; int g_commits = 0; +int g_opens = 0; +std::uintptr_t g_opened_begin = 0, g_opened_end = 0; + +void count_open(std::uintptr_t begin, std::uintptr_t end) { + ++g_opens; + g_opened_begin = begin; + g_opened_end = end; +} + +// The bytes of [begin, end) the pins cover. +std::size_t pinned_bytes(std::uintptr_t begin, std::uintptr_t end) { + std::size_t bytes = 0; + leonida::kernel_writes::for_each_pin(begin, end, [&](std::uintptr_t from, std::uintptr_t to) { bytes += to - from; }); + return bytes; +} + +void check_holds() { + namespace kw = leonida::kernel_writes; + static std::uint8_t pages[3 * 4096]; + const auto begin = reinterpret_cast(pages); + kw::set_opener(nullptr); + check(pinned_bytes(begin, begin + sizeof(pages)) == 0, "no pin before the hold"); + check(kw::hold(pages, 0) == 0 && kw::hold(nullptr, 4096) == 0, "an empty hold pins nothing"); + // No opener: begin_write records nothing, a hold does (a tracker made later must still pass over the pages). + check(kw::begin_write(pages, 4096, nullptr) == 0, "begin_write records nothing without an opener"); + const std::uint64_t first = kw::hold(pages, 2 * 4096); + check(first != 0 && pinned_bytes(begin, begin + sizeof(pages)) == 2 * 4096, + "a hold without an opener pins its range"); + check(pinned_bytes(begin + 2 * 4096, begin + sizeof(pages)) == 0, "and nothing past it"); + // With an opener: the range is opened once the pin is in. + kw::set_opener(&count_open); + const std::uint64_t second = kw::hold(pages + 4096, 2 * 4096); + check(second != 0 && second != first && g_opens == 1 && g_opened_begin == begin + 4096 && + g_opened_end == begin + 3 * 4096, + "a hold with an opener opens exactly its range"); + // A file read's pin over the same pages ends without ending the holds. + const std::uint64_t read = kw::begin_write(pages, sizeof(pages), nullptr); + check(read != 0 && pinned_bytes(begin, begin + sizeof(pages)) == 2 * 4096 + 2 * 4096 + sizeof(pages), + "a file read's pin adds to the holds"); + kw::end_write(read, false); + check(pinned_bytes(begin, begin + sizeof(pages)) == 4 * 4096, "its end leaves the holds"); + kw::end_write(first, true); // as a pending overlapped read would: a hold has no OVERLAPPED, so it ends too + check(pinned_bytes(begin, begin + 4096) == 0 && pinned_bytes(begin + 4096, begin + sizeof(pages)) == 2 * 4096, + "ending one hold leaves the other"); + kw::release(second); + kw::release(0); + check(pinned_bytes(begin, begin + sizeof(pages)) == 0, "released, no pin is left"); + kw::set_opener(nullptr); +} // Stands in for rt::xmem::commit_physical_range: commits the part of the test view the range covers. bool commit_view(const void* address, std::size_t bytes) { @@ -80,6 +130,7 @@ int main() { "a read into committed memory does not call the committer"); leonida::kernel_writes::set_committer(nullptr); + check_holds(); UnmapViewOfFile(g_view); CloseHandle(section); CloseHandle(file);