From 6fa8e60570b70f5f6a577d0d5fcae3e161be4c65 Mon Sep 17 00:00:00 2001 From: primoly <168267431+primoly@users.noreply.github.com> Date: Wed, 16 Jul 2025 20:25:44 +0200 Subject: [PATCH] Fix `uadd_overflow_trap` in interpreter --- cranelift/codegen/src/ir/instructions.rs | 8 ++++++-- cranelift/interpreter/src/step.rs | 8 +++----- cranelift/src/interpret.rs | 1 + 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cranelift/codegen/src/ir/instructions.rs b/cranelift/codegen/src/ir/instructions.rs index 2b8eeff60094..b36912d9583a 100644 --- a/cranelift/codegen/src/ir/instructions.rs +++ b/cranelift/codegen/src/ir/instructions.rs @@ -491,7 +491,9 @@ impl InstructionData { /// `None`. pub fn trap_code(&self) -> Option { match *self { - Self::CondTrap { code, .. } | Self::Trap { code, .. } => Some(code), + Self::CondTrap { code, .. } + | Self::IntAddTrap { code, .. } + | Self::Trap { code, .. } => Some(code), _ => None, } } @@ -519,7 +521,9 @@ impl InstructionData { /// trap code. Otherwise, return `None`. pub fn trap_code_mut(&mut self) -> Option<&mut TrapCode> { match self { - Self::CondTrap { code, .. } | Self::Trap { code, .. } => Some(code), + Self::CondTrap { code, .. } + | Self::IntAddTrap { code, .. } + | Self::Trap { code, .. } => Some(code), _ => None, } } diff --git a/cranelift/interpreter/src/step.rs b/cranelift/interpreter/src/step.rs index 25b402c978ca..3bf387bd4348 100644 --- a/cranelift/interpreter/src/step.rs +++ b/cranelift/interpreter/src/step.rs @@ -771,12 +771,10 @@ where assign_multiple(&[sum, DataValueExt::bool(carry, false, types::I8)?]) } Opcode::UaddOverflowTrap => { - let sum = DataValueExt::add(arg(0), arg(1))?; - let carry = sum < arg(0) && sum < arg(1); - if carry { - ControlFlow::Trap(CraneliftTrap::User(trap_code())) - } else { + if let Some(sum) = DataValueExt::uadd_checked(arg(0), arg(1))? { assign(sum) + } else { + ControlFlow::Trap(CraneliftTrap::User(trap_code())) } } Opcode::SsubOverflowBin => { diff --git a/cranelift/src/interpret.rs b/cranelift/src/interpret.rs index 4d68ce3d2d53..d6fa8664c90a 100644 --- a/cranelift/src/interpret.rs +++ b/cranelift/src/interpret.rs @@ -126,6 +126,7 @@ impl FileInterpreter { let state = InterpreterState::default().with_function_store(env.clone()); match Interpreter::new(state).call_by_name(func_name, args) { Ok(ControlFlow::Return(results)) => Ok(results.to_vec()), + Ok(ControlFlow::Trap(trap)) => Err(trap.to_string()), Ok(_) => panic!("Unexpected returned control flow--this is likely a bug."), Err(t) => Err(t.to_string()), }