loader: fix ClzeroLoop ModRM decoding, >=2 GiB skip, unaligned/empty bounds, and EFlags #407

Open
github-bot wants to merge 1 commit from github-bot/gh-407 into main AGit
Owner

@⁠AscilCH on GitHub · 2026-11-25 00:25 UTC · original
AscilCH:fix/loader-clzero-loop-decode-and-emulation → main · +124 −22 · 2 files · 1 commits · open

Problem

Five bugs in ClzeroLoop decoding and emulation (crates/victory-loader/src/console_insn_decode.rs and console_insn.rs):

  1. Missing ModRM mod == 3 && rm == RAX check: decode checked 48/4C 39 without verifying (modrm & 0xC7) == 0xC0 (or reg_idx != 0), misdecoding cmp rcx, rdx (48 39 D1), cmp [rax], rdx (48 39 10), and cmp rax, rax (48 39 C0) as ClzeroLoop.
  2. Silent skipped zeroing for >= 2 GiB: if end > start && end - start < 2 << 30 skipped zeroing entirely on runs >= 2 GiB, then still set ctx.Rax = end and jumped past the 12-byte loop — silently leaving the buffer unzeroed.
  3. Silent skipped zeroing when end <= start: Hardware clzero; add rax, 0x40; cmp rax, end; jb is a bottom-tested loop that always executes clzero at least once, whereas emulate zeroed nothing and overwrote ctx.Rax = end.
  4. Corrupted partial-line zeroing / final RAX on unaligned start or end - start: write_bytes(start, 0, end - start) failed to clear [start & !63, start) and [end, aligned_end) and set RAX = end instead of start + 64 * ceil((end - start)/64).
  5. Stale EFlags: Skipping cmp rax, end left EFlags unchanged instead of updating arithmetic flags for rax == end.

Fix

  • Require (code[i + 9] & 0xC7) == 0xC0 and reg_idx != 0 in console_insn_decode::decode before returning ConsoleOnly::ClzeroLoop.
  • In console_insn::emulate, fall back to single-instruction clzero (line = start & !63, ctx.Rip += len - 9) whenever start & 63 != 0 || end <= start || (end - start) & 63 != 0 so the CPU executes add/cmp/jb natively.
  • Remove the < 2 << 30 silent skip so while at < end walks writable regions of any size via writable_to.
  • Update ctx.EFlags (ZF=1, PF=1, CF=AF=SF=OF=0) on fast-path completion.
  • Add unit tests in console_insn_decode::tests and console_insn::tests.

Verification

  • cargo test -p victory-loader --locked: 25 unit + 3 integration passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-loader --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-loader --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-25 00:25 UTC · [original](https://github.com/fxceless/victory-fork/pull/407) `AscilCH:fix/loader-clzero-loop-decode-and-emulation` → `main` · +124 −22 · 2 files · 1 commits · open ## Problem Five bugs in `ClzeroLoop` decoding and emulation (`crates/victory-loader/src/console_insn_decode.rs` and `console_insn.rs`): 1. **Missing ModRM `mod == 3 && rm == RAX` check**: `decode` checked `48/4C 39` without verifying `(modrm & 0xC7) == 0xC0` (or `reg_idx != 0`), misdecoding `cmp rcx, rdx` (`48 39 D1`), `cmp [rax], rdx` (`48 39 10`), and `cmp rax, rax` (`48 39 C0`) as `ClzeroLoop`. 2. **Silent skipped zeroing for `>= 2 GiB`**: `if end > start && end - start < 2 << 30` skipped zeroing entirely on runs `>= 2 GiB`, then still set `ctx.Rax = end` and jumped past the 12-byte loop — silently leaving the buffer unzeroed. 3. **Silent skipped zeroing when `end <= start`**: Hardware `clzero; add rax, 0x40; cmp rax, end; jb` is a bottom-tested loop that always executes `clzero` at least once, whereas `emulate` zeroed nothing and overwrote `ctx.Rax = end`. 4. **Corrupted partial-line zeroing / final `RAX` on unaligned `start` or `end - start`**: `write_bytes(start, 0, end - start)` failed to clear `[start & !63, start)` and `[end, aligned_end)` and set `RAX = end` instead of `start + 64 * ceil((end - start)/64)`. 5. **Stale `EFlags`**: Skipping `cmp rax, end` left `EFlags` unchanged instead of updating arithmetic flags for `rax == end`. ## Fix - Require `(code[i + 9] & 0xC7) == 0xC0` and `reg_idx != 0` in `console_insn_decode::decode` before returning `ConsoleOnly::ClzeroLoop`. - In `console_insn::emulate`, fall back to single-instruction `clzero` (`line = start & !63`, `ctx.Rip += len - 9`) whenever `start & 63 != 0 || end <= start || (end - start) & 63 != 0` so the CPU executes `add/cmp/jb` natively. - Remove the `< 2 << 30` silent skip so `while at < end` walks writable regions of any size via `writable_to`. - Update `ctx.EFlags` (`ZF=1, PF=1, CF=AF=SF=OF=0`) on fast-path completion. - Add unit tests in `console_insn_decode::tests` and `console_insn::tests`. ## Verification - `cargo test -p victory-loader --locked`: 25 unit + 3 integration passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-loader --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-loader --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
Fix five bugs in `ClzeroLoop` decoding and emulation across
`console_insn_decode.rs` and `console_insn.rs`:

1. `decode` did not verify `(modrm & 0xC7) == 0xC0` (or `end_reg != 0`)
   on `48/4C 39 modrm`, causing non-`rax` comparisons such as
   `cmp rcx, rdx` (`48 39 D1`) or `cmp [rax], rdx` (`48 39 10`) to
   misdecode as `ClzeroLoop`.
2. `emulate` skipped zeroing completely when `end - start >= 2 GiB`, yet
   still set `ctx.Rax = end` and advanced `ctx.Rip += 12`, silently
   leaving large buffers (e.g., multi-GiB page pools) unzeroed. Remove
   the `< 2 GiB` gate; the loop already walks writable regions via
   `writable_to`.
3. When `end <= start` (do-while must execute at least one `clzero`) or
   when `start` or `end - start` is not 64-byte aligned, the fast-path
   either skipped zeroing or zeroed the wrong byte range and corrupted
   final `RAX`. Fall back to single-instruction `clzero` (`line = start & !63`,
   `ctx.Rip += len - 9`) so the CPU runs `add/cmp/jb` natively.
4. When the aligned fast-path completes (`at == end`), update `ctx.EFlags`
   to reflect `cmp rax, end` (`ZF=1, PF=1, CF=AF=SF=OF=0`).

Add decode and emulation unit tests in `console_insn_decode` and
`console_insn`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:42:47 +00:00
This pull request has changes conflicting with the target branch.
  • crates/victory-loader/src/console_insn.rs
  • crates/victory-loader/src/console_insn_decode.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/407/head:github-bot/gh-407'
git switch 'github-bot/gh-407'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-407'
git switch 'github-bot/gh-407'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-407'
git switch 'github-bot/gh-407'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-407'
git switch 'main'
git merge --squash 'github-bot/gh-407'
git switch 'main'
git merge --ff-only 'github-bot/gh-407'
git switch 'main'
git merge 'github-bot/gh-407'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.