cpu: keep memory reads whose value is unused in the shared lowering #660
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/666"
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-22T18:30:23Z.
Problem
#437 made
cpu_switch_aotkeep every executed guest memory read, following the rule on that PR: an executed read keeps its fault behaviour even when the value or theCMP/TESTflags are unused, each source memory operand is evaluated once, at its own width, in guest order, and a private frame read may stay optimizable when it is provably valid. The survey on #437 found the same bug in the shared integer lowering,ffx_version_query.lower, whichcpu_exe_aot,bink_*,ffx_*and the x87 profile use. Itsload<T>is amemcpy. At/O2the compiler drops a load whose value is overwritten, or whoseCMP/TESTflags nothing reads, so a null or unmapped operand returns where the original raises an access violation. (CMOVccwith a memory source already used a volatile read onmain.)Change
tools/ffx_version_query.py, following #437's emitted shape:const T mem = load_always<T>(addr);, declared before the rest of the statement. Every use namesmem, so the read comes before any write of the instruction. A read whose value nothing uses becomes the declaration plusstatic_cast<void>(mem);: This applies tooperand()(MOV/MOVZX/MOVSX,CMP,TEST, ALU and read-modify-write forms,CMOVcc, shifts and rotates, BMI,MUL/DIV,CRC32,PINSR*, ...), toXADD/CMPXCHG, and to the SSE/AVX sources that read guest memory withmemcpyorload<>(VMOVQ/VMOVSD,MOVDQU,VMOVDQU,VMOVUPS, SHA, AES/PCLMUL, SSE2 saturating/multiply, SSE4.1,PMOVSX/ZX,VPBROADCAST*,VPSLLV*, scalar FMA, and the AVX2 forms that already usedload_alwaysinline).ADD [mem], regperforms one read and then its store. TheCMOVccspecial case is removed:operand()reads its source before the condition is evaluated.load_alwaysis now one volatile access at the operand's width for arithmetic types, so a 4-byte operand is one 4-byte read. Vector types (std::array,__m128i) keep the byte-by-byte volatile loop. It faults on exactly the addresses a single access would, with no write in between.MOVS,LODS,SCAS,CMPS) read each element throughload_alwaysin every iteration.XLATBdoes too.tools/x87_lower.py:statement()takes the read template, and the integer lowering passesload_alwaysfor x87 memory operands (FLD,FILD, the arithmetic and compare forms,FLDCW).[rsp + d]with no index that lies inside the function's ownword frame[16]stays an ordinaryload. This is allowed only when no instruction outside the host replacements names RSP as a register and no initial line, binding or exit mentionsrsp, so RSP points at the array throughout.cpu_exe_aot's operands ([rsp+32..72), with itsSUB/ADD RSP, 72replaced by host ops) are exactly this case, and its generated C++ keepsload<.tests/cpu/lower_dead_read.{asm,def,py}andcpu_aot_lower_dead_reads(CMake) are a new fixture modelled oncpu_aot_dead_reads. Every export stores 1 to a trace word before its read and 2 after it:read_deadoverwrites the value it loads;cmp_deadandtest_deadread memory whose flags nothing reads;cmov_deadis aCMOVNEthat never moves;add_in_placeisadd dword ptr [rcx], edxfollowed bysete;read_liveis the control;fld_deadis an x87fld qword ptr [rcx]followed byfstp st(0).The translation is built by the preset's build and again by a separate
cl /O2 /W4 /EHsc /LDcommand. The original and both translations run throughguarded_call(__try/GetExceptionCode()), each with fresh trace words and a fresh page. The test compares the exception code, RAX, the trace and the page. A source control checks that each export has exactly oneload_always<T>at the operand's width and noload<. The asm declares; coverage: source cpu_aot_lower_dead_reads, soform_coverage.pyneeds no entry.Updated control regexes: the mutation controls in
string_ops.py,xlatb.py,x87_iarith.pyandx87_integers.pymatch the generated text, so they now matchload_always<. The x87 ones accept eitherloadorload_always. The controls still mutate the same sites, and each still has to disagree with the reference.docs/experiments.mddescribes the rule and the fixture next to theCMOVcc/SETccparagraph of the shared lowering.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 (pefile 2024.8.26, iced-x86 1.21.0). Commit
3be2e29on main2bb9ee0.Before and after. I emitted the new fixture with
main'stools/(2bb9ee0) and with this branch, then compiled each/Odand/O2(cl /nologo /EHsc /LD). Each export was called with a null RCX underguarded_call. Trace1,0means only the first marker was written./Od/O2/O2read_dead(overwrittenMOVload)0xc0000005, trace 1,00xc00000050xc0000005, 1,00xc0000005, 1,0cmp_dead(CMP m32, 0, flags unused)0xc00000050xc00000050xc00000050xc0000005test_dead(TEST m8, 1, flags unused)0xc00000050xc00000050xc00000050xc0000005cmov_dead(non-movingCMOVNE)0xc00000050xc00000050xc0000005(already volatile on main)0xc00000050xc0000005add_in_place(ADD m32, r32)0xc00000050xc00000050xc00000050xc00000050xc0000005read_live(control)0xc00000050xc00000050xc00000050xc00000050xc0000005fld_dead(x87 load popped unused)0xc00000050xc00000050xc00000050xc00000050xc0000005On
mainthe/O2check fails at its first dead case:AssertionError: /O2 translation read_dead, null operand: code 0x0, 5, trace (1, 2, 18446744073709551615); original code 0xc0000005, None, trace (1, 18446744073709551615, 18446744073709551615). The source control also fails onmain, because the reads areload<. Onmain,fld_deadkeeps its read only because cl does not inlinex87_from_f64.add_in_placekeeps its read because the value is stored. With this change both reads are explicit volatile reads.The new test on this branch:
ctest --preset dev --no-tests=error -R '^cpu_aot_lower_dead_reads$' -Vpasses, with 7 source-control lines and 41 comparisons. Every export is tested with:y, the second wrappingadd_in_placeto zero sosetegives 1;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 ...: 289 of 290 passed. The one failure wasdocs_links, which ran before the new files were staged in git (it requires linked files to be tracked); rerun aftergit addit passes. 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, including the same 3 capability skips.pyflakeson the changed files: clean.tests/docs_links_check.py,tests/form_coverage_check.pyandtests/bidi_controls_check.py: pass.Generated C++ change. In the Debug build, 193 generated sources come from
lower(word frame[16]). 83 of them now contain 1,631 captured reads (const T mem = load_always<T>(...)). What changed in the emitted text:memlocal, ahead of the statement;memcpy/load<>reads of guest memory becameload_always;std::arraylocal;load_always.What did not change: the values computed, the stores, the control flow, and
cpu_exe_aot's frame reads (cpu_exe_*_lifted.cppkeep their oneload<and gain noload_always). I did not run #437's corpus or image differential. The hash-pinnedbink_*/ffx_*translations build and pass their existing tests, but I did not check whether those images contain a dead read.Evidence and follow-up
The fixture is source-built MASM in this PR (MIT), with no external data. Assumption: an access violation raised by the byte-wise vector read has the same code and address set as one raised by a single wide access, and no store happens in between.
cpu_chained_aot.read_wordhas the same bug and is a separate follow-up PR.Checklist
Refs #437
AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T19:10:59Z.
Verification of
3be2e29on an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029,devpreset. It merges cleanly withmain.I also checked that the change is pinned. I edited
tools/ffx_version_query.py, rebuilt, and ran allcpu_tests. Afterwards I restored the file, verified its hash, and confirmed the tests passed again.load_alwaysreads throughconst T*instead ofconst volatile T*cpu_test fails (detected)*reinterpret_cast<const T*>(rsi)instead ofload_alwaysThe second mutation stops the build in
tests/cpu/string_ops.py emitwithcontrol ... changed nothing. That existing memmove control builds its mutant by replacing the exactload_alwaystext in the MOVS step. This is a fragile coupling rather than a detection: any rewording of that line breaks the build before a test runs. So I could not judge the MOVS path this way. The scalar path is covered.AI-assisted: run and summarized with Claude Code; outputs copied from the local runs.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T02:30:58Z.
The scalar
/O2mutation provides a useful regression check. The patch also changes vector loads, for whichload_alwaysemits multiple volatile byte accesses. Please add an executed, overwritten 16-byte load to the native/AOT comparison, with a successful read and a page-crossing failure, and document the access granularity the vector path actually provides. For MOVS, the independent mutation stopped while generating the memmove control. Please add a runtime discriminator for the read or adjust the control so the mutation gets to the test run. Keep the scalar and branch controls when resolving the current main conflict.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T14:37:26Z.
I resolved the shared lowerer conflicts after #602. The guest MOVDQU result can be overwritten without deleting its 16-byte read. The REP MOVSB controls cover zero count, a successful copy and a fault on the second source read after the first byte was copied. CMPXCHG16B retains the native alignment rejection and the full-width read. The mixed candidate passed the seven focused CTests for dead reads, RCX branches, string operations, CMPXCHG, shuffles, SSE3/4.1 and x87 examination. Windows CI is rerunning.