macOS: agc_write_tracking fails, ImportWatched marks the whole import window as CPU-written #10344

Closed
opened 2026-10-23 18:27:11 +00:00 by krishalivaibhav · 0 comments
krishalivaibhav commented 2026-10-23 18:27:11 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by krishalivaibhav on 2026-10-23T18:27:11Z.


Environment: MacBook Air M2, macOS (Darwin 27.2), AppleClang, Rosetta 2, main d6e7ab42d plus #8658 (needed for a macOS build), Release (also reproduced in RelWithDebInfo), BUILD_TESTING=ON.

Reproduction

ctest --test-dir build -R '^agc_write_tracking$' --output-on-failure
write tracking test failed: a block the import window did not touch reads as written

From core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp:1151 (CheckImportWindow).

Findings. With a debug print before the check: changed = {0, 1, 0} (correct, from the page-hash window) but cpu = {1, 1, 1}; the test requires cpu[2] == 0. ImportWatched (GuestMemory.cpp) opens a GuestArena::HostWrite over the whole [first, stop) window on macOS; Watch::BeginHostWrite (libc/src/GuestWriteWatch.cpp) unprotects every page in it and sets Written on each, because the host cannot see which pages the import will store to. Every block of the window then reports a CPU store, including blocks the import left untouched.

Impact. Over-reporting only (conservative): the byte-change result is right, but CPU-written blocks inside an import window always look dirty on macOS, so cached GPU data for them is invalidated more often than on Linux.

Possible direction. See the fix attempt below: a hash-based clear is not enough, because the test needs written-but-unchanged pages to stay CPU-touched. A real fix needs per-page write detection during the import.

Not proven: whether main alone shows the same (it does not build on macOS without #8658; #8658 does not change ImportWatched or GuestWriteWatch.cpp).

Fix attempt (not viable as a small patch)

Tried: in stampWrittenRun (GuestMemory.cpp), on macOS skip stamping pages whose post-import hash equals the pre-import hash (8 lines, saved as macos-import-window-attempt.diff). Result on M2, full ctest: agc_write_tracking still fails, now on the opposite assertion "a page an import window resolved no longer counts as possibly touched by the CPU" (cpu[0] must stay 1 for a page the import wrote without changing bytes). The test needs both: cpu=1 for a written-but-unchanged page and cpu=0 for an untouched page. On macOS BeginHostWrite unprotects and marks the whole window Written up front, so written-unchanged and untouched pages are indistinguishable; only precise per-page write detection during the import can satisfy both.
Design for a real fix (larger, not done): keep the import window write-protected and let the fault handler's record() mark only the pages actually stored to; use the blanket BeginHostWrite unprotect only for imports that call into the kernel with the guest buffer (read()/recv into guest memory fault with EFAULT instead of SIGSEGV). That means an opt-in per call site: ImportWatched would take a flag, and the GuestBufferMemory callers (GuestBufferMemory.cpp:1774, 1787) would decide whether their import lambda is in-process. Estimated 60-120 lines plus a test; needs maintainers' view on the API.
Separate observation: with RelWithDebInfo (-O2 -g) guest_user_context aborts deterministically on this M2 (passed in the Release build), so the macOS test results depend on the build type.

AI-assisted: yes (Claude).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6003326811 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/10344). Originally posted by [krishalivaibhav](https://github.com/krishalivaibhav) on 2026-10-23T18:27:11Z. --- **Environment:** MacBook Air M2, macOS (Darwin 27.2), AppleClang, Rosetta 2, `main` d6e7ab42d plus #8658 (needed for a macOS build), Release (also reproduced in RelWithDebInfo), `BUILD_TESTING=ON`. **Reproduction** ``` ctest --test-dir build -R '^agc_write_tracking$' --output-on-failure ``` ``` write tracking test failed: a block the import window did not touch reads as written ``` From `core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp:1151` (`CheckImportWindow`). **Findings.** With a debug print before the check: `changed = {0, 1, 0}` (correct, from the page-hash window) but `cpu = {1, 1, 1}`; the test requires `cpu[2] == 0`. `ImportWatched` (`GuestMemory.cpp`) opens a `GuestArena::HostWrite` over the whole `[first, stop)` window on macOS; `Watch::BeginHostWrite` (`libc/src/GuestWriteWatch.cpp`) unprotects every page in it and sets `Written` on each, because the host cannot see which pages the import will store to. Every block of the window then reports a CPU store, including blocks the import left untouched. **Impact.** Over-reporting only (conservative): the byte-change result is right, but CPU-written blocks inside an import window always look dirty on macOS, so cached GPU data for them is invalidated more often than on Linux. **Possible direction.** See the fix attempt below: a hash-based clear is not enough, because the test needs written-but-unchanged pages to stay CPU-touched. A real fix needs per-page write detection during the import. Not proven: whether `main` alone shows the same (it does not build on macOS without #8658; #8658 does not change `ImportWatched` or `GuestWriteWatch.cpp`). ## Fix attempt (not viable as a small patch) Tried: in `stampWrittenRun` (GuestMemory.cpp), on macOS skip stamping pages whose post-import hash equals the pre-import hash (8 lines, saved as macos-import-window-attempt.diff). Result on M2, full ctest: agc_write_tracking still fails, now on the opposite assertion "a page an import window resolved no longer counts as possibly touched by the CPU" (cpu[0] must stay 1 for a page the import wrote without changing bytes). The test needs both: cpu=1 for a written-but-unchanged page and cpu=0 for an untouched page. On macOS `BeginHostWrite` unprotects and marks the whole window `Written` up front, so written-unchanged and untouched pages are indistinguishable; only precise per-page write detection during the import can satisfy both. Design for a real fix (larger, not done): keep the import window write-protected and let the fault handler's `record()` mark only the pages actually stored to; use the blanket `BeginHostWrite` unprotect only for imports that call into the kernel with the guest buffer (read()/recv into guest memory fault with EFAULT instead of SIGSEGV). That means an opt-in per call site: ImportWatched would take a flag, and the GuestBufferMemory callers (GuestBufferMemory.cpp:1774, 1787) would decide whether their import lambda is in-process. Estimated 60-120 lines plus a test; needs maintainers' view on the API. Separate observation: with RelWithDebInfo (-O2 -g) guest_user_context aborts deterministically on this M2 (passed in the Release build), so the macOS test results depend on the build type. AI-assisted: yes (Claude).
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#10344
No description provided.