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

Open
AscilCH wants to merge 1 commit from upstream-pr/407 into main
AscilCH commented 2026-11-25 00:25:45 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T00:25:45Z.


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.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6594262166 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/407). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T00:25:45Z. --- ## 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.)*
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/407:upstream-pr/407'
git switch 'upstream-pr/407'

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 'upstream-pr/407'
git switch 'upstream-pr/407'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/407'
git switch 'upstream-pr/407'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/407'
git switch 'main'
git merge --squash 'upstream-pr/407'
git switch 'main'
git merge --ff-only 'upstream-pr/407'
git switch 'main'
git merge 'upstream-pr/407'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
fxceless/victory-fork!407
No description provided.