fix: keep hint-less guest mappings at the canonical PS5 base (fixes the #135 macOS regression) #138

Manually merged
github merged 3 commits from gh-pr/138/head into gh-pr/138/base 2026-11-26 09:11:59 +00:00
Owner

@⁠m1abdullahh on GitHub · 2026-07-31 04:15 UTC · original
m1abdullahh:fix-135-macos → main · +166 −5 · 3 files · 3 commits · merged 2026-08-02 00:23 UTC

This is the root cause and fix for the Raiden III regression I reported in #136 after #135 landed.

What broke

FindGuestFreeRange searches the low system-managed range first for mappings with no address hint, so the first hint-less direct-memory map could land as low as 0x200000. The PS5 kernel never places hint-less user mappings below 0x200000000, and guest code relies on that. Sony's libc.prx maps 4 MiB of direct memory for its internal heap during module init, fails its mspace setup when the returned address is that low, and leaves its default mspace null. The first malloc then does cmp [rdi+0x38], magic with rdi = 0, which is the read at 0x38 from my report. On macOS this killed Raiden III on the main guest thread about 2 seconds after boot, 100 percent reproducible with --printf-direction Silent.

The fix anchors hint-less searches at 0x200000000 (the same base the mmap path already uses), falls back to the user range, and does not fall through to HOST_SYSTEM_MANAGED_MIN. This is shared code, so it applies to all platforms. I believe Windows and Linux have the same latent hazard and just have not been observed hitting it yet. Two regression tests are included and run in the existing virtual_memory_allocation suite on Windows and macOS CI: the libc-shaped allocation must come back at or above the canonical base and hold writes, and direct-memory content must survive an unmap and remap of the same physical range.

Why it looked like a timing bug

The crash actually fired on every run in every log mode. What differed was the reporting. On macOS, IsReadableRange accepted any nonzero address, so the fatal report's guest memory dumps dereferenced whatever the crashed thread had in its registers (rbx was 0x30). The reporter faulted, re-entered the signal handler, and the reporting thread wedged in a fault loop while the rest of the game kept running. So with Console or File logging the game appeared healthy, and only Silent mode (which skips the dumps) ever reported the crash. The second commit makes the macOS reporter walk the Mach regions and require read permission before dumping, matching what the Linux implementation already does, so a real crash can never be swallowed like this again.

Tested

On an M3 Pro MacBook Pro, on top of current main:

  • Raiden III: was 6 out of 6 crashes at about 5 seconds with Silent logging, now 3 out of 3 clean runs, plays normally at 60 fps
  • Metal Slug Tactics: unaffected, still reaches menus and gameplay (its remaining issue is the separate Rosetta AVX EmulateForward abort, unrelated to this)
  • virtual_memory_allocation tests: all pass, including the two new cases
**@⁠m1abdullahh** on GitHub · 2026-07-31 04:15 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/138) `m1abdullahh:fix-135-macos` → `main` · +166 −5 · 3 files · 3 commits · merged 2026-08-02 00:23 UTC This is the root cause and fix for the Raiden III regression I reported in #136 after #135 landed. ## What broke FindGuestFreeRange searches the low system-managed range first for mappings with no address hint, so the first hint-less direct-memory map could land as low as 0x200000. The PS5 kernel never places hint-less user mappings below 0x200000000, and guest code relies on that. Sony's libc.prx maps 4 MiB of direct memory for its internal heap during module init, fails its mspace setup when the returned address is that low, and leaves its default mspace null. The first malloc then does `cmp [rdi+0x38], magic` with rdi = 0, which is the read at 0x38 from my report. On macOS this killed Raiden III on the main guest thread about 2 seconds after boot, 100 percent reproducible with --printf-direction Silent. The fix anchors hint-less searches at 0x200000000 (the same base the mmap path already uses), falls back to the user range, and does not fall through to HOST_SYSTEM_MANAGED_MIN. This is shared code, so it applies to all platforms. I believe Windows and Linux have the same latent hazard and just have not been observed hitting it yet. Two regression tests are included and run in the existing virtual_memory_allocation suite on Windows and macOS CI: the libc-shaped allocation must come back at or above the canonical base and hold writes, and direct-memory content must survive an unmap and remap of the same physical range. ## Why it looked like a timing bug The crash actually fired on every run in every log mode. What differed was the reporting. On macOS, IsReadableRange accepted any nonzero address, so the fatal report's guest memory dumps dereferenced whatever the crashed thread had in its registers (rbx was 0x30). The reporter faulted, re-entered the signal handler, and the reporting thread wedged in a fault loop while the rest of the game kept running. So with Console or File logging the game appeared healthy, and only Silent mode (which skips the dumps) ever reported the crash. The second commit makes the macOS reporter walk the Mach regions and require read permission before dumping, matching what the Linux implementation already does, so a real crash can never be swallowed like this again. ## Tested On an M3 Pro MacBook Pro, on top of current main: - Raiden III: was 6 out of 6 crashes at about 5 seconds with Silent logging, now 3 out of 3 clean runs, plays normally at 60 fps - Metal Slug Tactics: unaffected, still reaches menus and gameplay (its remaining issue is the separate Rosetta AVX EmulateForward abort, unrelated to this) - virtual_memory_allocation tests: all pass, including the two new cases
FindGuestFreeRange searched the low system-managed range first for
mappings with no address hint, so the first hint-less direct-memory map
could land as low as 0x200000. The PS5 kernel never places hint-less
user mappings below 0x200000000 and guest code relies on that: Sony's
libc maps 4 MiB of direct memory for its internal heap, fails its
mspace setup when the returned address is that low, and the first
malloc then dereferences a null mspace (a read at 0x38, the mspace
magic check). On macOS this made Raiden III crash on the main guest
thread a couple of seconds after boot, 100 percent reproducible with
--printf-direction Silent.

Search from the canonical base first, fall back to the user range, and
keep the low system-managed range only as a last resort. The mmap path
already anchored hint-less searches at 0x200000000; this aligns the
shared search helper with it.

Adds two regression tests: the libc-shaped allocation must come back at
or above the canonical base and hold writes, and direct-memory content
must survive an unmap and remap of the same physical range.
IsReadableRange returned true for any nonzero address on macOS, so the
fatal report's guest memory dumps dereferenced whatever the crashed
thread had in its registers. A fault inside the reporter re-enters the
signal handler and wedges the reporting thread, which hid real guest
crashes whenever logging was enabled: the game kept running with a dead
thread and the report was never completed.

Walk the Mach regions covering the range and require read permission
before dumping, the same contract the Linux implementation provides.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:11:58 +00:00
github manually merged commit 454548f29c into gh-pr/138/base 2026-11-26 09:11:59 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.