cpu: keep memory reads whose value is unused in the chained-range translator #668
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/674"
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 grahamster2 on 2026-11-22T19:06:01Z.
Problem
#437 made
cpu_switch_aotkeep every executed guest memory read. It follows the rule set on that PR: an executed read keeps its fault behaviour even when its value is unused, and each source memory operand is evaluated once, at its own width, in guest order. A private frame read may stay optimizable when it is provably valid.The survey on #437 found the same bug in
cpu_chained_aot. ItsMOV r64, [reg + disp]load becomesconst word load_X = read_word(addr);, andread_wordis amemcpy. When nothing uses the loaded value,/O2drops the read. A null or unmapped operand then returns status 1 where the original raises an access violation.Change
tools/cpu_chained_aot.py:MOVload is emitted asconst word load_X = load_always<word>(addr);.load_alwaysis #437's singlevolatileaccess at the operand's width (*reinterpret_cast<const volatile T*>(address)).static_cast<void>(load_X);on the same line, as in #437'sCMP/TESTcase.read_wordstays only for the binding checks of--callback-offsetand--ffx-destroy, where the table or allocator word is always compared. It is emitted only in those two modes, which removes the C4505 warning from plain translations.[rsp + n]home and frame slots), so there is no private frame read to exempt.New fixture
tests/cpu/chained_dead_read.{asm,def}(first line; coverage: source cpu_aot_chained_dead_reads) and testtests/cpu/chained_dead_read_check.py, registered ascpu_aot_chained_dead_reads. The translator lifts one entry per call, so each export is translated separately: once by the preset's build, and once by a separatecl /O2 /W4 /LDcommand, as #437 does. The exports are:read_dead:mov rax, [rcx], then the value is overwritten bymov rax, rdx.disp_dead:mov r8, [rcx + 8], into a register nothing reads.pair_dead: two loads,[rcx]and[rcx + 8], neither used.read_skip:test rdx, rdx; jzaround a dead load.read_live: the control, which returns the value it reads.The check calls the original and both translations through
guarded_call(__try/GetExceptionCode()). Each call gets a fresh page that is followed by aPAGE_NOACCESSpage, and fresh guard words around the translation's output word. It compares the exception code and the result.Not covered: the instruction subset has no
CMP/TESTon memory, noCMOVccand no read-modify-write, so the fixture has none. Those shapes are in #437 and #666. The translation writes no memory, so this fixture uses no trace markers; ordering is checked by the single declaration per operand.docs/experiments.md: a paragraph and the command under "Chained function ranges and early returns".Evidence
Environment: Windows 11 Pro build 26200, VS Build Tools 2026 18.3 / MSVC 19.50.35725, ML64, Windows SDK 10.0.26100, Python 3.12. Commit
308e5daon main2bb9ee0.Before and after. I translated the fixture with
main'stools/cpu_chained_aot.pyand with this branch, and compiled each export with/Odand/O2. Each export was called with a null RCX underguarded_call:/Od/O2/O2read_dead(overwritten load)0xc00000050xc00000050xc00000050xc0000005disp_dead([rcx+8], unused)0xc00000050xc00000050xc00000050xc0000005pair_dead(two unused loads)0xc00000050xc00000050xc00000050xc0000005read_skip(y = 5, read taken)0xc00000050xc00000050xc00000050xc0000005read_live(control)0xc00000050xc00000050xc00000050xc00000050xc0000005Run against
main's translations, the new check fails at the first dead case:AssertionError: /O2 translation read_dead, null operand: code 0x0, 5; original code 0xc0000005, None. Its source control also fails onmain, because the loads areread_word.The new test on this branch.
ctest --preset dev --no-tests=error -R '^cpu_aot_chained_dead_reads$' -Vpasses, with 5 source-control lines and 29 comparisons. Every export is tested with:Two further cases:
pair_deadwith its second operand entirely in the inaccessible page faults.read_skipwith a null operand or an operand inside the inaccessible page returns normally when y = 0, and faults when y = 7.The same test passes under
--preset release.Full suites, serial, with
-E '^(conditional_import_paths|static_exe_intake|bundled_export_forwarders)$':cmake --preset dev -DINTAKE_PYTHON=C:/Users/graha/src/Xbox2Windows/out/pe-env/Scripts/python.exe,cmake --build --preset dev,ctest --preset dev --no-tests=error -E ...: 100%, 290 of 290 passed, with 3 capability skips (bvh_wave64_hardware,bvh_wave64_warp,wave_shuffle_warp).cmake --preset release -DINTAKE_PYTHON=...,cmake --build --preset release,ctest --preset release --no-tests=error -E ...: 100%, 290 of 290 passed, with the same 3 capability skips.pyflakeson the changed Python files,tests/docs_links_check.py,tests/form_coverage_check.py: clean / pass.Generated C++ change. I regenerated the seven existing
cpu_chained_aottranslations (cpu_chained,_jne,_zero,_imm,_frame,cpu_callbackandcpu_destroy) withmainand with this branch and diffed them.read_word(becomingload_always<word>(on theMOVloads: one load in eachchoose_valuevariant, two incpu_callbackand three incpu_destroy.load_alwaystemplate. It keepsread_wordonly forcpu_callbackandcpu_destroy, whose binding checks still use it.static_cast<void>, because each of their loads is used.cpu_aot_chained_ranges,cpu_aot_object_callbackandcpu_aot_destroy_contractpass unchanged.Evidence and follow-up
The fixture is source-built MASM in this PR (MIT), with no external data. I did not check whether the local FidelityFX loader translations contain a dead load. The shared lowering (
ffx_version_query.lower) is #666.Checklist
Refs #437
AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T19:20:42Z.
Verified
308e5daon an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029,devpreset. It merges cleanly withmainatff7f68a.ctest --preset dev --no-tests=error -R "^cpu_" -j 4: 100% tests passed, 0 failed out of 119. This is the chained-range counterpart of #666, which I checked on the same host.AI-assisted (Claude Code).