cpu: keep memory reads whose value is unused in the jump-table translator #431
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/437"
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 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.pyloses such a read in two ways:load<T>, amemcpy. An optimizing host compiler removes it when the value is dead, for examplemov rax, [rcx]followed bymov rax, rdx. The docs already describe this for aCMOVccmemory source, which is read through avolatileload for that reason.CMPorTESTwith 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.
load_always.loadis no longer emitted and is removed from the shared header.CMPorTESTwith a memory operand and no flag consumer emits the read alone, asstatic_cast<void>(load_always<T>(address));.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 threemaincommits 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), aCMPwhose 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).byte_deadreturns andread_deadfaults;cmp_deadreturns at four bytes and faults at three), andpair_deadfaults when only its first read is readable.load_alwaysonly.read_dead,byte_deadandpair_deadpass with or without this change; there, only the source control guards item 1. Item 2 is different: with theCMPbranch absent, the generated source ofcmp_deadcontains 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.main(992306b) and with this change, compiled both withcl /O2 /LD /W4and ran the same check:main: fails,translated read_dead with a null operand did not fault;/O2,/O1and/Od), againstmain: the same functions translate (642, 639 and 15), no status or message changes, and across every object including library code each translation is identical tomain's onceload_always<is read asload<(1,064, 1,061 and 441 translations). No corpus function contains aCMPorTESTof item 2./O2, up to 64 inputs each: 639 of the 642 checked with no mismatch, as onmain(one faults on both sides for every input, two are excluded by name)./Odcorpus 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.cpu_aot_jump_tables(which holds theCMOVcccontrol),cpu_aot_switch_addr32,cpu_aot_entry_order,cpu_aot_narrow_returnsandcpu_aot_fourth_argumentpass. Othercpu_*tests: same results asmain; under Wine,cpu_aot_import_call,cpu_aot_ordinal_import,cpu_native_reloc_freeandcpu_native_tlsfail onmaintoo, andcpu_native_exe_starttimes out intermittently (#281).pyflakes,tests/docs_links_check.pyandtests/form_coverage_check.pypassed.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.asmis original MASM assembled in the build. No binaries were added.dead_readis classified intools/form_coverage.py.CMOVccsource 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.load. A frame is a local array and cannot fault, so those reads can stay ordinary; I will keeploadthere when one of the two is rebased over the other.AI-assisted (Claude Code).
🤖 Generated with Claude Code
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:/O2cpu_chained_aot.pyread_word(memcpy)ffx_version_query.lower(used bycpu_exe_aot,bink_*,ffx_*, the x87 profile)load<T>(memcpy)CMP/TESTon memory, aCMOVccthat does not movex87_lower.py(throughlower(x87=True))load<T>x87_from_f64, which cl does not inline)cpu_leaf_aot.py,cpu_call_aot.pycpu_exe_aot.pylower[rsp+32..72), the virtual frame, which cannot faultReproducers, source-built MASM with
PROC FRAME/.endprolog, linked likecpu_asm_fixture:Each was called with a null RCX in a separate process. All five dead cases (
ch_read_dead, andmov,cmp,test,cmovnethroughlower) fault natively and return from thecl /O2translation. The live controls (mov rax, [rcx]; ret) fault on both sides. In the/O2listings the read is gone:try_liftedismov [r8], rdx; mov eax, 1; ret, the threelowercases aremov rax, rdx; ret, andlw_cmov_deadisxor eax, eax; ret.I only tested source-built fixtures. I did not check whether any of the hash-pinned images that
bink_*andffx_*translate actually contain a dead read.If you accept the
load_alwaysapproach here, I can send follow-ups that apply it (or whatever you decide) tocpu_chained_aot.read_wordand theloadinffx_version_query.INTEGER_HEADER, including the x87 loads, each with a fixture likedead_read.Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T05:56:56Z.
Rebased onto
mainat86dff9fnow that #321 has landed; one commit,4dae96e.Reconciliation.
tools/cpu_switch_aot.py: the conflict was in the shared header.loadstays, becausemain'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 throughload_always, including the image-data operands of #416. One line was added for this: a frame operand keepsload, so the C++ of frame accesses is the same as onmain.tools/form_coverage.py: both entries kept.Rerun on
4dae96e.cpu_aot_dead_readspasses in each. The/O2job is the one that exercises the behaviour: it builds the translation optimized.main(86dff9f) and by this branch, each compiled withcl /O2 /LD /W4, same check:mainfails withtranslated read_dead with a null operand did not fault; this branch passes.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_arithmeticandcpu_aot_dead_dividespass.cpu_native_archive_readerandcpu_native_archive_blastcould not be built here because my local Python lacks theziglangmodule; they pass in CI.main: the same 655, 651 and 400 functions translate at/O2,/O1and/Od, with no status or message change. Over every object, each translation equalsmain's onceload_always<is read asload<(1,100, 1,096 and 849 translations; 396, 398 and 278 of them contain a guest read and so change). No corpus function has aCMPorTESTon memory with unused flags./O2, callees compiled in: 652 checked with no mismatch on this branch and onmainalike (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.pyandffx_version_query.lowerare separate changes.AI-assisted (Claude Code).
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
4dae96ethe 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.Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T06:48:53Z.
Done in
4a6b06a, on top of4dae96e.One read per operand. Each instruction's memory operand is now read once, at its own width, into a
constlocal declared before the rest of the statement, and every use of the operand names that local:CMPorTESTwithout a flag consumer) becomes the declaration plusstatic_cast<void>(mem);.CMOVccmemory source uses the same local, read before the condition is evaluated.loadand is not captured.Lifter.statement()applies the capture itself.tests/cpu/archive/translate_blast.pycalls it directly, and its translation is unchanged in how it is produced;cpu_native_archive_blastpasses in CI.Revised fixture and test.
ctest --preset dev --no-tests=error -R '^cpu_aot_dead_reads$' -V, and the same with--preset release.read_skip(test rdx, rdx; jzaround the read, then a third marker on both paths),sum_zero(add rax, [rcx]; sete r9b: result and flag from one read) andadd_in_place(add [rcx], rdx; sete al: one read, one write).cl /O2 /W4 /LDcommand. The original and each translation run independently throughguarded_call(__try,GetExceptionCode()), each call with fresh trace words and a freshly filled page.y, the second making the sum wrap to zero so both flag values occur;0xC0000005after the first marker and before the second, for all eight exports;byte_dead1 byte before an inaccessible page returns andread_deadthere faults;read_deadreturns at 8;cmp_deadreturns at 4 and faults at 3;pair_deadreturns at 16 and faults at 8;read_livefaults at 7;sum_zeroreturns at 8 and faults at 7;add_in_placereturns at 8 and faults at 4 with the page unchanged;read_skipwith a null operand and with an operand inside the inaccessible page:y = 0returns normally with markers 1 and 3;y = 7faults after marker 1.load_always, and forsum_zeroandadd_in_placethe captured value is used twice.Results.
4a6b06a: both Windows jobs pass (173 of 173 each), so the test ran with the Debug and the/O2preset builds, each also against the separate/O2build.main's translation of this fixture, built/Odand/O2, fails:/O2 translation read_dead, null operand: code 0x0, 5, trace (1, 2, …)where the original raises0xC0000005with only the first marker.main: the same 655, 651 and 400 functions translate, with no status or message change. Writing each captured read back inline givesmain'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 aCMOVccmemory source, whose local was namedsourcebefore)./O2and 400 at/Odchecked with no mismatch.pyflakes,tests/docs_links_check.pyandtests/form_coverage_check.pypassed.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).