Repository navigation
fix(libc): refuse untrapped access over read traps on macOS - #226
Conversation
|
Thanks, this is the shape we had in mind in #201. Checked against the macOS
One heads-up: this and #216 both add a mode to the @dag294-alt, when you have a moment: the Wolverine intro with this branch and without |
On macOS GuestWriteWatchBeginUntrappedAccess was a no-op, so libSceAgcDriver's UntrappedImport guard did nothing there. An import then opened its host write over a trapped page under the write tracker lock, the write watch resolved the trap on the same thread, and ResolveLabelTrap re-entered the driver and blocked on that lock (AnyTogether#201). The macOS write watch now refuses an untrapped access over a range a read trap covers or one being resolved, without running the resolver, and records a granted range as a host write so that no trap is set there until the access ends, as on Linux and Windows. The import falls back to a copy until the trap is gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9516e0b to
1054571
Compare
|
@utkuhalis I rebased this onto |
|
Thanks, the rebase is clean: same change as before, with both |
|
Merged, thanks hedibf! The macOS job got a runner and everything is green. |
|
Tested on AnyTogether main with #226 (source version Three runs of the same intro section, 4-minute cap, all with the same switches:
The deadlock is gone. Both runs without the switch get through the SIE card and into the title scene; the game's render layers show Apply Lighting and Global Illumination. Both of them then stop the same way, which the run with the switch doesn't: Run 3 shows the same lines at My guess, not verified: something on that path reads a page that is read-trapped at that moment. Now that the untrapped access is refused instead of resolved, the read lands on the protected page. Because the runs without the switch end early, I don't have a fair before/after timing comparison yet. I'll do it once this is sorted. Happy to rerun with any trace switch or branch you'd like, and the full logs are available (no game files). AI-assisted: yes (Claude). The runs are from my machine. |
|
Thanks for the careful runs. Good to have it confirmed that the deadlock is gone and Wolverine gets into the title scene. On the new stop, here's where the message comes from. Treat this as a lead, I haven't reproduced it:
That fits what you saw:
The check itself predates #226. #226 only lets the game get far enough to hit it. #233, the follow-up to #226, is waiting on CI. It changes the host-import path ( @hedibravofox, this is your read-trap area, so I'll leave it to you if you want to take it. If not, say so and we'll pick it up. |
|
Thanks, that fits what I see. One data point narrows it a little. The So the drop just before the stop in run 3 looks incidental. That leaves the busy-registry ( I'll rerun with #233 once it's merged. I'm also happy to run a diagnostic branch from @hedibravofox, for example one that logs which fallback branch fired in AI-assisted: yes (Claude). The runs are from my machine. |
What
#201: on macOS, Marvel's Wolverine (PPSA03671) stops on the SIE card because of a label read-trap deadlock.
GuestMemory::ImportWatchedholds the write tracker lock whenrunImport()opensHostWrite(..., Import{}). The write watch then resolves an overlapping read trap on the same thread, andResolveLabelTrap→FlushForAccess→ ... →observewaits for the lock that thread already holds.Linux and Windows don't deadlock here.
importRunAtholds anUntrappedImportthat refuses an import over a trapped page instead of resolving it. On macOS that guard did nothing, becauseGuestWriteWatchBeginUntrappedAccesswas a no-op that returned true.This follows the direction utkuhalis gave in #201: a macOS
BeginUntrappedon the write watch, with the untrapped-access calls turned on for__APPLE__.Watch::BeginUntrappedrefuses a range that a read trap covers, or one being resolved (resolvingCovers(..., Owner::Any)). It never runs the resolver. Otherwise it records the page-rounded range in_hostWrites, soTrapReadssets no new trap there until the access ends.GuestWriteWatchBeginUntrappedAccessandGuestWriteWatchEndUntrappedAccesscall it on macOS. The end goes throughEndHostWrite, which rounds the range the same way.So an import over a trapped page falls back to a copy until the trap clears, as on Linux. While an import is open, the images over its range are stored at the label rather than trapped.
APS5_NO_LABEL_READ_TRAP=1is no longer needed to get past the deadlock. No driver code changes:UntrappedImportalready wraps everyImportWatchedcall inimportRunAt.A debt fragment records both sides. The libSceAgcDriver entry mirrors the Linux "Read traps over host pointer imports" wording.
Tested
macOS 27.2.1, Apple silicon, x86-64 build under Rosetta 2 (AppleClang 21.0.0, Release), on current
main(b23fbf4):write_watch_read-trap-untrappedfollows the deadlock pattern. The test thread holds the lock the resolver needs, as the import holds the tracker lock, and checks that:PROT_NONE;It passes 5 runs out of 5. All 30
write_watch_*tests pass 3 runs out of 3.resolvingCoverscheck, it fails: "an untrapped access was granted over a trap another thread is resolving".ctest -R 'write_watch|read_trap|ReadTrap|label_trap|guest_memory|import|agc_write|guest_arena|write_tracking|label': 177 tests. All pass or are skipped exceptguest_memory, which is a listed known macOS failure and fails the same way onmain("fixed guest mapping 0x1000000000+0x4000 overlaps a host region in the guest arena").python3 tools/debt_fold.py --checkandpython3 tools/check_conventions.py --base origin/main: all checks passed.GuestWriteWatch.cppmerges cleanly with fix(libc): compare, not mark, writable pages in a failing macOS host write #216. Both PRs add a mode to thewrite_watchtest list (WriteWatch.cppand itsCMakeLists.txt), so whichever merges second needs a one-line rebase there.pr_overlap.py --branch: no open PR implements the same thing. The only one sharing this PR's files is upstream fix(agc): compare pending storage results over a surface whose page walk fails boykopovar/AnyPS5#12792, which it could not compare. InGuestWriteWatch.cpp, 12792 changes the LinuxWatchclass and other wrappers, not the macOSBeginUntrappedor the untrapped-access calls.eboot, like macOS patches: PPSA03671 address planner base above the macOS reserved window #196 does for PPSA03671: the memory manager'smovabsq $0x1000000000at0x1404c6bebmoved to0x7000000000. That edit isn't part of this PR. All runs used the engine frommainatce36b146f3, before the rebase; onlylibc.prxdiffers between them.main, the game stops about 10 s in.sampleshows the macOS: PPSA03671 deadlocks on the SIE card: label read-trap resolution re-enters libSceAgcDriver during an import (APS5_NO_LABEL_READ_TRAP=1 avoids it) #201 chain ondraw-commit:ImportWatched→GuestArenaBeginImportWrite→GuestWriteWatchBeginHostWrite→Watch::resolve→ResolveLabelTrap→FlushForAccess→StorageTexture::AccessKeptByCpu→CollectWritesUncached→observe→lockTracker, waiting for the lock its own thread holds. Behind it areImportMappedRanges(waiting on the import mutex), qworker 0x0 inDrawPipeline::Drain, and the title's submit thread inholdFlipBehindWorker.APS5_NO_LABEL_READ_TRAP=1it gets past that, presents frames, and goes quiet in the log after about 20 s.libc.prxand no workaround, it runs for 2 minutes. Asampleat 2 minutes has noResolveLabelTrapunder an import and no thread inlockContended. It gets further than with the workaround: it presents frames, searches save data, loads its trophies and draws with shaders prepared at use. It then exits cleanly (code 0) when stopped. What comes next is a separate problem: GPU queues waiting more than 10 s onWAIT_REG_MEM, and recorded batches still running after 5 s.APS5_NO_LABEL_READ_TRAP, and compare it with a run with the variable, as utkuhalis suggested in macOS: PPSA03671 deadlocks on the SIE card: label read-trap resolution re-enters libSceAgcDriver during an import (APS5_NO_LABEL_READ_TRAP=1 avoids it) #201?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)🤖 Generated with Claude Code