Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(interpreter): factor out jump logic #1146

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading