From f5355d917a45de3003ffe37ad67934a401fdb7a5 Mon Sep 17 00:00:00 2001 From: Dean Galvin Date: Thu, 1 Oct 2026 16:05:24 +1300 Subject: [PATCH] fix(agc): snapshot misaligned draw inputs from the offset the shader reads A guest buffer view off minStorageBufferOffsetAlignment is bound from the aligned offset below it, and the shader adds the difference to every access (3dec6ced). PrepareDrawBindings (9f1c680b) copied such a read-only in-place view into a snapshot that starts at the view itself, so the shader read the snapshot that many bytes past the view. The snapshot now covers the view from the binding's start. --- .../Graphics/src/ShaderResources.cpp | 10 ++- .../prx/libSceAgcDriver/tests/Recorder.cpp | 83 +++++++++++++++++++ 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp index c4b2930f9..14f98482a 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp @@ -2517,11 +2517,13 @@ std::shared_ptr ShaderResources::PrepareDrawBindi if (!item.guest || item.written || guestMemory.WritesOverlap(item.address, item.size)) continue; const bool direct = std::any_of(reads.begin(), reads.end(), [&](const auto& range) { return item.address >= range.first && item.address < range.second && item.size <= range.second - item.address; }); if (!direct || recorder.PendingWriteOverlaps(item.address, item.size)) continue; - auto buffer = std::make_shared(context, item.size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT); - std::memcpy(buffer->Bytes().data(), reinterpret_cast(item.address), item.size); + const auto begin = item.address - item.adjustment; + const auto bytes = item.size + item.adjustment; + auto buffer = std::make_shared(context, bytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT); + std::memcpy(buffer->Bytes().data(), reinterpret_cast(begin), bytes); selected.push_back(index); - result->snapshots.push_back({item.address, std::move(buffer)}); - CaptureTrace::Log("draw-snapshot batch=%llu address=%llx bytes=%zu", static_cast(recorder.Submissions() + 1), static_cast(item.address), item.size); + result->snapshots.push_back({begin, std::move(buffer)}); + CaptureTrace::Log("draw-snapshot batch=%llu address=%llx bytes=%zu", static_cast(recorder.Submissions() + 1), static_cast(begin), bytes); } if (selected.empty()) return {}; Require(context.descriptorCache != nullptr, "draw snapshots require a descriptor cache"); diff --git a/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp b/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp index e55e6fdc6..938f9cdaa 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp @@ -810,6 +810,88 @@ void resourceReadTests(const Device& device, Recorder& recorder) { HostImportFor(context, address, bytes); } +void misalignedSnapshotTests(const Device& device, Recorder& recorder) { + const auto& context = device.GetContext(); + const auto alignment = context.limits.minStorageBufferOffsetAlignment; + if (context.hostImportAlignment == 0) { + std::cout << "host imports unavailable: misaligned draw snapshots not tested\n"; + return; + } + if (alignment < 8 || alignment > 256) { + std::cout << "storage buffer offset alignment " << alignment << ": misaligned draw snapshots not tested\n"; + return; + } + constexpr std::size_t bytes = 65536; +#ifdef _WIN32 + void* block = VirtualAlloc(nullptr, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); +#else + void* block = std::aligned_alloc(65536, bytes); +#endif + Require(block != nullptr, "cannot allocate the misaligned snapshot block"); + auto* guest = static_cast(block); + for (std::size_t at = 0; at < bytes; ++at) guest[at] = static_cast(at * 7u + 3u); + const auto address = reinterpret_cast(block); + { + GuestAllocations::Mutation mutation; + mutation.Add(block, bytes, true, true); + } + struct Unregister { + const Context& context; + void* block; + ~Unregister() { + { + GuestAllocations::Mutation mutation; + mutation.Remove(block); + } + HostImportFor(context, reinterpret_cast(block), bytes); + } + } unregister{context, block}; + if (HostImportFor(context, address, bytes) == nullptr) { + std::cout << "host import of the test block refused: misaligned draw snapshots not tested\n"; + return; + } + constexpr std::uint32_t outer = 4096; + constexpr std::uint32_t offset = outer + 4; + constexpr std::size_t outerBytes = 128; + constexpr std::size_t elementBytes = 64; + const auto words = [](std::uint64_t at, std::size_t size) { return std::array{static_cast(at), static_cast(at >> 32u) & 0xffffu, static_cast(size), 0x31000000u}; }; + ShaderRecompiler::RecompileResult program; + ShaderRecompiler::DescriptorBinding binding; + binding.kind = ShaderRecompiler::DescriptorKind::StorageBuffer; + binding.role = ShaderRecompiler::DescriptorRole::GuestBuffers; + binding.descriptorSet = 0; + binding.binding = 0; + binding.count = 2; + for (const auto word : words(address + outer, outerBytes)) binding.guestDescriptor.push_back(word); + for (const auto word : words(address + offset, elementBytes)) binding.guestDescriptor.push_back(word); + binding.bufferWritten = {false, false}; + program.bindings.push_back(binding); + program.pushConstants.resize(16); + program.memoryOffsetDword = 0; + const CompiledShader compute{ShaderRecompiler::ShaderStage::Compute, &program, 0}; + { + auto snapshotContext = context; + DescriptorCache cache(snapshotContext); + snapshotContext.descriptorCache = &cache; + Recorder snapshotRecorder(snapshotContext); + snapshotRecorder.Activate(); + ShaderResources resources(snapshotContext, compute); + std::array push{}; + resources.PatchPushConstants(push); + const auto adjustment = std::to_integer(push[1]); + Require(push[0] == std::byte{0} && adjustment == offset % alignment, "the inner view's shader offset is not its distance from the binding"); + const auto bindings = resources.PrepareDrawBindings(snapshotRecorder); + Require(bindings != nullptr && bindings->snapshots.size() == 2, "read-only draw inputs were not snapshotted"); + const auto outerContents = bindings->snapshots[0].buffer->Bytes(); + Require(outerContents.size() >= outerBytes && std::memcmp(outerContents.data(), guest + outer, outerBytes) == 0, "an aligned draw snapshot misses its view's bytes"); + const auto contents = bindings->snapshots[1].buffer->Bytes(); + Require(contents.size() >= adjustment + elementBytes, "a draw snapshot ends before the bytes the shader reads"); + Require(std::memcmp(contents.data() + adjustment, guest + offset, elementBytes) == 0, "the shader's offset into a draw snapshot misses the view's bytes"); + snapshotRecorder.Sync(); + } + recorder.Activate(); +} + // Unit shadows (UnitShadow.hpp) over a host import of write-watched arena memory: a retile piece's // slab destination and its seeds, freshness from the tracker (a publish never stamps, a CPU write // makes the unit stale), the scopes, the slab boundary, and the retire publish. With @@ -1571,6 +1653,7 @@ int main() { closeRaceTests(device, recorder); keyProofTests(device, recorder); resourceReadTests(device, recorder); + misalignedSnapshotTests(device, recorder); storeRunTests(device, recorder); movedMetadataTests(device, recorder); unitShadowTests(device, recorder);