Skip to content

Commit

Permalink
chore(interpreter): factor out jump logic (bluealloy#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored and fubuloubu committed Apr 11, 2024
1 parent efb8521 commit 95724fd
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions crates/interpreter/src/instructions/control.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
use revm_primitives::Bytes;

use crate::{
gas,
primitives::{Spec, U256},
primitives::{Bytes, Spec, U256},
Host, InstructionResult, Interpreter, InterpreterResult,
};

pub fn jump<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::MID);
pop!(interpreter, dest);
let dest = as_usize_or_fail!(interpreter, dest, InstructionResult::InvalidJump);
if interpreter.contract.is_valid_jump(dest) {
// SAFETY: In analysis we are checking create our jump table and we do check above to be
// sure that jump is safe to execute.
interpreter.instruction_pointer =
unsafe { interpreter.contract.bytecode.as_ptr().add(dest) };
} else {
interpreter.instruction_result = InstructionResult::InvalidJump;
}
jump_inner(interpreter, dest);
}

pub fn jumpi<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::HIGH);
pop!(interpreter, dest, value);
if value != U256::ZERO {
let dest = as_usize_or_fail!(interpreter, dest, InstructionResult::InvalidJump);
if interpreter.contract.is_valid_jump(dest) {
// SAFETY: In analysis we are checking if jump is valid destination and
// this `if` makes this unsafe block safe.
interpreter.instruction_pointer =
unsafe { interpreter.contract.bytecode.as_ptr().add(dest) };
} else {
interpreter.instruction_result = InstructionResult::InvalidJump
}
jump_inner(interpreter, dest);
}
}

#[inline(always)]
fn jump_inner(interpreter: &mut Interpreter, dest: U256) {
let dest = as_usize_or_fail!(interpreter, dest, InstructionResult::InvalidJump);
if !interpreter.contract.is_valid_jump(dest) {
interpreter.instruction_result = InstructionResult::InvalidJump;
return;
}
// SAFETY: `is_valid_jump` ensures that `dest` is in bounds.
interpreter.instruction_pointer = unsafe { interpreter.contract.bytecode.as_ptr().add(dest) };
}

pub fn jumpdest<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
Expand Down

0 comments on commit 95724fd

Please sign in to comment.