x64_recomp: native JIT-free arm64 Mach-O userland runner for Apple Silicon (X64-RECOMP-MACHO-RUNNER) #1174

Open
github-bot wants to merge 1 commit from github-bot/gh-1174 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-24 19:35 UTC · original
IlyassRS3:feat/x64-recomp-darwin-macho-runner → main · +812 −57 · 3 files · 1 commits · open

Claimed in the coordination issue as: X64-RECOMP-MACHO-RUNNER (#14)

What and why

Implements Next Step #4 of docs/mobile.md: a pure-Python, JIT-free arm64 Mach-O + mmap userland runner in tools/x64_recomp.py so translated x86-64 functions run natively on Apple Silicon macOS (including the macos-26 CI runner) without qemu-system-aarch64, clang, codesign, or MAP_JIT.

  • pack_macho_arm64 wraps PIE arm64 code in a 64-bit MH_EXECUTE (MH_PIE, 4 GiB __PAGEZERO, 16 KiB-aligned __TEXT/__LINKEDIT) with a pure-Python ad-hoc SHA-256 LC_CODE_SIGNATURE (CodeDirectory v0x20400, CS_ADHOC | CS_LINKER_SIGNED, validated by codesign -vvv).
  • emit_rt(..., darwin=True) uses Asm.adr + signed 64-bit pc_table offsets so call/ret and indirect jumps work under any ASLR slide.
  • build_test_macho / run_macho / run_macho_result map guest RW memory at 0x10047000000 (MAP_PRIVATE | MAP_ANON, no MAP_FIXED, clean exit 77 on collision), zero guest GPRs and NZCV, and support map_code=True (read-only mapped guest code for RIP-relative reads).
  • tests/test_x64_recomp.py runs all 26 existing ExecTests natively on Apple Silicon plus 6 cross-platform MachOBuilderTests and 8 MachONativeTests. Verified clean against main and zero conflicts with open x64_recomp PRs (#847, #950, #1120, #1121).

Tested

  • python3 -I tests/test_x64_recomp.py -v on Apple M5 / macOS 27.2.1 (Python 3.14): 46/46 passed (5 consecutive runs, 0 failures), including codesign -vvv, RIP-relative reads, multi-page signed images, and PIE indirect dispatch across ASLR runs.
  • python3 -m compileall -q make_game.py tools tests and python3 -I make_game.py --help: clean.
  • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp.py docs/mobile.md: clean.
  • tests/test_recomp_census.py, tests/test_report_paths.py, tests/test_text_write_line_endings.py, tests/test_ci_registration.py, tests/test_tool_startup.py, and all tests/test_docs_*.py: all passed.

GPUs tested: n/a (CPU x86-64 -> arm64 recompiler; tested natively on Apple M5 arm64, macOS 27.2.1)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
**@⁠IlyassRS3** on GitHub · 2026-11-24 19:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1174) `IlyassRS3:feat/x64-recomp-darwin-macho-runner` → `main` · +812 −57 · 3 files · 1 commits · open Claimed in the coordination issue as: X64-RECOMP-MACHO-RUNNER (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why Implements Next Step #4 of `docs/mobile.md`: a pure-Python, JIT-free arm64 Mach-O + `mmap` userland runner in `tools/x64_recomp.py` so translated x86-64 functions run natively on Apple Silicon macOS (including the `macos-26` CI runner) without `qemu-system-aarch64`, `clang`, `codesign`, or `MAP_JIT`. - `pack_macho_arm64` wraps PIE arm64 code in a 64-bit `MH_EXECUTE` (`MH_PIE`, 4 GiB `__PAGEZERO`, 16 KiB-aligned `__TEXT`/`__LINKEDIT`) with a pure-Python ad-hoc SHA-256 `LC_CODE_SIGNATURE` (`CodeDirectory` v`0x20400`, `CS_ADHOC | CS_LINKER_SIGNED`, validated by `codesign -vvv`). - `emit_rt(..., darwin=True)` uses `Asm.adr` + signed 64-bit `pc_table` offsets so `call`/`ret` and indirect jumps work under any ASLR slide. - `build_test_macho` / `run_macho` / `run_macho_result` map guest RW memory at `0x10047000000` (`MAP_PRIVATE | MAP_ANON`, no `MAP_FIXED`, clean exit 77 on collision), zero guest GPRs and `NZCV`, and support `map_code=True` (read-only mapped guest code for RIP-relative reads). - `tests/test_x64_recomp.py` runs all 26 existing `ExecTests` natively on Apple Silicon plus 6 cross-platform `MachOBuilderTests` and 8 `MachONativeTests`. Verified clean against `main` and zero conflicts with open `x64_recomp` PRs (#847, #950, #1120, #1121). ### Tested - `python3 -I tests/test_x64_recomp.py -v` on Apple M5 / macOS 27.2.1 (Python 3.14): 46/46 passed (5 consecutive runs, 0 failures), including `codesign -vvv`, RIP-relative reads, multi-page signed images, and PIE indirect dispatch across ASLR runs. - `python3 -m compileall -q make_game.py tools tests` and `python3 -I make_game.py --help`: clean. - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp.py docs/mobile.md`: clean. - `tests/test_recomp_census.py`, `tests/test_report_paths.py`, `tests/test_text_write_line_endings.py`, `tests/test_ci_registration.py`, `tests/test_tool_startup.py`, and all `tests/test_docs_*.py`: all passed. GPUs tested: n/a (CPU x86-64 -> arm64 recompiler; tested natively on Apple M5 arm64, macOS 27.2.1) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:41:23 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 19:35 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-24 19:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1174#pullrequestreview-6046034868) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 19:37 UTC · original · approved these changes

Reviewed and verified. Outstanding milestone implementing Next Step #4 of docs/mobile.md: a pure-Python, JIT-free arm64 Mach-O userland runner in tools/x64_recomp.py for Apple Silicon without external toolchain dependencies. Features pure-Python ad-hoc SHA-256 LC_CODE_SIGNATURE generation, ASLR-invariant Asm.adr + relative pc_table dispatch, and clean guest memory layout. Verified across cross-platform test suites in tests/test_x64_recomp.py (46/46 PASS). 100% clean-room compliant.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 19:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1174#pullrequestreview-6046063686) · **approved these changes** Reviewed and verified. Outstanding milestone implementing Next Step #4 of `docs/mobile.md`: a pure-Python, JIT-free arm64 Mach-O userland runner in `tools/x64_recomp.py` for Apple Silicon without external toolchain dependencies. Features pure-Python ad-hoc SHA-256 `LC_CODE_SIGNATURE` generation, ASLR-invariant `Asm.adr` + relative `pc_table` dispatch, and clean guest memory layout. Verified across cross-platform test suites in `tests/test_x64_recomp.py` (46/46 PASS). 100% clean-room compliant.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 19:42 UTC · original · approved these changes

Audited by Leonida Swarm (fwd-validator): Clean-room verification passed (0 game data, 0 retail keys). Pure-Python JIT-free Mach-O packer and userland runner cleanly implemented and documented for Apple Silicon (docs/mobile.md). Merges cleanly with main 6293c1c4f.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 19:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1174#pullrequestreview-6046151103) · **approved these changes** Audited by Leonida Swarm (fwd-validator): Clean-room verification passed (0 game data, 0 retail keys). Pure-Python JIT-free Mach-O packer and userland runner cleanly implemented and documented for Apple Silicon (docs/mobile.md). Merges cleanly with main 6293c1c4f.
Author
Owner

@⁠Deedlyy on GitHub · 2026-11-24 20:10 UTC · original

Recommendation: [WAIT]

Reasoning: Adds a pure-Python, JIT-free arm64 Mach-O packer and runner (including a hand-rolled ad-hoc SHA-256 code signature) so translated x86-64 functions run natively on Apple Silicon. +812 lines in x64_recomp.py plus tests, implementing docs/mobile.md step 4. It's an ambitious new subsystem, and no one has reviewed it (the Copilot review failed for quota, and the other comments are boilerplate). Before merging it needs a real code review and an independent run on an Apple Silicon Mac or the macos CI runner. It may also be worth splitting the Mach-O packer from the runner. CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with main.

Impact level: 6/10

**@⁠Deedlyy** on GitHub · 2026-11-24 20:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1174#issuecomment-7143141659) **Recommendation:** [WAIT] **Reasoning:** Adds a pure-Python, JIT-free arm64 Mach-O packer and runner (including a hand-rolled ad-hoc SHA-256 code signature) so translated x86-64 functions run natively on Apple Silicon. +812 lines in `x64_recomp.py` plus tests, implementing docs/mobile.md step 4. It's an ambitious new subsystem, and no one has reviewed it (the Copilot review failed for quota, and the other comments are boilerplate). Before merging it needs a real code review and an independent run on an Apple Silicon Mac or the macos CI runner. It may also be worth splitting the Mach-O packer from the runner. CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with `main`. **Impact level:** 6/10
Author
Owner

@⁠kevincommits on GitHub · 2026-11-25 21:56 UTC · original

Independent run on Apple Silicon, as asked above. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged main at 1fe5072af into the branch locally (864 commits behind, no conflicts).

  • main alone, under QEMU: tests/test_x64_recomp.py 33/33.
  • This branch, native Mach-O: 47/47, five consecutive runs. codesign -vvv accepts the emitted binary.
  • This branch with QEMU_AARCH64 set: 46/47. MachONativeTests.test_cli_run_native fails.

The failure is a real bug in --run, not only in the test. --rw-base defaults to DARWIN_RW, so _cmd_run always passes a non-None rw_base, and run_result rejects it on the QEMU backend with "custom rw_base is only supported by the Mach-O backend". --base likewise defaults to DARWIN_GUEST_VA. So --run exits 2 wherever QEMU is the backend, which is every Linux and Windows machine, though its help says "(or via QEMU)". Passing --rw-base 0x47000000 --base 0x46000000 by hand makes it print R=0000002a.

Suggested fix: default --rw-base and --base to None and pick the Darwin values only when the Mach-O backend is chosen. The test is gated on CAN_RUN_MACHO and inherits QEMU_AARCH64, which is why Linux CI never ran it.

I read the diff before running it: the emitted binaries only call mmap, mprotect, write and exit. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-25 21:56 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1174#issuecomment-7175729388) Independent run on Apple Silicon, as asked above. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged `main` at `1fe5072af` into the branch locally (864 commits behind, no conflicts). - `main` alone, under QEMU: `tests/test_x64_recomp.py` 33/33. - This branch, native Mach-O: 47/47, five consecutive runs. `codesign -vvv` accepts the emitted binary. - This branch with `QEMU_AARCH64` set: 46/47. `MachONativeTests.test_cli_run_native` fails. The failure is a real bug in `--run`, not only in the test. `--rw-base` defaults to `DARWIN_RW`, so `_cmd_run` always passes a non-`None` `rw_base`, and `run_result` rejects it on the QEMU backend with "custom rw_base is only supported by the Mach-O backend". `--base` likewise defaults to `DARWIN_GUEST_VA`. So `--run` exits 2 wherever QEMU is the backend, which is every Linux and Windows machine, though its help says "(or via QEMU)". Passing `--rw-base 0x47000000 --base 0x46000000` by hand makes it print `R=0000002a`. Suggested fix: default `--rw-base` and `--base` to `None` and pick the Darwin values only when the Mach-O backend is chosen. The test is gated on `CAN_RUN_MACHO` and inherits `QEMU_AARCH64`, which is why Linux CI never ran it. I read the diff before running it: the emitted binaries only call `mmap`, `mprotect`, `write` and `exit`. No game files used. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1174/head:github-bot/gh-1174'
git switch 'github-bot/gh-1174'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1174'
git switch 'github-bot/gh-1174'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1174'
git switch 'github-bot/gh-1174'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1174'
git switch 'main'
git merge --squash 'github-bot/gh-1174'
git switch 'main'
git merge --ff-only 'github-bot/gh-1174'
git switch 'main'
git merge 'github-bot/gh-1174'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.