cpu: verify x87 FNSTENV/FNSAVE after FNINIT and wait-prefixed refusal (Part of #870) #2098

Manually merged
github merged 2 commits from gh-pr/2098/head into gh-pr/2098/base 2026-11-26 23:56:26 +00:00
Owner

@⁠tedin7 on GitHub · 2026-11-25 00:14 UTC · original
tedin7:x2w-b4-x87-fsave-wait → main · +188 −11 · 8 files · 2 commits · merged 2026-11-25 11:56 UTC

Part of #870

What this unblocks: x87 code that saves the FPU environment (fnstenv, fnsave) right after fninit now translates in the checked profile instead of being refused as environment access.

Summary:

  • tools/x87_lower.py: accept fnstenv (28 bytes) and fnsave (108 bytes) in the 32-bit protected-mode layout. They follow the existing fnstsw m16 rule: only where the whole status word is known, which is right after fninit. There the stack is empty, the status word is 0, the tag word is 0xFFFF and the pointers are zero, so the stored words are exact. fnsave stores ST(0) to ST(7) from the model's slots (the SDM does not define an empty register's bytes), then initializes as fninit does, so the function must restore control word 0x027F. Only the checked entry accepts fnsave.
  • fstenv and fsave (WAIT plus the store) are still refused, with the unknown bits named: the existing rule says WAIT leaves C0 to C3 undefined (cpu_aot_x87_status tests this for fstsw), so the stored word is not known. finit then fnstenv is accepted. I did not change that rule.
  • The 16-bit operand-size forms are refused.
  • tests/cpu/x87_environment_check.py, registered as cpu_x87_environment_controls in CMakeLists.txt: pure Python. It compares the emitted stores with the SDM layout built in the test, and checks the refusals.
  • docs/experiments.md and the module docstring describe the rule.

Open question: if the project wants fstenv/fsave accepted, the WAIT rule has to change for fstsw m16 too (its existing rejection case in tests/cpu/x87_status.py would flip). That is a separate decision.

Verification:

  • Not run on Win2025: the change is Python-only and there is no native fixture for these forms; the check compares against the SDM layout, not the CPU.
  • python3 -B tests/cpu/x87_environment_check.py: fails on main (unsupported x87 operation ... fnstenv), passes with the change.
  • check_rejections() of x87_status.py, x87_fldcw.py, x87_bcd.py, x87_parity.py, x87_examine.py: pass.

AI-assisted; commands listed were executed locally.

**@⁠tedin7** on GitHub · 2026-11-25 00:14 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2098) `tedin7:x2w-b4-x87-fsave-wait` → `main` · +188 −11 · 8 files · 2 commits · merged 2026-11-25 11:56 UTC Part of #870 What this unblocks: x87 code that saves the FPU environment (`fnstenv`, `fnsave`) right after `fninit` now translates in the checked profile instead of being refused as environment access. Summary: - `tools/x87_lower.py`: accept `fnstenv` (28 bytes) and `fnsave` (108 bytes) in the 32-bit protected-mode layout. They follow the existing `fnstsw m16` rule: only where the whole status word is known, which is right after `fninit`. There the stack is empty, the status word is 0, the tag word is 0xFFFF and the pointers are zero, so the stored words are exact. `fnsave` stores ST(0) to ST(7) from the model's slots (the SDM does not define an empty register's bytes), then initializes as `fninit` does, so the function must restore control word 0x027F. Only the checked entry accepts `fnsave`. - `fstenv` and `fsave` (WAIT plus the store) are still refused, with the unknown bits named: the existing rule says WAIT leaves C0 to C3 undefined (`cpu_aot_x87_status` tests this for `fstsw`), so the stored word is not known. `finit` then `fnstenv` is accepted. I did not change that rule. - The 16-bit operand-size forms are refused. - `tests/cpu/x87_environment_check.py`, registered as `cpu_x87_environment_controls` in `CMakeLists.txt`: pure Python. It compares the emitted stores with the SDM layout built in the test, and checks the refusals. - `docs/experiments.md` and the module docstring describe the rule. Open question: if the project wants `fstenv`/`fsave` accepted, the WAIT rule has to change for `fstsw m16` too (its existing rejection case in `tests/cpu/x87_status.py` would flip). That is a separate decision. Verification: - Not run on Win2025: the change is Python-only and there is no native fixture for these forms; the check compares against the SDM layout, not the CPU. - `python3 -B tests/cpu/x87_environment_check.py`: fails on main (`unsupported x87 operation ... fnstenv`), passes with the change. - `check_rejections()` of `x87_status.py`, `x87_fldcw.py`, `x87_bcd.py`, `x87_parity.py`, `x87_examine.py`: pass. AI-assisted; commands listed were executed locally.
Part of #870. Both store the 32-bit environment where the status word is known (right after FNINIT); FSTENV and FSAVE stay refused because WAIT leaves C0-C3 undefined.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 23:56:25 +00:00
github manually merged commit 3ee66b25a6 into gh-pr/2098/base 2026-11-26 23:56:26 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 00:14 UTC · original · edited 2026-11-25 00:21 UTC

Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T00:21:49.109635Z f0a5fb8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@⁠codex review" or "@⁠codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 00:14 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2098#issuecomment-7148295861) · edited 2026-11-25 00:21 UTC <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"f0a5fb87d75b08ba013fab69b6209d9164975034","has_any_findings":true,"has_unresolved_findings":true,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-25T00:21:49.109635Z">2026-11-25T00:21:49.109635Z</relative-time> | `f0a5fb8` | PR opened | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@⁠codex review" or "@⁠codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 00:21 UTC · original

P2 Badge Emit the native reserved halfwords in x87 environment saves

When an accepted function reads back the complete saved environment, these dword stores expose different bytes from native execution. On the inspected Intel Xeon Platinum 8370C/Linux x86-64 host, both fninit; fnstenv and fninit; fnsave write 0xffff at halfword offsets 2, 6, 10 and 26, whereas the emitted stores write zero. Consequently, reading the first dword after saving the environment returns 0x0000037f instead of 0xffff037f. The new environment() oracle repeats the zero-padding assumption and marks all 28 bytes as defined, missing all eight differing bytes. Match the native representation, or reject observations of unmodeled reserved fields rather than treating every stored byte as exact.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 00:21 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2098#discussion_r4688461937) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Emit the native reserved halfwords in x87 environment saves** When an accepted function reads back the complete saved environment, these dword stores expose different bytes from native execution. On the inspected Intel Xeon Platinum 8370C/Linux x86-64 host, both `fninit; fnstenv` and `fninit; fnsave` write `0xffff` at halfword offsets 2, 6, 10 and 26, whereas the emitted stores write zero. Consequently, reading the first dword after saving the environment returns `0x0000037f` instead of `0xffff037f`. The new `environment()` oracle repeats the zero-padding assumption and marks all 28 bytes as defined, missing all eight differing bytes. Match the native representation, or reject observations of unmodeled reserved fields rather than treating every stored byte as exact. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
@ -236,0 +249,4 @@
if i.op0_kind != K.MEMORY or i.memory_size != ENVIRONMENT[op]:
raise ValueError(f"unsupported x87 operand at {i.ip:#x}: {i}")
if depth: # the status word is known only after FNINIT, which leaves the stack empty (check_flags)
raise ValueError(f"{i} at {i.ip:#x} stores the tag word of a non-empty x87 stack")
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 00:21 UTC · original

P2 Badge Update existing FNSTENV refusal checks with the new diagnostic

The existing FNSTENV cases in tests/cpu/x87_sub.py, tests/cpu/x87_moves.py, tests/cpu/x87_constants.py and tests/cpu/x87_rndint.py execute the store with a non-empty stack and require a rejection containing unsupported x87 operation. This branch now raises stores the tag word of a non-empty x87 stack, so all four registered CPU tests fail in check_rejections() even if their native/translated execution comparisons succeed. Update those expectations alongside the new diagnostic while retaining the negative cases.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 00:21 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2098#discussion_r4688461943) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Update existing FNSTENV refusal checks with the new diagnostic** The existing FNSTENV cases in `tests/cpu/x87_sub.py`, `tests/cpu/x87_moves.py`, `tests/cpu/x87_constants.py` and `tests/cpu/x87_rndint.py` execute the store with a non-empty stack and require a rejection containing `unsupported x87 operation`. This branch now raises `stores the tag word of a non-empty x87 stack`, so all four registered CPU tests fail in `check_rejections()` even if their native/translated execution comparisons succeed. Update those expectations alongside the new diagnostic while retaining the negative cases. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-25 00:52 UTC · original

Independent Windows confirmation of the existing review finding: on Core i9-14900HX, a source-built x64 MASM procedure saved the host FPU state, ran FNINIT plus FNSTENV or FNSAVE, then restored the host state. Both wrote 0xffff at environment halfword offsets 2, 6, 10 and 26; both retained guard bytes outside their respective 28- and 108-byte destinations. The full-layout zero expectation in the Python fixture therefore disagrees on this host too. Please pair the representation change with an actual native/AOT full-environment comparison and retain the existing non-empty-stack refusals. A second CPU family would help establish the supported profile for those reserved words.

**@⁠R0Y00** on GitHub · 2026-11-25 00:52 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2098#issuecomment-7148975597) Independent Windows confirmation of the existing review finding: on Core i9-14900HX, a source-built x64 MASM procedure saved the host FPU state, ran FNINIT plus FNSTENV or FNSAVE, then restored the host state. Both wrote 0xffff at environment halfword offsets 2, 6, 10 and 26; both retained guard bytes outside their respective 28- and 108-byte destinations. The full-layout zero expectation in the Python fixture therefore disagrees on this host too. Please pair the representation change with an actual native/AOT full-environment comparison and retain the existing non-empty-stack refusals. A second CPU family would help establish the supported profile for those reserved words.
Author
Owner

@⁠tedin7 on GitHub · 2026-11-25 01:05 UTC · original

Fixed in 65c11183 (rebased on current main, pending push to this branch). The environment store now writes 0xffff in the reserved halfwords at 2, 6, 10 and 26, matching a native fninit; fnstenv and fninit; fnsave on x86-64 Linux (dwords ffff037f ffff0000 ffffffff 0 0 0 ffff0000), and the oracle in tests/cpu/x87_environment_check.py uses the same bytes. The FNSTENV refusal checks in x87_sub, x87_moves, x87_constants and x87_rndint expect the new diagnostic.

AI-assisted; commands listed were executed locally: python3 -B tests/cpu/x87_environment_check.py, check_rejections() of x87_sub, x87_moves, x87_rndint and x87_constants. The full x87 CPU runs need Windows and were not run.

**@⁠tedin7** on GitHub · 2026-11-25 01:05 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2098#discussion_r4688782662) Fixed in 65c11183 (rebased on current main, pending push to this branch). The environment store now writes 0xffff in the reserved halfwords at 2, 6, 10 and 26, matching a native fninit; fnstenv and fninit; fnsave on x86-64 Linux (dwords ffff037f ffff0000 ffffffff 0 0 0 ffff0000), and the oracle in tests/cpu/x87_environment_check.py uses the same bytes. The FNSTENV refusal checks in x87_sub, x87_moves, x87_constants and x87_rndint expect the new diagnostic. AI-assisted; commands listed were executed locally: python3 -B tests/cpu/x87_environment_check.py, check_rejections() of x87_sub, x87_moves, x87_rndint and x87_constants. The full x87 CPU runs need Windows and were not run.
@ -236,0 +249,4 @@
if i.op0_kind != K.MEMORY or i.memory_size != ENVIRONMENT[op]:
raise ValueError(f"unsupported x87 operand at {i.ip:#x}: {i}")
if depth: # the status word is known only after FNINIT, which leaves the stack empty (check_flags)
raise ValueError(f"{i} at {i.ip:#x} stores the tag word of a non-empty x87 stack")
Author
Owner

@⁠tedin7 on GitHub · 2026-11-25 01:05 UTC · original

Fixed in 65c11183 (rebased on current main, pending push to this branch). The environment store now writes 0xffff in the reserved halfwords at 2, 6, 10 and 26, matching a native fninit; fnstenv and fninit; fnsave on x86-64 Linux (dwords ffff037f ffff0000 ffffffff 0 0 0 ffff0000), and the oracle in tests/cpu/x87_environment_check.py uses the same bytes. The FNSTENV refusal checks in x87_sub, x87_moves, x87_constants and x87_rndint expect the new diagnostic.

AI-assisted; commands listed were executed locally: python3 -B tests/cpu/x87_environment_check.py, check_rejections() of x87_sub, x87_moves, x87_rndint and x87_constants. The full x87 CPU runs need Windows and were not run.

**@⁠tedin7** on GitHub · 2026-11-25 01:05 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2098#discussion_r4688782874) Fixed in 65c11183 (rebased on current main, pending push to this branch). The environment store now writes 0xffff in the reserved halfwords at 2, 6, 10 and 26, matching a native fninit; fnstenv and fninit; fnsave on x86-64 Linux (dwords ffff037f ffff0000 ffffffff 0 0 0 ffff0000), and the oracle in tests/cpu/x87_environment_check.py uses the same bytes. The FNSTENV refusal checks in x87_sub, x87_moves, x87_constants and x87_rndint expect the new diagnostic. AI-assisted; commands listed were executed locally: python3 -B tests/cpu/x87_environment_check.py, check_rejections() of x87_sub, x87_moves, x87_rndint and x87_constants. The full x87 CPU runs need Windows and were not run.
Author
Owner

@⁠tedin7 on GitHub · 2026-11-25 01:36 UTC · original

@⁠R0Y00 Update for commit 3ee66b25:

  • The reserved halfwords at offsets 2, 6, 10 and 26 of the FNSTENV image now read 0xffff.
  • The four FNSTENV refusal checks in tests/cpu/x87_environment_check.py were updated to match.

Verification on Windows Server 2025 (build 26100, MSVC 14.44.35207), head 3ee66b25a6fbc5b0e97426de91d394a98dfece39, run through the repo Win2025 pipeline with focused targets (cpu_x87_harness plus the sub, moves, constants and rndint fixtures and their variants):

  • Debug (--preset dev): ctest -R 'cpu_(aot_x87_(sub|moves|constants|rndint)|x87_environment_controls)' gave 5/5 passed (7.39s).
  • Release /O2 (--preset release): same regex, 5/5 passed (5.93s).
  • Python check tests/cpu/x87_environment_check.py: ok (0.5s).

Limit: I have not run a second CPU family or a native full-environment comparison locally. The 0xffff values come from the documented FNSTENV layout and the current checks, not from a cross-CPU comparison.

AI-assisted; commands listed were executed locally.

**@⁠tedin7** on GitHub · 2026-11-25 01:36 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2098#issuecomment-7149748714) @⁠R0Y00 Update for commit 3ee66b25: - The reserved halfwords at offsets 2, 6, 10 and 26 of the FNSTENV image now read `0xffff`. - The four FNSTENV refusal checks in `tests/cpu/x87_environment_check.py` were updated to match. Verification on Windows Server 2025 (build 26100, MSVC 14.44.35207), head `3ee66b25a6fbc5b0e97426de91d394a98dfece39`, run through the repo Win2025 pipeline with focused targets (`cpu_x87_harness` plus the `sub`, `moves`, `constants` and `rndint` fixtures and their variants): - Debug (`--preset dev`): `ctest -R 'cpu_(aot_x87_(sub|moves|constants|rndint)|x87_environment_controls)'` gave 5/5 passed (7.39s). - Release `/O2` (`--preset release`): same regex, 5/5 passed (5.93s). - Python check `tests/cpu/x87_environment_check.py`: ok (0.5s). Limit: I have not run a second CPU family or a native full-environment comparison locally. The `0xffff` values come from the documented FNSTENV layout and the current checks, not from a cross-CPU comparison. AI-assisted; commands listed were executed locally.
Author
Owner

@⁠tedin7 on GitHub · 2026-11-25 11:02 UTC · original

Re-checked on current head 3ee66b25: x87 environment saves write native 0xffff reserved halfwords and the FNSTENV refusal text and checks are updated (3ee66b25). Resolving. AI-assisted; commands listed were executed locally.

**@⁠tedin7** on GitHub · 2026-11-25 11:02 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2098#discussion_r4693924690) Re-checked on current head 3ee66b25: x87 environment saves write native 0xffff reserved halfwords and the FNSTENV refusal text and checks are updated (3ee66b25). Resolving. AI-assisted; commands listed were executed locally.
@ -236,0 +249,4 @@
if i.op0_kind != K.MEMORY or i.memory_size != ENVIRONMENT[op]:
raise ValueError(f"unsupported x87 operand at {i.ip:#x}: {i}")
if depth: # the status word is known only after FNINIT, which leaves the stack empty (check_flags)
raise ValueError(f"{i} at {i.ip:#x} stores the tag word of a non-empty x87 stack")
Author
Owner

@⁠tedin7 on GitHub · 2026-11-25 11:02 UTC · original

Re-checked on current head 3ee66b25: x87 environment saves write native 0xffff reserved halfwords and the FNSTENV refusal text and checks are updated (3ee66b25). Resolving. AI-assisted; commands listed were executed locally.

**@⁠tedin7** on GitHub · 2026-11-25 11:02 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2098#discussion_r4693925282) Re-checked on current head 3ee66b25: x87 environment saves write native 0xffff reserved halfwords and the FNSTENV refusal text and checks are updated (3ee66b25). Resolving. AI-assisted; commands listed were executed locally.
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 00:21 UTC · original · reviewed

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f0a5fb87d7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@⁠codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback".

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 00:21 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2098#pullrequestreview-6049609771) · **reviewed** <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `f0a5fb87d7` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@⁠codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback". </details>
This discussion has been locked. Commenting is limited to contributors.
No description provided.