fix(agc): retain GPU copy buffers before fallible diagnostics (standalone patch for review) #8659

Closed
opened 2026-10-20 20:13:23 +00:00 by VicRaster · 1 comment
VicRaster commented 2026-10-20 20:13:23 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by VicRaster on 2026-10-20T20:13:23Z.


Standalone correctness patch for review. The diff below has no mail-format author metadata and includes only the lifetime fix and synthetic regressions. AI-assisted: yes.

At submission, main is 9a97ac784da5d40cc7b7d968898ffbf5b8b5377a. The four contribution files are unchanged since the tested base, and the patch applies cleanly to this newer main. The newer main was not rebuilt; the GPU results below apply to the stated tested base.

What

With APS5_CAPTURE_GPU_COPIES=1, diagnostic readback allocation can fail after the input copy has been recorded. The batch keeps the destination later, so unwinding can return that buffer to the pool while its command is still pending.

Keep the destination before recording the copy, and keep the readback buffer before recording commands that use it. Add deferred-fence mock and real Vulkan CTest regressions, including a successful-readback control. This change is independent of experimental memory policies.

Tested

Windows 11 Pro build 26300, WinLibs MinGW-w64 GCC 15.2.0, NVIDIA GeForce RTX 3080 Ti, driver 610.88.

  • Upstream base: 2185dbf92e8d9013a1620d2dfc95de50494e19f8.
  • agc_copy_input_failure_lifetime and agc_driver_copy_input_lifetime: both fail before the fix because the destination is reusable before its fence; both pass afterward with identical test sources.
  • The GPU regression checks the successful readback path, injects Vulkan allocation failure after the first copy command, checks exact copy offsets, completed GPU data, surrounding guest guards and allocation cleanup. It observes pool reuse without overwriting pending buffers.
  • Same focused controls before and after: agc_buffer_pool, agc_driver_graphics, agc_driver_bda_device, agc_driver_bda_span_reads, agc_driver_guest_memory_lifetime, agc_driver_host_import_windows and agc_copy_input_kept_bytes; 7/7 pass on both sides.
  • Runs require ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. Full logs positively show validation instance/device insertion and the RTX 3080 Ti, with zero VUID- messages, no missing-layer warnings and no skipped GPU tests. Convention and patch-application checks pass.

Overlap review covered 770 open PRs. The upstream checker found no confirmed direct conflict; its 59 stale shared-file warnings were inspected separately. In particular, #6185 and #6186 change allocation usage and later shadow copies, while leaving this diagnostic ownership sequence unchanged. No duplicate fix or dependency was identified.

The separate native allocation failure during completion-callback registration was not fault-injected. Tests use synthetic memory; no game compatibility or performance improvement is claimed.

Proposed patch

Header-free standalone diff: four files
diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt
index cb211a410..a5a9c142f 100644
--- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt
+++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt
@@ -2164,6 +2164,11 @@ if(BUILD_TESTING)
                 "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
     endif()
     add_test(NAME agc_sparse_buffer_holes COMMAND agc_driver_graphics_tests --sparse-buffer-holes)
+    set_tests_properties(agc_driver_copy_input_lifetime PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_CAPTURE_TRACE=1;APS5_CAPTURE_GPU_COPIES=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
+    if(WIN32)
+        add_test(NAME agc_copy_input_failure_lifetime COMMAND agc_driver_graphics_tests --copy-input-failure-lifetime)
+        set_tests_properties(agc_copy_input_failure_lifetime PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_CAPTURE_TRACE=1;APS5_CAPTURE_GPU_COPIES=1" WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" TIMEOUT 30 ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
+    endif()
     add_test(NAME agc_copy_input_kept_bytes COMMAND agc_driver_graphics_tests --copy-input-kept-bytes)
     add_test(NAME agc_depth_copy_kept_bytes COMMAND agc_driver_graphics_tests --depth-copy-kept-bytes)
     set_tests_properties(agc_copy_input_kept_bytes agc_depth_copy_kept_bytes PROPERTIES
diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
index 56368f852..4cbd855a9 100644
--- a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
+++ b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
@@ -4917,9 +4917,11 @@ void GuestBufferMemory::recordGpuCopies(std::span<Region* const> copies, bool ad
                 expected.assign(snapshotBytes.begin(), snapshotBytes.end());
             }
         }
+        recorder->Keep(region->buffer);
         CopyBuffer(context, commands, copySource, copyOffset, region->buffer->Handle(), 0, bytes);
         if (!expected.empty()) {
             auto readback = std::make_shared<Buffer>(context, expected.size(), VK_BUFFER_USAGE_TRANSFER_DST_BIT);
+            recorder->Keep(readback, readback->Bytes().size());
             RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT);
             CopyBuffer(context, commands, region->buffer->Handle(), 0, readback->Handle(), 0, bytes);
             RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_HOST_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT);
@@ -4935,11 +4937,6 @@ void GuestBufferMemory::recordGpuCopies(std::span<Region* const> copies, bool ad
             });
         }
         if (inputSnapshot == nullptr) recorder->NotePendingRead(region->begin, static_cast<std::size_t>(bytes), Recorder::ReadKind::GpuCopy);
-        // Kept by the batch at record time, as every other recorded target is: the caller keeps
-        // the resources only after descriptor and pipeline work that may throw, and a released
-        // buffer would go back to the pool (reused or destroyed) under this copy command. The
-        // source is a live import or one retireImport already handed to this batch.
-        recorder->Keep(region->buffer);
         region->snapshot.clear();
         if (region->deviceLocal) traceStaged(region->begin, region->end, region->atomic);
         if (profile) {
diff --git a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
index 92e2141a3..a52480763 100644
--- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
+++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
@@ -5025,6 +5025,89 @@ void bufferBudgetPlacementTests() {
     }
 }
 
+bool copyFailureArmed = false;
+
+VKAPI_ATTR void VKAPI_CALL mockFailureCopy(VkCommandBuffer commands, VkBuffer source, VkBuffer destination, std::uint32_t count, const VkBufferCopy* regions) {
+    mockCmdCopyBuffer(commands, source, destination, count, regions);
+    if (copyFailureArmed && count == 1 && regions[0].size == 32 && (mock.bufferUsage.at(destination) & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) != 0) {
+        copyFailureArmed = false;
+        mock.memoryLimit = 0;
+    }
+}
+
+PFN_vkVoidFunction VKAPI_CALL mockCopyFailureProc(VkDevice device, const char* name) {
+    if (std::strcmp(name, "vkCmdCopyBuffer") == 0) return reinterpret_cast<PFN_vkVoidFunction>(mockFailureCopy);
+    return mockProc(device, name);
+}
+
+void copyInputFailureLifetimeTests() {
+#ifdef _WIN32
+    using namespace AgcDriver::Graphics;
+    constexpr std::size_t piece = 65536;
+    constexpr std::size_t bytes = 32;
+    constexpr auto properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
+    constexpr auto usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
+    auto block = std::unique_ptr<std::byte[], void(*)(std::byte*)>(static_cast<std::byte*>(::operator new(piece, std::align_val_t{piece})), [](std::byte* value) { ::operator delete(value, std::align_val_t{piece}); });
+    std::fill_n(block.get(), piece, std::byte{0});
+    const auto base = reinterpret_cast<std::uint64_t>(block.get());
+    bool refused = false;
+    bool earlyReuse = false;
+    mock = MockVulkan{};
+    mock.deferFences = true;
+    {
+        auto context = mockContext();
+        context.device = makeHandle<VkDevice>();
+        context.deviceProc = mockCopyFailureProc;
+        context.bufferDeviceAddress = true;
+        context.hostImportAlignment = piece;
+        context.limits.minStorageBufferOffsetAlignment = 256;
+        context.limits.nonCoherentAtomSize = piece;
+        context.limits.maxMemoryAllocationCount = 4096;
+        SetImportWatch(context, ImportWatch::Unwatch);
+        const auto pool = GetBufferPool(context);
+        auto* mutation = GuestAllocations::GuestAllocationsBegin_nid_postfix();
+        GuestAllocations::GuestAllocationsAdd_nid_postfix(mutation, block.get(), piece, true, false, true);
+        GuestAllocations::GuestAllocationsEnd_nid_postfix(mutation);
+        const auto unregister = [&](std::byte*) {
+            auto* mutation = GuestAllocations::GuestAllocationsBegin_nid_postfix();
+            GuestAllocations::GuestAllocationsRemove_nid_postfix(mutation, block.get());
+            GuestAllocations::GuestAllocationsEnd_nid_postfix(mutation);
+        };
+        std::unique_ptr<std::byte, decltype(unregister)> registered(block.get(), unregister);
+        {
+            std::lock_guard lock(AgcDriver::GuestMemory::GpuMutex());
+            Recorder recorder(context);
+            recorder.Activate();
+            copyFailureArmed = true;
+            try {
+                GuestBufferMemory memory(context);
+                memory.AddReadable(base + 4, bytes);
+                memory.Upload(false);
+            } catch (const std::exception&) {
+                refused = true;
+            }
+            Require(refused && !copyFailureArmed && mock.bufferCopies.size() == 1, "diagnostic readback pressure was not exercised after the copy command");
+            const auto destination = mock.bufferCopies.front().destination;
+            std::vector<BufferAllocation> taken;
+            while (const auto allocation = pool->Take(bytes, usage, properties)) {
+                earlyReuse |= allocation->buffer == destination;
+                taken.push_back(*allocation);
+            }
+            for (const auto& allocation : taken) pool->Put(allocation);
+            mock.memoryLimit.reset();
+            recorder.Sync();
+        }
+        registered.reset();
+        ClearHostImports(context.device);
+    }
+    Require(mock.live == 0, "copy failure lifetime test leaked an object");
+    std::cout << "diagnostic pressure refused=" << refused << " destination_reusable_before_fence=" << earlyReuse << '\n';
+    Require(!earlyReuse, "recorded copy destination became reusable before its fence after readback pressure");
+#else
+    throw std::runtime_error("the copy failure mock fixture requires Windows guest registration");
+#endif
+}
+
 void copyInputKeptBytesTests() {
     using namespace AgcDriver::Graphics;
     constexpr std::size_t piece = 65536;
@@ -10185,6 +10268,10 @@ int main(int argc, char** argv) {
             std::cout << "cached pipelines survive lazy buffer pool creation\n";
             return 0;
         }
+        if (argc == 2 && std::string_view(argv[1]) == "--copy-input-failure-lifetime") {
+            copyInputFailureLifetimeTests();
+            return 0;
+        }
         if (argc == 2 && std::string_view(argv[1]) == "--copy-input-kept-bytes") {
             copyInputKeptBytesTests();
             std::cout << "GPU-copy input snapshot accounting and retention passed\n";
diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/CopyInputLifetime.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/CopyInputLifetime.cpp
new file mode 100644
index 000000000..4cc28c67b
--- /dev/null
+++ b/core/libs/prx/libSceAgcDriver/tests/execution/CopyInputLifetime.cpp
@@ -0,0 +1,155 @@
+#include "VulkanTestDevice.hpp"
+#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
+#include "prx/libSceAgcDriver/Graphics/include/BufferPool.hpp"
+#include "prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp"
+#include "prx/libc/include/GuestAllocations.hpp"
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <iostream>
+#include <memory>
+#include <mutex>
+#include <new>
+#include <vector>
+
+namespace {
+
+namespace Graphics = AgcDriver::Graphics;
+using Graphics::Require;
+
+PFN_vkGetDeviceProcAddr originalProc = nullptr;
+PFN_vkAllocateMemory originalAllocate = nullptr;
+PFN_vkFreeMemory originalFree = nullptr;
+PFN_vkCmdCopyBuffer originalCopy = nullptr;
+bool refuseReadback = false;
+bool copied = false;
+unsigned refusals = 0;
+unsigned copies = 0;
+unsigned allocations = 0;
+VkBuffer destination = VK_NULL_HANDLE;
+
+VKAPI_ATTR VkResult VKAPI_CALL Allocate(VkDevice device, const VkMemoryAllocateInfo* info, const VkAllocationCallbacks* callbacks, VkDeviceMemory* memory) {
+    if (refuseReadback && copied) {
+        ++refusals;
+        return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+    }
+    const auto result = originalAllocate(device, info, callbacks, memory);
+    if (result == VK_SUCCESS) ++allocations;
+    return result;
+}
+
+VKAPI_ATTR void VKAPI_CALL Free(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* callbacks) {
+    if (memory != VK_NULL_HANDLE) --allocations;
+    originalFree(device, memory, callbacks);
+}
+
+VKAPI_ATTR void VKAPI_CALL Copy(VkCommandBuffer commands, VkBuffer source, VkBuffer target, std::uint32_t count, const VkBufferCopy* regions) {
+    originalCopy(commands, source, target, count, regions);
+    if (!copied) {
+        Require(count == 1 && regions[0].size == 32 && regions[0].srcOffset == 0 && regions[0].dstOffset == 0, "expected one exact 32-byte input copy");
+        destination = target;
+        copied = true;
+    }
+    ++copies;
+}
+
+PFN_vkVoidFunction VKAPI_CALL Resolve(VkDevice device, const char* name) {
+    if (std::strcmp(name, "vkAllocateMemory") == 0) return reinterpret_cast<PFN_vkVoidFunction>(&Allocate);
+    if (std::strcmp(name, "vkFreeMemory") == 0) return reinterpret_cast<PFN_vkVoidFunction>(&Free);
+    if (std::strcmp(name, "vkCmdCopyBuffer") == 0) return reinterpret_cast<PFN_vkVoidFunction>(&Copy);
+    return originalProc(device, name);
+}
+
+void Scenario(Graphics::Context context, bool fail) {
+    constexpr std::size_t blockBytes = 65536;
+    constexpr auto usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
+    constexpr auto properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
+    auto block = std::unique_ptr<std::byte[], void(*)(std::byte*)>(static_cast<std::byte*>(::operator new(blockBytes, std::align_val_t{blockBytes})), [](std::byte* value) { ::operator delete(value, std::align_val_t{blockBytes}); });
+    std::fill_n(block.get(), blockBytes, std::byte{0x11});
+    std::fill_n(block.get() + 4, 32, std::byte{0x5a});
+    GuestAllocations::Mutation().Add(block.get(), blockBytes, true, false, true);
+    const auto unregister = [&](std::byte*) { GuestAllocations::Mutation().Remove(block.get()); };
+    std::unique_ptr<std::byte, decltype(unregister)> registered(block.get(), unregister);
+    context.functions = nullptr;
+    context.deviceProc = &Resolve;
+    context.limits.minStorageBufferOffsetAlignment = 256;
+    context.limits.nonCoherentAtomSize = blockBytes;
+    context.bufferPool = std::make_shared<Graphics::BufferPool>(context);
+    copied = false;
+    copies = refusals = allocations = 0;
+    destination = VK_NULL_HANDLE;
+    refuseReadback = fail;
+    bool caught = false;
+    bool earlyReuse = false;
+    {
+        Graphics::Recorder recorder(context);
+        recorder.Activate();
+        try {
+            Graphics::GuestBufferMemory memory(context);
+            memory.AddReadable(reinterpret_cast<std::uintptr_t>(block.get()) + 4, 32);
+            memory.Upload(false);
+        } catch (const std::exception&) {
+            caught = true;
+        }
+        refuseReadback = false;
+        Require(copied && destination != VK_NULL_HANDLE, "the fixture did not record an input copy");
+        Require(fail ? caught && refusals != 0 && copies == 1 : !caught && refusals == 0 && copies == 2, "the diagnostic readback did not take the expected allocation path");
+        std::vector<Graphics::BufferAllocation> taken;
+        while (const auto allocation = context.bufferPool->Take(32, usage, properties)) {
+            earlyReuse |= allocation->buffer == destination;
+            taken.push_back(*allocation);
+        }
+        for (const auto& allocation : taken) context.bufferPool->Put(allocation);
+        VkMemoryBarrier barrier{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
+        barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
+        barrier.dstAccessMask = VK_ACCESS_HOST_READ_BIT;
+        context.Function<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier")(recorder.Commands(), VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &barrier, 0, nullptr, 0, nullptr);
+        recorder.Sync();
+    }
+    bool checked = false;
+    std::vector<Graphics::BufferAllocation> completed;
+    while (const auto allocation = context.bufferPool->Take(32, usage, properties)) {
+        if (allocation->buffer == destination) {
+            Require(allocation->mapping != nullptr, "the completed destination is not mapped");
+            const auto* bytes = static_cast<const std::byte*>(allocation->mapping);
+            Require(std::all_of(bytes, bytes + 32, [](std::byte value) { return value == std::byte{0x5a}; }), "GPU input copy data changed");
+            checked = true;
+        }
+        completed.push_back(*allocation);
+    }
+    for (const auto& allocation : completed) context.bufferPool->Put(allocation);
+    for (std::size_t index = 0; index < blockBytes; ++index) Require(block[index] == (index >= 4 && index < 36 ? std::byte{0x5a} : std::byte{0x11}), "the copy changed guest memory or surrounding guards");
+    registered.reset();
+    Graphics::ClearHostImports(context.device);
+    context.bufferPool.reset();
+    Require(allocations == 0, "the input copy leaked Vulkan allocations");
+    std::printf("copy-input lifetime: fail=%d refusals=%u early_reuse=%d GPU_data_checked=%d\n", fail, refusals, earlyReuse, checked);
+    Require(checked && !earlyReuse, "a recorded copy destination became reusable before its fence");
+}
+
+}
+
+int main() {
+    try {
+        auto device = OpenVulkanTestDevice();
+        if (!device) return VulkanTestSkipped;
+        std::lock_guard lock(AgcDriver::GuestMemory::GpuMutex());
+        auto context = device->graphicsContext();
+        if (context.hostImportAlignment == 0 || context.hostImportAlignment > 65536) {
+            Require(std::getenv("ANYPS5_REQUIRE_VULKAN") == nullptr, "the lifetime test requires host imports with at most 64 KiB alignment");
+            std::puts("skipped, suitable host imports are unavailable");
+            return VulkanTestSkipped;
+        }
+        Graphics::ClearHostImports(context.device);
+        originalProc = context.deviceProc;
+        originalAllocate = context.Function<PFN_vkAllocateMemory>("vkAllocateMemory");
+        originalFree = context.Function<PFN_vkFreeMemory>("vkFreeMemory");
+        originalCopy = context.Function<PFN_vkCmdCopyBuffer>("vkCmdCopyBuffer");
+        Scenario(context, false);
+        Scenario(context, true);
+        return 0;
+    } catch (const std::exception& error) {
+        std::cerr << error.what() << '\n';
+        return 1;
+    }
+}
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5948893909 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/8659). Originally posted by [VicRaster](https://github.com/VicRaster) on 2026-10-20T20:13:23Z. --- Standalone correctness patch for review. The diff below has no mail-format author metadata and includes only the lifetime fix and synthetic regressions. AI-assisted: yes. At submission, main is `9a97ac784da5d40cc7b7d968898ffbf5b8b5377a`. The four contribution files are unchanged since the tested base, and the patch applies cleanly to this newer main. The newer main was not rebuilt; the GPU results below apply to the stated tested base. ### What With `APS5_CAPTURE_GPU_COPIES=1`, diagnostic readback allocation can fail after the input copy has been recorded. The batch keeps the destination later, so unwinding can return that buffer to the pool while its command is still pending. Keep the destination before recording the copy, and keep the readback buffer before recording commands that use it. Add deferred-fence mock and real Vulkan CTest regressions, including a successful-readback control. This change is independent of experimental memory policies. ### Tested Windows 11 Pro build 26300, WinLibs MinGW-w64 GCC 15.2.0, NVIDIA GeForce RTX 3080 Ti, driver 610.88. - Upstream base: `2185dbf92e8d9013a1620d2dfc95de50494e19f8`. - `agc_copy_input_failure_lifetime` and `agc_driver_copy_input_lifetime`: both fail before the fix because the destination is reusable before its fence; both pass afterward with identical test sources. - The GPU regression checks the successful readback path, injects Vulkan allocation failure after the first copy command, checks exact copy offsets, completed GPU data, surrounding guest guards and allocation cleanup. It observes pool reuse without overwriting pending buffers. - Same focused controls before and after: `agc_buffer_pool`, `agc_driver_graphics`, `agc_driver_bda_device`, `agc_driver_bda_span_reads`, `agc_driver_guest_memory_lifetime`, `agc_driver_host_import_windows` and `agc_copy_input_kept_bytes`; 7/7 pass on both sides. - Runs require `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. Full logs positively show validation instance/device insertion and the RTX 3080 Ti, with zero `VUID-` messages, no missing-layer warnings and no skipped GPU tests. Convention and patch-application checks pass. Overlap review covered 770 open PRs. The upstream checker found no confirmed direct conflict; its 59 stale shared-file warnings were inspected separately. In particular, #6185 and #6186 change allocation usage and later shadow copies, while leaving this diagnostic ownership sequence unchanged. No duplicate fix or dependency was identified. The separate native allocation failure during completion-callback registration was not fault-injected. Tests use synthetic memory; no game compatibility or performance improvement is claimed. ### Proposed patch <details> <summary>Header-free standalone diff: four files</summary> ```diff diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index cb211a410..a5a9c142f 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -2164,6 +2164,11 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>") endif() add_test(NAME agc_sparse_buffer_holes COMMAND agc_driver_graphics_tests --sparse-buffer-holes) + set_tests_properties(agc_driver_copy_input_lifetime PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_CAPTURE_TRACE=1;APS5_CAPTURE_GPU_COPIES=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") + if(WIN32) + add_test(NAME agc_copy_input_failure_lifetime COMMAND agc_driver_graphics_tests --copy-input-failure-lifetime) + set_tests_properties(agc_copy_input_failure_lifetime PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_CAPTURE_TRACE=1;APS5_CAPTURE_GPU_COPIES=1" WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" TIMEOUT 30 ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>") + endif() add_test(NAME agc_copy_input_kept_bytes COMMAND agc_driver_graphics_tests --copy-input-kept-bytes) add_test(NAME agc_depth_copy_kept_bytes COMMAND agc_driver_graphics_tests --depth-copy-kept-bytes) set_tests_properties(agc_copy_input_kept_bytes agc_depth_copy_kept_bytes PROPERTIES diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp index 56368f852..4cbd855a9 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp @@ -4917,9 +4917,11 @@ void GuestBufferMemory::recordGpuCopies(std::span<Region* const> copies, bool ad expected.assign(snapshotBytes.begin(), snapshotBytes.end()); } } + recorder->Keep(region->buffer); CopyBuffer(context, commands, copySource, copyOffset, region->buffer->Handle(), 0, bytes); if (!expected.empty()) { auto readback = std::make_shared<Buffer>(context, expected.size(), VK_BUFFER_USAGE_TRANSFER_DST_BIT); + recorder->Keep(readback, readback->Bytes().size()); RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT); CopyBuffer(context, commands, region->buffer->Handle(), 0, readback->Handle(), 0, bytes); RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_HOST_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT); @@ -4935,11 +4937,6 @@ void GuestBufferMemory::recordGpuCopies(std::span<Region* const> copies, bool ad }); } if (inputSnapshot == nullptr) recorder->NotePendingRead(region->begin, static_cast<std::size_t>(bytes), Recorder::ReadKind::GpuCopy); - // Kept by the batch at record time, as every other recorded target is: the caller keeps - // the resources only after descriptor and pipeline work that may throw, and a released - // buffer would go back to the pool (reused or destroyed) under this copy command. The - // source is a live import or one retireImport already handed to this batch. - recorder->Keep(region->buffer); region->snapshot.clear(); if (region->deviceLocal) traceStaged(region->begin, region->end, region->atomic); if (profile) { diff --git a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp index 92e2141a3..a52480763 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp @@ -5025,6 +5025,89 @@ void bufferBudgetPlacementTests() { } } +bool copyFailureArmed = false; + +VKAPI_ATTR void VKAPI_CALL mockFailureCopy(VkCommandBuffer commands, VkBuffer source, VkBuffer destination, std::uint32_t count, const VkBufferCopy* regions) { + mockCmdCopyBuffer(commands, source, destination, count, regions); + if (copyFailureArmed && count == 1 && regions[0].size == 32 && (mock.bufferUsage.at(destination) & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) != 0) { + copyFailureArmed = false; + mock.memoryLimit = 0; + } +} + +PFN_vkVoidFunction VKAPI_CALL mockCopyFailureProc(VkDevice device, const char* name) { + if (std::strcmp(name, "vkCmdCopyBuffer") == 0) return reinterpret_cast<PFN_vkVoidFunction>(mockFailureCopy); + return mockProc(device, name); +} + +void copyInputFailureLifetimeTests() { +#ifdef _WIN32 + using namespace AgcDriver::Graphics; + constexpr std::size_t piece = 65536; + constexpr std::size_t bytes = 32; + constexpr auto properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + constexpr auto usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; + auto block = std::unique_ptr<std::byte[], void(*)(std::byte*)>(static_cast<std::byte*>(::operator new(piece, std::align_val_t{piece})), [](std::byte* value) { ::operator delete(value, std::align_val_t{piece}); }); + std::fill_n(block.get(), piece, std::byte{0}); + const auto base = reinterpret_cast<std::uint64_t>(block.get()); + bool refused = false; + bool earlyReuse = false; + mock = MockVulkan{}; + mock.deferFences = true; + { + auto context = mockContext(); + context.device = makeHandle<VkDevice>(); + context.deviceProc = mockCopyFailureProc; + context.bufferDeviceAddress = true; + context.hostImportAlignment = piece; + context.limits.minStorageBufferOffsetAlignment = 256; + context.limits.nonCoherentAtomSize = piece; + context.limits.maxMemoryAllocationCount = 4096; + SetImportWatch(context, ImportWatch::Unwatch); + const auto pool = GetBufferPool(context); + auto* mutation = GuestAllocations::GuestAllocationsBegin_nid_postfix(); + GuestAllocations::GuestAllocationsAdd_nid_postfix(mutation, block.get(), piece, true, false, true); + GuestAllocations::GuestAllocationsEnd_nid_postfix(mutation); + const auto unregister = [&](std::byte*) { + auto* mutation = GuestAllocations::GuestAllocationsBegin_nid_postfix(); + GuestAllocations::GuestAllocationsRemove_nid_postfix(mutation, block.get()); + GuestAllocations::GuestAllocationsEnd_nid_postfix(mutation); + }; + std::unique_ptr<std::byte, decltype(unregister)> registered(block.get(), unregister); + { + std::lock_guard lock(AgcDriver::GuestMemory::GpuMutex()); + Recorder recorder(context); + recorder.Activate(); + copyFailureArmed = true; + try { + GuestBufferMemory memory(context); + memory.AddReadable(base + 4, bytes); + memory.Upload(false); + } catch (const std::exception&) { + refused = true; + } + Require(refused && !copyFailureArmed && mock.bufferCopies.size() == 1, "diagnostic readback pressure was not exercised after the copy command"); + const auto destination = mock.bufferCopies.front().destination; + std::vector<BufferAllocation> taken; + while (const auto allocation = pool->Take(bytes, usage, properties)) { + earlyReuse |= allocation->buffer == destination; + taken.push_back(*allocation); + } + for (const auto& allocation : taken) pool->Put(allocation); + mock.memoryLimit.reset(); + recorder.Sync(); + } + registered.reset(); + ClearHostImports(context.device); + } + Require(mock.live == 0, "copy failure lifetime test leaked an object"); + std::cout << "diagnostic pressure refused=" << refused << " destination_reusable_before_fence=" << earlyReuse << '\n'; + Require(!earlyReuse, "recorded copy destination became reusable before its fence after readback pressure"); +#else + throw std::runtime_error("the copy failure mock fixture requires Windows guest registration"); +#endif +} + void copyInputKeptBytesTests() { using namespace AgcDriver::Graphics; constexpr std::size_t piece = 65536; @@ -10185,6 +10268,10 @@ int main(int argc, char** argv) { std::cout << "cached pipelines survive lazy buffer pool creation\n"; return 0; } + if (argc == 2 && std::string_view(argv[1]) == "--copy-input-failure-lifetime") { + copyInputFailureLifetimeTests(); + return 0; + } if (argc == 2 && std::string_view(argv[1]) == "--copy-input-kept-bytes") { copyInputKeptBytesTests(); std::cout << "GPU-copy input snapshot accounting and retention passed\n"; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/CopyInputLifetime.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/CopyInputLifetime.cpp new file mode 100644 index 000000000..4cc28c67b --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/CopyInputLifetime.cpp @@ -0,0 +1,155 @@ +#include "VulkanTestDevice.hpp" +#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" +#include "prx/libSceAgcDriver/Graphics/include/BufferPool.hpp" +#include "prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp" +#include "prx/libc/include/GuestAllocations.hpp" +#include <algorithm> +#include <cstdio> +#include <cstring> +#include <iostream> +#include <memory> +#include <mutex> +#include <new> +#include <vector> + +namespace { + +namespace Graphics = AgcDriver::Graphics; +using Graphics::Require; + +PFN_vkGetDeviceProcAddr originalProc = nullptr; +PFN_vkAllocateMemory originalAllocate = nullptr; +PFN_vkFreeMemory originalFree = nullptr; +PFN_vkCmdCopyBuffer originalCopy = nullptr; +bool refuseReadback = false; +bool copied = false; +unsigned refusals = 0; +unsigned copies = 0; +unsigned allocations = 0; +VkBuffer destination = VK_NULL_HANDLE; + +VKAPI_ATTR VkResult VKAPI_CALL Allocate(VkDevice device, const VkMemoryAllocateInfo* info, const VkAllocationCallbacks* callbacks, VkDeviceMemory* memory) { + if (refuseReadback && copied) { + ++refusals; + return VK_ERROR_OUT_OF_DEVICE_MEMORY; + } + const auto result = originalAllocate(device, info, callbacks, memory); + if (result == VK_SUCCESS) ++allocations; + return result; +} + +VKAPI_ATTR void VKAPI_CALL Free(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* callbacks) { + if (memory != VK_NULL_HANDLE) --allocations; + originalFree(device, memory, callbacks); +} + +VKAPI_ATTR void VKAPI_CALL Copy(VkCommandBuffer commands, VkBuffer source, VkBuffer target, std::uint32_t count, const VkBufferCopy* regions) { + originalCopy(commands, source, target, count, regions); + if (!copied) { + Require(count == 1 && regions[0].size == 32 && regions[0].srcOffset == 0 && regions[0].dstOffset == 0, "expected one exact 32-byte input copy"); + destination = target; + copied = true; + } + ++copies; +} + +PFN_vkVoidFunction VKAPI_CALL Resolve(VkDevice device, const char* name) { + if (std::strcmp(name, "vkAllocateMemory") == 0) return reinterpret_cast<PFN_vkVoidFunction>(&Allocate); + if (std::strcmp(name, "vkFreeMemory") == 0) return reinterpret_cast<PFN_vkVoidFunction>(&Free); + if (std::strcmp(name, "vkCmdCopyBuffer") == 0) return reinterpret_cast<PFN_vkVoidFunction>(&Copy); + return originalProc(device, name); +} + +void Scenario(Graphics::Context context, bool fail) { + constexpr std::size_t blockBytes = 65536; + constexpr auto usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; + constexpr auto properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + auto block = std::unique_ptr<std::byte[], void(*)(std::byte*)>(static_cast<std::byte*>(::operator new(blockBytes, std::align_val_t{blockBytes})), [](std::byte* value) { ::operator delete(value, std::align_val_t{blockBytes}); }); + std::fill_n(block.get(), blockBytes, std::byte{0x11}); + std::fill_n(block.get() + 4, 32, std::byte{0x5a}); + GuestAllocations::Mutation().Add(block.get(), blockBytes, true, false, true); + const auto unregister = [&](std::byte*) { GuestAllocations::Mutation().Remove(block.get()); }; + std::unique_ptr<std::byte, decltype(unregister)> registered(block.get(), unregister); + context.functions = nullptr; + context.deviceProc = &Resolve; + context.limits.minStorageBufferOffsetAlignment = 256; + context.limits.nonCoherentAtomSize = blockBytes; + context.bufferPool = std::make_shared<Graphics::BufferPool>(context); + copied = false; + copies = refusals = allocations = 0; + destination = VK_NULL_HANDLE; + refuseReadback = fail; + bool caught = false; + bool earlyReuse = false; + { + Graphics::Recorder recorder(context); + recorder.Activate(); + try { + Graphics::GuestBufferMemory memory(context); + memory.AddReadable(reinterpret_cast<std::uintptr_t>(block.get()) + 4, 32); + memory.Upload(false); + } catch (const std::exception&) { + caught = true; + } + refuseReadback = false; + Require(copied && destination != VK_NULL_HANDLE, "the fixture did not record an input copy"); + Require(fail ? caught && refusals != 0 && copies == 1 : !caught && refusals == 0 && copies == 2, "the diagnostic readback did not take the expected allocation path"); + std::vector<Graphics::BufferAllocation> taken; + while (const auto allocation = context.bufferPool->Take(32, usage, properties)) { + earlyReuse |= allocation->buffer == destination; + taken.push_back(*allocation); + } + for (const auto& allocation : taken) context.bufferPool->Put(allocation); + VkMemoryBarrier barrier{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; + barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + barrier.dstAccessMask = VK_ACCESS_HOST_READ_BIT; + context.Function<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier")(recorder.Commands(), VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &barrier, 0, nullptr, 0, nullptr); + recorder.Sync(); + } + bool checked = false; + std::vector<Graphics::BufferAllocation> completed; + while (const auto allocation = context.bufferPool->Take(32, usage, properties)) { + if (allocation->buffer == destination) { + Require(allocation->mapping != nullptr, "the completed destination is not mapped"); + const auto* bytes = static_cast<const std::byte*>(allocation->mapping); + Require(std::all_of(bytes, bytes + 32, [](std::byte value) { return value == std::byte{0x5a}; }), "GPU input copy data changed"); + checked = true; + } + completed.push_back(*allocation); + } + for (const auto& allocation : completed) context.bufferPool->Put(allocation); + for (std::size_t index = 0; index < blockBytes; ++index) Require(block[index] == (index >= 4 && index < 36 ? std::byte{0x5a} : std::byte{0x11}), "the copy changed guest memory or surrounding guards"); + registered.reset(); + Graphics::ClearHostImports(context.device); + context.bufferPool.reset(); + Require(allocations == 0, "the input copy leaked Vulkan allocations"); + std::printf("copy-input lifetime: fail=%d refusals=%u early_reuse=%d GPU_data_checked=%d\n", fail, refusals, earlyReuse, checked); + Require(checked && !earlyReuse, "a recorded copy destination became reusable before its fence"); +} + +} + +int main() { + try { + auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + std::lock_guard lock(AgcDriver::GuestMemory::GpuMutex()); + auto context = device->graphicsContext(); + if (context.hostImportAlignment == 0 || context.hostImportAlignment > 65536) { + Require(std::getenv("ANYPS5_REQUIRE_VULKAN") == nullptr, "the lifetime test requires host imports with at most 64 KiB alignment"); + std::puts("skipped, suitable host imports are unavailable"); + return VulkanTestSkipped; + } + Graphics::ClearHostImports(context.device); + originalProc = context.deviceProc; + originalAllocate = context.Function<PFN_vkAllocateMemory>("vkAllocateMemory"); + originalFree = context.Function<PFN_vkFreeMemory>("vkFreeMemory"); + originalCopy = context.Function<PFN_vkCmdCopyBuffer>("vkCmdCopyBuffer"); + Scenario(context, false); + Scenario(context, true); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} ``` </details>
VicRaster commented 2026-10-20 20:33:16 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by VicRaster on 2026-10-20T20:33:16Z.


The fix and synthetic regression tests are now in PR #8667: https://github.com/boykopovar/AnyPS5/pull/8667. Closing this standalone patch-review issue so review can continue on the PR. The fix is awaiting review and has not been merged or released.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6300859734 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/8659#issuecomment-6300859734). Originally posted by [VicRaster](https://github.com/VicRaster) on 2026-10-20T20:33:16Z. --- The fix and synthetic regression tests are now in PR #8667: https://github.com/boykopovar/AnyPS5/pull/8667. Closing this standalone patch-review issue so review can continue on the PR. The fix is awaiting review and has not been merged or released.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#8659
No description provided.