Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions crates/common/types/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ impl Code {
}
}

/// Returns true iff `target` indexes a valid JUMPDEST in this bytecode.
///
/// `jump_targets` is built by [`Self::compute_jump_targets`] and contains every
/// position whose byte is `JUMPDEST` (0x5B) and that is not inside a PUSH
/// literal; its entries are `u32` (bytecode length fits in `u32`).
///
/// Uses `u32::try_from` rather than `target as u32` so that a `target` with
/// nonzero upper bits (e.g. `2^32 + k` for a real JUMPDEST at `k`) is
/// rejected instead of aliasing down to a valid-looking low-32-bit index.
pub fn is_valid_jump_target(&self, target: usize) -> bool {
u32::try_from(target).is_ok_and(|t| self.jump_targets.binary_search(&t).is_ok())
}

fn compute_jump_targets(code: &[u8]) -> Vec<u32> {
debug_assert!(code.len() <= u32::MAX as usize);
let mut targets = Vec::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate::{
gas_cost::{self, SSTORE_STIPEND, STATE_GAS_STORAGE_SET},
memory::calculate_memory_size,
opcode_handlers::OpcodeHandler,
opcodes::Opcode,
utils::{size_offset_to_usize, u256_to_usize},
vm::VM,
};
Expand Down Expand Up @@ -417,22 +416,7 @@ fn jump(vm: &mut VM<'_>, target: usize) -> Result<(), VMError> {
// Check target address validity.
// - Target bytecode has to be a JUMPDEST.
// - Target address must not be blacklisted (aka. the JUMPDEST must not be part of a literal).
#[expect(clippy::as_conversions, reason = "safe")]
if vm
.current_call_frame
.bytecode
.bytecode
.get(target)
.is_some_and(|&value| {
value == Opcode::JUMPDEST as u8
&& vm
.current_call_frame
.bytecode
.jump_targets
.binary_search(&(target as u32))
.is_ok()
})
{
if vm.current_call_frame.bytecode.is_valid_jump_target(target) {
// Update PC and skip the JUMPDEST instruction.
vm.current_call_frame.pc = target.wrapping_add(1);
vm.current_call_frame
Expand Down
Loading