From 73a07e2e593d2b140e63c95b08058249b8c87129 Mon Sep 17 00:00:00 2001 From: Shrey Ratna Date: Fri, 20 Nov 2026 16:03:53 +0530 Subject: [PATCH 1/2] fix(agc): do not cache page states of host regions in the guest arena The page-state cache records the access of every page it queries in the guest arena range, and relies on libc reporting mapping and protection changes to forget them. Host-owned regions inside that range (memory in use when the arena was reserved) change without libc knowing, so their cached state went stale: a page committed after a query stayed inaccessible, and a page decommitted or made no-access stayed readable. On a machine where the test's VirtualAlloc landed in such a region, agc_driver_image_table_cache found its newly committed records still Unmapped (#24186). Pages that overlap a host region are no longer recorded in the arena span, and the image span is looked up before the arena span so the main image keeps its cache when it lies inside the arena range. Co-Authored-By: Claude Opus 5.5 --- .../Execution/src/GuestMemory.cpp | 3 +- .../prx/libSceAgcDriver/tests/PageStates.cpp | 30 +++++++++++++++++++ .../tests/cmake/agc_page_state_tests.cmake | 4 ++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp index d7cd1c2cf574..1612af462bfc 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp @@ -782,8 +782,8 @@ struct PageStates { void initialize(); PageSpan* spanOf(std::uintptr_t address) { - if (arena.covers(address)) return &arena; if (image.covers(address)) return ℑ + if (arena.covers(address)) return &arena; return nullptr; } @@ -800,6 +800,7 @@ struct PageStates { const std::uint8_t value = readable ? static_cast(PageReadable | (writable ? PageWritable : 0u)) : PageInaccessible; const auto first = std::max(base, span->base); const auto last = std::min(regionEnd, span->base + span->size); + if (span == &arena && GuestArena::GuestArenaHostRegionOverlaps_nid_postfix(first, static_cast(last - first))) continue; span->store(first, last, value); if (GuestAllocations::GuestAllocationsGeneration_nid_postfix() != generation || accessSerial.load(std::memory_order_acquire) != access) span->forget(first, static_cast(last - first)); } diff --git a/core/libs/prx/libSceAgcDriver/tests/PageStates.cpp b/core/libs/prx/libSceAgcDriver/tests/PageStates.cpp index d4e6add733ed..9dfd9ce1e321 100644 --- a/core/libs/prx/libSceAgcDriver/tests/PageStates.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/PageStates.cpp @@ -233,11 +233,41 @@ void CheckRuns() { Require(!DescribeCommitted(edgeFirst, edgePages * page + Unit, true).whole && CommittedRanges(edgeFirst, edgePages * page + Unit, true) == edgeRun, "a run into an unallocated chunk was reported past its committed pages"); std::puts("page-state block scans across 64-page, 512-page and sparse-chunk boundaries passed"); } + +int CheckHostRegion() { + constexpr std::size_t page = 4096; + auto* block = static_cast(HeapAlloc(GetProcessHeap(), 0, 4 * page)); + Require(block != nullptr, "cannot allocate a host-region fixture"); + auto* readable = reinterpret_cast((reinterpret_cast(block) + page - 1) & ~static_cast(page - 1)); + auto* reserved = readable + page; + if (!GuestArena::GuestArenaHostRegionOverlaps_nid_postfix(reinterpret_cast(readable), 2 * page)) { + HeapFree(GetProcessHeap(), 0, block); + std::puts("the process heap is outside the guest arena's host regions"); + return 77; + } + Require(VirtualFree(reserved, page, MEM_DECOMMIT) != FALSE, "cannot decommit the host-region fixture"); + const bool accessible = Accessible(readable, page, true); + const bool decommitted = !Accessible(reserved, page); + Require(VirtualAlloc(reserved, page, MEM_COMMIT, PAGE_READWRITE) == reserved, "cannot commit the host-region fixture"); + const bool committed = Accessible(reserved, page, true); + DWORD original = 0; + Require(VirtualProtect(readable, page, PAGE_NOACCESS, &original) != FALSE, "cannot protect the host-region fixture"); + const bool hidden = !Accessible(readable, page); + Require(VirtualProtect(readable, page, original, &original) != FALSE, "cannot restore the host-region fixture"); + HeapFree(GetProcessHeap(), 0, block); + Require(accessible, "a committed host-region page was inaccessible"); + Require(decommitted, "a decommitted host-region page was accessible"); + Require(committed, "a host-region page committed again kept its inaccessible state"); + Require(hidden, "a host-region page made no-access kept its accessible state"); + std::puts("page states of host regions in the guest arena follow protection changes"); + return 0; +} } int main(int argc, char** argv) { try { Require(GuestArena::GuestArenaAvailable_nid_postfix(), "page-state fixture needs the guest arena"); + if (argc == 2 && std::string_view(argv[1]) == "--host-region") return CheckHostRegion(); if (argc == 2 && std::string_view(argv[1]) == "--metadata") CheckMetadata(); else if (argc == 2 && std::string_view(argv[1]) == "--runs") CheckRuns(); else CheckBoundaries(); diff --git a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_page_state_tests.cmake b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_page_state_tests.cmake index e73b40c8de51..1a3929d14c91 100644 --- a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_page_state_tests.cmake +++ b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_page_state_tests.cmake @@ -6,6 +6,8 @@ if(BUILD_TESTING AND WIN32) add_test(NAME agc_page_states COMMAND agc_page_state_tests) add_test(NAME agc_page_state_metadata COMMAND agc_page_state_tests --metadata) add_test(NAME agc_page_state_runs COMMAND agc_page_state_tests --runs) - set_tests_properties(agc_page_states agc_page_state_metadata agc_page_state_runs PROPERTIES + add_test(NAME agc_page_state_host_region COMMAND agc_page_state_tests --host-region) + set_tests_properties(agc_page_states agc_page_state_metadata agc_page_state_runs agc_page_state_host_region PROPERTIES TIMEOUT 30 ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") + set_tests_properties(agc_page_state_host_region PROPERTIES SKIP_RETURN_CODE 77) endif() From a3a71daf879145675fc18a7475f5e3e293f2505d Mon Sep 17 00:00:00 2001 From: Dean <158058829+oneandonlydean@users.noreply.github.com> Date: Fri, 27 Nov 2026 22:00:33 +1300 Subject: [PATCH 2/2] fix(agc): guard the host region skip to Windows The arena host region query is declared only on Windows, so the unconditional call broke the Linux build. --- core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp index 1612af462bfc..b3aa4e830ad3 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp @@ -800,7 +800,9 @@ struct PageStates { const std::uint8_t value = readable ? static_cast(PageReadable | (writable ? PageWritable : 0u)) : PageInaccessible; const auto first = std::max(base, span->base); const auto last = std::min(regionEnd, span->base + span->size); +#ifdef _WIN32 if (span == &arena && GuestArena::GuestArenaHostRegionOverlaps_nid_postfix(first, static_cast(last - first))) continue; +#endif span->store(first, last, value); if (GuestAllocations::GuestAllocationsGeneration_nid_postfix() != generation || accessSerial.load(std::memory_order_acquire) != access) span->forget(first, static_cast(last - first)); }