loader: a loop-shaped cmp is the clzero loop only against rax #593

Open
qnjk wants to merge 1 commit from qnjk/Darner:clzero-loop-modrm into Main
Contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

  • The problem on main: the clzero-loop recogniser (crates/darner-loader/src/console_insn_decode.rs, around :117-130) matched the four-instruction shape clzero; add rax, 0x40; cmp rax, <reg>; jb -12 by checking the bytes of each instruction but never the ModRM of the cmp. Its comment says cmp rax, <reg>, i.e. the register form with r/m = rax; the code accepted any ModRM there. A title sequence shaped like the loop but comparing against something else — cmp rbx, rax, or cmp [rax], rax — was therefore decoded as ClzeroLoop { end_reg }, and the loader zeroed a whole range [rax..reg) that the title never asked to zero, instead of leaving it as one clzero and the title's own add/cmp/jb.

  • This change adds the missing condition: (code[i + 9] & 0xC7) == 0xC0 — mod = 3 (register direct), r/m = 0 (rax) — so only the shape the comment describes is taken as the loop.

How it was tested

  • Tested revision: a42ab0a (one commit on c8258f9).
  • Commands and results:
    • cargo test --locked -p darner-loader --lib -- console_insn — 11 passed, 0 failed.
    • New test console_insn::decode::tests::a_compare_that_is_not_against_rax_is_not_the_loop: clzero; add rax, 0x40; cmp rbx, rax; jb and clzero; add rax, 0x40; cmp [rax], rax; jb must both decode as one Clzero (3 bytes), not as the loop.
    • Fail-before, with only the new condition removed:
      a_compare_that_is_not_against_rax_is_not_the_loop ... FAILED
        left:  Some((ClzeroLoop { end_reg: 0 }, 12))
       right: Some((Clzero, 3))
      
      i.e. on main the wrong range is zeroed.
    • cargo clippy --locked -p darner-loader --all-targets -- -D warnings passes.
  • Changes a default: no. The sequence the comment describes is recognised exactly as before; only the near-misses stop being taken for it.
  • A/B result: not applicable — no default changes and no speed claim.
  • Gates run on that revision:
    • Gate 1, cargo clippy --workspace --all-targets --locked -- -D warnings: passes.
    • Gate 2, cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: passes.
    • Gate 3, cargo test --workspace --locked: not run to completion here; the pre-existing darner-gpu test deadlock (#224) and darner-shader waterfall fixture failure (#227) make the whole-workspace run non-green on this machine. darner-loader's suite is green.
  • Gameplay, when relevant: no gameplay run. No title on this machine runs a cmp shaped like that against anything but rax; the tests supply the near-misses.
  • Shader wave sizes tested, when relevant: not applicable (the emulated instruction is CPU-side).
  • Hardware dependence: none.
  • Coverage gaps: only the two near-misses above are covered; a cmp with a REX prefix of the other group (0x49) and a SIB/displacement form are not.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
    • Not run in gameplay: the defect is a rare instruction shape and the fix only makes the recogniser narrower. The evidence is the test, which fails on main.
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md (cargo xtask policy: 576 files ok).
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none found. No open pull request changes crates/darner-loader/src/console_insn_decode.rs (checked against the file list of all 189 open pull requests), and no open or closed issue mentions clzero loop recognition.
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: ## What it changes - The problem on main: the `clzero`-loop recogniser (`crates/darner-loader/src/console_insn_decode.rs`, around `:117-130`) matched the four-instruction shape `clzero; add rax, 0x40; cmp rax, <reg>; jb -12` by checking the bytes of each instruction but **never the ModRM of the `cmp`**. Its comment says `cmp rax, <reg>`, i.e. the register form with r/m = `rax`; the code accepted any ModRM there. A title sequence shaped like the loop but comparing against something else — `cmp rbx, rax`, or `cmp [rax], rax` — was therefore decoded as `ClzeroLoop { end_reg }`, and the loader zeroed a whole range `[rax..reg)` that the title never asked to zero, instead of leaving it as one `clzero` and the title's own `add`/`cmp`/`jb`. - This change adds the missing condition: `(code[i + 9] & 0xC7) == 0xC0` — mod = 3 (register direct), r/m = 0 (`rax`) — so only the shape the comment describes is taken as the loop. ## How it was tested - Tested revision: `a42ab0a` (one commit on `c8258f9`). - Commands and results: - `cargo test --locked -p darner-loader --lib -- console_insn` — 11 passed, 0 failed. - New test `console_insn::decode::tests::a_compare_that_is_not_against_rax_is_not_the_loop`: `clzero; add rax, 0x40; cmp rbx, rax; jb` and `clzero; add rax, 0x40; cmp [rax], rax; jb` must both decode as one `Clzero` (3 bytes), not as the loop. - Fail-before, with only the new condition removed: ``` a_compare_that_is_not_against_rax_is_not_the_loop ... FAILED left: Some((ClzeroLoop { end_reg: 0 }, 12)) right: Some((Clzero, 3)) ``` i.e. on `main` the wrong range is zeroed. - `cargo clippy --locked -p darner-loader --all-targets -- -D warnings` passes. - Changes a default: no. The sequence the comment describes is recognised exactly as before; only the near-misses stop being taken for it. - A/B result: not applicable — no default changes and no speed claim. - Gates run on that revision: - Gate 1, `cargo clippy --workspace --all-targets --locked -- -D warnings`: passes. - Gate 2, `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: passes. - Gate 3, `cargo test --workspace --locked`: not run to completion here; the pre-existing `darner-gpu` test deadlock (#224) and `darner-shader` waterfall fixture failure (#227) make the whole-workspace run non-green on this machine. `darner-loader`'s suite is green. - Gameplay, when relevant: no gameplay run. No title on this machine runs a `cmp` shaped like that against anything but `rax`; the tests supply the near-misses. - Shader wave sizes tested, when relevant: not applicable (the emulated instruction is CPU-side). - Hardware dependence: none. - Coverage gaps: only the two near-misses above are covered; a `cmp` with a REX prefix of the other group (`0x49`) and a SIB/displacement form are not. ## Checklist - [x] I ran this change in the game, or say below why it can't be (docs, tests only). - Not run in gameplay: the defect is a rare instruction shape and the fix only makes the recogniser narrower. The evidence is the test, which fails on `main`. - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md` (`cargo xtask policy`: 576 files ok). - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none found. No open pull request changes `crates/darner-loader/src/console_insn_decode.rs` (checked against the file list of all 189 open pull requests), and no open or closed issue mentions `clzero` loop recognition. - Replaces or overlaps: none found
`decode` matched the loop on three bytes of its `cmp` — `[48|4C] 39` — and
never looked at the ModRM byte that says what the compare's other operand is.
That byte was read for the register number alone, so `48 39 C3` (`cmp rbx,
rax`) and `48 39 00` (`cmp [rax], rax`) were taken as the loop too, with
`end_reg` from the reg field. `emulate` then read the limit from that register
— `rax` in both, so the range was empty — zeroed one line, stepped the whole
12 bytes and made the flags from a compare the CPU never made: a loop the
title wanted run is skipped, and the title executes from the wrong place.

The crate's two other implementations of this pattern already refuse those
forms: `clzero_stub` requires the register form with r/m `rax`, and
`compare_registers` in `crash.rs` needs mod 3 with its caller checking r/m 0.
The same clause here: mod 3 and r/m `rax`, so the compare is against `rax`
whichever register it is with. `cmp rax, rax` (`48 39 C0`) is still the loop —
the one form the stub cannot take and the emulation does a line at a time
(`a_loop_the_stub_cannot_take_reserves_no_room`) — and a new test pins the two
near misses as one `clzero` of three bytes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
TrustNo1 changed title from [pr-fix][startup] loader: a loop-shaped cmp is the clzero loop only against rax to loader: a loop-shaped cmp is the clzero loop only against rax 2026-12-06 03:26:46 +00:00
Owner

Test needed — commit a42ab0ac; what to check:

  • The problem on main: the clzero-loop recogniser (crates/darner-loader/src/console_insn_decode.rs, around :117-130) matched the four-instruction shape clzero; add rax, 0x40; cmp rax, <reg>; jb -12 by checking the bytes of each instruction but never the ModRM of the cmp. Its comment says cmp rax, <reg>, i.e. the register form with r/m = rax; the code accepted any ModRM there. A title sequence shaped like the loop but comparing against something else — cmp rbx, rax, or cmp [rax], rax — was therefore decoded as ClzeroLoop { end_reg }, and the loader zeroed a whole range [rax..reg) that the title never asked to zero, instead of leaving it as one clzero and the title's own add/cmp/jb.

  • This change adds the missing condition: (code[i + 9] & 0xC7) == 0xC0 — mod = 3 (register direct), r/m = 0 (rax) — so only the shape the comment describes is taken as the loop.

Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture.

Reply testing to take this run; it's free again after 24 h with no results.

**Test needed** — commit `a42ab0ac`; what to check: - The problem on main: the `clzero`-loop recogniser (`crates/darner-loader/src/console_insn_decode.rs`, around `:117-130`) matched the four-instruction shape `clzero; add rax, 0x40; cmp rax, <reg>; jb -12` by checking the bytes of each instruction but **never the ModRM of the `cmp`**. Its comment says `cmp rax, <reg>`, i.e. the register form with r/m = `rax`; the code accepted any ModRM there. A title sequence shaped like the loop but comparing against something else — `cmp rbx, rax`, or `cmp [rax], rax` — was therefore decoded as `ClzeroLoop { end_reg }`, and the loader zeroed a whole range `[rax..reg)` that the title never asked to zero, instead of leaving it as one `clzero` and the title's own `add`/`cmp`/`jb`. - This change adds the missing condition: `(code[i + 9] & 0xC7) == 0xC0` — mod = 3 (register direct), r/m = 0 (`rax`) — so only the shape the comment describes is taken as the loop. Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture. Reply `testing` to take this run; it's free again after 24 h with no results.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'clzero-loop-modrm:qnjk-clzero-loop-modrm'
git switch 'qnjk-clzero-loop-modrm'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'Main'
git merge --no-ff 'qnjk-clzero-loop-modrm'
git switch 'qnjk-clzero-loop-modrm'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'qnjk-clzero-loop-modrm'
git switch 'qnjk-clzero-loop-modrm'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'qnjk-clzero-loop-modrm'
git switch 'Main'
git merge --squash 'qnjk-clzero-loop-modrm'
git switch 'Main'
git merge --ff-only 'qnjk-clzero-loop-modrm'
git switch 'Main'
git merge 'qnjk-clzero-loop-modrm'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!593
No description provided.