x64_recomp: the QEMU harness maps guest RAM as Normal memory, so unaligned accesses work (X64-RECOMP-QEMU-UNALIGNED) #2143
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2143"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 thetools/x64_recomp.pytest harness hangs until the 15 s timeout. This is onmain, with QEMU 11.1.3:-d intshows why: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_mmuwrites 1 GiB block entries for the image and the RW window into one level-1 table page atKERNEL_RW + 0x10000(between the guest stack at+0x8000and the tests' scratch at+0x100000), setsMAIR_EL1,TCR_EL1(39-bit VA, 4 KiB granule) andTTBR0_EL1, then setsSCTLR_EL1M, 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_movsbends inmov 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.ExecTests.test_unaligned_memory_access: 2-, 4- and 8-byte unaligned loads and stores, seven cases. Withmain'stools/x64_recomp.pyall 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 onmain, so the existing exec tests are unaffected by the MMU being on).python -I tests/test_recomp_census.py: 11/11.tests/test_*.pyunderpython -I: 511 run, 507 pass; the 4 that fail are the same 4 that fail onmain.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-aarch64on 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
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no shader translator change)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_kernelonmain, a guestret(or any otherrt_dispatch) hangs whenever the pc table lands on an address that is 4 mod 8.build_test_kernelplaces 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_dispatchreads 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
mainata73a57e13:test_call_and_retfromtests/test_x64_recomp.py0x400801a0(0 mod 8)R=7call helper; mov edi,1; ret; helper: mov eax,7; ret0x4008019c(4 mod 8)So the existing call test passes on
mainonly because its translation has the right length. Eight small call/ret variants I tried (sub rsp/add rsparound the call,push/poparound it, and so on) all time out onmainand 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.py35/35. The same test againstmain's tool: 4 of 4 subtests time out.This is why exec tests with a guest call or
ret imm16in other open PRs fail under QEMU for some inputs and not others (for exampleret 16in #1321 hangs whileret 0x1000passes; with this branch merged in, that file is 9/9). No game files used. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T22:29:13Z.
Native Windows 11 Verification (
2aeacc34f3)Verified
2aeacc34f3dd25ebe4dd577310f16473159b4aa2merged cleanly ontoorigin/main(adfb3821a23612263c1c5b195453ed01c4c114db,git merge-tree --write-treerc=0) on native Windows 11 (26200):origin/mainadfb3821a): Inspectedx64_recomp.build_test_kernelboot stub (428bytes): 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.origin/main + 2aeacc34f3):build_test_kernel(492bytes) emits the 1 GiB identity block descriptors atrw_base + 0x10000plusmair_el1(0xD518A201),tcr_el1(0xD5182041),ttbr0_el1(0xD5182000),sctlr_el1(0xD5181001,M|C|Iset,Aclear), andisbbarriers;python -I tests/test_x64_recomp.pypassed (47 ran, OK (skipped=34)on x86_64 Windows withoutqemu-system-aarch64).AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-28T23:38:14Z.
Tested
2aeacc34fmerged (no commit) withmainaa918e5d9: Python 3.12.4, Windows 11 10.0.26300, CPU only.check_no_game_data.pyis clean.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).
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 head2aeacc34f: red 11 errors / green 47 OKpython3.12 -I tests/test_x64_recomp.py: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)git merge --no-ff): 47 tests OK in 1.0 s.git merge-tree).Verdict: READY
AI-assisted: Claude Code
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
main25cf1aa4ewithout conflict, and its test file(s) (tests/test_x64_recomp.py) pass there. Earlier today the same tests failed onmainwithout the PR (red/green) and a bundle with other open PRs showed no new failures.check_no_game_data.pyis clean. Windows 11, Python 3.14; no GPU needed for these tests.AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.