Skip to content

jit: translate ORR (shifted register) instead of a plain MOV - #108

Open
bluetoe-99 wants to merge 2 commits into
pound-emu:mainfrom
bluetoe-99:feature/tier1-orr-shifted-register
Open

bluetoe-99 wants to merge 2 commits into
pound-emu:mainfrom
bluetoe-99:feature/tier1-orr-shifted-register

Conversation

@bluetoe-99

@bluetoe-99 bluetoe-99 commented Oct 29, 2026 •

Copy link
Copy Markdown

What is broken

Silent miscompile of ORR (shifted register) in the x86 Tier 1 JIT.

The decoder-table generator gives OPCODE_MOV to every entry named ORR, because MOV Xd, Xm is encoded as ORR Xd, XZR, Xm. So translate_mov_register compiled real three-operand words like ORR X8, X9, X10 (0xAA0A0128) as a plain copy X8 = X9, silently dropping X10. No exception, no reject: just a wrong guest register value that shows up later as an unrelated crash.

The differential fuzzer cannot catch this kind of bug. Its seeds run with all registers zeroed and each seed varies only one operand field at a time (REGISTER_SEEDS = {0,1,15,30,31}), so both source registers are never non-zero at the same time, and 0 | 0 compares equal to the broken copy.

Proof (tests passed)

New engine-level Unity suite tests/unit/bal_tier1/test_bal_tier1_orr.c compiles and runs single-ORR blocks with non-zero register state.

  • Before the fix: 7 of 9 tests fail. ORR X8, X9, X10 returns exactly X9's value (X10 is never read).
  • Worker emit trace for 0xAA0A0128 before the fix: only X8's guest slot is touched, no load of X9 or X10, no OR instruction anywhere.
  • Worker emit trace after the fix: mov r0, [rbp+0x48] (X9), mov r1, [rbp+0x50] (X10), mov r2, r0, or r2, r1, store r2 to [rbp+0x40] (X8).
  • After the fix: 9 of 9 pass, zero warnings under -Werror -Wconversion -Wsign-conversion.
  • Full suite: ctest 15/15 pass.
  • Differential fuzzer, byte-identical before vs after: 60389 seeds | 465 passed | 38 failed | 28171 skipped | 6 errors, same 38 failing seeds, same per-seed mismatch counts (pre-existing LDR register-offset/writeback and LDRAA/LDRAB failures, untouched by this PR).
  • bal_fuzzer_worker_ballistic 0xB2401128 (a true ORR-immediate) now aborts the block with a clear log line instead of silently compiling as a MOV.

What the change does

  1. In case OPCODE_MOV, entries named ORR that carry a REGISTER operand at bit position 5 dispatch to a new translate_or_reg. The MOV-alias entries keep operands[1] at bit 16, so that check separates them cleanly. No decoder-table regeneration needed.
  2. translate_or_reg implements logical shifted-register semantics, including field 31 being the zero register, not SP. allocate_x86_register loads guest slot 31 as the stack pointer, so the handler has to deal with XZR explicitly: Rn/Rm = 31 reads as zero, Rd = 31 discards the result. This also makes MOV-alias words compile correctly, since the decoder lookup returns the 3-operand entry for them.
  3. Shifts reuse the existing sliding-window macros (SHL/SHR/SAR/ROR). Allocation order is sources-before-destination so aliasing (rd == rn, rd == rm) reuses already-loaded values, and rd == rm shifts use the pre-shift source. Because OR is commutative, the rd == rn case needs no scratch register.
  4. True ORR (immediate) (0xB2000000 / 0x32000000 base forms) now returns BAL_ERROR_UNKNOWN_INSTRUCTION with a log line instead of miscompiling, the same explicit-reject pattern already used for AND (immediate). Seeds hitting it move from "passing with wrong semantics" to cleanly skipped.

Left out on purpose

Found while working here, kept out to keep this PR small:

  • translate_and_reg has the same XZR bug (reads slot 31 as SP). Fixing it changes AND-alias behaviour and deserves its own tests and PR.
  • W-form (32-bit) logical ops may not clear bits 63:32 of the host register in the Tier 1 handlers.
  • OR/ORN/EOR (immediate) implementation.
  • The decoder generator's name-based OPCODE_MOV alias mapping is the root cause of this whole bug class. An eventual table-level fix (separate opcodes per alias family) would remove the need for name checks entirely.

Tests added

9 Unity tests: general combine, LSL #4, rd == rm uses the pre-shift value, rd == rn, write to XZR discarded, XZR source reads as zero and not SP (an SP-slot sentinel proves it), rm == XZR keeps rn, both sources XZR clear the destination, XZR-shaped alias word still copies correctly.

Diff: +426 / -1, 3 files.

@bluetoe-99
bluetoe-99 force-pushed the feature/tier1-orr-shifted-register branch 2 times, most recently from a2dedd7 to 90b8cdd Compare October 29, 2026 02:37
@bluetoe-99
bluetoe-99 force-pushed the feature/tier1-orr-shifted-register branch 2 times, most recently from 1572cdb to 145baf2 Compare October 29, 2026 19:21
The decoder table generator maps every entry named ORR to OPCODE_MOV,
so translate_mov_register compiled genuine three-operand
ORR Xd, Xn, Xm words as a plain register copy, silently discarding the
second source operand. Only the MOV (register) alias encodings (Rn =
XZR) were handled correctly by that path.

Dispatch ORR-named entries carrying a register operand at bit 5 to a
new translate_or_reg handler implementing the logical shifted-register
semantics. Field 31 reads as the zero register (not SP) per the
group's semantics, matching ARM's behaviour for XZR sources and a
discarded XZR destination.

ORR (immediate) now aborts the block with a clear diagnostic instead
of silently miscompiling as a MOV, mirroring the existing AND
(immediate) guard. OR (immediate) support and latent XZR/W-form
issues in translate_and_reg are left as follow-up work.

Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com>
Nine engine-level Unity tests that compile and execute single-ORR
blocks with non-zero register state: general three-register combine,
LSL pound-emu#4 shift, destination aliasing (rd == rm uses the pre-shift value,
rd == rn), writes to XZR discarded, XZR sources read as zero rather
than the stack-pointer slot, and both-sources-zero clearing.

The differential fuzzer cannot catch the miscompile this guards
against: its seeds execute with zeroed registers and vary one operand
field at a time, so both sources were never simultaneously non-zero
and 0 | 0 compares equal to the broken copy.

Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com>
@bluetoe-99
bluetoe-99 force-pushed the feature/tier1-orr-shifted-register branch from 145baf2 to 4b7a2dd Compare October 29, 2026 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant