From 62e0a3c18684f7005eb6fa6b4f0e5f42dc7888ad Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Sun, 29 Nov 2026 16:15:24 +0100 Subject: [PATCH 1/2] runtime: probe an unreadable memory SRV range once per refusal --- src/startup_stubs/d3d12_memory_srv.inl | 28 ++++++++++-------- tests/startup_memory_srv_diag.cpp | 39 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/startup_stubs/d3d12_memory_srv.inl b/src/startup_stubs/d3d12_memory_srv.inl index 9d98b3eaee..57663f42ab 100644 --- a/src/startup_stubs/d3d12_memory_srv.inl +++ b/src/startup_stubs/d3d12_memory_srv.inl @@ -542,19 +542,20 @@ const char* console_address_hold(Wrapper* device, u64 address, const D3D12_RESOU // Why publish_tiled_texture (or the lookup of a live resource) gave no texture: the causes the census keeps apart // (x2w_startup::texture_unavailable_cause). -const char* texture_unavailable_reason(Wrapper* device, u64 address, const D3D12_RESOURCE_DESC& desc, bool published) +const char* texture_unavailable_reason(Wrapper* device, const D3D12_RESOURCE_DESC& desc, bool published, bool unreadable) { const u64 bytes = published ? address_texture_bytes(device, desc) : 0; - return x2w_startup::texture_unavailable_cause(published, bytes, [&] { - return x2w_local::readable(reinterpret_cast(address), bytes); - }); + return x2w_startup::texture_unavailable_cause(published, bytes, [unreadable] { return !unreadable; }); } -Wrapper* publish_tiled_texture(Wrapper* device, u64 address, const D3D12_RESOURCE_DESC& desc) +Wrapper* publish_tiled_texture(Wrapper* device, u64 address, const D3D12_RESOURCE_DESC& desc, bool& unreadable) { + unreadable = false; const auto bytes = address_texture_bytes(device, desc); // The transient upload ring accepts individual copies up to 64 MiB. - if (bytes > x2w_startup::max_published_texture_bytes || !x2w_local::readable(reinterpret_cast(address), bytes)) return nullptr; + if (bytes > x2w_startup::max_published_texture_bytes) return nullptr; + unreadable = !x2w_local::readable(reinterpret_cast(address), bytes); + if (unreadable) return nullptr; std::lock_guard lock(tiled_srv_mutex); const auto [first, last] = tiled_srv_addresses.equal_range(address); for (auto it = first; it != last; ++it) { @@ -706,17 +707,18 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou refuse_memory_srv(refused, "unsupported memory SRV arguments", reason, resource, input, destination); return; } + bool unreadable = false; // A single-element texture is published by address like a tiled view: its texel is read at offset 0. if (x2w_startup::supports_single_element_srv(*resource, input->view)) { if (gpu_written_overlap(input->address, x2w_startup::single_element_bytes(*resource))) { refuse_memory_srv(refused, "GPU-written resource at the supplied address", "gpu_written_resource", resource, input, destination); - } else if (Wrapper* texture = publish_tiled_texture(self, input->address, *resource)) { + } else if (Wrapper* texture = publish_tiled_texture(self, input->address, *resource, unreadable)) { view_slot<18, 2>(self, reinterpret_cast(texture), reinterpret_cast(&input->view), destination, 0); note_memory_srv_write(resource, input, destination); } else { refuse_memory_srv(refused, "unavailable texture at the supplied base and layout", - texture_unavailable_reason(self, input->address, *resource, true), resource, input, destination); + texture_unavailable_reason(self, *resource, true, unreadable), resource, input, destination); } return; } @@ -764,7 +766,8 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou held = "partial_tile_beyond_xg_size"; } Wrapper* texture = held ? nullptr - : (tiled_bc_srv(*resource) || x2w_startup::bc64::supports(*resource)) ? publish_tiled_texture(self, input->address, *resource) + : (tiled_bc_srv(*resource) || x2w_startup::bc64::supports(*resource)) + ? publish_tiled_texture(self, input->address, *resource, unreadable) : existing_memory_texture(input->address, *resource); if (first) log_partial_tile_view(*resource, input->address, held, texture != nullptr); // A new console-layout view reads the address only when there is no matching live resource. @@ -778,14 +781,15 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou held = console_address_hold(self, input->address, *resource); if (!held && address_resource_overlap(input->address, address_texture_bytes(self, *resource))) held = "overlapping_resource"; - texture = held ? nullptr : publish_tiled_texture(self, input->address, *resource); + texture = held ? nullptr : publish_tiled_texture(self, input->address, *resource, unreadable); outcome = "published console layout"; } } // A publication was attempted for a BC / console-layout view; any other view only looks up a live resource. const char* unavailable = texture || held ? nullptr - : texture_unavailable_reason(self, input->address, *resource, - tiled_bc_srv(*resource) || x2w_startup::bc64::supports(*resource) || console_layout_srv(*resource)); + : texture_unavailable_reason(self, *resource, + tiled_bc_srv(*resource) || x2w_startup::bc64::supports(*resource) || console_layout_srv(*resource), + unreadable); if (!texture) outcome = held ? held : unavailable; log_address_kind(outcome, *resource, input->view, input->address); if (!texture) { diff --git a/tests/startup_memory_srv_diag.cpp b/tests/startup_memory_srv_diag.cpp index a088bd1720..913b9acb65 100644 --- a/tests/startup_memory_srv_diag.cpp +++ b/tests/startup_memory_srv_diag.cpp @@ -27,6 +27,18 @@ struct ViewAt { Word tail[2]; }; static_assert(offsetof(ViewAt, address) == 0x28 && sizeof(ViewAt) == 0x40); + +thread_local ULONG_PTR reserved_texel_address; +thread_local unsigned reserved_texel_reads; + +LONG CALLBACK count_reserved_texel_reads(EXCEPTION_POINTERS* exception) +{ + const auto& record = *exception->ExceptionRecord; + if (record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION && record.NumberParameters >= 2 && + record.ExceptionInformation[0] == 0 && record.ExceptionInformation[1] == reserved_texel_address) + ++reserved_texel_reads; + return EXCEPTION_CONTINUE_SEARCH; +} } // namespace int draw_memory_srv_diagnostics(ID3D12Device* desktop, void* guest, void* list) @@ -108,6 +120,33 @@ int draw_memory_srv_diagnostics(ID3D12Device* desktop, void* guest, void* list) invoke(guest, 70, ptr(&r8), 0, cpu); if (!has_report_prefix("x2w-d3d12 memory SRV refusals (slot 70): total 12; " "null_destination 1, unreadable_resource 1, unreadable_view 1,")) return 55; + + // Each readability probe of this reserved, one-texel range faults on its first read. + const D3D12_RESOURCE_DESC texel{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, 1, 1, 1, 1, + DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE}; + void* reserved = VirtualAlloc(nullptr, 4096, MEM_RESERVE, PAGE_READWRITE); + if (!reserved) return 56; + ViewAt unreadable = v; + unreadable.view.Format = texel.Format; + unreadable.address = ptr(reserved); + reserved_texel_address = unreadable.address; + reserved_texel_reads = 0; + const auto handler = AddVectoredExceptionHandler(1, count_reserved_texel_reads); + if (handler) invoke(guest, 70, ptr(&texel), ptr(&unreadable), cpu); + const bool removed = handler && RemoveVectoredExceptionHandler(handler); + reserved_texel_address = 0; + VirtualFree(reserved, 0, MEM_RELEASE); + if (!removed) return 57; + if (reserved_texel_reads != 1) { + std::fprintf(stderr, "Expected one reserved texel probe, observed %u\n", reserved_texel_reads); + return 58; + } + x2w_d3d12_census_report(report.data(), report.size()); + if (!std::strstr(report.data(), "texture_unreadable 1,") || + !std::strstr(report.data(), "reason=texture_unreadable ")) { + std::fprintf(stderr, "Expected one texture_unreadable refusal and shape:\n%s", report.data()); + return 59; + } } return 0; } From 5ca1d954827a8fc825ab22f0137812701e59d8bd Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Sun, 29 Nov 2026 16:22:26 +0100 Subject: [PATCH 2/2] tests: count the logged refusal's probe in the memory SRV diag check --- tests/startup_memory_srv_diag.cpp | 20 +++++++++++++++----- tests/startup_memory_srv_diag_check.py | 8 ++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/tests/startup_memory_srv_diag.cpp b/tests/startup_memory_srv_diag.cpp index 913b9acb65..ff90989f53 100644 --- a/tests/startup_memory_srv_diag.cpp +++ b/tests/startup_memory_srv_diag.cpp @@ -132,19 +132,29 @@ int draw_memory_srv_diagnostics(ID3D12Device* desktop, void* guest, void* list) reserved_texel_address = unreadable.address; reserved_texel_reads = 0; const auto handler = AddVectoredExceptionHandler(1, count_reserved_texel_reads); - if (handler) invoke(guest, 70, ptr(&texel), ptr(&unreadable), cpu); + unsigned logged_reads = 0; + if (handler) { + invoke(guest, 70, ptr(&texel), ptr(&unreadable), cpu); + logged_reads = reserved_texel_reads; + reserved_texel_reads = 0; + invoke(guest, 70, ptr(&texel), ptr(&unreadable), cpu); + } const bool removed = handler && RemoveVectoredExceptionHandler(handler); reserved_texel_address = 0; VirtualFree(reserved, 0, MEM_RELEASE); if (!removed) return 57; - if (reserved_texel_reads != 1) { - std::fprintf(stderr, "Expected one reserved texel probe, observed %u\n", reserved_texel_reads); + // A logged refusal faults in publish_tiled_texture's probe and the 1-byte probe in + // log_refused_memory_srv. Main had three faults because the reason function probed again. + // The repeat uses the same shape after the log limit, so only the publication probe faults. + if (logged_reads != 2 || reserved_texel_reads != 1) { + std::fprintf(stderr, "Expected 2 logged and 1 repeat reserved texel probe faults, observed %u and %u\n", + logged_reads, reserved_texel_reads); return 58; } x2w_d3d12_census_report(report.data(), report.size()); - if (!std::strstr(report.data(), "texture_unreadable 1,") || + if (!std::strstr(report.data(), "texture_unreadable 2,") || !std::strstr(report.data(), "reason=texture_unreadable ")) { - std::fprintf(stderr, "Expected one texture_unreadable refusal and shape:\n%s", report.data()); + std::fprintf(stderr, "Expected two texture_unreadable refusals and their shape:\n%s", report.data()); return 59; } } diff --git a/tests/startup_memory_srv_diag_check.py b/tests/startup_memory_srv_diag_check.py index 3ebacd9c2f..8a787cf3ee 100644 --- a/tests/startup_memory_srv_diag_check.py +++ b/tests/startup_memory_srv_diag_check.py @@ -87,10 +87,10 @@ def check(directory, consumer, backend, control=False): log = trap_log.read_text(errors="replace").splitlines() slot70 = [line for line in log if "Device::slot70" in line] reasons = [re.search(r"reason=(\S+)", line).group(1) for line in slot70] - if reasons != ["null_address"] * 8 + ["unsupported_description"]: - raise AssertionError("slot 70 lines (the ninth is past the line limit):\n" + "\n".join(slot70)) - if "fmt=71 size=2048x2048x1 mips=10" not in slot70[-1]: - raise AssertionError(f"the ninth slot 70 line does not name the BC1 shape:\n{slot70[-1]}") + if reasons != ["null_address"] * 8 + ["unsupported_description", "texture_unreadable"]: + raise AssertionError("slot 70 lines (new shapes log past the line limit):\n" + "\n".join(slot70)) + if "fmt=71 size=2048x2048x1 mips=10" not in slot70[8]: + raise AssertionError(f"the ninth slot 70 line does not name the BC1 shape:\n{slot70[8]}") written = [line.rstrip() for line in log if "memory SRV written" in line] if len(written) != 1 or not re.fullmatch(WRITTEN, written[0]): raise AssertionError("slot 70 write lines (only the R8 publish writes):\n" + "\n".join(written))