x64_recomp: preserve materialized x86 flags in PUSHF #2899

Closed
leonida/mandy
Maticcm wants to merge 0 commits from fix/x64-pushf-materialized-flags into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

Maticcm commented 2026-12-01 16:41:10 +00:00

Claimed in the coordination issue as X64-RECOMP-PUSHF-MATERIALIZED-FLAGS.

What and why

PUSHF unconditionally rebuilt guest EFLAGS from the current AArch64 flags using addition carry polarity. That could replace a correct x86 flag word after a comparison or another subtraction, corrupt the carry preserved by DEC, or recalculate the auxiliary-carry bit of flags restored by POPF.

PUSHF now uses the flag dataflow’s materialized x86 state. The analysis marks its contributing writers for materialization, including a single writer in the current block, and the instruction reads the saved status bits, forces bit 1 and includes DF from the separate direction state. Reserved bits and RF/VM from a raw POPF operand stay clear.

Tested

  • On bd643e46e784daf6bf3a45d09f0ec20e669c4ddb, all four new QEMU controls failed: same-block and cross-block comparison, compare then decrement, and POPF round trip. All pass with the fix.

  • The revised core x64 recompiler suite passed 49/49, including a raw POPF operand case that returned 0x30437 on the initial candidate and 0x417 with the supported-bit mask. In a separate run with identical parsed-result test fixtures on both sides, main passed 17/18 suites and the candidate passed 18/18. The inc/dec fixture went from five flag assertion failures to 8/8 tests passing. The separate result-adapter change was used to run that comparative corpus and is not a dependency of this PR.

  • Python compilation, changed-file game-data screening, and Git whitespace checks passed.

  • Full semantic suite ran 529 files and 6,526 tests. The only failing file, test_interp_computed_ij_fallback, produced the same two interpolation assertions on an unmodified checkout of the exact main commit.

GPUs tested: n/a. Windows 11, Python 3.12, QEMU 11.1.0. Synthetic inputs only.

AI-assisted with OpenAI Codex (GPT-6.2 Sol), with separate review.

Checklist

  • Clean merge verified against upstream main at the time of queue preparation.
  • One topic.
  • No game data, keys or protection workarounds.
  • No shader translator rule changed.
Claimed in the coordination issue as [X64-RECOMP-PUSHF-MATERIALIZED-FLAGS](https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7257603204). ### What and why PUSHF unconditionally rebuilt guest EFLAGS from the current AArch64 flags using addition carry polarity. That could replace a correct x86 flag word after a comparison or another subtraction, corrupt the carry preserved by DEC, or recalculate the auxiliary-carry bit of flags restored by POPF. PUSHF now uses the flag dataflow’s materialized x86 state. The analysis marks its contributing writers for materialization, including a single writer in the current block, and the instruction reads the saved status bits, forces bit 1 and includes DF from the separate direction state. Reserved bits and RF/VM from a raw POPF operand stay clear. ### Tested - On `bd643e46e784daf6bf3a45d09f0ec20e669c4ddb`, all four new QEMU controls failed: same-block and cross-block comparison, compare then decrement, and POPF round trip. All pass with the fix. - The revised core x64 recompiler suite passed 49/49, including a raw POPF operand case that returned 0x30437 on the initial candidate and 0x417 with the supported-bit mask. In a separate run with identical parsed-result test fixtures on both sides, main passed 17/18 suites and the candidate passed 18/18. The inc/dec fixture went from five flag assertion failures to 8/8 tests passing. The separate result-adapter change was used to run that comparative corpus and is not a dependency of this PR. - Python compilation, changed-file game-data screening, and Git whitespace checks passed. - Full semantic suite ran 529 files and 6,526 tests. The only failing file, test_interp_computed_ij_fallback, produced the same two interpolation assertions on an unmodified checkout of the exact main commit. GPUs tested: n/a. Windows 11, Python 3.12, QEMU 11.1.0. Synthetic inputs only. AI-assisted with OpenAI Codex (GPT-6.2 Sol), with separate review. ### Checklist - [x] Clean merge verified against upstream main at the time of queue preparation. - [x] One topic. - [x] No game data, keys or protection workarounds. - [x] No shader translator rule changed.
copilot-pull-request-reviewer[bot] reviewed 2026-12-01 16:45:52 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
<!-- ccr-overview-v2 --> ### 🟡 Changes recommended A critical flag-preservation bug causes PUSHF to read stale flags after zero-count repeated SCAS/CMPS, requiring conditional-writer handling and a regression test. <details open> <summary><strong>1 open issue</strong></summary> - <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture> [Materialize incoming flags for zero-count repeated SCAS/CMPS](https://github.com/ProjectLeonida/mandy/pull/2899#discussion_r4757609235) · New </details> <details> <summary><strong>What changed in this PR</strong></summary> This PR updates the x64-to-AArch64 recompiler so PUSHF uses saved x86 flags rather than rebuilding them from AArch64 flags. **Changes:** - Materializes flag producers for PUSHF, including single writers in the same block. - Preserves supported status bits and the direction flag while clearing unsupported bits. - Adds regression tests for comparisons, decrement, and POPF round trips. | File | Description | | ---- | ----------- | | `tools/​x64_recomp.py` | Updates flag analysis and PUSHF emission to use saved x86 flags. | | `tests/​test_x64_recomp.py` | Adds PUSHF preservation and unsupported-bit masking tests. | </details> <sub>Review effort: Balanced</sub> **`tools/x64_recomp.py` line 1486:** > PUSHF now reads stale flags after a zero-count repeated SCAS/CMPS: `xor eax,eax; mov ecx,0; repne scasb; pushfq; pop rdi; ret` pushes 0x2 instead of 0x46 when the saved flag word starts at zero. Analysis materializes only SCAS, whose zero-count branch skips that store, leaving the preceding XOR flags unsaved. Treat repeated SCAS/CMPS as conditional writers in `_cond_flag_writer` so their incoming producers are also materialized, and add a zero-count regression.
KelboLive reviewed 2026-12-01 17:18:18 +00:00
KelboLive left a comment
No description provided.
The direction is right — PUSHF should read the materialized EFLAGS word rather than reconstructing it — and the mask itself is correct: `0x8D5` is CF/PF/AF/ZF/SF/OF, DF is added from `CTX_DF`, and bit 1 is forced set (`tools/x64_recomp.py:2512-2520`). But it needs a rebase before the mask can be judged on its own merits. **`origin/main` already fixed the same-block case.** `9b7674d64` (today) replaced `self._mkflags(0)` with `self._ensure_eflags(ins)` at the pushf site (`origin/main:tools/x64_recomp.py:2549`, helper at `:2041-2046`, also used by `lahf`), so this branch conflicts exactly at the prologue it deletes (`merge-tree` exits 1 on `tools/x64_recomp.py`; the test file auto-merges). After rebasing, the only unique value left is the mask — which is worth having, but say so, and drop the now-duplicate analysis arm if `_ensure_eflags` covers it. **The uncovered writer case is real.** `_cond_flag_writer` (`:411-414`) models only shift/rotate-by-CL, so `rep{ne,} scas/cmps` is treated as a hard writer, but `_emit_scas_cmps` skips `_maybe_mkflags` on the RCX==0 path (`:3400-3401`, `:3424-3433`). For `cmp; mov ecx,0; repne scasb; pushf`, the analysis stops at the `scas` and the `cmp` is never materialized (`cmp mat=False` in my probe), so PUSHF pushes a stale word. Either extend `_cond_flag_writer` or materialize the reaching producers. Two smaller things: - **`66 9C` (PUSHFW) emits the same 8-byte push as `9C`** — byte-identical ARM, RSP−8/store 8 instead of 2/2, while `push`/`pop` do handle 16-bit (`:2495`, `:2500`). Pre-existing, but this is the PR that owns pushf. - **All five new tests are QEMU-only**: 36 of 49 skip on this host because `qemu-system-aarch64` is absent, so on plain CI the new behaviour is asserted by nothing. A `tr.mat` / `arm_path` assertion (the style used by `ShiftCarryAnalysisTests`) would exercise the guard without QEMU. I verified three mutations that only such a test catches: dropping the bit-1 `orr`, `0x8D5`→`0x8C5`, and removing the `ins.mnem != "pushf"` exclusion. Also worth stating as deliberate: IF, IOPL, NT, RF, VM, AC, VIF, VIP and ID are all forced to 0, so `pushf; bts 21; popf; pushf; bt 21` answers "no ID" — fine for a title that never probes CPUID this way, but a sentence in the body would save the next reader the question. POPF is also not the exact inverse (it stores the raw operand and derives only NZCV+DF), which is harmless today because nothing reads the other bits.
aegish-zagi commented 2026-12-02 04:52:14 +00:00

Verification — VERIFY-2899

Verdict: NOT VERIFIED on this host — two blockers: the PR conflicts with current main, and its new tests cannot run without qemu-system-aarch64

  • PR: #2899 | head sha: 004fd399ab17892338caab0489ec48246e1faa12 | merge state vs main 8422def759cda12c89f918438eeb8d53dd5972a6: CONFLICT in tools/x64_recomp.py (the test file merges cleanly). The PR head as-is was tested; it needs a rebase before merge in any case.
  • Tests run: python3 -I tests/test_x64_recomp.py
  • Results: on the PR head, Ran 49 tests — OK (skipped=36). The 36 skips are the whole ExecTests class, gated on qemu-system-aarch64, which this host does not have — and both new PUSHF tests live in that class, so neither executed here (MEASURED skip, not a pass). On main code with the PR test file the same skips occur, so no red/green for PUSHF is observable on a Profile Z host; the one failure there (test_opcode_90_nop_pause_and_rex_xchg in DecodeTests) is unrelated drift between the PR-era test file and current main, not a PUSHF result.
  • Audit (READ IN CODE only): _flag_analysis no longer takes the single-producer arm shortcut for pushf, so PF/AF writers materialize; _emit_pushpop masks the stored flags word to 0x8D5 (CF/PF/AF/ZF/SF/OF) plus bit 1 before OR-ing DF. The change is small and reads correctly, but that is a code read, not an execution result — a host with qemu-system-aarch64 still needs to run the two new cases.
  • Platform: Linux, Python 3.12.3, no GPU, no qemu-system-aarch64 (Profile Z)

AI-assisted: yes (Muse Wonder, Muse by Meta)

## Verification — VERIFY-2899 **Verdict: NOT VERIFIED on this host — two blockers: the PR conflicts with current main, and its new tests cannot run without qemu-system-aarch64** - PR: #2899 | head sha: `004fd399ab17892338caab0489ec48246e1faa12` | merge state vs main `8422def759cda12c89f918438eeb8d53dd5972a6`: CONFLICT in `tools/x64_recomp.py` (the test file merges cleanly). The PR head as-is was tested; it needs a rebase before merge in any case. - Tests run: `python3 -I tests/test_x64_recomp.py` - Results: on the PR head, `Ran 49 tests — OK (skipped=36)`. The 36 skips are the whole `ExecTests` class, gated on `qemu-system-aarch64`, which this host does not have — and both new PUSHF tests live in that class, so neither executed here (MEASURED skip, not a pass). On main code with the PR test file the same skips occur, so no red/green for PUSHF is observable on a Profile Z host; the one failure there (`test_opcode_90_nop_pause_and_rex_xchg` in DecodeTests) is unrelated drift between the PR-era test file and current main, not a PUSHF result. - Audit (READ IN CODE only): `_flag_analysis` no longer takes the single-producer arm shortcut for `pushf`, so PF/AF writers materialize; `_emit_pushpop` masks the stored flags word to 0x8D5 (CF/PF/AF/ZF/SF/OF) plus bit 1 before OR-ing DF. The change is small and reads correctly, but that is a code read, not an execution result — a host with qemu-system-aarch64 still needs to run the two new cases. - Platform: Linux, Python 3.12.3, no GPU, no qemu-system-aarch64 (Profile Z) AI-assisted: yes (Muse Wonder, Muse by Meta)
MoonEyes commented 2026-12-02 11:52:34 +00:00

Closing: this pull request no longer applies to current main (merge conflict in tools/x64_recomp.py), and open pull requests that have fallen out of step with main are being closed so the list stays workable. If you still want this change, rebase it on current main and reopen it (or open a new pull request); it will be looked at again.

AI-assisted (Claude Code).

Closing: this pull request no longer applies to current `main` (merge conflict in `tools/x64_recomp.py`), and open pull requests that have fallen out of step with `main` are being closed so the list stays workable. If you still want this change, rebase it on current `main` and reopen it (or open a new pull request); it will be looked at again. AI-assisted (Claude Code).
Sign in to join this conversation.
No description provided.