Skip to content

fix(libc): refuse untrapped access over read traps on macOS - #226

Merged
utkuhalis merged 1 commit into
AnyTogether:mainfrom
hedibravofox:fix/macos-untrapped-import
Dec 4, 2026
Merged

utkuhalis merged 1 commit into
AnyTogether:mainfrom
hedibravofox:fix/macos-untrapped-import

Conversation

@hedibravofox

Copy link
Copy Markdown

What

#201: on macOS, Marvel's Wolverine (PPSA03671) stops on the SIE card because of a label read-trap deadlock. GuestMemory::ImportWatched holds the write tracker lock when runImport() opens HostWrite(..., Import{}). The write watch then resolves an overlapping read trap on the same thread, and ResolveLabelTrap → FlushForAccess → ... → observe waits for the lock that thread already holds.

Linux and Windows don't deadlock here. importRunAt holds an UntrappedImport that refuses an import over a trapped page instead of resolving it. On macOS that guard did nothing, because GuestWriteWatchBeginUntrappedAccess was a no-op that returned true.

This follows the direction utkuhalis gave in #201: a macOS BeginUntrapped on the write watch, with the untrapped-access calls turned on for __APPLE__.

  • Watch::BeginUntrapped refuses 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, so TrapReads sets no new trap there until the access ends.
  • GuestWriteWatchBeginUntrappedAccess and GuestWriteWatchEndUntrappedAccess call it on macOS. The end goes through EndHostWrite, 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=1 is no longer needed to get past the deadlock. No driver code changes: UntrappedImport already wraps every ImportWatched call in importRunAt.

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):

  • New write_watch_read-trap-untrapped follows the deadlock pattern. The test thread holds the lock the resolver needs, as the import holds the tracker lock, and checks that:
    • an untrapped access over a trapped page is refused without running the resolver, and the page stays PROT_NONE;
    • an access over untrapped pages is granted, no trap can be set there, and a host write inside it runs no resolver;
    • an access over a trap that another thread is resolving is refused;
    • after the resolve, or after the access ends, both work again.
      It passes 5 runs out of 5. All 30 write_watch_* tests pass 3 runs out of 3.
  • Negative controls:
    • with the old no-op, the new test fails: "an untrapped access was granted over a trapped page";
    • without the resolvingCovers check, 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 except guest_memory, which is a listed known macOS failure and fails the same way on main ("fixed guest mapping 0x1000000000+0x4000 overlaps a host region in the guest arena").
  • python3 tools/debt_fold.py --check and python3 tools/check_conventions.py --base origin/main: all checks passed.
  • GuestWriteWatch.cpp merges cleanly with fix(libc): compare, not mark, writable pages in a failing macOS host write #216. Both PRs add a mode to the write_watch test list (WriteWatch.cpp and its CMakeLists.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. In GuestWriteWatch.cpp, 12792 changes the Linux Watch class and other wrappers, not the macOS BeginUntrapped or the untrapped-access calls.
  • Grand Theft Auto V (PPSA04263, content 01.011.001), same Mac. GTA V hits the same deadlock. To get it that far, I used a local base-address edit in its eboot, like macOS patches: PPSA03671 address planner base above the macOS reserved window #196 does for PPSA03671: the memory manager's movabsq $0x1000000000 at 0x1404c6beb moved to 0x7000000000. That edit isn't part of this PR. All runs used the engine from main at ce36b146f3, before the rebase; only libc.prx differs between them.
    • On main, the game stops about 10 s in. sample shows 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 on draw-commit: ImportWatched → GuestArenaBeginImportWrite → GuestWriteWatchBeginHostWrite → Watch::resolve → ResolveLabelTrap → FlushForAccess → StorageTexture::AccessKeptByCpu → CollectWritesUncached → observe → lockTracker, waiting for the lock its own thread holds. Behind it are ImportMappedRanges (waiting on the import mutex), qworker 0x0 in DrawPipeline::Drain, and the title's submit thread in holdFlipBehindWorker.
    • With APS5_NO_LABEL_READ_TRAP=1 it gets past that, presents frames, and goes quiet in the log after about 20 s.
    • With this branch's libc.prx and no workaround, it runs for 2 minutes. A sample at 2 minutes has no ResolveLabelTrap under an import and no thread in lockContended. 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 on WAIT_REG_MEM, and recorded batches still running after 5 s.
  • Not tested on PPSA03671: I don't have it. @dag294-alt, could you run the intro with this branch and without 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

🤖 Generated with Claude Code

@utkuhalis

Copy link
Copy Markdown
Collaborator

Thanks, this is the shape we had in mind in #201. Checked against the macOS Watch:

  • BeginUntrapped rounds the range the same way EndHostWrite does, so the end finds and erases the exact entry. EndHostWrite on macOS only erases it; it doesn't touch protections or Written, so ending an access that never opened pages is safe.
  • trapCovers compares the stored trap bytes with a page-aligned range, so any page overlap is refused. The added resolvingCovers(..., Owner::Any) check goes further than the Linux ReadTraps::BeginUntrapped, which is right here, since the resolver is what deadlocks.
  • TrapReads already refuses ranges in _hostWrites, so no trap can land inside a granted access.

One heads-up: this and #216 both add a mode to the write_watch test list, so whichever merges second needs that one-line rebase, as you noted. We'll merge once the macOS job gets a runner and CI is green.

@dag294-alt, when you have a moment: the Wolverine intro with this branch and without APS5_NO_LABEL_READ_TRAP would be the real check.

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>
@hedibravofox
hedibravofox force-pushed the fix/macos-untrapped-import branch from 9516e0b to 1054571 Compare December 3, 2026 19:49
@hedibravofox

Copy link
Copy Markdown
Author

@utkuhalis I rebased this onto main after #216 merged; both write_watch test modes are kept. Linux and Windows CI pass, but the macOS job was cancelled after 15 minutes without getting a runner, so ci-ok failed. Could you rerun the failed jobs when macOS runners are available? I can't rerun them from my side.

@utkuhalis

Copy link
Copy Markdown
Collaborator

Thanks, the rebase is clean: same change as before, with both write_watch modes in the list. I've started the rerun and will keep retrying until the macOS job gets a runner, then merge once it's green.

@utkuhalis
utkuhalis merged commit 1e3b87a into AnyTogether:main Dec 4, 2026
18 of 20 checks passed
@utkuhalis

Copy link
Copy Markdown
Collaborator

Merged, thanks hedibf! The macOS job got a runner and everything is green.

@dag294-alt

Copy link
Copy Markdown

Tested on AnyTogether main with #226 (source version 1372a4702f243b89): Wolverine PPSA03671 01.001.005, M4 Max, macOS 26.7.3, Rosetta, MoltenVK (mesh=0).

Three runs of the same intro section, 4-minute cap, all with the same switches:
APS5_HOST_INTERPOLATE_CUSTOM_INPUTS=1 APS5_SKIP_REFUSED_DISPATCHES=1 APS5_NULL_INVALID_BUFFERS=1 APS5_DEFAULT_UNRESOLVED_SAMPLERS=1 APS5_LOOP_GUARD=16384 APS5_COHERENT_ATOMIC_LOADS=1 APS5_NO_TABLE_SNAPSHOT_REUSE=1 APS5_SKIP_REFUSED_DRAWS=1 APS5_NO_ADAPTIVE_STAGING=1 APS5_HOST_IMPORT_MIB=12288 APS5_HEAP_MIRROR_MIB=12288

Run APS5_NO_LABEL_READ_TRAP Result
1 not set past the SIE card, reached RenderFrameIndex 566, then terminate
2 1 reached RenderFrameIndex 649, clean exit at the time limit
3 not set past the SIE card, reached RenderFrameIndex 300, then terminate

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:

[gpu] driver failure: AGC driver: guest memory is not readable at 0xb119d5c000 (range 0xb119d5c000+0x1000)
[videoout] presentation failed: AGC driver: guest memory is not readable at 0xb119d5c000 (range 0xb119d5c000+0x1000)
terminate called after throwing an instance of 'std::runtime_error'

Run 3 shows the same lines at 0xb11b3dc000+0x1000. In both runs it's a single page, and it surfaces as a presentation failure. Shortly before it, run 3 logged [gpu] storage image 0xb10ea30000+0x870000: memory no longer registered; 1 pending ranges dropped.

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.

@utkuhalis

Copy link
Copy Markdown
Collaborator

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:

  • The message is checkAccess in libSceAgcDriver/Execution/src/GuestMemory.cpp, which the driver runs before it reads guest memory.
  • On macOS that check first asks the allocation registry. It falls back to mach_vm_region and the page's host protection when:
    • the registry doesn't hold the range (your run 3 logged a memory no longer registered drop just before), or
    • the registry is busy (TryAcquire fails), or
    • the range is registered read-only.
  • A read-trapped page is PROT_NONE on the host. In that fallback it reads as not readable, even though the guest can read it: a guest read would fault and resolve the trap.
  • The write watch already has an equivalent case for writes (watchWritableOver). Read traps don't, so far as I can see.

That fits what you saw:

  • It only happens with the label read trap on.
  • It's a single page.
  • It shows up at different frames, which points at timing (a busy registry) more than at one specific buffer.

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 (BeginUntrapped is only called there), so I don't expect it to change this stop. A rerun with it once it's merged would still tell us.

@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.

@dag294-alt

Copy link
Copy Markdown

Thanks, that fits what I see. One data point narrows it a little.

The memory no longer registered lines aren't specific to the failing runs. Each of the three runs logged four of them, including the clean run with APS5_NO_LABEL_READ_TRAP=1. They're all for other images, in the 0xb10cb90000–0xb10ea30000 range, mostly +0x870000. The failing pages (0xb119d5c000 in run 1, 0xb11b3dc000 in run 3) don't appear anywhere earlier in either log.

So the drop just before the stop in run 3 looks incidental. That leaves the busy-registry (TryAcquire) branch or the read-only branch, and the stop landing at different frames fits the busy case.

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 checkAccess and whether a read trap covers the page at that moment.

AI-assisted: yes (Claude). The runs are from my machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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)

3 participants