macOS boot hang after "Relocate program": ProtectGuestMemory applies guest-only protection to the host mapping (regression from #135) #148

Closed
github-bot wants to merge 1 commit from gh-pr/148/head into gh-pr/148/base
Owner

@⁠Stepz97 on GitHub · 2026-08-01 15:40 UTC · original
Stepz97:fix-protect-guest-memory-host-clamp → main · +11 −1 · 1 files · 1 commits · closed without merging

Symptom

Since the guest memory rework (#135, first bad commit 2f5396c, found by bisect between f6e01e5 and a65d17a with a validated good/bad oracle), nothing boots on macOS: both homebrew samples and games freeze right after --- Relocate program: ... ---, with one thread spinning at 100% CPU and no further output. Before #135 (f6e01e5), the same binaries execute guest code normally.

This looks related to the Raiden III regression reported around #136 — same first bad commit territory, and #136 (signal dispatch) does not fix this hang.

Root cause

#135 establishes the invariant that guest-visible protection lives in the virtual range tracker while host mappings stay writable, and applies exactly that clamp in SetProgramMemoryProtection:

const auto host_mode = VirtualMemory::IsExecute(mode) ? VirtualMemory::Mode::ExecuteReadWrite
                                                      : VirtualMemory::Mode::ReadWrite;

But ProtectGuestMemory still applies the requested guest mode verbatim to the host mapping. The broken sequence on every boot:

  1. InstallRelocateHandler calls ProtectGuestMemory(pltgot, Write, &old_mode). old_mode is read from the range tracker, so for the pltgot (inside a read-only RELRO segment) it comes back as Read.
  2. After patching pltgot[1]/pltgot[2], it restores old_mode — and the restore mprotects the host page to read-only.
  3. The first relocation write in RelocateRecords (PatchGuestMemory64, a plain store) faults. The GPU fault handler path finds the page inside a GPU-mapped range, runs an invalidate that is a no-op (no GPU resources exist at boot), returns "handled" without changing any protection, and the retried store faults again. Infinite fault livelock.

Measured on macOS: 8,000+ identical faults (rip = RelocateRecords, fault addr = pltgot+0x18, page-fault error code 0x6 = user-mode write) with the handler returning "resolved" every time. Windows presumably survives because its fault path resolves the write differently, but the defect itself is platform-independent code.

Fix

Clamp the host mode in ProtectGuestMemory exactly like SetProgramMemoryProtection already does, preserving NoAccess so guard pages still trap. The tracker keeps recording the requested guest mode.

Validation

  • On pure a65d17a + this patch alone: guest code executes again on macOS (unresolved-import stub calls resume, where before the process never left Relocate).
  • On our macOS branch (this patch plus unrelated local work): full test suite passes (15/17, the 2 failures are the pre-existing Linux-only memory_tracker/page_manager), and the Mandelbrot homebrew sample renders byte-identical to its pre-#135 reference dump.

Two adjacent notes (no patch attached, maintainer's call)

  1. KernelMprotect has the same shape (applies the guest mode verbatim to the host mapping). Since that one is the guest-facing syscall, faithfully applying guest protection may be intentional — but any guest that mprotects its own memory read-only and later gets patched by the runtime linker will hit the same livelock. Possibly the actual Raiden III mechanism.
  2. #135 narrowed KernelMapNamedFlexibleMemory's accepted flags to the sce namespace and reused bit 0x400 (previously accepted as POSIX MAP_STACK) as MAP_DMEM_COMPAT. That silently breaks any POSIX-style mmap shim written against the old contract (MAP_PRIVATE|MAP_ANON = 0x1002 now returns EINVAL). Worth a mention in the changelog if the narrowing is intentional.

Disclosure (README § AI Use): this fix was developed with AI assistance. I reviewed the analysis and the code, ran the validation on my machine, and take responsibility for this submission.

**@⁠Stepz97** on GitHub · 2026-08-01 15:40 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/148) `Stepz97:fix-protect-guest-memory-host-clamp` → `main` · +11 −1 · 1 files · 1 commits · closed without merging ## Symptom Since the guest memory rework (#135, first bad commit `2f5396c`, found by bisect between `f6e01e5` and `a65d17a` with a validated good/bad oracle), nothing boots on macOS: both homebrew samples and games freeze right after `--- Relocate program: ... ---`, with one thread spinning at 100% CPU and no further output. Before #135 (`f6e01e5`), the same binaries execute guest code normally. This looks related to the Raiden III regression reported around #136 — same first bad commit territory, and #136 (signal dispatch) does not fix this hang. ## Root cause #135 establishes the invariant that guest-visible protection lives in the virtual range tracker while host mappings stay writable, and applies exactly that clamp in `SetProgramMemoryProtection`: ```cpp const auto host_mode = VirtualMemory::IsExecute(mode) ? VirtualMemory::Mode::ExecuteReadWrite : VirtualMemory::Mode::ReadWrite; ``` But `ProtectGuestMemory` still applies the requested guest mode verbatim to the host mapping. The broken sequence on every boot: 1. `InstallRelocateHandler` calls `ProtectGuestMemory(pltgot, Write, &old_mode)`. `old_mode` is read from the range tracker, so for the pltgot (inside a read-only RELRO segment) it comes back as `Read`. 2. After patching `pltgot[1]`/`pltgot[2]`, it restores `old_mode` — and the restore mprotects the host page to read-only. 3. The first relocation write in `RelocateRecords` (`PatchGuestMemory64`, a plain store) faults. The GPU fault handler path finds the page inside a GPU-mapped range, runs an invalidate that is a no-op (no GPU resources exist at boot), returns "handled" **without changing any protection**, and the retried store faults again. Infinite fault livelock. Measured on macOS: 8,000+ identical faults (rip = `RelocateRecords`, fault addr = pltgot+0x18, page-fault error code 0x6 = user-mode write) with the handler returning "resolved" every time. Windows presumably survives because its fault path resolves the write differently, but the defect itself is platform-independent code. ## Fix Clamp the host mode in `ProtectGuestMemory` exactly like `SetProgramMemoryProtection` already does, preserving `NoAccess` so guard pages still trap. The tracker keeps recording the requested guest mode. ## Validation - On pure `a65d17a` + this patch alone: guest code executes again on macOS (unresolved-import stub calls resume, where before the process never left `Relocate`). - On our macOS branch (this patch plus unrelated local work): full test suite passes (15/17, the 2 failures are the pre-existing Linux-only `memory_tracker`/`page_manager`), and the Mandelbrot homebrew sample renders byte-identical to its pre-#135 reference dump. ## Two adjacent notes (no patch attached, maintainer's call) 1. `KernelMprotect` has the same shape (applies the guest mode verbatim to the host mapping). Since that one is the guest-facing syscall, faithfully applying guest protection may be intentional — but any guest that mprotects its own memory read-only and later gets patched by the runtime linker will hit the same livelock. Possibly the actual Raiden III mechanism. 2. #135 narrowed `KernelMapNamedFlexibleMemory`'s accepted flags to the sce namespace and reused bit `0x400` (previously accepted as POSIX `MAP_STACK`) as `MAP_DMEM_COMPAT`. That silently breaks any POSIX-style mmap shim written against the old contract (`MAP_PRIVATE|MAP_ANON` = 0x1002 now returns EINVAL). Worth a mention in the changelog if the narrowing is intentional. --- *Disclosure (README § AI Use): this fix was developed with AI assistance. I reviewed the analysis and the code, ran the validation on my machine, and take responsibility for this submission.*
The guest memory rework keeps guest-visible protection in the virtual
range tracker while host mappings stay writable, and applies that clamp
in SetProgramMemoryProtection - but ProtectGuestMemory still applied the
requested guest mode verbatim to the host mapping. InstallRelocateHandler
captures the pltgot's tracked mode (read-only RELRO segment) and restores
it, leaving the page host-read-only: the first relocation write faulted,
the GPU fault handler claimed resolution without changing any protection,
and the retried store faulted again - an infinite livelock that froze
every macOS boot right after "Relocate program" (first bad upstream
commit 2f5396c, found by bisect with validated controls).

Clamp the host mode exactly like SetProgramMemoryProtection, preserving
NoAccess so guard pages still trap. The tracker keeps recording the
requested guest mode.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:12:24 +00:00
github-bot closed this pull request 2026-11-26 09:12:24 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.