Repository navigation
fix(libSceAgcDriver): refresh heap mirrors when the range is not write-watched - #6546
mariuszkreft wants to merge 1 commit into
Conversation
…e-watched CollectWrites returns 0 when the guest arena write watch does not cover a range, which the write-tracking contract says callers must survive by falling back to comparing bytes. DispatchLookup and the mirror creation path already do; the three heap mirror refresh paths called Require instead and terminated the process. ChangedBlocks already reports every block as written when the range is unwatched, so the refresh stays correct and heapUnwatched records how often the fallback was taken.
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
Minecraft Dungeons II (PPSA16064), Windows 11, RTX 5070 Laptop (NVIDIA 617.42), a build of about 51 s after the start of the row, in the loading screens. The line before the throw in the periodic report had no heap mirror yet ( This PR makes that case survive. A test that reproduces it, Run on Windows with Mesa 26.2.4 lavapipe (windows-latest has no Vulkan device; the lavapipe ICD is registered in the loader's registry), ctest entry
The test needs a Vulkan device and a write-watched guest arena, so it runs on Windows with a GPU or lavapipe; the Linux lavapipe job skips it ( --- a/core/libs/prx/libSceAgcDriver/tests/BdaResources.cpp
+++ b/core/libs/prx/libSceAgcDriver/tests/BdaResources.cpp
@@ -958,6 +958,62 @@ void importWatchTests() {
#endif
}
+void unwatchedHeapMirrorTests(const Context& context, const BdaTestAccess& access) {
+ if (!GuestArena::GuestArenaAvailable_nid_postfix() || !GuestArena::GuestArenaWriteWatched_nid_postfix()) {
+ std::cout << "guest arena unavailable or not write-watched: heap mirrors without a write watch not tested\n";
+ return;
+ }
+ constexpr std::size_t bytes = 3 * 65536;
+ void* block = GuestArena::GuestArenaAllocate_nid_postfix(bytes, 65536);
+#ifdef _WIN32
+ GuestArena::GuestArenaCommit_nid_postfix(block, bytes, PAGE_READWRITE, bytes);
+#endif
+ auto* guest = static_cast<std::uint8_t*>(block);
+ std::memset(block, 0x11, bytes);
+ const auto address = reinterpret_cast<std::uintptr_t>(block);
+ const auto registry = [&](bool add) {
+ auto* mutation = GuestAllocations::GuestAllocationsBegin_nid_postfix();
+ if (add) GuestAllocations::GuestAllocationsAdd_nid_postfix(mutation, block, bytes, true, false, true);
+ else GuestAllocations::GuestAllocationsRemove_nid_postfix(mutation, block);
+ GuestAllocations::GuestAllocationsEnd_nid_postfix(mutation);
+ };
+ const auto build = [&] {
+ GuestBufferMemory leased(context);
+ leased.AcquireRegistered();
+ leased.Upload(true);
+ const auto ranges = leased.AddressRanges();
+ const auto found = std::find_if(ranges.begin(), ranges.end(), [&](const auto& range) { return range.begin == address; });
+ Require(found != ranges.end() && found->deviceAddress != 0u, "the heap range is missing from the BDA table");
+ leased.WriteBack();
+ return found->deviceAddress;
+ };
+ const auto sweep = [&] {
+ GuestBufferMemory leased(context);
+ leased.AcquireRegistered();
+ leased.Upload(true);
+ leased.WriteBack();
+ };
+ sweep();
+ registry(true);
+ const auto before = MirrorCounters();
+ build();
+ const auto made = MirrorCounters();
+ Require(made.heapMirrors == before.heapMirrors + 1 && made.heapBytes == before.heapBytes + bytes, "a read-only heap range was not mirrored");
+ AgcDriver::GuestMemory::Unwatch(address + 65536, 65536);
+ Require(!AgcDriver::GuestMemory::Watched(address, bytes), "the heap range is still write-watched after one of its blocks left the watch");
+ guest[7] = 0x22;
+ guest[65536 + 9] = 0x33;
+ guest[2 * 65536 + 11] = 0x44;
+ const auto device = build();
+ Require(access.addressBytes(device)[7] == std::byte{0x22} && access.addressBytes(device)[65536 + 9] == std::byte{0x33} && access.addressBytes(device)[2 * 65536 + 11] == std::byte{0x44}, "a heap mirror whose range left the write watch missed a CPU write");
+ guest[65536 + 13] = 0x55;
+ const auto again = build();
+ Require(access.addressBytes(again)[65536 + 13] == std::byte{0x55} && access.addressBytes(again)[7] == std::byte{0x22}, "a heap mirror whose range left the write watch missed a CPU write of a later build");
+ registry(false);
+ sweep();
+ Require(MirrorCounters().heapMirrors == before.heapMirrors && MirrorCounters().heapBytes == before.heapBytes, "the heap mirror outlived its range");
+}
+
void heapMirrorTests(const Context& context, const BdaTestAccess& access) {
if (!GuestArena::GuestArenaAvailable_nid_postfix() || !GuestArena::GuestArenaWriteWatched_nid_postfix()) {
std::cout << "guest arena unavailable or not write-watched: heap mirrors not tested\n";
@@ -1791,6 +1847,7 @@ void RunBdaResourceTests(const Context& context, const BdaTestAccess& access) {
importNeighbouringEdgesTests(context);
importAtMapOverBudgetTests(context);
heapMirrorTests(context, access);
+ unwatchedHeapMirrorTests(context, access);
importedHeapMirrorTests(context, access);
importWatchTests();
importedFreshTests(context);AI-assisted: yes |
|
@mariuszkreft a note from #6646, which handles the same Both PRs keep a heap mirror whose range has left the write watch from failing the build: #6546 refreshes such a mirror in full, and #6646 does too for a refused heap (and plans the range again when the host import path can take it). One case behaves differently on this branch: a run of adjacent heap mirrors, which A test for it: two adjacent 2 MiB heaps registered as one run, one 64 KiB block of the first taken off the watch, a write into each heap, then the next build. On this branch's head, with only the test added, it fails on Linux and Windows, with and without the address-space cache (run): The unwatched heap does serve its new bytes there. #6646 handles it by collecting each mirror of such a run on its own ( |
|
Thanks for the fix and for tracking down the crash. Closing this one in favour of #6646, which handles the same three
Both PRs edit the same lines in |
What
GuestMemory::CollectWritesreturns 0 when the guest arena write watch does not cover a range.GuestMemory.hppdocuments that callers must survive this by falling back to comparing bytes, andDispatchLookup.cppand the mirror creation path (GuestBufferMemory.cpp,if (generation == 0) { ++state.heapUnwatched; return nullptr; }) both do. The three heap mirror refresh paths calledRequireinstead, so on a host without write watching the first heap mirror refresh ended the process:They now count the case in the existing
heapUnwatchedcounter and continue.ChangedBlocksalready reports every block as written when the range is unwatched, so the refresh stays conservative and correct, just slower.Tested
Ubuntu 24.04.5, kernel 7.0.0-31-generic. Grand Theft Auto V (PPSA04264), NVIDIA GeForce RTX 3090, driver 580.159.03. Write watching is off on this host (
vm.unprivileged_userfaultfd = 0), which is how the crash reproduces; with this change the title gets past the shader that killed it and reaches its main menu and Story Mode, where it stops later, on an unrelated unimplemented path.agc_write_tracking,agc_write_watch_coverage,agc_buffer_pool,agc_storage_written_buffer_order,nid_buffer_validation,agc_driver_typed_heapsandagc_driver_graphicspass, and the AGC/driver/buffer/shader ctest subset (498 tests) is green.Caveat worth stating plainly: those runs used a local checkout whose base is roughly 1,500 commits behind
main, since that was the tree the change was developed and debugged in. The branch itself is based on currentmainand passes the Conventions check, but the branch was not itself built and tested here. Reviewers may want to re-run the suite against this branch.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)