Repository navigation
Conversation
…PU does it (ivall#278) The loop decoder took any `[48|4C] 39 modrm` as `cmp rax, <reg>`: `cmp rdx, rcx` or a memory compare (`cmp [rax], rdx`) was emulated as a loop to the wrong register. It now needs ModRM mod 3 with rax in r/m, and leaves `cmp rax, rax` (which never loops) to the plain CLZERO. The emulation zeroed [rax, end) byte-exact and set rax = end: an unaligned rax or an end that is not a whole number of lines away zeroed a partial line, an end at or below rax zeroed nothing though the body runs once, and the cmp's flags were left as they were. insn::clzero_loop now gives what the CPU does: whole lines from the one rax is in, rax moved by whole lines, at least one iteration, the flags of the last cmp. A fault zeroes at most 256 MiB and stays at the clzero when the loop goes on (instead of the 2 GiB cap that skipped the loop), and a span that is not one writable region goes a line a fault, so it stops at the line the CPU would fault on. Part of ivall#278 (items 1 and 2; is_writable and the CLWB patch are separate PRs). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Independent static code & x86-64 ISA audit of Summary(Note: overlaps with #290, which also addresses #278.)
One subtle edge case
(AI-assisted) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part 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