Console CPU instructions: the CLZERO loop is decoded and run as the CPU does it (#278) #295
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!295
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/295/head"
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?
@TG0000 on GitHub · 2026-11-23 22:35 UTC · original
TG0000:fix/clzero-loop-decode→main· +224 −10 · 2 files · 1 commits · closed without mergingPart of #278 (items 1 and 2). This fixes how the CLZERO loop (
clzero; add rax, 0x40; cmp rax, <reg>; jb -12) is decoded and emulated on CPUs without CLZERO.is_writable(item 3) and the CLWB hot-patch race (item 4) will come in separate PRs.What is in it (one commit)
crates/xcl/src/insn.rs, decoder: the loop is now recognised only when the ModRM is mod 3 with rax in r/m (modrm & 0xC7 == 0xC0, no REX.B). Before,cmp rdx, rcx,cmp [rax], rdxand similar were taken for the loop.cmp rax, raxnever loops, so it is left to the plainClzero.crates/xcl/src/insn.rs, newclzero_loop(rax, end, max_lines)andcmp_flags: they compute what the CPU does. That means:end(unsigned);cmp.Nonewhen rax would wrap past the top of the address space.crates/xcl/src/eh.rs,ClzeroLoopemulation: it now usesclzero_loop, so the old bugs are gone:[rax, end)and setsrax = end;end <= rax;Tests (new)
insn::tests::clzero_loop_needs_cmp_rax_with_a_register: covers everycmp rax, regfor regs 1–15 and r8, plus the encodings that must not be read as the loop:cmp rax, rax, swapped registers, memory compares with and without displacement, and REX.B.insn::tests::cmp_flags_match_the_cpu: CF/PF/AF/ZF/SF/OF on hand-checked cases.insn::tests::clzero_loop_runs_like_the_cpu: covers an unaligned start, an end that is not a whole number of lines away, an end at or below rax, a run cut bymax_lines, wraparound, and a sweep checked against the loop stepped one iteration at a time.eh::tests::clzero_loop_zeroes_whole_lines_as_the_cpu: callsemulate_console_onlyon real memory. It checks the zeroed span, the final rax, rip and EFLAGS. It also covers a span across two committed regions (one line per fault until the rest fits in one region) and a span running into decommitted memory (the lines before stay zeroed, then the fault goes to the title).Tested
stable-x86_64-pc-windows-gnu(llvm-mingw clang), debug.cargo test --workspace --exclude testgame-mw --no-fail-fast -- --test-threads=4, run in this order:SKIPPED ...: no DXC(no DXC in this environment, so GPU tests did not run)main(9ab7d76) in the same environment, and neither comes from this change:pe::tests::parses_own_test_binary_tls("rust binaries have a TLS directory") fails because GNU-target binaries have no TLS directory.test_title_runs_and_game_folder_is_untouchedfails becausetestgame_mw.dllis not built whentestgame-mwis excluded.cargo build -p xcl --lib: no new warnings. The only warning is the existingfetch_updatedeprecation, whichmainhas too.Regression run
Windows 11, Ryzen 9 5900X, AMD Radeon RX 6800, Rust stable
x86_64-pc-windows-msvc(VS 2022 Build Tools, link.exe),cargo build --release -p xcl,scripts/smoke.ps1 -Seconds 45, one run at a time under a RAM watchdog.main (
9ab7d76, this PR's merge base):PR (head commit, no local changes):
No regression: 826 frames against 843 on its base, no device removals or crashes. The 5900X has CLZERO natively, so the emulation is not reached here. This branch conflicts with current
mainineh.rsandinsn.rsand needs a rebase. Currentmain(2b4bb43) on the same title, for reference: 709 frames in 45 s, 0 device removals, 0 crashes, peak commit 15183 MB.Not tested
AI use
🤖 Generated with Claude Code
@mastrr9 on GitHub · 2026-11-23 22:52 UTC · original
Independent static code & x86-64 ISA audit of
3ad409465(code inspection only —cargo testnot run on macOS; no game files):Summary
(Note: overlaps with #290, which also addresses #278.)
cmp_flags(insn.rs:68-77): Verified all 6 arithmetic flags (CF,PF,AF,ZF,SF,OF) against x86-64cmp a, b(a - b), includingAFvia(a ^ b ^ r) & 0x10 != 0andOFvia((a ^ b) & (a ^ r)) >> 63 != 0.insn.rs:161-172,eh.rs:327-345):code[i + 9] & 0xC7 == 0xC0with(code[i + 7], code[i + 9]) != (0x48, 0xC0)accurately matchescmp rax, <reg != rax>acrossrcx..r15. Chunking atCLZERO_LOOP_LINESand dropping to 1-line-per-fault across non-uniform regions preserves exact hardware fault boundaries.One subtle edge case
clzero(insn.rs:163,eh.rs:343):decodeonly excludes0x67fromcode[..i], not other prefixes (i > 0). Becausejb -12(72 F4) jumps back torip + i(0F 01 FC), returning-1on!run.doneleavesctx.Ripatrip(before the prefix) instead ofrip + i. Requiringi == 0forClzeroLoop(falling back to singleClzerowhen prefixed) eliminates that mismatch.(AI-assisted)
@raylitebull on GitHub · 2026-11-24 10:42 UTC · original
Closing as superseded by #290, which has already been merged into
main.Pull request closed