x64_recomp: the QEMU harness maps guest RAM as Normal memory, so unaligned accesses work (X64-RECOMP-QEMU-UNALIGNED) #2143

Merged
kevincommits merged 2 commits from upstream-pr/2143 into main 2026-11-29 02:16:41 +00:00
kevincommits commented 2026-11-25 23:05:36 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-25T23:05:36Z.


Claimed in the coordination issue as: X64-RECOMP-QEMU-UNALIGNED (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7177136024)

What and why

Under qemu-system-aarch64, every unaligned guest memory access in the tools/x64_recomp.py test harness hangs until the 15 s timeout. This is on main, with QEMU 11.1.3:

mov rbx, 0x47100000
mov edi, [rbx+2]      ; any offset that is not a multiple of the access size
ret

-d int shows why:

Taking exception 4 [Data Abort] on CPU 0
...with ESR 0x25/0x96000021        (DFSC 0x21: alignment fault)
...with FAR 0x47100002
...to EL1 PC 0x200
Taking exception 1 [Undefined Instruction] on CPU 0   (repeats: there is no vector table)

build_test_kernel's boot stub never turns the MMU on. With the MMU off every data access is Device memory, and an unaligned access to Device memory is an alignment fault. x86 code reads and writes unaligned everywhere, so the guest needs Normal memory.

The fix is in the boot stub only: _emit_identity_mmu writes 1 GiB block entries for the image and the RW window into one level-1 table page at KERNEL_RW + 0x10000 (between the guest stack at +0x8000 and the tests' scratch at +0x100000), sets MAIR_EL1, TCR_EL1 (39-bit VA, 4 KiB granule) and TTBR0_EL1, then sets SCTLR_EL1 M, C and I with A clear. The map is an identity map, so no address the translator or the tests use changes. The Linux ELF path and the translator itself are untouched.

I found this while running #1213, whose test_std_overlapping_backward_rep_movsb ends in mov edi, [rbx+2] and times out for this reason.

Tested

Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew, at 8564bb48b.

  • New ExecTests.test_unaligned_memory_access: 2-, 4- and 8-byte unaligned loads and stores, seven cases. With main's tools/x64_recomp.py all seven time out; with this change all seven return the expected value.
  • python -I tests/test_x64_recomp.py: 34/34 under QEMU (33/33 on main, so the existing exec tests are unaffected by the MMU being on). python -I tests/test_recomp_census.py: 11/11.
  • Every standalone tests/test_*.py under python -I: 511 run, 507 pass; the 4 that fail are the same 4 that fail on main.

Not tested: any QEMU older than 11.1.3 (I expect the MMU setup to be valid there too, but did not run it), qemu-system-aarch64 on Linux or Windows hosts, and real arm64 hardware. The table relies on QEMU zeroing RAM at reset.

GPUs tested: n/a

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. (n/a: no shader translator change)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5500708413 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2143). Originally posted by [kevincommits](https://github.com/kevincommits) on 2026-11-25T23:05:36Z. --- Claimed in the coordination issue as: X64-RECOMP-QEMU-UNALIGNED (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7177136024) ### What and why Under `qemu-system-aarch64`, every unaligned guest memory access in the `tools/x64_recomp.py` test harness hangs until the 15 s timeout. This is on `main`, with QEMU 11.1.3: ``` mov rbx, 0x47100000 mov edi, [rbx+2] ; any offset that is not a multiple of the access size ret ``` `-d int` shows why: ``` Taking exception 4 [Data Abort] on CPU 0 ...with ESR 0x25/0x96000021 (DFSC 0x21: alignment fault) ...with FAR 0x47100002 ...to EL1 PC 0x200 Taking exception 1 [Undefined Instruction] on CPU 0 (repeats: there is no vector table) ``` `build_test_kernel`'s boot stub never turns the MMU on. With the MMU off every data access is Device memory, and an unaligned access to Device memory is an alignment fault. x86 code reads and writes unaligned everywhere, so the guest needs Normal memory. The fix is in the boot stub only: `_emit_identity_mmu` writes 1 GiB block entries for the image and the RW window into one level-1 table page at `KERNEL_RW + 0x10000` (between the guest stack at `+0x8000` and the tests' scratch at `+0x100000`), sets `MAIR_EL1`, `TCR_EL1` (39-bit VA, 4 KiB granule) and `TTBR0_EL1`, then sets `SCTLR_EL1` M, C and I with A clear. The map is an identity map, so no address the translator or the tests use changes. The Linux ELF path and the translator itself are untouched. I found this while running #1213, whose `test_std_overlapping_backward_rep_movsb` ends in `mov edi, [rbx+2]` and times out for this reason. ### Tested Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew, at `8564bb48b`. - New `ExecTests.test_unaligned_memory_access`: 2-, 4- and 8-byte unaligned loads and stores, seven cases. With `main`'s `tools/x64_recomp.py` all seven time out; with this change all seven return the expected value. - `python -I tests/test_x64_recomp.py`: 34/34 under QEMU (33/33 on `main`, so the existing exec tests are unaffected by the MMU being on). `python -I tests/test_recomp_census.py`: 11/11. - Every standalone `tests/test_*.py` under `python -I`: 511 run, 507 pass; the 4 that fail are the same 4 that fail on `main`. Not tested: any QEMU older than 11.1.3 (I expect the MMU setup to be valid there too, but did not run it), `qemu-system-aarch64` on Linux or Windows hosts, and real arm64 hardware. The table relies on QEMU zeroing RAM at reset. GPUs tested: n/a 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. (n/a: no shader translator change)
kevincommits commented 2026-11-26 03:49:43 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-26T03:49:43Z.


A second symptom of the same harness bug, found while reviewing #1321, and it is more common than the one in the title: under run_kernel on main, a guest ret (or any other rt_dispatch) hangs whenever the pc table lands on an address that is 4 mod 8.

  • build_test_kernel places the pc table right after the translated code and the runtime, so its address depends on how many arm64 words the test happened to translate to. rt_dispatch reads it with 8-byte loads (ldr_post(0, LINK, 16)). With the MMU off those are Device-memory accesses, an 8-byte load at 4 mod 8 takes an alignment fault, and the run sits there until the timeout.

  • Measured on main at a73a57e13:

    guest code pc table at result
    test_call_and_ret from tests/test_x64_recomp.py 0x400801a0 (0 mod 8) R=7
    call helper; mov edi,1; ret; helper: mov eax,7; ret 0x4008019c (4 mod 8) timeout
  • So the existing call test passes on main only because its translation has the right length. Eight small call/ret variants I tried (sub rsp/add rsp around the call, push/pop around it, and so on) all time out on main and all pass on this branch, since with the MMU on the load is an ordinary Normal-memory access.

  • Pushed c6029b869: ExecTests.test_call_and_ret_at_any_code_length (4 subtests). With this branch's tool: tests/test_x64_recomp.py 35/35. The same test against main's tool: 4 of 4 subtests time out.

This is why exec tests with a guest call or ret imm16 in other open PRs fail under QEMU for some inputs and not others (for example ret 16 in #1321 hangs while ret 0x1000 passes; with this branch merged in, that file is 9/9). No game files used. AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7182058365 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2143#issuecomment-7182058365). Originally posted by [kevincommits](https://github.com/kevincommits) on 2026-11-26T03:49:43Z. --- A second symptom of the same harness bug, found while reviewing #1321, and it is more common than the one in the title: **under `run_kernel` on `main`, a guest `ret` (or any other `rt_dispatch`) hangs whenever the pc table lands on an address that is 4 mod 8.** - `build_test_kernel` places the pc table right after the translated code and the runtime, so its address depends on how many arm64 words the test happened to translate to. `rt_dispatch` reads it with 8-byte loads (`ldr_post(0, LINK, 16)`). With the MMU off those are Device-memory accesses, an 8-byte load at 4 mod 8 takes an alignment fault, and the run sits there until the timeout. - Measured on `main` at `a73a57e13`: | guest code | pc table at | result | |---|---|---| | `test_call_and_ret` from `tests/test_x64_recomp.py` | `0x400801a0` (0 mod 8) | `R=7` | | `call helper; mov edi,1; ret; helper: mov eax,7; ret` | `0x4008019c` (4 mod 8) | timeout | - So the existing call test passes on `main` only because its translation has the right length. Eight small call/ret variants I tried (`sub rsp`/`add rsp` around the call, `push`/`pop` around it, and so on) all time out on `main` and all pass on this branch, since with the MMU on the load is an ordinary Normal-memory access. - Pushed `c6029b869`: `ExecTests.test_call_and_ret_at_any_code_length` (4 subtests). With this branch's tool: `tests/test_x64_recomp.py` 35/35. The same test against `main`'s tool: 4 of 4 subtests time out. This is why exec tests with a guest call or `ret imm16` in other open PRs fail under QEMU for some inputs and not others (for example `ret 16` in #1321 hangs while `ret 0x1000` passes; with this branch merged in, that file is 9/9). No game files used. AI-assisted.
zmasi commented 2026-11-28 22:29:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T22:29:13Z.


Native Windows 11 Verification (2aeacc34f3)

Verified 2aeacc34f3dd25ebe4dd577310f16473159b4aa2 merged cleanly onto origin/main (adfb3821a23612263c1c5b195453ed01c4c114db, git merge-tree --write-tree rc=0) on native Windows 11 (26200):

  • Red (origin/main adfb3821a): Inspected x64_recomp.build_test_kernel boot stub (428 bytes): none of the EL1 MMU system-register writes (msr mair_el1, msr tcr_el1, msr ttbr0_el1, msr sctlr_el1) were emitted, leaving guest RAM mapped as Device memory with alignment faults on unaligned accesses.
  • Green (origin/main + 2aeacc34f3): build_test_kernel (492 bytes) emits the 1 GiB identity block descriptors at rw_base + 0x10000 plus mair_el1 (0xD518A201), tcr_el1 (0xD5182041), ttbr0_el1 (0xD5182000), sctlr_el1 (0xD5181001, M|C|I set, A clear), and isb barriers; python -I tests/test_x64_recomp.py passed (47 ran, OK (skipped=34) on x86_64 Windows without qemu-system-aarch64).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246768510 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2143#issuecomment-7246768510). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T22:29:13Z. --- ### Native Windows 11 Verification (`2aeacc34f3`) Verified `2aeacc34f3dd25ebe4dd577310f16473159b4aa2` merged cleanly onto `origin/main` (`adfb3821a23612263c1c5b195453ed01c4c114db`, `git merge-tree --write-tree` `rc=0`) on native **Windows 11 (`26200`)**: - **Red (`origin/main` `adfb3821a`)**: Inspected `x64_recomp.build_test_kernel` boot stub (`428` bytes): none of the EL1 MMU system-register writes (`msr mair_el1`, `msr tcr_el1`, `msr ttbr0_el1`, `msr sctlr_el1`) were emitted, leaving guest RAM mapped as Device memory with alignment faults on unaligned accesses. - **Green (`origin/main + 2aeacc34f3`)**: `build_test_kernel` (`492` bytes) emits the 1 GiB identity block descriptors at `rw_base + 0x10000` plus `mair_el1` (`0xD518A201`), `tcr_el1` (`0xD5182041`), `ttbr0_el1` (`0xD5182000`), `sctlr_el1` (`0xD5181001`, `M|C|I` set, `A` clear), and `isb` barriers; `python -I tests/test_x64_recomp.py` passed (`47 ran, OK (skipped=34)` on x86_64 Windows without `qemu-system-aarch64`). AI-assisted.
TheGeogeo commented 2026-11-28 23:38:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-28T23:38:14Z.


Tested 2aeacc34f merged (no commit) with main aa918e5d9: Python 3.12.4, Windows 11 10.0.26300, CPU only. check_no_game_data.py is clean.

  • Not testable here: the new QEMU checks skip (34 of 47 tests skipped, qemu-system-aarch64 not installed). The other 13 tests pass with and without the fix, so green and red can't be observed on this machine.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7247563187 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2143#issuecomment-7247563187). Originally posted by [TheGeogeo](https://github.com/TheGeogeo) on 2026-11-28T23:38:14Z. --- Tested `2aeacc34f` merged (no commit) with `main` `aa918e5d9`: Python 3.12.4, Windows 11 10.0.26300, CPU only. `check_no_game_data.py` is clean. - Not testable here: the new QEMU checks skip (34 of 47 tests skipped, `qemu-system-aarch64 not installed`). The other 13 tests pass with and without the fix, so green and red can't be observed on this machine. AI-assisted (Claude Code).
OreoFR commented 2026-11-29 02:03:02 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T02:03:02Z.


Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, qemu-system-aarch64 11.1.3 (Homebrew), main 59e65849b, PR head 2aeacc34f : red 11 errors / green 47 OK

python3.12 -I tests/test_x64_recomp.py:

  • The PR's test file is older than main's (main 45 tests, PR 46, merge 47). Red below uses the merged test file against main's code; the raw red (PR test file) gives the same 11 errors on 46 tests.
  • Red: 47 tests, 11 errors, all in the 2 tests added by this PR, all subprocess.TimeoutExpired (qemu-system-aarch64 exceeds 15 s: the guest code generated by main does not terminate):
    • ExecTests.test_unaligned_memory_access (7 subtests)
    • ExecTests.test_call_and_ret_at_any_code_length (4 subtests, pad 0 to 3)
  • Green (main + git merge --no-ff): 47 tests OK in 1.0 s.
  • (MEASURED n=3) both tests rerun alone 3 times: red 3/3 fail (same subtests), green 3/3 pass.
  • Skipped: none (qemu present, the exec tests run).
  • Merge: clean (git merge-tree).

Verdict: READY

AI-assisted: Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7249177981 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2143#issuecomment-7249177981). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T02:03:02Z. --- Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, qemu-system-aarch64 11.1.3 (Homebrew), main 59e65849b, PR head 2aeacc34f : red 11 errors / green 47 OK `python3.12 -I tests/test_x64_recomp.py`: - The PR's test file is older than main's (main 45 tests, PR 46, merge 47). Red below uses the merged test file against main's code; the raw red (PR test file) gives the same 11 errors on 46 tests. - **Red**: 47 tests, 11 errors, all in the 2 tests added by this PR, all `subprocess.TimeoutExpired` (qemu-system-aarch64 exceeds 15 s: the guest code generated by main does not terminate): - `ExecTests.test_unaligned_memory_access` (7 subtests) - `ExecTests.test_call_and_ret_at_any_code_length` (4 subtests, pad 0 to 3) - **Green** (main + `git merge --no-ff`): 47 tests OK in 1.0 s. - (MEASURED n=3) both tests rerun alone 3 times: red 3/3 fail (same subtests), green 3/3 pass. - Skipped: none (qemu present, the exec tests run). - Merge: clean (`git merge-tree`). **Verdict: READY** AI-assisted: Claude Code
MoonEyes commented 2026-11-29 02:16:43 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T02:16:43Z.


Merged after OreoFR's red/green check (check) (macOS 27.2.1, Apple M5) and a local re-check here. Merged after local re-verification: this PR merged onto main 25cf1aa4e without conflict, and its test file(s) (tests/test_x64_recomp.py) pass there. Earlier today the same tests failed on main without the PR (red/green) and a bundle with other open PRs showed no new failures. check_no_game_data.py is clean. Windows 11, Python 3.14; no GPU needed for these tests.

AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7249324973 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2143#issuecomment-7249324973). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-29T02:16:43Z. --- Merged after **OreoFR**'s red/green check ([check](https://github.com/ProjectLeonida/mandy/pull/2143#issuecomment-7249177981)) (macOS 27.2.1, Apple M5) and a local re-check here. Merged after local re-verification: this PR merged onto `main` 25cf1aa4e without conflict, and its test file(s) (`tests/test_x64_recomp.py`) pass there. Earlier today the same tests failed on `main` without the PR (red/green) and a bundle with other open PRs showed no new failures. `check_no_game_data.py` is clean. Windows 11, Python 3.14; no GPU needed for these tests. _AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes._
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2143
No description provided.