From fbec436b89735642eea2cbad3a60140f0e02e805 Mon Sep 17 00:00:00 2001 From: plokhi <37208555+Plokhi@users.noreply.github.com> Date: Tue, 24 Nov 2026 21:19:38 +0100 Subject: [PATCH] Fix ARM64 variable shift register encoding Signed-off-by: plokhi <37208555+Plokhi@users.noreply.github.com> --- CHANGELOG.md | 1 + crates/loader/src/arm64.rs | 56 +++++++++++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bdc634..c13f154 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -202,6 +202,7 @@ remains unsupported. ### Fixed - Fixed the native dependency audit reading Cargo JSON and inspection-tool output in the process locale encoding; it now reads and reports UTF-8 paths and diagnostics under an ASCII locale (#363). +- Fixed the ARM64 `LSLV` emitter setting bit 1 of the shift-count register field (#131). The `RBIT`, `CLZ` and `LSLV` emitters now reject out-of-range register numbers, with encoding tests checked against LLVM's assembler. - Made the static probe and the binder share one bounded import parser, after the truncated-name fix for #270. Truncated, unterminated, empty or non-ASCII names, reserved bits and endless tables are reported by the probe and refused by the binder. - Fixed the MXCSR flags of an unmasked floating-point overflow whose result is exact before the exponent is bounded, such as the largest binary32 value times two (#128). Only the overflow flag is set; the precision flag used to be set as well although nothing was rounded. A masked overflow still sets both flags and delivers infinity or the largest finite value, and an unmasked overflow that is also inexact still sets both. Five Intel SDM cases in `cpu_known_answers.rs` moved from open defect to verified on both engines, with three more for the other rounding modes, and the scalar and conversion sweeps now compare the precision flag in this case as well. Not compared with x86 hardware; awaiting independent review. - Fixed the decoder refusing an instruction whose REX prefix is followed by a legacy prefix (#69). Such a REX is ignored, as the Intel SDM (Vol. 2A, 2.2.1) specifies, so `48 66 01 D8` now runs as `add ax, bx`; it used to stop the guest with an illegal-instruction exit. The ignored byte still counts toward the instruction's length, and a REX that directly precedes the opcode behaves as before. The interpreter, the DBT and the static probe share the decoder. One SDM-derived case in `cpu_known_answers.rs` moved from refused to verified and three were added, for an ignored REX.B, an ignored REX.W before a segment prefix, and a later REX that does apply. Two REX bytes in a row and the address-size prefix stay rejected. Not compared with x86 hardware; awaiting independent review. diff --git a/crates/loader/src/arm64.rs b/crates/loader/src/arm64.rs index 6557f06..4c47a44 100644 --- a/crates/loader/src/arm64.rs +++ b/crates/loader/src/arm64.rs @@ -307,23 +307,27 @@ impl Arm64Assembler { } /// RBIT Xd, Xn: reverse the bit order of the source. + /// Panics if a register number exceeds 31. pub fn rbit_64(&mut self, xd: u8, xn: u8) { - self.emit(0xdac0_0000 | ((u32::from(xn) & 0x1f) << 5) | (u32::from(xd) & 0x1f)); + assert!(xd <= XZR && xn <= XZR, "RBIT register must be in 0..=31"); + self.emit(0xdac0_0000 | (u32::from(xn) << 5) | u32::from(xd)); } /// CLZ Xd, Xn: count the leading zero bits of the source. + /// Panics if a register number exceeds 31. pub fn clz_64(&mut self, xd: u8, xn: u8) { - self.emit(0xdac0_1000 | ((u32::from(xn) & 0x1f) << 5) | (u32::from(xd) & 0x1f)); + assert!(xd <= XZR && xn <= XZR, "CLZ register must be in 0..=31"); + self.emit(0xdac0_1000 | (u32::from(xn) << 5) | u32::from(xd)); } /// LSLV Xd, Xn, Xm: shift the source left by the amount in Xm. + /// Panics if a register number exceeds 31. pub fn lslv_64(&mut self, xd: u8, xn: u8, xm: u8) { - self.emit( - 0x9ac2_2000 - | ((u32::from(xm) & 0x1f) << 16) - | ((u32::from(xn) & 0x1f) << 5) - | (u32::from(xd) & 0x1f), + assert!( + xd <= XZR && xn <= XZR && xm <= XZR, + "LSLV register must be in 0..=31" ); + self.emit(0x9ac0_2000 | (u32::from(xm) << 16) | (u32::from(xn) << 5) | u32::from(xd)); } /// LDR Qt, [Xn, #offset] (offset a multiple of 16) @@ -791,6 +795,44 @@ mod tests { assert_eq!(branch.instructions()[0], 0x14000005); } + #[test] + fn bit_scan_and_bit_test_encodings_match_llvm() { + // A64 words from Apple Clang's integrated assembler and llvm-objdump. + let mut asm = Arm64Assembler::new(); + asm.rbit_64(X10, X9); // rbit x10, x9 + asm.rbit_64(X0, XZR); // rbit x0, xzr + asm.clz_64(X10, X10); // clz x10, x10 + asm.clz_64(XZR, X30); // clz xzr, x30 + asm.lslv_64(X11, X11, X10); // lslv x11, x11, x10 + asm.lslv_64(X0, X1, X0); // lslv x0, x1, x0 + asm.lslv_64(XZR, X30, XZR); // lslv xzr, x30, xzr + assert_eq!( + asm.instructions(), + [ + 0xdac0012a, 0xdac003e0, 0xdac0114a, 0xdac013df, 0x9aca216b, 0x9ac02020, 0x9adf23df, + ] + ); + } + + #[test] + fn bit_scan_and_bit_test_reject_out_of_range_registers() { + let invalid_operands: [fn(&mut Arm64Assembler); 7] = [ + |asm| asm.rbit_64(32, X0), + |asm| asm.rbit_64(X0, 32), + |asm| asm.clz_64(32, X0), + |asm| asm.clz_64(X0, 32), + |asm| asm.lslv_64(32, X0, X0), + |asm| asm.lslv_64(X0, 32, X0), + |asm| asm.lslv_64(X0, X0, 32), + ]; + for (index, emit) in invalid_operands.into_iter().enumerate() { + let mut asm = Arm64Assembler::new(); + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| emit(&mut asm))); + assert!(result.is_err(), "invalid operand case {index} was accepted"); + assert!(asm.instructions().is_empty()); + } + } + #[test] fn unsigned_offset_stores_scale_by_access_size() { let mut asm = Arm64Assembler::new();