Skip to content

Fix the JIT on x86_64 macOS (#2292): allocate code memory dynamically - #2781

Closed
mhfindik wants to merge 1 commit into
melonDS-emu:masterfrom
mhfindik:macos-x86_64-jit-code-memory
Closed

mhfindik wants to merge 1 commit into
melonDS-emu:masterfrom
mhfindik:macos-x86_64-jit-code-memory

Conversation

@mhfindik

Copy link
Copy Markdown

Fixes #2292. The x86_64 macOS JIT was disabled in 99d2900 because of this crash.

Cause

Since 99ce959 the JIT code memory on x86_64 is the static CodeMemory array, which ends up in __DATA. With current Apple linkers the maximum protection of __DATA is rw- (an x86_64 build of 906e9eb shows maxprot 0x3), so mprotect(PROT_EXEC) in ARMJIT_Global::Init() fails. Its result was not checked, so the first jump into JIT code faults.

Fix

  • Allocate JIT code memory dynamically on all Apple targets. aarch64 already does this. x86_64 now takes the generic anonymous RWX mmap path, which is what melonDS used up to 0.9.5.
  • Re-enable ENABLE_JIT for x86_64 macOS in CMake.
  • Check the mprotect result on the remaining static-memory targets and print an error instead of failing silently.

Testing

On a MacBookAir7,2 (i5-5350U, macOS 12.7.6), the affected class of machine, using a headless harness on the core at 906e9eb with this change:

  • JIT on, software 3D unthreaded: 20,000 frames of an 8-player battle in Bomberman Land Touch! 2 (EU). Battle frames averaged 4.97 ms/frame (max 10.9 ms). The per-frame state hashes were identical to the x64 JIT on Windows.
  • Interpreter: bit-identical per-frame hashes to an arm64 run over 20,000 frames.
  • Control, the old static path on the same Mac: mprotect(PROT_EXEC) ... Permission denied followed by SIGSEGV, which reproduces [JIT, macOS Sonoma (Intel)] melonDS crashes when booting anything if JIT is enabled #2292.
  • Build: the core target builds for x86_64 and arm64 macOS with ENABLE_JIT=ON.

One observation not addressed here: on the same Air, with the JIT on, our harness hung after boot when the software 3D renderer ran threaded. The unthreaded runs above are fine. We have not investigated this, and it may be specific to our harness.

🤖 Generated with Claude Code

The x86_64 macOS JIT was disabled in 99d2900 because of melonDS-emu#2292 (crash when
booting anything with the JIT enabled on Intel Macs).

Cause: since 99ce959 the JIT code memory on x86_64 is the static
CodeMemory array, which ends up in __DATA. With current Apple linkers the
maximum protection of __DATA is rw-, so the mprotect(PROT_EXEC) in Init()
fails (its result was not checked) and the first jump into JIT code
faults.

Fix: allocate JIT code memory dynamically on all Apple targets, like
aarch64 already does and like x86_64 did up to melonDS 0.9.5 (anonymous
RWX mapping). Re-enable ENABLE_JIT on x86_64 macOS, and report a failing
mprotect on the remaining static-memory targets instead of ignoring it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nadiaholmquist

Copy link
Copy Markdown
Collaborator

Fixed by 7d8dea4

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.

[JIT, macOS Sonoma (Intel)] melonDS crashes when booting anything if JIT is enabled

2 participants