fix(agc): release host imports before the device is destroyed #1239

Merged
awake-devel merged 2 commits from upstream-pr/1239 into main 2026-10-08 09:27:40 +00:00
awake-devel commented 2026-10-07 19:06:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by awake-devel on 2026-10-07T19:06:43Z.


What

Closes #1236.

VulkanDevice::State::~State releases the device's caches before vkDestroyDevice but never emptied the host import registry (Imports() in GuestBufferMemory.cpp, a function-local static). Every import still registered at teardown kept its VkBuffer and VkDeviceMemory past vkDestroyDevice, which the validation layer reports as VUID-vkDestroyDevice-device-05137. The next device then freed them through the destroyed device's handle in refreshImports, and that crashes: a second device importing the same block after the first was destroyed ends the process with SIGILL in the NVIDIA driver (SIGABRT under the validation layer). In the app the device is only destroyed in Driver::Shutdown, and the replacement at the first flip keeps the old device alive (#59), so no title reaches the crash today; tests and any later device recreation do.

  • ClearHostImports(VkDevice), beside ClearImageMirrors: under the registry lock, when the registry belongs to the device, destroys each import's buffer and memory (and unmaps its alias on Windows) as the device-change path of refreshImports does, then clears imports and failed, resets device and refreshedGeneration and bumps epoch.
  • ~State calls it right after ClearImageMirrors, once the recorder (with its retired imports), the resource cache and copiedWriters are gone.
  • New agc_driver_host_import_teardown: a device fills part of a registered guest block through VulkanDevice::FillBuffer, which imports it; the test checks that the fill reached guest memory and that the import exists, destroys the device and checks that the registry no longer holds an import for it. A second device then does the same with the same block, the sequence that crashed. It is skipped on devices without host imports.

tests/Recorder.cpp builds its own VkDevice and would need the same call before its vkDestroyDevice; it is not registered in ctest, and #1046 and #1210 rework that harness, so it is left alone here.

Tested

Linux (Arch, GCC 16.2.1), GeForce GTX 1050 Ti Max-Q, NVIDIA 580.178.04.

  • Full ctest: 400/400 pass (the 399 tests of main and the new one).
  • Full ctest under VK_LAYER_KHRONOS_validation 1.4.363.0 (core validation): 400/400 pass with no validation message. On main the 23 execution tests listed in #1236 report VUID-vkDestroyDevice-device-05137.
  • With the ClearHostImports call removed, the new test fails with the guest block's import outlived the first device and the layer reports 2 leaked objects; with it, the test passes.
  • The crash, measured with a variant of the test that goes on past that check: without this change the second device's import ends the process with SIGILL (exit 132), or SIGABRT (exit 134) under the layer; with it, both devices import, fill and release the block with no validation message.
  • Not tested on AMD or Windows; the _WIN32 alias unmapping follows refreshImports and was not built here.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_018RNrNcixatky8U4LfC7Ew5

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4777208277 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1239). Originally posted by [awake-devel](https://github.com/awake-devel) on 2026-10-07T19:06:43Z. --- ### What Closes #1236. `VulkanDevice::State::~State` releases the device's caches before `vkDestroyDevice` but never emptied the host import registry (`Imports()` in `GuestBufferMemory.cpp`, a function-local static). Every import still registered at teardown kept its `VkBuffer` and `VkDeviceMemory` past `vkDestroyDevice`, which the validation layer reports as `VUID-vkDestroyDevice-device-05137`. The next device then freed them through the destroyed device's handle in `refreshImports`, and that crashes: a second device importing the same block after the first was destroyed ends the process with `SIGILL` in the NVIDIA driver (`SIGABRT` under the validation layer). In the app the device is only destroyed in `Driver::Shutdown`, and the replacement at the first flip keeps the old device alive (#59), so no title reaches the crash today; tests and any later device recreation do. - `ClearHostImports(VkDevice)`, beside `ClearImageMirrors`: under the registry lock, when the registry belongs to the device, destroys each import's buffer and memory (and unmaps its alias on Windows) as the device-change path of `refreshImports` does, then clears `imports` and `failed`, resets `device` and `refreshedGeneration` and bumps `epoch`. - `~State` calls it right after `ClearImageMirrors`, once the recorder (with its retired imports), the resource cache and `copiedWriters` are gone. - New `agc_driver_host_import_teardown`: a device fills part of a registered guest block through `VulkanDevice::FillBuffer`, which imports it; the test checks that the fill reached guest memory and that the import exists, destroys the device and checks that the registry no longer holds an import for it. A second device then does the same with the same block, the sequence that crashed. It is skipped on devices without host imports. `tests/Recorder.cpp` builds its own `VkDevice` and would need the same call before its `vkDestroyDevice`; it is not registered in ctest, and #1046 and #1210 rework that harness, so it is left alone here. ### Tested Linux (Arch, GCC 16.2.1), GeForce GTX 1050 Ti Max-Q, NVIDIA 580.178.04. - Full `ctest`: 400/400 pass (the 399 tests of `main` and the new one). - Full `ctest` under `VK_LAYER_KHRONOS_validation` 1.4.363.0 (core validation): 400/400 pass with no validation message. On `main` the 23 execution tests listed in #1236 report `VUID-vkDestroyDevice-device-05137`. - With the `ClearHostImports` call removed, the new test fails with `the guest block's import outlived the first device` and the layer reports 2 leaked objects; with it, the test passes. - The crash, measured with a variant of the test that goes on past that check: without this change the second device's import ends the process with `SIGILL` (exit 132), or `SIGABRT` (exit 134) under the layer; with it, both devices import, fill and release the block with no validation message. - Not tested on AMD or Windows; the `_WIN32` alias unmapping follows `refreshImports` and was not built here. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_018RNrNcixatky8U4LfC7Ew5
adriadam10 commented 2026-10-08 09:27:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T09:27:37Z.


Checked on Linux (main + PR): full ctest 431/431; agc_driver_host_import_teardown passes, and fails with the ClearHostImports call reverted. ClearHostImports mirrors the device-change path of refreshImports (destroy buffer/memory, unmap alias on Windows, clear imports/failed, bump epoch). Linux, Windows and conventions CI are green on this head; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6056869750 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1239#issuecomment-6056869750). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T09:27:37Z. --- Checked on Linux (main + PR): full ctest 431/431; `agc_driver_host_import_teardown` passes, and fails with the `ClearHostImports` call reverted. `ClearHostImports` mirrors the device-change path of `refreshImports` (destroy buffer/memory, unmap alias on Windows, clear `imports`/`failed`, bump `epoch`). Linux, Windows and conventions CI are green on this head; merging.
Sign in to join this conversation.
No reviewers
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!1239
No description provided.