cpu: keep memory reads whose value is unused in the jump-table translator #431

Merged
bosseurdinternet merged 2 commits from upstream-pr/437 into main 2026-11-22 07:47:53 +00:00
bosseurdinternet commented 2026-11-22 02:51:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T02:51:42Z.


Change

A correctness fix on main, independent of #321, #343, #416 and #420. Found while running differential execution on #425 and #430, but the cause is older than those.

The processor performs every memory read of an instruction, so the original faults on an inaccessible operand whether or not the value is used afterwards. tools/cpu_switch_aot.py loses such a read in two ways:

  1. An ordinary load whose value is unused. A read is translated as load<T>, a memcpy. An optimizing host compiler removes it when the value is dead, for example mov rax, [rcx] followed by mov rax, rdx. The docs already describe this for a CMOVcc memory source, which is read through a volatile load for that reason.
  2. A CMP or TEST with a memory operand whose flags no consumer reads. The translator emits no statement at all for it, so the read is missing in every build.

In both cases the translation returns where the original faults.

  • Every guest read now goes through the existing load_always. load is no longer emitted and is removed from the shared header.
  • A CMP or TEST with a memory operand and no flag consumer emits the read alone, as static_cast<void>(load_always<T>(address));.
  • Nothing else changes: the same functions translate, with the same messages for those refused.

Verification

Local runs used the same compiler build as CI (MSVC 14.52.37025: cl, link, ml64) under Wine 11.0 on Linux. I ran everything below myself on this commit's translator; the three main commits it was rebased over do not touch these files.

  • ctest --no-tests=error -R '^cpu_aot_dead_reads$' -V: passed. The new fixture exports a value that is overwritten (read_dead), a CMP whose flags are never read (cmp_dead), a one-byte read (byte_dead), two reads whose sum is cleared (pair_dead) and a control whose value is returned (read_live).
    • Original and translation return the reference for a readable operand and raise an access violation for a null one.
    • Eight controls place the operand before an inaccessible page, each printed on one line: both sides fault exactly when the read's own width crosses into that page (one byte before it, byte_dead returns and read_dead faults; cmp_dead returns at four bytes and faults at three), and pair_dead faults when only its first read is readable.
    • A source control checks that each export's translation reads memory once per memory operand, through load_always only.
  • What the Debug build shows and does not show. CTest builds the translation unoptimized, where an ordinary load is performed too. So in CI the behavioural checks of read_dead, byte_dead and pair_dead pass with or without this change; there, only the source control guards item 1. Item 2 is different: with the CMP branch absent, the generated source of cmp_dead contains no read at all, so its null call cannot fault in any build. I saw that source during development, when the test stopped at the source control; I did not run the behavioural check on it.
  • Optimized build, before and after. I translated the fixture with main (992306b) and with this change, compiled both with cl /O2 /LD /W4 and ran the same check:
    • main: fails, translated read_dead with a null operand did not fault;
    • this change: passes.
  • Corpus (the repository's desktop C++ sources and one extra file, built with MSVC at /O2, /O1 and /Od), against main: the same functions translate (642, 639 and 15), no status or message changes, and across every object including library code each translation is identical to main's once load_always< is read as load< (1,064, 1,061 and 441 translations). No corpus function contains a CMP or TEST of item 2.
  • Differential execution of original and compiled translation, /O2, up to 64 inputs each: 639 of the 642 checked with no mismatch, as on main (one faults on both sides for every input, two are excluded by name).
  • Where the difference shows on real compiler output. With #430's translator, 9 of its 160 newly translated /Od corpus functions mismatch: the original faults on a bad pointer and the translation returns, because a value loaded only to be passed to a callee that ignores it is dead once the callee is inlined. With the one-line change of item 1 applied to a copy of that tree, all 9 agree and 159 of 160 are checked with no mismatch (the last one times out on the original side). Details are on #425.
  • Existing tests: cpu_aot_jump_tables (which holds the CMOVcc control), cpu_aot_switch_addr32, cpu_aot_entry_order, cpu_aot_narrow_returns and cpu_aot_fourth_argument pass. Other cpu_* tests: same results as main; under Wine, cpu_aot_import_call, cpu_aot_ordinal_import, cpu_native_reloc_free and cpu_native_tls fail on main too, and cpu_native_exe_start times out intermittently (#281).
  • pyflakes, tests/docs_links_check.py and tests/form_coverage_check.py passed.

Not run locally: Windows itself. Not measured: the cost of volatile reads in the generated code, which the host compiler can no longer merge or drop.

Evidence and follow-up

  • tests/cpu/dead_read.asm is original MASM assembled in the build. No binaries were added. dead_read is classified in tools/form_coverage.py.
  • For your decision. The docs state the rule for a CMOVcc source only. This PR reads it as a rule for every guest read. If dead reads other than that one may be dropped, item 1 should be closed and item 2 kept or dropped on the same reasoning.
  • Not covered: stores are unchanged; the order of a fault relative to the function's other effects is not examined; an instruction that reads the same operand for its flags and for its result is translated with two reads of that operand, as before.
  • Other open PRs. #420 reads its frame array through load. A frame is a local array and cannot fault, so those reads can stay ordinary; I will keep load there when one of the two is rebased over the other.
  • Jump-table translator notes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6537582289 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/437). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-22T02:51:42Z. --- ## Change A correctness fix on `main`, independent of #321, #343, #416 and #420. Found while running differential execution on #425 and #430, but the cause is older than those. The processor performs every memory read of an instruction, so the original faults on an inaccessible operand whether or not the value is used afterwards. `tools/cpu_switch_aot.py` loses such a read in two ways: 1. **An ordinary load whose value is unused.** A read is translated as `load<T>`, a `memcpy`. An optimizing host compiler removes it when the value is dead, for example `mov rax, [rcx]` followed by `mov rax, rdx`. The docs already describe this for a `CMOVcc` memory source, which is read through a `volatile` load for that reason. 2. **A `CMP` or `TEST` with a memory operand whose flags no consumer reads.** The translator emits no statement at all for it, so the read is missing in every build. In both cases the translation returns where the original faults. - Every guest read now goes through the existing `load_always`. `load` is no longer emitted and is removed from the shared header. - A `CMP` or `TEST` with a memory operand and no flag consumer emits the read alone, as `static_cast<void>(load_always<T>(address));`. - Nothing else changes: the same functions translate, with the same messages for those refused. ## Verification Local runs used the same compiler build as CI (MSVC 14.52.37025: `cl`, `link`, `ml64`) under Wine 11.0 on Linux. I ran everything below myself on this commit's translator; the three `main` commits it was rebased over do not touch these files. - **`ctest --no-tests=error -R '^cpu_aot_dead_reads$' -V`: passed.** The new fixture exports a value that is overwritten (`read_dead`), a `CMP` whose flags are never read (`cmp_dead`), a one-byte read (`byte_dead`), two reads whose sum is cleared (`pair_dead`) and a control whose value is returned (`read_live`). - Original and translation return the reference for a readable operand and raise an access violation for a null one. - Eight controls place the operand before an inaccessible page, each printed on one line: both sides fault exactly when the read's own width crosses into that page (one byte before it, `byte_dead` returns and `read_dead` faults; `cmp_dead` returns at four bytes and faults at three), and `pair_dead` faults when only its first read is readable. - A source control checks that each export's translation reads memory once per memory operand, through `load_always` only. - **What the Debug build shows and does not show.** CTest builds the translation unoptimized, where an ordinary load is performed too. So in CI the behavioural checks of `read_dead`, `byte_dead` and `pair_dead` pass with or without this change; there, only the source control guards item 1. Item 2 is different: with the `CMP` branch absent, the generated source of `cmp_dead` contains no read at all, so its null call cannot fault in any build. I saw that source during development, when the test stopped at the source control; I did not run the behavioural check on it. - **Optimized build, before and after.** I translated the fixture with `main` (`992306b`) and with this change, compiled both with `cl /O2 /LD /W4` and ran the same check: - `main`: fails, `translated read_dead with a null operand did not fault`; - this change: passes. - **Corpus** (the repository's desktop C++ sources and one extra file, built with MSVC at `/O2`, `/O1` and `/Od`), against `main`: the same functions translate (642, 639 and 15), no status or message changes, and across every object including library code each translation is identical to `main`'s once `load_always<` is read as `load<` (1,064, 1,061 and 441 translations). No corpus function contains a `CMP` or `TEST` of item 2. - **Differential execution** of original and compiled translation, `/O2`, up to 64 inputs each: 639 of the 642 checked with no mismatch, as on `main` (one faults on both sides for every input, two are excluded by name). - **Where the difference shows on real compiler output.** With #430's translator, 9 of its 160 newly translated `/Od` corpus functions mismatch: the original faults on a bad pointer and the translation returns, because a value loaded only to be passed to a callee that ignores it is dead once the callee is inlined. With the one-line change of item 1 applied to a copy of that tree, all 9 agree and 159 of 160 are checked with no mismatch (the last one times out on the original side). Details are on #425. - Existing tests: `cpu_aot_jump_tables` (which holds the `CMOVcc` control), `cpu_aot_switch_addr32`, `cpu_aot_entry_order`, `cpu_aot_narrow_returns` and `cpu_aot_fourth_argument` pass. Other `cpu_*` tests: same results as `main`; under Wine, `cpu_aot_import_call`, `cpu_aot_ordinal_import`, `cpu_native_reloc_free` and `cpu_native_tls` fail on `main` too, and `cpu_native_exe_start` times out intermittently (#281). - `pyflakes`, `tests/docs_links_check.py` and `tests/form_coverage_check.py` passed. Not run locally: Windows itself. Not measured: the cost of volatile reads in the generated code, which the host compiler can no longer merge or drop. ## Evidence and follow-up - `tests/cpu/dead_read.asm` is original MASM assembled in the build. No binaries were added. `dead_read` is classified in `tools/form_coverage.py`. - **For your decision.** The docs state the rule for a `CMOVcc` source only. This PR reads it as a rule for every guest read. If dead reads other than that one may be dropped, item 1 should be closed and item 2 kept or dropped on the same reasoning. - **Not covered:** stores are unchanged; the order of a fault relative to the function's other effects is not examined; an instruction that reads the same operand for its flags and for its result is translated with two reads of that operand, as before. - **Other open PRs.** #420 reads its frame array through `load`. A frame is a local array and cannot fault, so those reads can stay ordinary; I will keep `load` there when one of the two is rebased over the other. - [Jump-table translator notes](https://github.com/R0Y00/Xbox2Windows/blob/main/docs/experiments.md#switch-dispatch-through-jump-tables) _AI-assisted (Claude Code)._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:43:02 +00:00
grahamster2 commented 2026-11-24 06:48:22 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T03:19:47Z.


I checked whether the other translators on main (fb4fb85) have the same dead-read behaviour. Two do:

Translator How guest reads are emitted Dead read accepted Native null / translated null at /O2
cpu_chained_aot.py read_word (memcpy) yes faults / returns
ffx_version_query.lower (used by cpu_exe_aot, bink_*, ffx_*, the x87 profile) load<T> (memcpy) yes: overwritten value, CMP/TEST on memory, a CMOVcc that does not move faults / returns
x87_lower.py (through lower(x87=True)) load<T> only through a dead store faults / faults (the load is passed to x87_from_f64, which cl does not inline)
cpu_leaf_aot.py, cpu_call_aot.py no guest reads, only stores no not applicable
cpu_exe_aot.py through lower operands are limited to [rsp+32..72), the virtual frame, which cannot fault not applicable

Reproducers, source-built MASM with PROC FRAME/.endprolog, linked like cpu_asm_fixture:

ch_read_dead PROC FRAME      ; cpu_chained_aot.translate(image, "ch_read_dead")
.endprolog
    mov rax, [rcx]
    mov rax, rdx
    ret
ch_read_dead ENDP

lw_cmp_dead PROC FRAME       ; lower(region(...), ["rcx = a; rdx = b;"], {}, {}, {}), wrapped as in tests/cpu/x87_add.py
.endprolog
    cmp dword ptr [rcx], 0   ; also: test byte ptr [rcx], 1 / mov rax, [rcx]
    mov rax, rdx
    ret
lw_cmp_dead ENDP

lw_cmov_dead PROC FRAME
.endprolog
    xor eax, eax
    cmp edx, edx
    cmovne rax, [rcx]        ; lowered as zero ? rax : load<uint64_t>(rcx)
    ret
lw_cmov_dead ENDP

Each was called with a null RCX in a separate process. All five dead cases (ch_read_dead, and mov, cmp, test, cmovne through lower) fault natively and return from the cl /O2 translation. The live controls (mov rax, [rcx]; ret) fault on both sides. In the /O2 listings the read is gone: try_lifted is mov [r8], rdx; mov eax, 1; ret, the three lower cases are mov rax, rdx; ret, and lw_cmov_dead is xor eax, eax; ret.

I only tested source-built fixtures. I did not check whether any of the hash-pinned images that bink_* and ffx_* translate actually contain a dead read.

If you accept the load_always approach here, I can send follow-ups that apply it (or whatever you decide) to cpu_chained_aot.read_word and the load in ffx_version_query.INTEGER_HEADER, including the x87 loads, each with a fixture like dead_read.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073467319 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/437#issuecomment-7073467319). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-22T03:19:47Z. --- I checked whether the other translators on `main` (fb4fb85) have the same dead-read behaviour. Two do: | Translator | How guest reads are emitted | Dead read accepted | Native null / translated null at `/O2` | |---|---|---|---| | `cpu_chained_aot.py` | `read_word` (memcpy) | yes | faults / returns | | `ffx_version_query.lower` (used by `cpu_exe_aot`, `bink_*`, `ffx_*`, the x87 profile) | `load<T>` (memcpy) | yes: overwritten value, `CMP`/`TEST` on memory, a `CMOVcc` that does not move | faults / returns | | `x87_lower.py` (through `lower(x87=True)`) | `load<T>` | only through a dead store | faults / faults (the load is passed to `x87_from_f64`, which cl does not inline) | | `cpu_leaf_aot.py`, `cpu_call_aot.py` | no guest reads, only stores | no | not applicable | | `cpu_exe_aot.py` | through `lower` | operands are limited to `[rsp+32..72)`, the virtual frame, which cannot fault | not applicable | Reproducers, source-built MASM with `PROC FRAME`/`.endprolog`, linked like `cpu_asm_fixture`: ```asm ch_read_dead PROC FRAME ; cpu_chained_aot.translate(image, "ch_read_dead") .endprolog mov rax, [rcx] mov rax, rdx ret ch_read_dead ENDP lw_cmp_dead PROC FRAME ; lower(region(...), ["rcx = a; rdx = b;"], {}, {}, {}), wrapped as in tests/cpu/x87_add.py .endprolog cmp dword ptr [rcx], 0 ; also: test byte ptr [rcx], 1 / mov rax, [rcx] mov rax, rdx ret lw_cmp_dead ENDP lw_cmov_dead PROC FRAME .endprolog xor eax, eax cmp edx, edx cmovne rax, [rcx] ; lowered as zero ? rax : load<uint64_t>(rcx) ret lw_cmov_dead ENDP ``` Each was called with a null RCX in a separate process. All five dead cases (`ch_read_dead`, and `mov`, `cmp`, `test`, `cmovne` through `lower`) fault natively and return from the `cl /O2` translation. The live controls (`mov rax, [rcx]; ret`) fault on both sides. In the `/O2` listings the read is gone: `try_lifted` is `mov [r8], rdx; mov eax, 1; ret`, the three `lower` cases are `mov rax, rdx; ret`, and `lw_cmov_dead` is `xor eax, eax; ret`. I only tested source-built fixtures. I did not check whether any of the hash-pinned images that `bink_*` and `ffx_*` translate actually contain a dead read. If you accept the `load_always` approach here, I can send follow-ups that apply it (or whatever you decide) to `cpu_chained_aot.read_word` and the `load` in `ffx_version_query.INTEGER_HEADER`, including the x87 loads, each with a fixture like `dead_read`.
bosseurdinternet commented 2026-11-24 06:48:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T05:56:56Z.


Rebased onto main at 86dff9f now that #321 has landed; one commit, 4dae96e.

Reconciliation.

  • tools/cpu_switch_aot.py: the conflict was in the shared header. load stays, because main's frame code reads the function's own frame array with it, and a read of that local array cannot fault. Every other memory operand goes through load_always, including the image-data operands of #416. One line was added for this: a frame operand keeps load, so the C++ of frame accesses is the same as on main.
  • tools/form_coverage.py: both entries kept.

Rerun on 4dae96e.

  • CI: both Windows jobs pass, and cpu_aot_dead_reads passes in each. The /O2 job is the one that exercises the behaviour: it builds the translation optimized.
  • Locally (MSVC 14.52.37025 under Wine), the fixture translated by main (86dff9f) and by this branch, each compiled with cl /O2 /LD /W4, same check: main fails with translated read_dead with a null operand did not fault; this branch passes.
  • Local Debug run: cpu_aot_dead_reads, cpu_aot_jump_tables, cpu_aot_flag_conditions, cpu_aot_stack_frames, cpu_aot_direct_calls, cpu_aot_bound_callees, cpu_aot_image_data, cpu_aot_wide_arithmetic and cpu_aot_dead_divides pass. cpu_native_archive_reader and cpu_native_archive_blast could not be built here because my local Python lacks the ziglang module; they pass in CI.
  • Corpus against main: the same 655, 651 and 400 functions translate at /O2, /O1 and /Od, with no status or message change. Over every object, each translation equals main's once load_always< is read as load< (1,100, 1,096 and 849 translations; 396, 398 and 278 of them contain a guest read and so change). No corpus function has a CMP or TEST on memory with unused flags.
  • Differential execution of everything translated at /O2, callees compiled in: 652 checked with no mismatch on this branch and on main alike (one faults on both sides for every input, two are excluded by name).

@grahamster2 thanks for the survey of the other translators. This PR stays limited to cpu_switch_aot.py; cpu_chained_aot.py and ffx_version_query.lower are separate changes.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7075039090 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/437#issuecomment-7075039090). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-22T05:56:56Z. --- Rebased onto `main` at `86dff9f` now that #321 has landed; one commit, `4dae96e`. **Reconciliation.** - `tools/cpu_switch_aot.py`: the conflict was in the shared header. `load` stays, because `main`'s frame code reads the function's own frame array with it, and a read of that local array cannot fault. Every other memory operand goes through `load_always`, including the image-data operands of #416. One line was added for this: a frame operand keeps `load`, so the C++ of frame accesses is the same as on `main`. - `tools/form_coverage.py`: both entries kept. **Rerun on `4dae96e`.** - CI: both Windows jobs pass, and `cpu_aot_dead_reads` passes in each. The `/O2` job is the one that exercises the behaviour: it builds the translation optimized. - Locally (MSVC 14.52.37025 under Wine), the fixture translated by `main` (`86dff9f`) and by this branch, each compiled with `cl /O2 /LD /W4`, same check: `main` fails with `translated read_dead with a null operand did not fault`; this branch passes. - Local Debug run: `cpu_aot_dead_reads`, `cpu_aot_jump_tables`, `cpu_aot_flag_conditions`, `cpu_aot_stack_frames`, `cpu_aot_direct_calls`, `cpu_aot_bound_callees`, `cpu_aot_image_data`, `cpu_aot_wide_arithmetic` and `cpu_aot_dead_divides` pass. `cpu_native_archive_reader` and `cpu_native_archive_blast` could not be built here because my local Python lacks the `ziglang` module; they pass in CI. - Corpus against `main`: the same 655, 651 and 400 functions translate at `/O2`, `/O1` and `/Od`, with no status or message change. Over every object, each translation equals `main`'s once `load_always<` is read as `load<` (1,100, 1,096 and 849 translations; 396, 398 and 278 of them contain a guest read and so change). No corpus function has a `CMP` or `TEST` on memory with unused flags. - Differential execution of everything translated at `/O2`, callees compiled in: 652 checked with no mismatch on this branch and on `main` alike (one faults on both sides for every input, two are excluded by name). @grahamster2 thanks for the survey of the other translators. This PR stays limited to `cpu_switch_aot.py`; `cpu_chained_aot.py` and `ffx_version_query.lower` are separate changes. _AI-assisted (Claude Code)._
R0Y00 commented 2026-11-24 06:48:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T06:08:09Z.


For cpu_switch_aot, an executed guest memory read must retain its fault behavior even when the value or the CMP/TEST flags are unused. Evaluate each source memory operand once, at its original width and in guest instruction order. A private frame read may be optimized when it is provably valid and has no externally visible effect. On 4dae96e the PR description still identifies instructions that read the same operand twice for flags and result. Please capture the value once and check the generated code reuses it for both. For Windows acceptance, run the native and AOT versions independently with fresh trace words and an exception boundary around each call: write the first marker, perform the faulting read, then write the second marker. Compare the exception code and verify only the first write happened. Run the same inaccessible address on a branch that skips the read and verify normal return and the expected writes. Keep the readable, null and cross-page width controls and run the revised fixture in Debug and /O2.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7075147991 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/437#issuecomment-7075147991). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T06:08:09Z. --- For cpu_switch_aot, an executed guest memory read must retain its fault behavior even when the value or the CMP/TEST flags are unused. Evaluate each source memory operand once, at its original width and in guest instruction order. A private frame read may be optimized when it is provably valid and has no externally visible effect. On 4dae96e the PR description still identifies instructions that read the same operand twice for flags and result. Please capture the value once and check the generated code reuses it for both. For Windows acceptance, run the native and AOT versions independently with fresh trace words and an exception boundary around each call: write the first marker, perform the faulting read, then write the second marker. Compare the exception code and verify only the first write happened. Run the same inaccessible address on a branch that skips the read and verify normal return and the expected writes. Keep the readable, null and cross-page width controls and run the revised fixture in Debug and /O2.
bosseurdinternet commented 2026-11-24 06:48:27 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T06:48:53Z.


Done in 4a6b06a, on top of 4dae96e.

One read per operand. Each instruction's memory operand is now read once, at its own width, into a const local declared before the rest of the statement, and every use of the operand names that local:

{ const std::uint64_t mem = load_always<std::uint64_t>((rcx)); fa = static_cast<std::uint64_t>(rax); fb = mem; rax = static_cast<std::uint64_t>(rax + mem); }
  • The local is declared first, so the read precedes the instruction's writes, and statements stay in guest instruction order.
  • A read whose value nothing uses (CMP or TEST without a flag consumer) becomes the declaration plus static_cast<void>(mem);.
  • A CMOVcc memory source uses the same local, read before the condition is evaluated.
  • A read of the function's own frame array keeps the ordinary load and is not captured.
  • Lifter.statement() applies the capture itself. tests/cpu/archive/translate_blast.py calls it directly, and its translation is unchanged in how it is produced; cpu_native_archive_blast passes in CI.

Revised fixture and test. ctest --preset dev --no-tests=error -R '^cpu_aot_dead_reads$' -V, and the same with --preset release.

  • Every export stores 1 to the trace before its read and 2 after it. New exports: read_skip (test rdx, rdx; jz around the read, then a third marker on both paths), sum_zero (add rax, [rcx]; sete r9b: result and flag from one read) and add_in_place (add [rcx], rdx; sete al: one read, one write).
  • The translation is built by the preset's build and again by a separate cl /O2 /W4 /LD command. The original and each translation run independently through guarded_call (__try, GetExceptionCode()), each call with fresh trace words and a freshly filled page.
  • Compared per call: exception code, return value, the three trace words and the whole page that was read. The original must give the expected outcome; both translations must give what the original gives.
  • Cases, one printed line each (47):
    • readable operand, two values of y, the second making the sum wrap to zero so both flag values occur;
    • null operand: 0xC0000005 after the first marker and before the second, for all eight exports;
    • cross-page: byte_dead 1 byte before an inaccessible page returns and read_dead there faults; read_dead returns at 8; cmp_dead returns at 4 and faults at 3; pair_dead returns at 16 and faults at 8; read_live faults at 7; sum_zero returns at 8 and faults at 7; add_in_place returns at 8 and faults at 4 with the page unchanged;
    • read_skip with a null operand and with an operand inside the inaccessible page: y = 0 returns normally with markers 1 and 3; y = 7 faults after marker 1.
  • Source control: each memory operand is read once through load_always, and for sum_zero and add_in_place the captured value is used twice.

Results.

  • CI on 4a6b06a: both Windows jobs pass (173 of 173 each), so the test ran with the Debug and the /O2 preset builds, each also against the separate /O2 build.
  • Locally (MSVC 14.52.37025 under Wine): the test passes. The same check against main's translation of this fixture, built /Od and /O2, fails: /O2 translation read_dead, null operand: code 0x0, 5, trace (1, 2, …) where the original raises 0xC0000005 with only the first marker.
  • Corpus against main: the same 655, 651 and 400 functions translate, with no status or message change. Writing each captured read back inline gives main's text for every translation (1,100 of 1,100 at /O2, 849 of 849 at /Od, 1,095 of 1,096 at /O1; the one other is a CMOVcc memory source, whose local was named source before).
  • Differential execution with callees compiled in: 652 functions at /O2 and 400 at /Od checked with no mismatch.
  • pyflakes, tests/docs_links_check.py and tests/form_coverage_check.py passed.

Not covered: stores are unchanged; under Wine and on Windows the access violation code is the same, so nothing here depends on the host.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7075545110 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/437#issuecomment-7075545110). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-22T06:48:53Z. --- Done in `4a6b06a`, on top of `4dae96e`. **One read per operand.** Each instruction's memory operand is now read once, at its own width, into a `const` local declared before the rest of the statement, and every use of the operand names that local: ```cpp { const std::uint64_t mem = load_always<std::uint64_t>((rcx)); fa = static_cast<std::uint64_t>(rax); fb = mem; rax = static_cast<std::uint64_t>(rax + mem); } ``` - The local is declared first, so the read precedes the instruction's writes, and statements stay in guest instruction order. - A read whose value nothing uses (`CMP` or `TEST` without a flag consumer) becomes the declaration plus `static_cast<void>(mem);`. - A `CMOVcc` memory source uses the same local, read before the condition is evaluated. - A read of the function's own frame array keeps the ordinary `load` and is not captured. - `Lifter.statement()` applies the capture itself. `tests/cpu/archive/translate_blast.py` calls it directly, and its translation is unchanged in how it is produced; `cpu_native_archive_blast` passes in CI. **Revised fixture and test.** `ctest --preset dev --no-tests=error -R '^cpu_aot_dead_reads$' -V`, and the same with `--preset release`. - Every export stores 1 to the trace before its read and 2 after it. New exports: `read_skip` (`test rdx, rdx; jz` around the read, then a third marker on both paths), `sum_zero` (`add rax, [rcx]; sete r9b`: result and flag from one read) and `add_in_place` (`add [rcx], rdx; sete al`: one read, one write). - The translation is built by the preset's build and again by a separate `cl /O2 /W4 /LD` command. The original and each translation run independently through `guarded_call` (`__try`, `GetExceptionCode()`), each call with fresh trace words and a freshly filled page. - Compared per call: exception code, return value, the three trace words and the whole page that was read. The original must give the expected outcome; both translations must give what the original gives. - Cases, one printed line each (47): - readable operand, two values of `y`, the second making the sum wrap to zero so both flag values occur; - null operand: `0xC0000005` after the first marker and before the second, for all eight exports; - cross-page: `byte_dead` 1 byte before an inaccessible page returns and `read_dead` there faults; `read_dead` returns at 8; `cmp_dead` returns at 4 and faults at 3; `pair_dead` returns at 16 and faults at 8; `read_live` faults at 7; `sum_zero` returns at 8 and faults at 7; `add_in_place` returns at 8 and faults at 4 with the page unchanged; - `read_skip` with a null operand and with an operand inside the inaccessible page: `y = 0` returns normally with markers 1 and 3; `y = 7` faults after marker 1. - Source control: each memory operand is read once through `load_always`, and for `sum_zero` and `add_in_place` the captured value is used twice. **Results.** - CI on `4a6b06a`: both Windows jobs pass (173 of 173 each), so the test ran with the Debug and the `/O2` preset builds, each also against the separate `/O2` build. - Locally (MSVC 14.52.37025 under Wine): the test passes. The same check against `main`'s translation of this fixture, built `/Od` and `/O2`, fails: `/O2 translation read_dead, null operand: code 0x0, 5, trace (1, 2, …)` where the original raises `0xC0000005` with only the first marker. - Corpus against `main`: the same 655, 651 and 400 functions translate, with no status or message change. Writing each captured read back inline gives `main`'s text for every translation (1,100 of 1,100 at `/O2`, 849 of 849 at `/Od`, 1,095 of 1,096 at `/O1`; the one other is a `CMOVcc` memory source, whose local was named `source` before). - Differential execution with callees compiled in: 652 functions at `/O2` and 400 at `/Od` checked with no mismatch. - `pyflakes`, `tests/docs_links_check.py` and `tests/form_coverage_check.py` passed. **Not covered:** stores are unchanged; under Wine and on Windows the access violation code is the same, so nothing here depends on the host. _AI-assisted (Claude Code)._
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
R0Y00/Xbox2Windows!431
No description provided.