From 4ebe446647965c71eb1e6adb405638a1335336b3 Mon Sep 17 00:00:00 2001 From: TanmaySatyam Date: Wed, 25 Nov 2026 15:24:09 +0530 Subject: [PATCH] d3d12: translate GPU-view V#s and guard untranslated title V#s (RT-UNTRANSLATED-VSHARP-GUARD) Maps GPU-only title views via title_gpu_views::to_cpu in resolve_title_memory and rebase_title_vsharp, allows decoded title-memory V#s within kTitleWindowMax to resolve title windows in console heap rebasing, and redirects any remaining unresolvable raw title-memory V# to a zeroed arena scratch slot with num_records=0 via runtime/src/title_vsharp_guard.h. Adds portable unit tests in runtime/test/title_vsharp_guard_test.cpp. --- runtime/CMakeLists.txt | 3 + runtime/shims/d3d12_vulkan_device.cpp | 92 +++++++++++---- runtime/src/title_vsharp_guard.h | 43 +++++++ runtime/test/title_vsharp_guard_test.cpp | 136 +++++++++++++++++++++++ 4 files changed, 253 insertions(+), 21 deletions(-) create mode 100644 runtime/src/title_vsharp_guard.h create mode 100644 runtime/test/title_vsharp_guard_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 14cc5fbf23..043e9fe40e 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -4187,6 +4187,9 @@ if(TARGET Vulkan::Headers) # Title-placed buffer VAs (device slot 133) and the console VA shaders reach them by (src/title_va_alias.h). add_executable(title_va_alias_test test/title_va_alias_test.cpp) add_test(NAME title_va_alias COMMAND title_va_alias_test) + add_executable(title_vsharp_guard_test test/title_vsharp_guard_test.cpp) + target_link_libraries(title_vsharp_guard_test PRIVATE Vulkan::Headers) + add_test(NAME title_vsharp_guard COMMAND title_vsharp_guard_test) endif() else() include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/vulkan_headers_missing.cmake") diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 5dd6b42e47..d94a6c493e 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -53,6 +53,7 @@ #include "../src/spirv_f16_halves.h" #include "../src/spirv_stores.h" #include "../src/title_va_alias.h" +#include "../src/title_vsharp_guard.h" #include "../src/vulkan_present.h" #include "../src/vulkan_device_filter.h" #include "../src/vulkan13_features.h" @@ -2097,6 +2098,21 @@ struct DeviceState { std::atomic title_window_count{0}; // title_windows.size(), read without the mutex std::uint64_t title_window_bytes = 0; std::uint64_t title_window_cap = 64ull << 20; + std::uint64_t vsharp_guard_scratch = UINT64_MAX; + bool vsharp_guard_scratch_tried = false; + + std::uint64_t ensure_vsharp_guard_scratch() { + std::lock_guard lock(title_window_mutex); + if (!vsharp_guard_scratch_tried) { + vsharp_guard_scratch_tried = true; + VkDeviceSize scratch = 0; + if (arena.mapped && arena_allocate(kTitleWindowAlign, kTitleWindowAlign, scratch, arena_buffer)) { + std::memset(arena.mapped + scratch, 0, std::size_t(kTitleWindowAlign)); + vsharp_guard_scratch = scratch; + } + } + return vsharp_guard_scratch; + } rt::d3d12::GeometryViews geometry; static rt::d3d12::GeometryCopy copy_geometry(std::uint8_t* to, const std::uint8_t* from, std::size_t size) { @@ -2132,6 +2148,8 @@ struct DeviceState { return !flag || std::string_view(flag) != "0"; }(); if (context->knows_buffer_va(va)) return false; // an upload or readback buffer's biased VA, say + const std::uint64_t cpu_va = leonida::title_gpu_views::to_cpu(va, bytes); + if (cpu_va != va) va = cpu_va; if (va < arena.size) { // Below the arena's size an address is an arena address unless the title's own memory is there (its // heap is: arena_allocate keeps out of those ranges). @@ -2242,8 +2260,37 @@ struct DeviceState { // Slots the title wrote in its own backing of this heap, if it gave the heap one. aliases.pull_descriptors(arena.mapped + table_va, std::size_t(end - table_va)); if (sampler) return; // S# words describe sampling state. They have no buffer address to translate. - for (VkDeviceSize at = table_va; at + 16 <= end; at += kDescriptorSize) - rebase_title_vsharp(reinterpret_cast(arena.mapped + at), aliases); + for (VkDeviceSize at = table_va; at + 16 <= end; at += kDescriptorSize) { + auto* w = reinterpret_cast(arena.mapped + at); + if (!rebase_title_vsharp(w, aliases)) guard_untranslated_title_vsharp(w, aliases); + } + } + + bool guard_untranslated_title_vsharp(std::uint32_t* w, leonida::TitleVaAliases& aliases) { + std::uint64_t raw_va = 0, span = 0; + const bool guarded = leonida::title_vsharp_guard::redirect_untranslated_vsharp( + w, arena.size, + [](std::uint64_t va, std::uint64_t bytes) { return leonida::title_gpu_views::to_cpu(va, bytes); }, + [&](std::uint64_t base) { + std::uint64_t host{}, remaining{}; + return (context && context->knows_buffer_va(base)) || + (geometry.cap() && geometry.lookup(arena.address + base, host, remaining)); + }, + [&](std::uint64_t va) { + std::uint64_t lo = 0, hi = 0; + return aliases.title_memory(va, lo, hi); + }, + [&]() { return ensure_vsharp_guard_scratch(); }, &raw_va, &span); + if (guarded) { + static std::atomic count{0}; + const std::uint64_t n = count.fetch_add(1, std::memory_order_relaxed) + 1; + if (n <= 8 || (n & (n - 1)) == 0) + std::fprintf(stderr, + "[rt] d3d12: guarded untranslated title V# #%llu (raw VA 0x%llx, span %llu B -> zero-extent scratch)\n", + static_cast(n), static_cast(raw_va), + static_cast(span)); + } + return guarded; } // A buffer V# (its first four dwords at `w`) whose base is in the title's own memory gets the console VA of a GPU @@ -2257,30 +2304,32 @@ struct DeviceState { if (base < arena.size || (geometry.cap() && geometry.lookup(arena.address + base, host, remaining))) return false; const std::uint32_t stride = (w1 >> 16) & 0x3FFFu; const std::uint64_t span = std::uint64_t(w2) * (stride ? stride : 1); + const std::uint64_t cpu_base = leonida::title_gpu_views::to_cpu(base, span); + const leonida::gfx10::BufferWords words{w0, w1, w2, w3}; + leonida::gfx10::Buffer desc{}; + const bool decoded = leonida::gfx10::decode(words, desc); std::uint64_t to{}; - if (geometry.cap()) { - const leonida::gfx10::BufferWords words{w0, w1, w2, w3}; - leonida::gfx10::Buffer desc{}; - if (leonida::gfx10::decode(words, desc)) { - // Untyped heap slots can contain ordinary pointers or other data. Large extents with - // no format or fields outside our buffer encoding need independent evidence of a view. - // An explicit table or a placed SRV can still establish the backing for those words. - if (!make_window && span > kTitleWindowMax && - (!desc.format || leonida::gfx10::encode(desc) != words)) { - to = geometry.find(leonida::title_gpu_views::to_cpu(base, span), span); - if (!to) return false; - } else { - to = geometry_va(base, span, false); - } + if (geometry.cap() && decoded) { + // Untyped heap slots can contain ordinary pointers or other data. Large extents with + // no format or fields outside our buffer encoding need independent evidence of a view. + // An explicit table or a placed SRV can still establish the backing for those words. + if (!make_window && span > kTitleWindowMax && + (!desc.format || leonida::gfx10::encode(desc) != words)) { + to = geometry.find(cpu_base, span); + } else { + to = geometry_va(base, span, false); } } - // A heap scan sees free-list links, raw data and inactive slots. Only explicit bindings may - // request general-purpose windows. The heap can reuse an existing one or make a geometry mirror. - if (!to) to = make_window ? aliases.resolve(base, span, leonida::TitleVaAliases::table_view) - : aliases.shader_va(base, span); + // Allow general-purpose windows for explicit bindings or decoded title-memory V#s within kTitleWindowMax so + // directly-indexed buffer descriptors receive real arena backing when geometry views do not claim them. + std::uint64_t title_lo = 0, title_hi = 0; + const bool allow_window = + make_window || (decoded && span > 0 && span <= kTitleWindowMax && aliases.title_memory(cpu_base, title_lo, title_hi)); + if (!to) to = allow_window ? aliases.resolve(cpu_base, span, leonida::TitleVaAliases::table_view) + : aliases.shader_va(cpu_base, span); - if (to == base || (to >> 48)) return false; + if (!to || to == base || to == cpu_base || (to >> 48)) return false; w[0] = std::uint32_t(to); w[1] = (w1 & 0xFFFF0000u) | std::uint32_t((to >> 32) & 0xFFFFu); return true; @@ -2513,6 +2562,7 @@ struct DeviceState { if (rebase_title_vsharp(w, aliases, !heap.scratch)) { ++heap.vsharps; } else { + guard_untranslated_title_vsharp(w, aliases); // The title's memory with no copy yet (a page not committed when it was looked at) is tried again. const std::uint64_t base = std::uint64_t(w[0]) | (std::uint64_t(w[1] & 0xFFFFu) << 32); std::uint64_t lo = 0, hi = 0; diff --git a/runtime/src/title_vsharp_guard.h b/runtime/src/title_vsharp_guard.h new file mode 100644 index 0000000000..219ecfd24f --- /dev/null +++ b/runtime/src/title_vsharp_guard.h @@ -0,0 +1,43 @@ +#pragma once + +#include "gfx10_descriptors.h" + +#include + +namespace leonida::title_vsharp_guard { + +// Redirects a decoded RDNA2 buffer V# whose base is an unrebased title-memory virtual address (or GPU-only title view) +// to a zeroed arena scratch address with num_records = 0 (w[2] = 0). Translated shaders bounds-check buffer loads and +// stores against num_records, so an OOB read returns 0 and an OOB write is dropped instead of reaching the GPU with an +// unmapped raw title VA (READ_INVALID / WRITE_INVALID -> VK_ERROR_DEVICE_LOST). +// +// Leaves untouched: null descriptors, image/sampler descriptors (w[3] >> 30 != 0), arena addresses (base < arena_size), +// any known GPU VA (host imports, geometry views, upload/readback buffers), non-V# data that fails gfx10::decode, and +// addresses outside title memory. Returns true iff `w` was redirected. +template +bool redirect_untranslated_vsharp(std::uint32_t w[4], std::uint64_t arena_size, ToCpu&& to_cpu, + IsKnownGpuVa&& is_known_gpu_va, IsTitleMemory&& is_title_memory, + GetScratchVa&& get_scratch_va, std::uint64_t* raw_va_out = nullptr, + std::uint64_t* span_out = nullptr) { + const std::uint32_t w0 = w[0], w1 = w[1], w2 = w[2], w3 = w[3]; + if (!(w0 | w1 | w2 | w3) || (w3 >> 30)) return false; + const std::uint64_t base = std::uint64_t(w0) | (std::uint64_t(w1 & 0xFFFFu) << 32); + if (base < arena_size || is_known_gpu_va(base)) return false; + const leonida::gfx10::BufferWords words{w0, w1, w2, w3}; + leonida::gfx10::Buffer desc{}; + if (!leonida::gfx10::decode(words, desc)) return false; + const std::uint32_t stride = (w1 >> 16) & 0x3FFFu; + const std::uint64_t span = std::uint64_t(w2) * (stride ? stride : 1); + const std::uint64_t cpu_base = to_cpu(base, span); + if (!is_title_memory(base) && (cpu_base == base || !is_title_memory(cpu_base))) return false; + const std::uint64_t scratch = get_scratch_va(); + if (scratch == UINT64_MAX || (scratch >> 48)) return false; + w[0] = std::uint32_t(scratch); + w[1] = (w1 & 0xFFFF0000u) | std::uint32_t((scratch >> 32) & 0xFFFFu); + w[2] = 0; + if (raw_va_out) *raw_va_out = base; + if (span_out) *span_out = span; + return true; +} + +} // namespace leonida::title_vsharp_guard diff --git a/runtime/test/title_vsharp_guard_test.cpp b/runtime/test/title_vsharp_guard_test.cpp new file mode 100644 index 0000000000..201f78cd7d --- /dev/null +++ b/runtime/test/title_vsharp_guard_test.cpp @@ -0,0 +1,136 @@ +// Deterministic unit tests for leonida::title_vsharp_guard::redirect_untranslated_vsharp (no GPU or game files). +#include "../src/title_vsharp_guard.h" + +#include +#include +#include + +namespace { + +int g_checks = 0; +int g_failures = 0; + +void check(bool cond, const char* what) { + ++g_checks; + if (!cond) { + ++g_failures; + std::fprintf(stderr, "FAIL: %s\n", what); + } +} + +leonida::gfx10::BufferWords make_buffer_vsharp(std::uint64_t base, std::uint32_t num_records, + std::uint32_t stride = 16) { + leonida::gfx10::Buffer b{}; + b.address = base; + b.stride = stride; + b.num_records = num_records; + b.format = 4; // R32_FLOAT + return leonida::gfx10::encode(b); +} + +} // namespace + +int main() { + constexpr std::uint64_t kArenaSize = 0x40000000ull; // 1 GiB + constexpr std::uint64_t kScratchVa = 0x00020000ull; + constexpr std::uint64_t kTitleBase = 0x0000015EB72CE000ull; + constexpr std::uint64_t kGpuViewBase = 0x0000020010000000ull; + constexpr std::uint64_t kKnownImportVa = 0x0000000180000000ull; + + const auto to_cpu = [&](std::uint64_t va, std::uint64_t) -> std::uint64_t { + if (va == kGpuViewBase) return kTitleBase; + return va; + }; + const auto is_known = [&](std::uint64_t va) -> bool { return va == kKnownImportVa; }; + const auto is_title = [&](std::uint64_t va) -> bool { + return va >= kTitleBase && va < kTitleBase + 0x1000000ull; + }; + const auto get_scratch = [&]() -> std::uint64_t { return kScratchVa; }; + + // 1. Null descriptor is untouched. + { + std::uint32_t w[4] = {0, 0, 0, 0}; + check(!leonida::title_vsharp_guard::redirect_untranslated_vsharp( + w, kArenaSize, to_cpu, is_known, is_title, get_scratch) && + w[0] == 0 && w[1] == 0 && w[2] == 0 && w[3] == 0, + "null descriptor untouched"); + } + + // 2. Image T# (w[3] >> 30 != 0) is untouched even if low bits match a title VA. + { + std::uint32_t w[4] = {std::uint32_t(kTitleBase), std::uint32_t(kTitleBase >> 32), 64, 0x80000000u}; + const std::uint32_t orig[4] = {w[0], w[1], w[2], w[3]}; + check(!leonida::title_vsharp_guard::redirect_untranslated_vsharp( + w, kArenaSize, to_cpu, is_known, is_title, get_scratch) && + std::memcmp(w, orig, sizeof(w)) == 0, + "image T# untouched"); + } + + // 3. Arena-resident buffer V# (base < arena_size) is untouched. + { + auto words = make_buffer_vsharp(0x00100000ull, 256, 16); + const auto orig = words; + check(!leonida::title_vsharp_guard::redirect_untranslated_vsharp( + words.data(), kArenaSize, to_cpu, is_known, is_title, get_scratch) && + words == orig, + "arena V# untouched"); + } + + // 4. Known GPU VA (host-imported buffer or geometry view) is untouched. + { + auto words = make_buffer_vsharp(kKnownImportVa, 256, 16); + const auto orig = words; + check(!leonida::title_vsharp_guard::redirect_untranslated_vsharp( + words.data(), kArenaSize, to_cpu, is_known, is_title, get_scratch) && + words == orig, + "known GPU VA untouched"); + } + + // 5. Address outside title memory (e.g. arbitrary non-title pointer) is untouched. + { + auto words = make_buffer_vsharp(0x0000030000000000ull, 64, 16); + const auto orig = words; + check(!leonida::title_vsharp_guard::redirect_untranslated_vsharp( + words.data(), kArenaSize, to_cpu, is_known, is_title, get_scratch) && + words == orig, + "non-title address untouched"); + } + + // 6. Unrebased raw title-memory V# is redirected to scratch with num_records=0, preserving stride & w[3]. + { + auto words = make_buffer_vsharp(kTitleBase, 1024, 32); + const std::uint32_t orig_w1_hi = words[1] & 0xFFFF0000u; + const std::uint32_t orig_w3 = words[3]; + std::uint64_t raw_va = 0, span = 0; + const bool guarded = leonida::title_vsharp_guard::redirect_untranslated_vsharp( + words.data(), kArenaSize, to_cpu, is_known, is_title, get_scratch, &raw_va, &span); + const std::uint64_t new_base = std::uint64_t(words[0]) | (std::uint64_t(words[1] & 0xFFFFu) << 32); + check(guarded && new_base == kScratchVa && words[2] == 0 && (words[1] & 0xFFFF0000u) == orig_w1_hi && + words[3] == orig_w3 && raw_va == kTitleBase && span == 1024ull * 32ull, + "unrebased raw title V# redirected to zero-extent scratch"); + } + + // 7. Unrebased GPU-only title view (mapped via to_cpu into title memory) is also redirected safely. + { + auto words = make_buffer_vsharp(kGpuViewBase, 512, 16); + std::uint64_t raw_va = 0, span = 0; + const bool guarded = leonida::title_vsharp_guard::redirect_untranslated_vsharp( + words.data(), kArenaSize, to_cpu, is_known, is_title, get_scratch, &raw_va, &span); + const std::uint64_t new_base = std::uint64_t(words[0]) | (std::uint64_t(words[1] & 0xFFFFu) << 32); + check(guarded && new_base == kScratchVa && words[2] == 0 && raw_va == kGpuViewBase && span == 512ull * 16ull, + "unrebased GPU-only title view redirected to zero-extent scratch"); + } + + // 8. When no scratch slot is available (UINT64_MAX), returns false and leaves descriptor untouched. + { + auto words = make_buffer_vsharp(kTitleBase, 128, 16); + const auto orig = words; + check(!leonida::title_vsharp_guard::redirect_untranslated_vsharp( + words.data(), kArenaSize, to_cpu, is_known, is_title, [] { return UINT64_MAX; }) && + words == orig, + "unavailable scratch leaves V# untouched"); + } + + std::printf("title_vsharp_guard_test: %d checks, %d failures\n", g_checks, g_failures); + return g_failures ? 1 : 0; +}