From 15d18ee169af606953f968541aae26a8c996f930 Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 24 Jul 2023 16:37:37 +0200 Subject: [PATCH] Fixed panic on shift outta bounds - Pointed out by 5225225 --- hbvm/src/vm/mod.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/hbvm/src/vm/mod.rs b/hbvm/src/vm/mod.rs index 7b37814..33fdd60 100644 --- a/hbvm/src/vm/mod.rs +++ b/hbvm/src/vm/mod.rs @@ -50,6 +50,20 @@ macro_rules! binary_op { }}; } +/// Parform bitshift operations +macro_rules! binary_op_sh { + ($self:expr, $ty:ident, $handler:expr) => {{ + let ParamBBB(tg, a0, a1) = param!($self, ParamBBB); + $self.write_reg( + tg, + $handler( + Value::$ty(&$self.read_reg(a0)), + $self.read_reg(a1).as_u64() as u32, + ) + ) + }}; +} + /// Perform binary operation with immediate `#0 ← #1 OP imm #2` macro_rules! binary_op_imm { ($self:expr, $ty:ident, $handler:expr) => {{ @@ -168,9 +182,9 @@ impl<'a, PfHandler: HandlePageFault, const TIMER_QUOTIENT: usize> AND => binary_op!(self, as_u64, ops::BitAnd::bitand), OR => binary_op!(self, as_u64, ops::BitOr::bitor), XOR => binary_op!(self, as_u64, ops::BitXor::bitxor), - SL => binary_op!(self, as_u64, ops::Shl::shl), - SR => binary_op!(self, as_u64, ops::Shr::shr), - SRS => binary_op!(self, as_i64, ops::Shr::shr), + SL => binary_op_sh!(self, as_u64, u64::wrapping_shl), + SR => binary_op_sh!(self, as_u64, u64::wrapping_shr), + SRS => binary_op_sh!(self, as_i64, i64::wrapping_shr), CMP => { // Compare a0 <=> a1 // < → -1