From 22a4006265cf08d4fce9dde3111e01dc0313221d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20FASANO?= Date: Tue, 18 Jul 2023 12:54:10 +0200 Subject: [PATCH 1/3] fix: removed bad unsafe block --- src/core_module/op_codes/arithmetic/signed.rs | 102 ++--- .../op_codes/arithmetic/unsigned.rs | 268 ++++++----- src/core_module/op_codes/bitwise.rs | 248 +++++------ src/core_module/op_codes/comparison.rs | 416 +++++++++--------- src/core_module/op_codes/environment.rs | 178 ++++---- src/core_module/op_codes/flow.rs | 26 +- src/core_module/op_codes/log.rs | 40 +- src/core_module/op_codes/memory.rs | 24 +- src/core_module/op_codes/stack/dup.rs | 32 +- src/core_module/op_codes/stack/pop.rs | 2 +- src/core_module/op_codes/stack/push.rs | 50 ++- src/core_module/op_codes/stack/swap.rs | 32 +- src/core_module/op_codes/storage.rs | 49 +-- src/core_module/op_codes/system.rs | 106 ++--- src/core_module/runner.rs | 12 +- src/core_module/stack.rs | 8 +- 16 files changed, 752 insertions(+), 841 deletions(-) diff --git a/src/core_module/op_codes/arithmetic/signed.rs b/src/core_module/op_codes/arithmetic/signed.rs index 226f580..f5c84e9 100644 --- a/src/core_module/op_codes/arithmetic/signed.rs +++ b/src/core_module/op_codes/arithmetic/signed.rs @@ -9,96 +9,58 @@ use ethers::types::U256; // Colored output use colored::*; -/// Divides the second item on the stack by the topmost item, treating both values as signed. -/// If the divisor is zero, the result is zero. -/// -/// # Example -/// -/// ``` -/// use bytecode_rs::core_module::op_codes::math::unsigned::sdiv; -/// use bytecode_rs::core_module::runner::Runner; -/// -/// let mut runner = Runner::new([0xaa; 20], None, None, None); -/// runner.stack.push([0x01, 0x00, 0x00, 0x00].to_vec()).unwrap(); -/// runner.stack.push([0x00, 0x00, 0x00, 0x00].to_vec()).unwrap(); -/// -/// sdiv(&mut runner).unwrap(); -/// -/// assert_eq!(runner.stack.pop().unwrap(), [0x00, 0x00, 0x00, 0x00].to_vec()); -/// ``` pub fn sdiv(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = I256::from_raw(U256::from_big_endian(&pop1)); - let b = I256::from_raw(U256::from_big_endian(&pop2)); + let a = I256::from_raw(U256::from_big_endian(&pop1)); + let b = I256::from_raw(U256::from_big_endian(&pop2)); - let result = a.checked_div(b); + let result = a.checked_div(b); - let mut result_bytes = [0u8; 32]; - result - .unwrap_or(I256::from(0)) - .to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result + .unwrap_or(I256::from(0)) + .to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SDIV".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SDIV".bright_blue(), hex)); } // Increment PC runner.increment_pc(1) } -/// Computes the remainder of the signed division of the two topmost items on the stack. -/// If the divisor is zero, the result is zero. -/// -/// # Example -/// -/// ``` -/// use bytecode_rs::core_module::op_codes::math::unsigned::smodulo; -/// use bytecode_rs::core_module::runner::Runner; -/// -/// let mut runner = Runner::new([0xaa; 20], None, None, None); -/// runner.stack.push([0x01, 0x00, 0x00, 0x00].to_vec()).unwrap(); -/// runner.stack.push([0x00, 0x00, 0x00, 0x00].to_vec()).unwrap(); -/// -/// smodulo(&mut runner).unwrap(); -/// -/// assert_eq!(runner.stack.pop().unwrap(), [0x00, 0x00, 0x00, 0x00].to_vec()); -/// ``` pub fn smodulo(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = I256::from_raw(U256::from_big_endian(&pop1)); - let b = I256::from_raw(U256::from_big_endian(&pop2)); + let a = I256::from_raw(U256::from_big_endian(&pop1)); + let b = I256::from_raw(U256::from_big_endian(&pop2)); - let result = a.checked_rem(b); + let result = a.checked_rem(b); - let mut result_bytes = [0u8; 32]; - result - .unwrap_or(I256::from(0)) - .to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result + .unwrap_or(I256::from(0)) + .to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SMOD".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SMOD".bright_blue(), hex)); } // Increment PC diff --git a/src/core_module/op_codes/arithmetic/unsigned.rs b/src/core_module/op_codes/arithmetic/unsigned.rs index 3f0b768..b2c511b 100644 --- a/src/core_module/op_codes/arithmetic/unsigned.rs +++ b/src/core_module/op_codes/arithmetic/unsigned.rs @@ -18,28 +18,26 @@ use colored::*; /// /// Returns an `ExecutionError` if there are not enough elements on the stack or if there is an error pushing the result onto the stack. pub fn add(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); - let (result, _) = a.overflowing_add(b); + let (result, _) = a.overflowing_add(b); - let mut result_bytes = [0u8; 32]; - result.to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result.to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "ADD".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "ADD".bright_blue(), hex)); } // Increment PC @@ -56,28 +54,26 @@ pub fn add(runner: &mut Runner) -> Result<(), ExecutionError> { /// /// Returns an `ExecutionError` if there are not enough elements on the stack or if there is an error pushing the result onto the stack. pub fn mul(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); - let (result, _) = a.overflowing_mul(b); + let (result, _) = a.overflowing_mul(b); - let mut result_bytes = [0u8; 32]; - result.to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result.to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "MUL".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "MUL".bright_blue(), hex)); } // Increment PC @@ -94,28 +90,26 @@ pub fn mul(runner: &mut Runner) -> Result<(), ExecutionError> { /// /// Returns an `ExecutionError` if there are not enough elements on the stack or if there is an error pushing the result onto the stack. pub fn sub(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); - let (result, _) = a.overflowing_sub(b); + let (result, _) = a.overflowing_sub(b); - let mut result_bytes = [0u8; 32]; - result.to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result.to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SUB".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SUB".bright_blue(), hex)); } // Increment PC @@ -132,30 +126,28 @@ pub fn sub(runner: &mut Runner) -> Result<(), ExecutionError> { /// /// Returns an `ExecutionError` if there are not enough elements on the stack or if there is an error pushing the result onto the stack. pub fn modulo(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); - let result = a.checked_rem(b); + let result = a.checked_rem(b); - let mut result_bytes = [0u8; 32]; - result - .unwrap_or(U256::from(0)) - .to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result + .unwrap_or(U256::from(0)) + .to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "MOD".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "MOD".bright_blue(), hex)); } // Increment PC @@ -172,30 +164,28 @@ pub fn modulo(runner: &mut Runner) -> Result<(), ExecutionError> { /// /// Returns an `ExecutionError` if there are not enough elements on the stack or if there is an error pushing the result onto the stack. pub fn div(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); - let result = a.checked_div(b); + let result = a.checked_div(b); - let mut result_bytes = [0u8; 32]; - result - .unwrap_or(U256::from(0)) - .to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result + .unwrap_or(U256::from(0)) + .to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "DIV".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "DIV".bright_blue(), hex)); } // Increment PC @@ -212,33 +202,31 @@ pub fn div(runner: &mut Runner) -> Result<(), ExecutionError> { /// /// Returns an `ExecutionError` if there are not enough elements on the stack or if there is an error pushing the result onto the stack. pub fn addmod(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; - let pop3 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; + let pop3 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); - let c = U256::from_big_endian(&pop3); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); + let c = U256::from_big_endian(&pop3); - let (result, _) = a.overflowing_add(b); - let result = result.checked_rem(c); + let (result, _) = a.overflowing_add(b); + let result = result.checked_rem(c); - let mut result_bytes = [0u8; 32]; - result - .unwrap_or(U256::from(0)) - .to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result + .unwrap_or(U256::from(0)) + .to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "ADDMOD".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "ADDMOD".bright_blue(), hex)); } // Increment PC @@ -255,33 +243,31 @@ pub fn addmod(runner: &mut Runner) -> Result<(), ExecutionError> { /// /// Returns an `ExecutionError` if there are not enough elements on the stack or if there is an error pushing the result onto the stack. pub fn mulmod(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; - let pop3 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; + let pop3 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); - let c = U256::from_big_endian(&pop3); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); + let c = U256::from_big_endian(&pop3); - let (result, _) = a.overflowing_mul(b); - let result = result.checked_rem(c); + let (result, _) = a.overflowing_mul(b); + let result = result.checked_rem(c); - let mut result_bytes = [0u8; 32]; - result - .unwrap_or(U256::from(0)) - .to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result + .unwrap_or(U256::from(0)) + .to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "MULMOD".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "MULMOD".bright_blue(), hex)); } // Increment PC @@ -298,28 +284,26 @@ pub fn mulmod(runner: &mut Runner) -> Result<(), ExecutionError> { /// /// Returns an `ExecutionError` if there are not enough elements on the stack or if there is an error pushing the result onto the stack. pub fn exp(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); - let (result, _) = a.overflowing_pow(b); + let (result, _) = a.overflowing_pow(b); - let mut result_bytes = [0u8; 32]; - result.to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result.to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "EXP".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "EXP".bright_blue(), hex)); } // Increment PC diff --git a/src/core_module/op_codes/bitwise.rs b/src/core_module/op_codes/bitwise.rs index e18a3d0..f4c6900 100644 --- a/src/core_module/op_codes/bitwise.rs +++ b/src/core_module/op_codes/bitwise.rs @@ -10,26 +10,24 @@ use ethers::utils::keccak256; use colored::*; pub fn not(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); + let a = U256::from_big_endian(&pop1); - let result = !a; + let result = !a; - let mut result_bytes = [0u8; 32]; - result.to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result.to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "NOT".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "NOT".bright_blue(), hex)); } // Increment PC @@ -37,28 +35,26 @@ pub fn not(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn xor(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); - let result = a ^ b; + let result = a ^ b; - let mut result_bytes = [0u8; 32]; - result.to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result.to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "XOR".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "XOR".bright_blue(), hex)); } // Increment PC @@ -66,28 +62,26 @@ pub fn xor(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn or(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); - let result = a | b; + let result = a | b; - let mut result_bytes = [0u8; 32]; - result.to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result.to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "OR".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "OR".bright_blue(), hex)); } // Increment PC @@ -95,28 +89,26 @@ pub fn or(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn and(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); - let result = a & b; + let result = a & b; - let mut result_bytes = [0u8; 32]; - result.to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result.to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "AND".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "AND".bright_blue(), hex)); } // Increment PC @@ -124,28 +116,26 @@ pub fn and(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn shl(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); - let result = b << a; + let result = b << a; - let mut result_bytes = [0u8; 32]; - result.to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result.to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SHL".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SHL".bright_blue(), hex)); } // Increment PC @@ -153,28 +143,26 @@ pub fn shl(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn shr(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); - let result = b >> a; + let result = b >> a; - let mut result_bytes = [0u8; 32]; - result.to_big_endian(&mut result_bytes); + let mut result_bytes = [0u8; 32]; + result.to_big_endian(&mut result_bytes); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SHR".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SHR".bright_blue(), hex)); } // Increment PC @@ -182,27 +170,25 @@ pub fn shr(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn sha(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; - let offset = U256::from_big_endian(&pop1).as_usize(); - let size = U256::from_big_endian(&pop2).as_usize(); + let offset = U256::from_big_endian(&pop1).as_usize(); + let size = U256::from_big_endian(&pop2).as_usize(); - let data_to_hash = runner.memory.read(offset, size); + let data_to_hash = unsafe { runner.memory.read(offset, size) }; - if data_to_hash.is_err() { - return Err(data_to_hash.unwrap_err()); - } + if data_to_hash.is_err() { + return Err(data_to_hash.unwrap_err()); + } - let bytes = keccak256(&data_to_hash?); + let bytes = keccak256(&data_to_hash?); - runner.stack.push(bytes)?; + runner.stack.push(bytes)?; - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SHA".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SHA".bright_blue(), hex)); } // Increment PC @@ -217,11 +203,11 @@ mod tests { #[test] fn test_not() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; + let _ = runner.stack.push(pad_left(&[0x04])); not(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, @@ -235,12 +221,12 @@ mod tests { #[test] fn test_xor() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x08])) }; + let _ = runner.stack.push(pad_left(&[0x04])); + let _ = runner.stack.push(pad_left(&[0x08])); xor(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[0x04 ^ 0x08]); assert_eq!(result, expected_result); @@ -250,12 +236,12 @@ mod tests { #[test] fn or_test() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x08])) }; + let _ = runner.stack.push(pad_left(&[0x04])); + let _ = runner.stack.push(pad_left(&[0x08])); or(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[0x04 | 0x08]); assert_eq!(result, expected_result); @@ -265,12 +251,12 @@ mod tests { #[test] fn test_and() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x08])) }; + let _ = runner.stack.push(pad_left(&[0x04])); + let _ = runner.stack.push(pad_left(&[0x08])); and(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[0x04 & 0x08]); assert_eq!(result, expected_result); @@ -280,12 +266,12 @@ mod tests { #[test] fn test_shl() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x02])) }; + let _ = runner.stack.push(pad_left(&[0x04])); + let _ = runner.stack.push(pad_left(&[0x02])); shl(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[0x10]); assert_eq!(result, expected_result); @@ -295,12 +281,12 @@ mod tests { #[test] fn test_shr() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x02])) }; + let _ = runner.stack.push(pad_left(&[0x04])); + let _ = runner.stack.push(pad_left(&[0x02])); shr(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[0x01]); assert_eq!(result, expected_result); @@ -311,14 +297,14 @@ mod tests { fn test_sha256() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0xff, 0xff, 0xff, 0xff])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; + let _ = runner.stack.push(pad_left(&[0xff, 0xff, 0xff, 0xff])); + let _ = runner.stack.push(pad_left(&[0x00])); mstore(&mut runner).unwrap(); - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; + let _ = runner.stack.push(pad_left(&[0x04])); + let _ = runner.stack.push(pad_left(&[0x00])); sha(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_output = [ 0xe8, 0xe7, 0x76, 0x26, 0x58, 0x6f, 0x73, 0xb9, 0x55, 0x36, 0x4c, 0x7b, 0x4b, 0xbf, 0x0b, 0xb7, 0xf7, 0x68, 0x5e, 0xbd, 0x40, 0xe8, 0x52, 0xb1, 0x64, 0x63, 0x3a, 0x4a, diff --git a/src/core_module/op_codes/comparison.rs b/src/core_module/op_codes/comparison.rs index 2329868..5e68f05 100644 --- a/src/core_module/op_codes/comparison.rs +++ b/src/core_module/op_codes/comparison.rs @@ -9,32 +9,30 @@ use ethers::types::{I256, U256}; use colored::*; pub fn iszero(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; + let pop1 = runner.stack.pop()?; - let a = U256::from_big_endian(&pop1); + let a = U256::from_big_endian(&pop1); - let bool = a.is_zero(); + let bool = a.is_zero(); - let result_bytes = [ - [0u8; 31].to_vec(), - [if bool { 1u8 } else { 0u8 }; 1].to_vec(), - ] - .concat() - .as_slice() - .try_into() - .unwrap(); + let result_bytes = [ + [0u8; 31].to_vec(), + [if bool { 1u8 } else { 0u8 }; 1].to_vec(), + ] + .concat() + .as_slice() + .try_into() + .unwrap(); - let result = runner.stack.push(result_bytes); + let result = runner.stack.push(result_bytes); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); + } - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "ISZERO".bright_blue(), hex)); - } + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "ISZERO".bright_blue(), hex)); } // Increment PC @@ -42,34 +40,32 @@ pub fn iszero(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn eq(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; - - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); - - let bool = a.eq(&b); - - let result_bytes = [ - [0u8; 31].to_vec(), - [if bool { 1u8 } else { 0u8 }; 1].to_vec(), - ] - .concat() - .as_slice() - .try_into() - .unwrap(); - - let result = runner.stack.push(result_bytes); - - if result.is_err() { - return Err(result.unwrap_err()); - } - - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "EQ".bright_blue(), hex)); - } + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; + + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); + + let bool = a.eq(&b); + + let result_bytes = [ + [0u8; 31].to_vec(), + [if bool { 1u8 } else { 0u8 }; 1].to_vec(), + ] + .concat() + .as_slice() + .try_into() + .unwrap(); + + let result = runner.stack.push(result_bytes); + + if result.is_err() { + return Err(result.unwrap_err()); + } + + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "EQ".bright_blue(), hex)); } // Increment PC @@ -77,34 +73,32 @@ pub fn eq(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn lt(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; - - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); - - let bool = a.lt(&b); - - let result_bytes = [ - [0u8; 31].to_vec(), - [if bool { 1u8 } else { 0u8 }; 1].to_vec(), - ] - .concat() - .as_slice() - .try_into() - .expect("Wrong length"); - - let result = runner.stack.push(result_bytes); - - if result.is_err() { - return Err(result.unwrap_err()); - } - - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "LT".bright_blue(), hex)); - } + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; + + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); + + let bool = a.lt(&b); + + let result_bytes = [ + [0u8; 31].to_vec(), + [if bool { 1u8 } else { 0u8 }; 1].to_vec(), + ] + .concat() + .as_slice() + .try_into() + .expect("Wrong length"); + + let result = runner.stack.push(result_bytes); + + if result.is_err() { + return Err(result.unwrap_err()); + } + + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "LT".bright_blue(), hex)); } // Increment PC @@ -112,34 +106,32 @@ pub fn lt(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn gt(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; - - let a = U256::from_big_endian(&pop1); - let b = U256::from_big_endian(&pop2); - - let bool = a.gt(&b); - - let result_bytes = [ - [0u8; 31].to_vec(), - [if bool { 1u8 } else { 0u8 }; 1].to_vec(), - ] - .concat() - .as_slice() - .try_into() - .unwrap(); - - let result = runner.stack.push(result_bytes); - - if result.is_err() { - return Err(result.unwrap_err()); - } - - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "GT".bright_blue(), hex)); - } + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; + + let a = U256::from_big_endian(&pop1); + let b = U256::from_big_endian(&pop2); + + let bool = a.gt(&b); + + let result_bytes = [ + [0u8; 31].to_vec(), + [if bool { 1u8 } else { 0u8 }; 1].to_vec(), + ] + .concat() + .as_slice() + .try_into() + .unwrap(); + + let result = runner.stack.push(result_bytes); + + if result.is_err() { + return Err(result.unwrap_err()); + } + + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "GT".bright_blue(), hex)); } // Increment PC @@ -147,34 +139,32 @@ pub fn gt(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn slt(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; - - let a = I256::from_raw(U256::from_big_endian(&pop1)); - let b = I256::from_raw(U256::from_big_endian(&pop2)); - - let bool = a.lt(&b); - - let result_bytes = [ - [0u8; 31].to_vec(), - [if bool { 1u8 } else { 0u8 }; 1].to_vec(), - ] - .concat() - .as_slice() - .try_into() - .unwrap(); - - let result = runner.stack.push(result_bytes); - - if result.is_err() { - return Err(result.unwrap_err()); - } - - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SLT".bright_blue(), hex)); - } + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; + + let a = I256::from_raw(U256::from_big_endian(&pop1)); + let b = I256::from_raw(U256::from_big_endian(&pop2)); + + let bool = a.lt(&b); + + let result_bytes = [ + [0u8; 31].to_vec(), + [if bool { 1u8 } else { 0u8 }; 1].to_vec(), + ] + .concat() + .as_slice() + .try_into() + .unwrap(); + + let result = runner.stack.push(result_bytes); + + if result.is_err() { + return Err(result.unwrap_err()); + } + + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SLT".bright_blue(), hex)); } // Increment PC @@ -182,34 +172,32 @@ pub fn slt(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn sgt(runner: &mut Runner) -> Result<(), ExecutionError> { - unsafe { - let pop1 = runner.stack.pop()?; - let pop2 = runner.stack.pop()?; - - let a = I256::from_raw(U256::from_big_endian(&pop1)); - let b = I256::from_raw(U256::from_big_endian(&pop2)); - - let bool = a.gt(&b); - - let result_bytes = [ - [0u8; 31].to_vec(), - [if bool { 1u8 } else { 0u8 }; 1].to_vec(), - ] - .concat() - .as_slice() - .try_into() - .unwrap(); - - let result = runner.stack.push(result_bytes); - - if result.is_err() { - return Err(result.unwrap_err()); - } - - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(result_bytes); - runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SGT".bright_blue(), hex)); - } + let pop1 = runner.stack.pop()?; + let pop2 = runner.stack.pop()?; + + let a = I256::from_raw(U256::from_big_endian(&pop1)); + let b = I256::from_raw(U256::from_big_endian(&pop2)); + + let bool = a.gt(&b); + + let result_bytes = [ + [0u8; 31].to_vec(), + [if bool { 1u8 } else { 0u8 }; 1].to_vec(), + ] + .concat() + .as_slice() + .try_into() + .unwrap(); + + let result = runner.stack.push(result_bytes); + + if result.is_err() { + return Err(result.unwrap_err()); + } + + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(result_bytes); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SGT".bright_blue(), hex)); } // Increment PC @@ -223,22 +211,22 @@ mod tests { #[test] fn iszero_test() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; + let _ = runner.stack.push(pad_left(&[0x00])); iszero(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[1]); assert_eq!(result, expected_result); assert_eq!(runner.stack.stack.len(), 0); let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x01])) }; + let _ = runner.stack.push(pad_left(&[0x01])); iszero(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[0]); assert_eq!(result, expected_result); @@ -248,24 +236,24 @@ mod tests { #[test] fn eq_test() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; + let _ = runner.stack.push(pad_left(&[0x04])); + let _ = runner.stack.push(pad_left(&[0x04])); eq(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[1]); assert_eq!(result, expected_result); assert_eq!(runner.stack.stack.len(), 0); let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x05])) }; + let _ = runner.stack.push(pad_left(&[0x04])); + let _ = runner.stack.push(pad_left(&[0x05])); eq(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[0]); assert_eq!(result, expected_result); @@ -275,24 +263,24 @@ mod tests { #[test] fn lt_test() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x08])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; + let _ = runner.stack.push(pad_left(&[0x08])); + let _ = runner.stack.push(pad_left(&[0x04])); lt(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[1]); assert_eq!(result, expected_result); assert_eq!(runner.stack.stack.len(), 0); let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x08])) }; + let _ = runner.stack.push(pad_left(&[0x04])); + let _ = runner.stack.push(pad_left(&[0x08])); lt(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[0]); assert_eq!(result, expected_result); @@ -302,24 +290,24 @@ mod tests { #[test] fn gt_test() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x08])) }; + let _ = runner.stack.push(pad_left(&[0x04])); + let _ = runner.stack.push(pad_left(&[0x08])); gt(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[1]); assert_eq!(result, expected_result); assert_eq!(runner.stack.stack.len(), 0); let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x08])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x04])) }; + let _ = runner.stack.push(pad_left(&[0x08])); + let _ = runner.stack.push(pad_left(&[0x04])); gt(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[0]); assert_eq!(result, expected_result); @@ -329,36 +317,32 @@ mod tests { #[test] fn slt_test() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x09])) }; - let _ = unsafe { - runner.stack.push([ - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - ]) - }; + let _ = runner.stack.push(pad_left(&[0x09])); + let _ = runner.stack.push([ + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + ]); slt(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[1]); assert_eq!(result, expected_result); assert_eq!(runner.stack.stack.len(), 0); let mut runner = Runner::_default(3); - let _ = unsafe { - runner.stack.push([ - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - ]) - }; - let _ = unsafe { runner.stack.push(pad_left(&[0x09])) }; + let _ = runner.stack.push([ + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + ]); + let _ = runner.stack.push(pad_left(&[0x09])); slt(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[0]); assert_eq!(result, expected_result); @@ -368,36 +352,32 @@ mod tests { #[test] fn sgt_test() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&[0x09])) }; - let _ = unsafe { - runner.stack.push([ - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - ]) - }; + let _ = runner.stack.push(pad_left(&[0x09])); + let _ = runner.stack.push([ + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + ]); sgt(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[0]); assert_eq!(result, expected_result); assert_eq!(runner.stack.stack.len(), 0); let mut runner = Runner::_default(3); - let _ = unsafe { - runner.stack.push([ - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - ]) - }; - let _ = unsafe { runner.stack.push(pad_left(&[0x09])) }; + let _ = runner.stack.push([ + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + ]); + let _ = runner.stack.push(pad_left(&[0x09])); sgt(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop() }.unwrap(); + let result = runner.stack.pop().unwrap(); let expected_result = pad_left(&[1]); assert_eq!(result, expected_result); diff --git a/src/core_module/op_codes/environment.rs b/src/core_module/op_codes/environment.rs index 20a81f6..356d8e9 100644 --- a/src/core_module/op_codes/environment.rs +++ b/src/core_module/op_codes/environment.rs @@ -19,7 +19,7 @@ pub fn address(runner: &mut Runner) -> Result<(), ExecutionError> { .try_into() .unwrap(); - let result = unsafe { runner.stack.push(address) }; + let result = runner.stack.push(address); if result.is_err() { return Err(result.unwrap_err()); @@ -35,12 +35,12 @@ pub fn address(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn balance(runner: &mut Runner) -> Result<(), ExecutionError> { - let address: [u8; 32] = unsafe { runner.stack.pop()? }; + let address: [u8; 32] = runner.stack.pop()?; let address: [u8; 20] = address[12..].try_into().unwrap(); let balance = get_balance(address, runner)?; - let result = unsafe { runner.stack.push(pad_left(&balance)) }; + let result = runner.stack.push(pad_left(&balance)); if result.is_err() { return Err(result.unwrap_err()); @@ -62,7 +62,7 @@ pub fn origin(runner: &mut Runner) -> Result<(), ExecutionError> { .try_into() .unwrap(); - let result = unsafe { runner.stack.push(origin) }; + let result = runner.stack.push(origin); if result.is_err() { return Err(result.unwrap_err()); @@ -84,7 +84,7 @@ pub fn caller(runner: &mut Runner) -> Result<(), ExecutionError> { .try_into() .unwrap(); - let result = unsafe { runner.stack.push(caller) }; + let result = runner.stack.push(caller); if result.is_err() { return Err(result.unwrap_err()); @@ -100,7 +100,7 @@ pub fn caller(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn callvalue(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.push(runner.callvalue) }; + let result = runner.stack.push(runner.callvalue); if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { let hex: String = utils::debug::to_hex_string(runner.callvalue); @@ -116,13 +116,13 @@ pub fn callvalue(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn calldataload(runner: &mut Runner) -> Result<(), ExecutionError> { - let address = unsafe { runner.stack.pop()? }; + let address = runner.stack.pop()?; let address = U256::from_big_endian(&address).as_usize(); let calldata = unsafe { runner.calldata.read(address, 32)? }; let calldata = calldata.as_slice().try_into().unwrap(); - let result = unsafe { runner.stack.push(calldata) }; + let result = runner.stack.push(calldata); if result.is_err() { return Err(result.unwrap_err()); @@ -143,7 +143,7 @@ pub fn calldatasize(runner: &mut Runner) -> Result<(), ExecutionError> { // Convert the usize to bytes in little-endian order let calldatasize = pad_left(&size); - let result = unsafe { runner.stack.push(calldatasize) }; + let result = runner.stack.push(calldatasize); if result.is_err() { return Err(result.unwrap_err()); @@ -159,9 +159,9 @@ pub fn calldatasize(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn calldatacopy(runner: &mut Runner) -> Result<(), ExecutionError> { - let dest_offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }).as_usize(); - let _offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }).as_usize(); - let _size = U256::from_big_endian(&unsafe { runner.stack.pop()? }).as_usize(); + let dest_offset = U256::from_big_endian(&runner.stack.pop()?).as_usize(); + let _offset = U256::from_big_endian(&runner.stack.pop()?).as_usize(); + let _size = U256::from_big_endian(&runner.stack.pop()?).as_usize(); let calldata = unsafe { runner.calldata.read(_offset, _size)? }; @@ -188,7 +188,7 @@ pub fn codesize(runner: &mut Runner) -> Result<(), ExecutionError> { pad_left(&code.unwrap().len().to_be_bytes()) }; - let result = unsafe { runner.stack.push(codesize) }; + let result = runner.stack.push(codesize); if result.is_err() { return Err(result.unwrap_err()); @@ -205,9 +205,9 @@ pub fn codesize(runner: &mut Runner) -> Result<(), ExecutionError> { // Can be mocked with a fork pub fn codecopy(runner: &mut Runner) -> Result<(), ExecutionError> { - let dest_offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }).as_usize(); - let offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }).as_usize(); - let size = U256::from_big_endian(&unsafe { runner.stack.pop()? }).as_usize(); + let dest_offset = U256::from_big_endian(&runner.stack.pop()?).as_usize(); + let offset = U256::from_big_endian(&runner.stack.pop()?).as_usize(); + let size = U256::from_big_endian(&runner.stack.pop()?).as_usize(); let code = runner.state.get_code_at(runner.address); @@ -241,7 +241,7 @@ pub fn gasprice(runner: &mut Runner) -> Result<(), ExecutionError> { .try_into() .unwrap(); - let result = unsafe { runner.stack.push(gasprice) }; + let result = runner.stack.push(gasprice); if result.is_err() { return Err(result.unwrap_err()); @@ -257,7 +257,7 @@ pub fn gasprice(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn extcodesize(runner: &mut Runner) -> Result<(), ExecutionError> { - let address = unsafe { runner.stack.pop()? }; + let address = runner.stack.pop()?; let code = runner.state.get_code_at(bytes32_to_address(&address)); @@ -267,7 +267,7 @@ pub fn extcodesize(runner: &mut Runner) -> Result<(), ExecutionError> { pad_left(&code.unwrap().len().to_be_bytes()) }; - let result = unsafe { runner.stack.push(codesize) }; + let result = runner.stack.push(codesize); if result.is_err() { return Err(result.unwrap_err()); @@ -284,10 +284,10 @@ pub fn extcodesize(runner: &mut Runner) -> Result<(), ExecutionError> { // Can be mocked with a fork pub fn extcodecopy(runner: &mut Runner) -> Result<(), ExecutionError> { - let address = unsafe { runner.stack.pop()? }; - let dest_offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }).as_usize(); - let offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }).as_usize(); - let size = U256::from_big_endian(&unsafe { runner.stack.pop()? }).as_usize(); + let address = runner.stack.pop()?; + let dest_offset = U256::from_big_endian(&runner.stack.pop()?).as_usize(); + let offset = U256::from_big_endian(&runner.stack.pop()?).as_usize(); + let size = U256::from_big_endian(&runner.stack.pop()?).as_usize(); let code = runner.state.get_code_at(bytes32_to_address(&address)); @@ -320,7 +320,7 @@ pub fn returndatasize(runner: &mut Runner) -> Result<(), ExecutionError> { // Convert the usize to bytes in little-endian order let returndatasize = pad_left(&size); - let result = unsafe { runner.stack.push(returndatasize) }; + let result = runner.stack.push(returndatasize); if result.is_err() { return Err(result.unwrap_err()); @@ -336,9 +336,9 @@ pub fn returndatasize(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn returndatacopy(runner: &mut Runner) -> Result<(), ExecutionError> { - let dest_offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }).as_usize(); - let _offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }).as_usize(); - let _size = U256::from_big_endian(&unsafe { runner.stack.pop()? }).as_usize(); + let dest_offset = U256::from_big_endian(&runner.stack.pop()?).as_usize(); + let _offset = U256::from_big_endian(&runner.stack.pop()?).as_usize(); + let _size = U256::from_big_endian(&runner.stack.pop()?).as_usize(); let returndata = unsafe { runner.returndata.read(_offset, _size)? }; @@ -358,12 +358,12 @@ pub fn returndatacopy(runner: &mut Runner) -> Result<(), ExecutionError> { // Can be mocked with a fork pub fn extcodehash(runner: &mut Runner) -> Result<(), ExecutionError> { - let address = unsafe { runner.stack.pop()? }; + let address = runner.stack.pop()?; let code = runner.state.get_code_at(bytes32_to_address(&address))?; let codehash = keccak256(&code); - let result = unsafe { runner.stack.push(codehash) }; + let result = runner.stack.push(codehash); if result.is_err() { return Err(result.unwrap_err()); @@ -379,13 +379,13 @@ pub fn extcodehash(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn blockhash(runner: &mut Runner) -> Result<(), ExecutionError> { - let block: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let block: U256 = U256::from_big_endian(&runner.stack.pop()?); let mut bytes = [0; 32]; block.to_big_endian(&mut bytes); let blockhash = keccak256(bytes); - let result = unsafe { runner.stack.push(blockhash) }; + let result = runner.stack.push(blockhash); if result.is_err() { return Err(result.unwrap_err()); @@ -403,7 +403,7 @@ pub fn blockhash(runner: &mut Runner) -> Result<(), ExecutionError> { pub fn coinbase(runner: &mut Runner) -> Result<(), ExecutionError> { let coinbase = pad_left(&[0xc0u8; 20]); - let result = unsafe { runner.stack.push(coinbase) }; + let result = runner.stack.push(coinbase); if result.is_err() { return Err(result.unwrap_err()); @@ -434,7 +434,7 @@ pub fn timestamp(runner: &mut Runner) -> Result<(), ExecutionError> { let bytes = pad_left(×tamp_bytes); - let result = unsafe { runner.stack.push(bytes) }; + let result = runner.stack.push(bytes); if result.is_err() { return Err(result.unwrap_err()); @@ -453,7 +453,7 @@ pub fn timestamp(runner: &mut Runner) -> Result<(), ExecutionError> { pub fn number(runner: &mut Runner) -> Result<(), ExecutionError> { let number = pad_left(&[0xff; 4]); - let result = unsafe { runner.stack.push(number) }; + let result = runner.stack.push(number); if result.is_err() { return Err(result.unwrap_err()); @@ -472,7 +472,7 @@ pub fn number(runner: &mut Runner) -> Result<(), ExecutionError> { pub fn difficulty(runner: &mut Runner) -> Result<(), ExecutionError> { let difficulty = pad_left(&[0x45; 8]); - let result = unsafe { runner.stack.push(difficulty) }; + let result = runner.stack.push(difficulty); if result.is_err() { return Err(result.unwrap_err()); @@ -491,7 +491,7 @@ pub fn difficulty(runner: &mut Runner) -> Result<(), ExecutionError> { pub fn gaslimit(runner: &mut Runner) -> Result<(), ExecutionError> { let gaslimit = pad_left(&[0x01, 0xC9, 0xC3, 0x80]); - let result = unsafe { runner.stack.push(gaslimit) }; + let result = runner.stack.push(gaslimit); if result.is_err() { return Err(result.unwrap_err()); @@ -510,7 +510,7 @@ pub fn gaslimit(runner: &mut Runner) -> Result<(), ExecutionError> { pub fn chainid(runner: &mut Runner) -> Result<(), ExecutionError> { let chainid = pad_left(&[0x01]); - let result = unsafe { runner.stack.push(chainid) }; + let result = runner.stack.push(chainid); if result.is_err() { return Err(result.unwrap_err()); @@ -528,7 +528,7 @@ pub fn chainid(runner: &mut Runner) -> Result<(), ExecutionError> { pub fn selfbalance(runner: &mut Runner) -> Result<(), ExecutionError> { let balance = get_balance(runner.address, runner)?; - let result = unsafe { runner.stack.push(balance) }; + let result = runner.stack.push(balance); if result.is_err() { return Err(result.unwrap_err()); @@ -546,7 +546,7 @@ pub fn selfbalance(runner: &mut Runner) -> Result<(), ExecutionError> { pub fn basefee(runner: &mut Runner) -> Result<(), ExecutionError> { let basefee = pad_left(&[0x0a]); - let result = unsafe { runner.stack.push(basefee) }; + let result = runner.stack.push(basefee); if result.is_err() { return Err(result.unwrap_err()); @@ -571,17 +571,17 @@ mod tests { let mut runner = Runner::_default(3); address(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&runner.address)); } #[test] fn test_balance() { let mut runner = Runner::_default(3); - let _ = unsafe { runner.stack.push(pad_left(&runner.caller)) }; + let _ = runner.stack.push(pad_left(&runner.caller)); balance(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!( result, pad_left(&[0x36, 0x35, 0xC9, 0xAD, 0xC5, 0xDE, 0xA0, 0x00, 0x00]) @@ -592,10 +592,10 @@ mod tests { .state .transfer(runner.caller, runner.address, pad_left(&[0x01])); - let _ = unsafe { runner.stack.push(pad_left(&runner.caller)) }; + let _ = runner.stack.push(pad_left(&runner.caller)); balance(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!( result, pad_left(&[0x36, 0x35, 0xC9, 0xAD, 0xC5, 0xDE, 0x9F, 0xFF, 0xFF]) @@ -607,7 +607,7 @@ mod tests { let mut runner = Runner::_default(3); origin(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&runner.origin)); } @@ -616,7 +616,7 @@ mod tests { let mut runner = Runner::_default(3); caller(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&runner.caller)); } @@ -625,7 +625,7 @@ mod tests { let mut runner = Runner::_default(3); callvalue(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x00])); } @@ -634,16 +634,16 @@ mod tests { let mut runner = Runner::_default(3); runner.calldata.heap = vec![0xff, 0xff, 0xff, 0xff]; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; + let _ = runner.stack.push(pad_left(&[0x00])); calldataload(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, _pad_right(&[0xff, 0xff, 0xff, 0xff])); - let _ = unsafe { runner.stack.push(pad_left(&[0x02])) }; + let _ = runner.stack.push(pad_left(&[0x02])); calldataload(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, _pad_right(&[0xff, 0xff])); } @@ -654,7 +654,7 @@ mod tests { calldatasize(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x04])); } @@ -663,26 +663,26 @@ mod tests { let mut runner = Runner::_default(3); runner.calldata.heap = [0xff; 32].to_vec(); - let _ = unsafe { runner.stack.push(pad_left(&[0x20])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; + let _ = runner.stack.push(pad_left(&[0x20])); + let _ = runner.stack.push(pad_left(&[0x00])); + let _ = runner.stack.push(pad_left(&[0x00])); calldatacopy(&mut runner).unwrap(); let result = unsafe { runner.memory.read(0x00, 0x20).unwrap() }; assert_eq!(result, [0xff; 32].to_vec()); - let _ = unsafe { runner.stack.push(pad_left(&[0x10])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; + let _ = runner.stack.push(pad_left(&[0x10])); + let _ = runner.stack.push(pad_left(&[0x00])); + let _ = runner.stack.push(pad_left(&[0x00])); calldatacopy(&mut runner).unwrap(); let result = unsafe { runner.memory.read(0x00, 0x20).unwrap() }; assert_eq!(result, [0xff; 32].to_vec()); runner.memory.heap = vec![0x00; 32]; - let _ = unsafe { runner.stack.push(pad_left(&[0x10])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; + let _ = runner.stack.push(pad_left(&[0x10])); + let _ = runner.stack.push(pad_left(&[0x00])); + let _ = runner.stack.push(pad_left(&[0x00])); calldatacopy(&mut runner).unwrap(); let result = unsafe { runner.memory.read(0x00, 0x20).unwrap() }; @@ -700,7 +700,7 @@ mod tests { codesize(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0xa])); } @@ -718,9 +718,9 @@ mod tests { ); assert!(interpret_result.is_ok()); - let _ = unsafe { runner.stack.push(pad_left(&[0x20])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; + let _ = runner.stack.push(pad_left(&[0x20])); + let _ = runner.stack.push(pad_left(&[0x00])); + let _ = runner.stack.push(pad_left(&[0x00])); codecopy(&mut runner).unwrap(); let result = unsafe { runner.memory.read(0x00, 0x20).unwrap() }; @@ -732,9 +732,9 @@ mod tests { // reset memory runner.memory.heap = vec![]; - let _ = unsafe { runner.stack.push(pad_left(&[0x05])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; + let _ = runner.stack.push(pad_left(&[0x05])); + let _ = runner.stack.push(pad_left(&[0x00])); + let _ = runner.stack.push(pad_left(&[0x00])); codecopy(&mut runner).unwrap(); let result = unsafe { runner.memory.read(0x00, 0x20).unwrap() }; @@ -746,7 +746,7 @@ mod tests { let mut runner = Runner::_default(3); gasprice(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0xff])); } @@ -764,7 +764,7 @@ mod tests { extcodesize(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x17])); } @@ -783,10 +783,10 @@ mod tests { // reset memory runner.memory.heap = vec![]; - let _ = unsafe { runner.stack.push(pad_left(&[0x17])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; - let _ = unsafe { runner.stack.dup(4) }; + let _ = runner.stack.push(pad_left(&[0x17])); + let _ = runner.stack.push(pad_left(&[0x00])); + let _ = runner.stack.push(pad_left(&[0x00])); + let _ = runner.stack.dup(4); extcodecopy(&mut runner).unwrap(); let result = unsafe { runner.memory.read(0x00, 0x20).unwrap() }; @@ -800,10 +800,10 @@ mod tests { // reset memory runner.memory.heap = vec![]; - let _ = unsafe { runner.stack.push(pad_left(&[0xa])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x00])) }; - let _ = unsafe { runner.stack.push(pad_left(&[0x20])) }; - let _ = unsafe { runner.stack.dup(4) }; + let _ = runner.stack.push(pad_left(&[0xa])); + let _ = runner.stack.push(pad_left(&[0x00])); + let _ = runner.stack.push(pad_left(&[0x20])); + let _ = runner.stack.dup(4); extcodecopy(&mut runner).unwrap(); let result = unsafe { runner.memory.read(0x00, 0x20).unwrap() }; @@ -827,7 +827,7 @@ mod tests { ); assert!(interpret_result.is_ok()); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x20])); } @@ -863,7 +863,7 @@ mod tests { ); assert!(interpret_result.is_ok()); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!( result, pad_left(&_hex_string_to_bytes("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")) @@ -880,7 +880,7 @@ mod tests { let mut runner = Runner::_default(3); coinbase(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0xc0; 20])); } @@ -901,7 +901,7 @@ mod tests { // Convert the timestamp to bytes in big-endian order let timestamp_bytes = timestamp_secs.to_be_bytes(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(×tamp_bytes)); } @@ -911,7 +911,7 @@ mod tests { let mut runner = Runner::_default(3); number(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0xff; 4])); } @@ -921,7 +921,7 @@ mod tests { let mut runner = Runner::_default(3); difficulty(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x45; 8])); } @@ -930,7 +930,7 @@ mod tests { let mut runner = Runner::_default(3); gaslimit(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x01, 0xC9, 0xC3, 0x80])); } @@ -939,7 +939,7 @@ mod tests { let mut runner = Runner::_default(3); chainid(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x01])); } @@ -948,7 +948,7 @@ mod tests { let mut runner = Runner::_default(3); selfbalance(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x00])); // transfer 100 wei to the contract @@ -957,7 +957,7 @@ mod tests { .transfer(runner.caller, runner.address, pad_left(&[0x64])); selfbalance(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x64])); } @@ -966,7 +966,7 @@ mod tests { let mut runner = Runner::_default(3); basefee(&mut runner).unwrap(); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x0a])); } } diff --git a/src/core_module/op_codes/flow.rs b/src/core_module/op_codes/flow.rs index cf90e91..24c67d9 100644 --- a/src/core_module/op_codes/flow.rs +++ b/src/core_module/op_codes/flow.rs @@ -27,8 +27,8 @@ pub fn stop(runner: &mut Runner) -> Result<(), ExecutionError> { // Revert data from memory heap pub fn revert(runner: &mut Runner) -> Result<(), ExecutionError> { - let offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let size = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let offset = U256::from_big_endian(&runner.stack.pop()?); + let size = U256::from_big_endian(&runner.stack.pop()?); let revert_data = unsafe { runner.memory.read(offset.as_usize(), size.as_usize()) }; @@ -58,7 +58,7 @@ pub fn revert(runner: &mut Runner) -> Result<(), ExecutionError> { // jump pub fn jump(runner: &mut Runner) -> Result<(), ExecutionError> { let mut bytes = [0u8; 32]; - let jump_address = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let jump_address = U256::from_big_endian(&runner.stack.pop()?); jump_address.to_big_endian(&mut bytes); // Check if the address is out of bounds @@ -79,10 +79,10 @@ pub fn jump(runner: &mut Runner) -> Result<(), ExecutionError> { // jumpi pub fn jumpi(runner: &mut Runner) -> Result<(), ExecutionError> { let mut bytes = [0u8; 32]; - let jump_address = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let jump_address = U256::from_big_endian(&runner.stack.pop()?); jump_address.to_big_endian(&mut bytes); - let condition = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let condition = U256::from_big_endian(&runner.stack.pop()?); // Check if the address is out of bounds if jump_address.as_usize() > runner.bytecode.len() { @@ -121,7 +121,7 @@ pub fn pc(runner: &mut Runner) -> Result<(), ExecutionError> { let pc = pad_left(&pc.to_vec()); // Push the program counter to the stack - unsafe { runner.stack.push(pc)? }; + runner.stack.push(pc)?; if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { let hex = utils::debug::to_hex_string(pc); @@ -138,7 +138,7 @@ pub fn gas(runner: &mut Runner) -> Result<(), ExecutionError> { let gas = pad_left(&gas.to_vec()); // Push the gas to the stack - unsafe { runner.stack.push(gas)? }; + runner.stack.push(gas)?; if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { let hex = utils::debug::to_hex_string(gas); @@ -169,7 +169,7 @@ mod tests { ); assert!(interpret_result.is_ok()); - let result: [u8; 32] = unsafe { runner.stack.pop().unwrap() }; + let result: [u8; 32] = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x04])); assert_eq!(runner.pc, 15); } @@ -190,7 +190,7 @@ mod tests { runner.interpret(_hex_string_to_bytes("600456fe5b6001"), Some(2), true); assert!(interpret_result.is_ok()); - let result: [u8; 32] = unsafe { runner.stack.pop().unwrap() }; + let result: [u8; 32] = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x01])); assert_eq!(runner.pc, 7); } @@ -205,7 +205,7 @@ mod tests { ); assert!(interpret_result.is_ok()); - let result: [u8; 32] = unsafe { runner.stack.pop().unwrap() }; + let result: [u8; 32] = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x01])); assert_eq!(runner.pc, 15); } @@ -216,7 +216,7 @@ mod tests { let interpret_result = runner.interpret(_hex_string_to_bytes("58"), Some(2), true); assert!(interpret_result.is_ok()); - let result: [u8; 32] = unsafe { runner.stack.pop().unwrap() }; + let result: [u8; 32] = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x00])); assert_eq!(runner.pc, 1); @@ -228,7 +228,7 @@ mod tests { ); assert!(interpret_result.is_ok()); - let result: [u8; 32] = unsafe { runner.stack.pop().unwrap() }; + let result: [u8; 32] = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x0a])); assert_eq!(runner.pc, 11); } @@ -239,7 +239,7 @@ mod tests { let interpret_result = runner.interpret(_hex_string_to_bytes("5a"), Some(2), true); assert!(interpret_result.is_ok()); - let result: [u8; 32] = unsafe { runner.stack.pop().unwrap() }; + let result: [u8; 32] = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&runner.gas.to_be_bytes())); } } diff --git a/src/core_module/op_codes/log.rs b/src/core_module/op_codes/log.rs index 422e33d..799e849 100644 --- a/src/core_module/op_codes/log.rs +++ b/src/core_module/op_codes/log.rs @@ -14,8 +14,8 @@ pub fn log0(runner: &mut Runner) -> Result<(), ExecutionError> { if runner.state.static_mode { return Err(ExecutionError::StaticCallStateChanged); } - let offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let size = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let offset = U256::from_big_endian(&runner.stack.pop()?); + let size = U256::from_big_endian(&runner.stack.pop()?); let log_data = unsafe { runner.memory.read(offset.as_usize(), size.as_usize())? }; @@ -61,10 +61,10 @@ pub fn log1(runner: &mut Runner) -> Result<(), ExecutionError> { if runner.state.static_mode { return Err(ExecutionError::StaticCallStateChanged); } - let offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let size: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let offset = U256::from_big_endian(&runner.stack.pop()?); + let size: U256 = U256::from_big_endian(&runner.stack.pop()?); - let raw_topic1: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let raw_topic1: U256 = U256::from_big_endian(&runner.stack.pop()?); let mut topic1 = [0u8; 32]; raw_topic1.to_big_endian(&mut topic1); @@ -122,14 +122,14 @@ pub fn log2(runner: &mut Runner) -> Result<(), ExecutionError> { if runner.state.static_mode { return Err(ExecutionError::StaticCallStateChanged); } - let offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let size: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let offset = U256::from_big_endian(&runner.stack.pop()?); + let size: U256 = U256::from_big_endian(&runner.stack.pop()?); - let raw_topic1: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let raw_topic1: U256 = U256::from_big_endian(&runner.stack.pop()?); let mut topic1 = [0u8; 32]; raw_topic1.to_big_endian(&mut topic1); - let raw_topic2: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let raw_topic2: U256 = U256::from_big_endian(&runner.stack.pop()?); let mut topic2 = [0u8; 32]; raw_topic2.to_big_endian(&mut topic2); @@ -195,18 +195,18 @@ pub fn log3(runner: &mut Runner) -> Result<(), ExecutionError> { if runner.state.static_mode { return Err(ExecutionError::StaticCallStateChanged); } - let offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let size: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let offset = U256::from_big_endian(&runner.stack.pop()?); + let size: U256 = U256::from_big_endian(&runner.stack.pop()?); - let raw_topic1: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let raw_topic1: U256 = U256::from_big_endian(&runner.stack.pop()?); let mut topic1 = [0u8; 32]; raw_topic1.to_big_endian(&mut topic1); - let raw_topic2: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let raw_topic2: U256 = U256::from_big_endian(&runner.stack.pop()?); let mut topic2 = [0u8; 32]; raw_topic2.to_big_endian(&mut topic2); - let raw_topic3: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let raw_topic3: U256 = U256::from_big_endian(&runner.stack.pop()?); let mut topic3 = [0u8; 32]; raw_topic3.to_big_endian(&mut topic3); @@ -281,22 +281,22 @@ pub fn log4(runner: &mut Runner) -> Result<(), ExecutionError> { if runner.state.static_mode { return Err(ExecutionError::StaticCallStateChanged); } - let offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let size: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let offset = U256::from_big_endian(&runner.stack.pop()?); + let size: U256 = U256::from_big_endian(&runner.stack.pop()?); - let raw_topic1: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let raw_topic1: U256 = U256::from_big_endian(&runner.stack.pop()?); let mut topic1 = [0u8; 32]; raw_topic1.to_big_endian(&mut topic1); - let raw_topic2: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let raw_topic2: U256 = U256::from_big_endian(&runner.stack.pop()?); let mut topic2 = [0u8; 32]; raw_topic2.to_big_endian(&mut topic2); - let raw_topic3: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let raw_topic3: U256 = U256::from_big_endian(&runner.stack.pop()?); let mut topic3 = [0u8; 32]; raw_topic3.to_big_endian(&mut topic3); - let raw_topic4: U256 = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let raw_topic4: U256 = U256::from_big_endian(&runner.stack.pop()?); let mut topic4 = [0u8; 32]; raw_topic4.to_big_endian(&mut topic4); diff --git a/src/core_module/op_codes/memory.rs b/src/core_module/op_codes/memory.rs index 5bda746..6d764a5 100644 --- a/src/core_module/op_codes/memory.rs +++ b/src/core_module/op_codes/memory.rs @@ -10,14 +10,12 @@ use colored::*; // Load 32 bytes from memory pub fn mload(runner: &mut Runner) -> Result<(), ExecutionError> { - let address = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let address = U256::from_big_endian(&runner.stack.pop()?); let word = unsafe { runner.memory.mload(address.as_usize())? }; - unsafe { - let result = runner.stack.push(word); + let result = runner.stack.push(word); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); } if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { @@ -31,8 +29,8 @@ pub fn mload(runner: &mut Runner) -> Result<(), ExecutionError> { // Store 32 bytes in memory pub fn mstore(runner: &mut Runner) -> Result<(), ExecutionError> { - let address = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let data = unsafe { runner.stack.pop()? }; + let address = U256::from_big_endian(&runner.stack.pop()?); + let data = runner.stack.pop()?; let result = unsafe { runner.memory.mstore(address.as_usize(), data) }; @@ -53,7 +51,7 @@ pub fn msize(runner: &mut Runner) -> Result<(), ExecutionError> { let mut bytes_msize = [0u8; 32]; U256::from(runner.memory.msize() as u64).to_big_endian(&mut bytes_msize); - let result = unsafe { runner.stack.push(bytes_msize) }; + let result = runner.stack.push(bytes_msize); if result.is_err() { return Err(result.unwrap_err()); @@ -80,7 +78,7 @@ mod tests { let interpret_result: Result<(), ExecutionError> = runner.interpret(_hex_string_to_bytes("7f00000000000000000000000000000000000000000000000000000000000000ff600052600051600151"), Some(2), true); assert!(interpret_result.is_ok()); - let result: [u8; 32] = unsafe { runner.stack.pop().unwrap() }; + let result: [u8; 32] = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0xff, 0x00])); } @@ -108,9 +106,9 @@ mod tests { let interpret_result: Result<(), ExecutionError> = runner.interpret(_hex_string_to_bytes("5960005150596039515059"), Some(2), true); assert!(interpret_result.is_ok()); - let result1: [u8; 32] = unsafe { runner.stack.pop().unwrap() }; - let result2: [u8; 32] = unsafe { runner.stack.pop().unwrap() }; - let result3: [u8; 32] = unsafe { runner.stack.pop().unwrap() }; + let result1: [u8; 32] = runner.stack.pop().unwrap(); + let result2: [u8; 32] = runner.stack.pop().unwrap(); + let result3: [u8; 32] = runner.stack.pop().unwrap(); assert_eq!(result1, pad_left(&[0x60])); assert_eq!(result2, pad_left(&[0x20])); diff --git a/src/core_module/op_codes/stack/dup.rs b/src/core_module/op_codes/stack/dup.rs index 23e3e2c..6ffc3ce 100644 --- a/src/core_module/op_codes/stack/dup.rs +++ b/src/core_module/op_codes/stack/dup.rs @@ -7,7 +7,7 @@ use colored::*; // Dup first element pub fn dup1(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(1) }; + let result = runner.stack.dup(1); if result.is_err() { return Err(result.unwrap_err()); @@ -29,7 +29,7 @@ pub fn dup1(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup second element pub fn dup2(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(2) }; + let result = runner.stack.dup(2); if result.is_err() { return Err(result.unwrap_err()); @@ -51,7 +51,7 @@ pub fn dup2(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup third element pub fn dup3(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(3) }; + let result = runner.stack.dup(3); if result.is_err() { return Err(result.unwrap_err()); @@ -73,7 +73,7 @@ pub fn dup3(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup fourth element pub fn dup4(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(4) }; + let result = runner.stack.dup(4); if result.is_err() { return Err(result.unwrap_err()); @@ -95,7 +95,7 @@ pub fn dup4(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup fifth element pub fn dup5(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(5) }; + let result = runner.stack.dup(5); if result.is_err() { return Err(result.unwrap_err()); @@ -117,7 +117,7 @@ pub fn dup5(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup sixth element pub fn dup6(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(6) }; + let result = runner.stack.dup(6); if result.is_err() { return Err(result.unwrap_err()); @@ -139,7 +139,7 @@ pub fn dup6(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup seventh element pub fn dup7(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(7) }; + let result = runner.stack.dup(7); if result.is_err() { return Err(result.unwrap_err()); @@ -161,7 +161,7 @@ pub fn dup7(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup eighth element pub fn dup8(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(8) }; + let result = runner.stack.dup(8); if result.is_err() { return Err(result.unwrap_err()); @@ -183,7 +183,7 @@ pub fn dup8(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup ninth element pub fn dup9(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(9) }; + let result = runner.stack.dup(9); if result.is_err() { return Err(result.unwrap_err()); @@ -205,7 +205,7 @@ pub fn dup9(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup tenth element pub fn dup10(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(10) }; + let result = runner.stack.dup(10); if result.is_err() { return Err(result.unwrap_err()); @@ -227,7 +227,7 @@ pub fn dup10(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup eleventh element pub fn dup11(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(11) }; + let result = runner.stack.dup(11); if result.is_err() { return Err(result.unwrap_err()); @@ -249,7 +249,7 @@ pub fn dup11(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup twelfth element pub fn dup12(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(12) }; + let result = runner.stack.dup(12); if result.is_err() { return Err(result.unwrap_err()); @@ -271,7 +271,7 @@ pub fn dup12(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup thirteenth element pub fn dup13(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(13) }; + let result = runner.stack.dup(13); if result.is_err() { return Err(result.unwrap_err()); @@ -293,7 +293,7 @@ pub fn dup13(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup fourteenth element pub fn dup14(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(14) }; + let result = runner.stack.dup(14); if result.is_err() { return Err(result.unwrap_err()); @@ -315,7 +315,7 @@ pub fn dup14(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup fifteenth element pub fn dup15(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(15) }; + let result = runner.stack.dup(15); if result.is_err() { return Err(result.unwrap_err()); @@ -337,7 +337,7 @@ pub fn dup15(runner: &mut Runner) -> Result<(), ExecutionError> { // Dup sixteenth element pub fn dup16(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.dup(16) }; + let result = runner.stack.dup(16); if result.is_err() { return Err(result.unwrap_err()); diff --git a/src/core_module/op_codes/stack/pop.rs b/src/core_module/op_codes/stack/pop.rs index 7d34802..7c3346d 100644 --- a/src/core_module/op_codes/stack/pop.rs +++ b/src/core_module/op_codes/stack/pop.rs @@ -6,7 +6,7 @@ use crate::core_module::utils; use colored::*; pub fn pop(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.pop() }; + let result = runner.stack.pop(); if result.is_err() { return Err(result.unwrap_err()); diff --git a/src/core_module/op_codes/stack/push.rs b/src/core_module/op_codes/stack/push.rs index 0a16c28..9e2ca68 100644 --- a/src/core_module/op_codes/stack/push.rs +++ b/src/core_module/op_codes/stack/push.rs @@ -11,28 +11,26 @@ pub fn push(runner: &mut Runner, data_len: usize) -> Result<(), ExecutionError> return Err(ExecutionError::OutOfBoundsByteCode); } - unsafe { - let data = &runner.bytecode[runner.pc + 1..runner.pc + 1 + data_len]; - - let mut padded = [0u8; 32]; // Start with an array of zeroes - let start = 32 - data.len(); // Calculate where to start copying - padded[start..].copy_from_slice(data); // Copy the slice into the end of the array - - let result = runner.stack.push(padded); - - if result.is_err() { - return Err(result.unwrap_err()); - } - - if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - let hex: String = utils::debug::to_hex_string(padded); - runner.print_debug(&format!( - "{}{:<10} 👉 [ {} ]", - "PUSH".bright_blue(), - data_len.to_string().magenta(), - hex - )); - } + let data = &runner.bytecode[runner.pc + 1..runner.pc + 1 + data_len]; + + let mut padded = [0u8; 32]; // Start with an array of zeroes + let start = 32 - data.len(); // Calculate where to start copying + padded[start..].copy_from_slice(data); // Copy the slice into the end of the array + + let result = runner.stack.push(padded); + + if result.is_err() { + return Err(result.unwrap_err()); + } + + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let hex: String = utils::debug::to_hex_string(padded); + runner.print_debug(&format!( + "{}{:<10} 👉 [ {} ]", + "PUSH".bright_blue(), + data_len.to_string().magenta(), + hex + )); } // Increment PC @@ -49,6 +47,12 @@ mod tests { let _ = runner.interpret(vec![0x60, 0xff], Some(2), true); assert_eq!(runner.stack.stack.len(), 1); - assert_eq!(unsafe { runner.stack.pop().unwrap() }, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255]); + assert_eq!( + runner.stack.pop().unwrap(), + [ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 255 + ] + ); } } diff --git a/src/core_module/op_codes/stack/swap.rs b/src/core_module/op_codes/stack/swap.rs index 25bda6e..7227d09 100644 --- a/src/core_module/op_codes/stack/swap.rs +++ b/src/core_module/op_codes/stack/swap.rs @@ -6,7 +6,7 @@ use colored::*; // Swap first and second element pub fn swap1(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(1) }; + let result = runner.stack.swap(1); if result.is_err() { return Err(result.unwrap_err()); @@ -47,7 +47,7 @@ pub fn swap1(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and third element pub fn swap2(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(2) }; + let result = runner.stack.swap(2); if result.is_err() { return Err(result.unwrap_err()); @@ -88,7 +88,7 @@ pub fn swap2(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and fourth element pub fn swap3(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(3) }; + let result = runner.stack.swap(3); if result.is_err() { return Err(result.unwrap_err()); @@ -129,7 +129,7 @@ pub fn swap3(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and fifth element pub fn swap4(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(4) }; + let result = runner.stack.swap(4); if result.is_err() { return Err(result.unwrap_err()); @@ -170,7 +170,7 @@ pub fn swap4(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and sixth element pub fn swap5(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(5) }; + let result = runner.stack.swap(5); if result.is_err() { return Err(result.unwrap_err()); @@ -211,7 +211,7 @@ pub fn swap5(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and seventh element pub fn swap6(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(6) }; + let result = runner.stack.swap(6); if result.is_err() { return Err(result.unwrap_err()); @@ -252,7 +252,7 @@ pub fn swap6(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and eighth element pub fn swap7(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(7) }; + let result = runner.stack.swap(7); if result.is_err() { return Err(result.unwrap_err()); @@ -293,7 +293,7 @@ pub fn swap7(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and ninth element pub fn swap8(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(8) }; + let result = runner.stack.swap(8); if result.is_err() { return Err(result.unwrap_err()); @@ -334,7 +334,7 @@ pub fn swap8(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and tenth element pub fn swap9(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(9) }; + let result = runner.stack.swap(9); if result.is_err() { return Err(result.unwrap_err()); @@ -375,7 +375,7 @@ pub fn swap9(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and eleventh element pub fn swap10(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(10) }; + let result = runner.stack.swap(10); if result.is_err() { return Err(result.unwrap_err()); @@ -416,7 +416,7 @@ pub fn swap10(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and twelfth element pub fn swap11(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(11) }; + let result = runner.stack.swap(11); if result.is_err() { return Err(result.unwrap_err()); @@ -457,7 +457,7 @@ pub fn swap11(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and thirteenth element pub fn swap12(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(12) }; + let result = runner.stack.swap(12); if result.is_err() { return Err(result.unwrap_err()); @@ -498,7 +498,7 @@ pub fn swap12(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and fourteenth element pub fn swap13(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(13) }; + let result = runner.stack.swap(13); if result.is_err() { return Err(result.unwrap_err()); @@ -539,7 +539,7 @@ pub fn swap13(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and fifteenth element pub fn swap14(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(14) }; + let result = runner.stack.swap(14); if result.is_err() { return Err(result.unwrap_err()); @@ -580,7 +580,7 @@ pub fn swap14(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and sixteenth element pub fn swap15(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(15) }; + let result = runner.stack.swap(15); if result.is_err() { return Err(result.unwrap_err()); @@ -621,7 +621,7 @@ pub fn swap15(runner: &mut Runner) -> Result<(), ExecutionError> { // Swap first and seventeenth element pub fn swap16(runner: &mut Runner) -> Result<(), ExecutionError> { - let result = unsafe { runner.stack.swap(16) }; + let result = runner.stack.swap(16); if result.is_err() { return Err(result.unwrap_err()); diff --git a/src/core_module/op_codes/storage.rs b/src/core_module/op_codes/storage.rs index 07197a7..090e959 100644 --- a/src/core_module/op_codes/storage.rs +++ b/src/core_module/op_codes/storage.rs @@ -1,31 +1,24 @@ +use crate::core_module::runner::Runner; use crate::core_module::utils; use crate::core_module::utils::errors::ExecutionError; -use crate::core_module::runner::Runner; // Colored output use colored::*; // Load 32 bytes from memory pub fn sload(runner: &mut Runner) -> Result<(), ExecutionError> { - let address = unsafe { runner.stack.pop()? }; + let address = runner.stack.pop()?; let word = runner.state.sload(runner.address, address)?; - unsafe { - let result = runner.stack - .push(word); + let result = runner.stack.push(word); - if result.is_err() { - return Err(result.unwrap_err()); - } + if result.is_err() { + return Err(result.unwrap_err()); } if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { let hex: String = utils::debug::to_hex_string(word); - runner.print_debug(&format!( - "{:<14} 👉 [ {} ]", - "SLOAD".bright_blue(), - hex - )); + runner.print_debug(&format!("{:<14} 👉 [ {} ]", "SLOAD".bright_blue(), hex)); } // Increment PC @@ -34,8 +27,8 @@ pub fn sload(runner: &mut Runner) -> Result<(), ExecutionError> { // Store 32 bytes in memory pub fn sstore(runner: &mut Runner) -> Result<(), ExecutionError> { - let address = unsafe { runner.stack.pop()? }; - let word = unsafe { runner.stack.pop()? }; + let address = runner.stack.pop()?; + let word = runner.stack.pop()?; let result = runner.state.sstore(runner.address, address, word); @@ -45,18 +38,13 @@ pub fn sstore(runner: &mut Runner) -> Result<(), ExecutionError> { if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { let hex: String = utils::debug::to_hex_string(word); - runner.print_debug(&format!( - "{:<14} ⛔️ [ {} ]", - "SSTORE".bright_blue(), - hex - )); + runner.print_debug(&format!("{:<14} ⛔️ [ {} ]", "SSTORE".bright_blue(), hex)); } // Increment PC runner.increment_pc(1) } - #[cfg(test)] mod tests { use crate::core_module::runner::Runner; @@ -66,23 +54,30 @@ mod tests { #[test] fn test_sload() { let mut runner = Runner::_default(3); - let interpret_result: Result<(), ExecutionError> = runner.interpret(_hex_string_to_bytes("602e600055600054600154"), Some(2), true); + let interpret_result: Result<(), ExecutionError> = runner.interpret( + _hex_string_to_bytes("602e600055600054600154"), + Some(2), + true, + ); assert!(interpret_result.is_ok()); - let result: [u8; 32] = unsafe { runner.stack.pop().unwrap() }; + let result: [u8; 32] = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x00])); - let result: [u8; 32] = unsafe { runner.stack.pop().unwrap() }; + let result: [u8; 32] = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x2e])); } #[test] fn test_sstore() { let mut runner = Runner::_default(3); - let interpret_result: Result<(), ExecutionError> = runner.interpret(_hex_string_to_bytes("602e600055"), Some(2), true); + let interpret_result: Result<(), ExecutionError> = + runner.interpret(_hex_string_to_bytes("602e600055"), Some(2), true); assert!(interpret_result.is_ok()); - let result = runner.state.sload(runner.address, pad_left(&[0x00])).unwrap(); + let result = runner + .state + .sload(runner.address, pad_left(&[0x00])) + .unwrap(); assert_eq!(result, pad_left(&[0x2e])); } - } diff --git a/src/core_module/op_codes/system.rs b/src/core_module/op_codes/system.rs index 9f749d3..6719be4 100644 --- a/src/core_module/op_codes/system.rs +++ b/src/core_module/op_codes/system.rs @@ -16,7 +16,11 @@ use colored::*; pub fn invalid(runner: &mut Runner) -> Result<(), ExecutionError> { if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - runner.print_debug(&format!("{:} 0x{:X}", "INVALID".red(), runner.bytecode[runner.pc])); + runner.print_debug(&format!( + "{:} 0x{:X}", + "INVALID".red(), + runner.bytecode[runner.pc] + )); } Err(ExecutionError::InvalidOpcode(runner.bytecode[runner.pc])) @@ -24,9 +28,9 @@ pub fn invalid(runner: &mut Runner) -> Result<(), ExecutionError> { pub fn create(runner: &mut Runner) -> Result<(), ExecutionError> { // Get the values on the stack - let value = unsafe { runner.stack.pop()? }; - let offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let size = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let value = runner.stack.pop()?; + let offset = U256::from_big_endian(&runner.stack.pop()?); + let size = U256::from_big_endian(&runner.stack.pop()?); // Load the init code from memory let init_code = unsafe { runner.memory.read(offset.as_usize(), size.as_usize())? }; @@ -54,9 +58,9 @@ pub fn create(runner: &mut Runner) -> Result<(), ExecutionError> { // Check if the call failed if call_result.is_err() { - unsafe { runner.stack.push(pad_left(&[0x00]))? }; + runner.stack.push(pad_left(&[0x00]))?; } else { - unsafe { runner.stack.push(pad_left(&contract_address)) }?; + runner.stack.push(pad_left(&contract_address))?; } // Get the return data to store the real contract code @@ -81,10 +85,10 @@ pub fn create(runner: &mut Runner) -> Result<(), ExecutionError> { pub fn create2(runner: &mut Runner) -> Result<(), ExecutionError> { // Get the values on the stack - let value = unsafe { runner.stack.pop()? }; - let offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let size = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let salt = unsafe { runner.stack.pop()? }; + let value = runner.stack.pop()?; + let offset = U256::from_big_endian(&runner.stack.pop()?); + let size = U256::from_big_endian(&runner.stack.pop()?); + let salt = runner.stack.pop()?; // Load the init code from memory let init_code = unsafe { runner.memory.read(offset.as_usize(), size.as_usize())? }; @@ -112,9 +116,9 @@ pub fn create2(runner: &mut Runner) -> Result<(), ExecutionError> { // Check if the call failed if call_result.is_err() { - unsafe { runner.stack.push(pad_left(&[0x00]))? }; + runner.stack.push(pad_left(&[0x00]))?; } else { - unsafe { runner.stack.push(pad_left(&contract_address)) }?; + runner.stack.push(pad_left(&contract_address))?; } // Get the return data to store the real contract code @@ -142,18 +146,18 @@ pub fn call(runner: &mut Runner, bypass_static: bool) -> Result<(), ExecutionErr } // Get the values on the stack - let gas = unsafe { runner.stack.pop()? }; - let to = unsafe { runner.stack.pop()? }; + let gas = runner.stack.pop()?; + let to = runner.stack.pop()?; let value = if bypass_static { [0u8; 32] } else { - unsafe { runner.stack.pop()? } + runner.stack.pop()? }; - let calldata_offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let calldata_size = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let returndata_offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let returndata_size = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let calldata_offset = U256::from_big_endian(&runner.stack.pop()?); + let calldata_size = U256::from_big_endian(&runner.stack.pop()?); + let returndata_offset = U256::from_big_endian(&runner.stack.pop()?); + let returndata_size = U256::from_big_endian(&runner.stack.pop()?); // Load the input data from memory let calldata = unsafe { @@ -188,9 +192,9 @@ pub fn call(runner: &mut Runner, bypass_static: bool) -> Result<(), ExecutionErr ); if call_result.is_err() { - unsafe { runner.stack.push(pad_left(&[0x00]))? }; + runner.stack.push(pad_left(&[0x00]))?; } else { - unsafe { runner.stack.push(pad_left(&[0x01]))? }; + runner.stack.push(pad_left(&[0x01]))?; } let return_data = runner.returndata.heap.clone(); @@ -213,7 +217,11 @@ pub fn call(runner: &mut Runner, bypass_static: bool) -> Result<(), ExecutionErr } }, if call_result.is_err() { "❌" } else { "✅" }, - if call_result.is_err() { call_result.unwrap_err().to_string() } else { " ".to_string() }, + if call_result.is_err() { + call_result.unwrap_err().to_string() + } else { + " ".to_string() + }, "Returndata".bright_blue(), returndata_hex )); @@ -244,12 +252,12 @@ pub fn callcode(_: &mut Runner) -> Result<(), ExecutionError> { pub fn delegatecall(runner: &mut Runner) -> Result<(), ExecutionError> { // Get the values on the stack - let gas = unsafe { runner.stack.pop()? }; - let to = unsafe { runner.stack.pop()? }; - let calldata_offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let calldata_size = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let returndata_offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let returndata_size = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let gas = runner.stack.pop()?; + let to = runner.stack.pop()?; + let calldata_offset = U256::from_big_endian(&runner.stack.pop()?); + let calldata_size = U256::from_big_endian(&runner.stack.pop()?); + let returndata_offset = U256::from_big_endian(&runner.stack.pop()?); + let returndata_size = U256::from_big_endian(&runner.stack.pop()?); // Load the input data from memory let calldata = unsafe { @@ -280,9 +288,9 @@ pub fn delegatecall(runner: &mut Runner) -> Result<(), ExecutionError> { ); if call_result.is_err() { - unsafe { runner.stack.push(pad_left(&[0x00]))? }; + runner.stack.push(pad_left(&[0x00]))?; } else { - unsafe { runner.stack.push(pad_left(&[0x01]))? }; + runner.stack.push(pad_left(&[0x01]))?; } let return_data = runner.returndata.heap.clone(); @@ -334,7 +342,7 @@ pub fn staticcall(runner: &mut Runner) -> Result<(), ExecutionError> { pub fn selfdestruct(runner: &mut Runner) -> Result<(), ExecutionError> { // Get the values on the stack - let address = unsafe { runner.stack.pop()? }; + let address = runner.stack.pop()?; let contract_balance = get_balance(runner.address, runner)?; @@ -349,10 +357,7 @@ pub fn selfdestruct(runner: &mut Runner) -> Result<(), ExecutionError> { delete_account(runner.address, runner)?; if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - runner.print_debug(&format!( - "{}", - "SELFDESTRUCT".bright_blue() - )); + runner.print_debug(&format!("{}", "SELFDESTRUCT".bright_blue())); } // Increment PC @@ -361,8 +366,8 @@ pub fn selfdestruct(runner: &mut Runner) -> Result<(), ExecutionError> { pub fn return_(runner: &mut Runner) -> Result<(), ExecutionError> { // Get the values on the stack - let offset = U256::from_big_endian(&unsafe { runner.stack.pop()? }); - let size = U256::from_big_endian(&unsafe { runner.stack.pop()? }); + let offset = U256::from_big_endian(&runner.stack.pop()?); + let size = U256::from_big_endian(&runner.stack.pop()?); // Load the return data from memory let returndata = unsafe { runner.memory.read(offset.as_usize(), size.as_usize())? }; @@ -371,10 +376,7 @@ pub fn return_(runner: &mut Runner) -> Result<(), ExecutionError> { runner.returndata.heap = returndata; if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { - runner.print_debug(&format!( - "{}", - "RETURN".red() - )); + runner.print_debug(&format!("{}", "RETURN".red())); } // Increment PC @@ -395,7 +397,7 @@ mod tests { runner.interpret(_hex_string_to_bytes("60fffe50fe60fffe"), Some(2), true); assert!(interpret_result.is_err()); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0xff])); } @@ -409,7 +411,7 @@ mod tests { ); assert!(interpret_result.is_ok()); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!( result, pad_left(&[ @@ -436,7 +438,7 @@ mod tests { ); assert!(interpret_result.is_ok()); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!( result, pad_left(&[ @@ -466,11 +468,11 @@ mod tests { assert!(interpret_result.is_ok()); // Second call succeeded - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert!(result == pad_left(&[0x01])); // First call failed - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert!(result == pad_left(&[0x00])); } @@ -501,11 +503,11 @@ mod tests { assert!(interpret_result.is_ok()); // Second call succeeded - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert!(result == pad_left(&[0x01])); // First call failed - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert!(result == pad_left(&[0x00])); } @@ -522,11 +524,11 @@ mod tests { assert!(interpret_result.is_ok()); // Second call succeeded - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert!(result == pad_left(&[0x01])); // First call failed - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert!(result == pad_left(&[0x00])); } @@ -542,7 +544,7 @@ mod tests { ); assert!(interpret_result.is_ok()); - let address = unsafe { runner.stack.pop().unwrap() }; + let address = runner.stack.pop().unwrap(); assert_eq!( address, pad_left(&[ @@ -577,7 +579,7 @@ mod tests { runner.interpret(_hex_string_to_bytes(bytecode), Some(2), true); assert!(selfdestruct_result.is_ok()); - let result = unsafe { runner.stack.pop().unwrap() }; + let result = runner.stack.pop().unwrap(); assert_eq!(result, pad_left(&[0x01])); let stored_code = runner.state.get_code_at(bytes32_to_address(&result)); diff --git a/src/core_module/runner.rs b/src/core_module/runner.rs index 85f67e4..6fe1b39 100644 --- a/src/core_module/runner.rs +++ b/src/core_module/runner.rs @@ -627,9 +627,9 @@ mod tests { let mut runner = Runner::new([0xaa; 20], None, None, None, None, None); let _ = runner.interpret(vec![0x5f, 0x5f, 0x5f], Some(1), true); - assert_eq!(unsafe { runner.stack.pop().unwrap() }, [0u8; 32]); - assert_eq!(unsafe { runner.stack.pop().unwrap() }, [0u8; 32]); - assert_eq!(unsafe { runner.stack.pop().unwrap() }, [0u8; 32]); + assert_eq!(runner.stack.pop().unwrap(), [0u8; 32]); + assert_eq!(runner.stack.pop().unwrap(), [0u8; 32]); + assert_eq!(runner.stack.pop().unwrap(), [0u8; 32]); } #[test] @@ -638,21 +638,21 @@ mod tests { let _ = runner.interpret(vec![0x60, 0x01, 0x60, 0x02, 0x60, 0x03], Some(1), true); assert_eq!( - unsafe { runner.stack.pop().unwrap() }, + runner.stack.pop().unwrap(), [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 ] ); assert_eq!( - unsafe { runner.stack.pop().unwrap() }, + runner.stack.pop().unwrap(), [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 ] ); assert_eq!( - unsafe { runner.stack.pop().unwrap() }, + runner.stack.pop().unwrap(), [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 diff --git a/src/core_module/stack.rs b/src/core_module/stack.rs index 2784de4..b0eedfc 100644 --- a/src/core_module/stack.rs +++ b/src/core_module/stack.rs @@ -13,7 +13,7 @@ impl Stack { } // Push a word onto the stack - pub unsafe fn push(&mut self, word: [u8; 32]) -> Result<(), ExecutionError>{ + pub fn push(&mut self, word: [u8; 32]) -> Result<(), ExecutionError>{ // Check if the stack is too deep if self.stack.len() >= 1024 { // Return an error @@ -24,7 +24,7 @@ impl Stack { } // Pop a word off the stack - pub unsafe fn pop(&mut self) -> Result<[u8; 32], ExecutionError> { + pub fn pop(&mut self) -> Result<[u8; 32], ExecutionError> { // Check if the stack is empty if self.stack.is_empty() { // Return an error @@ -35,7 +35,7 @@ impl Stack { } // Duplicate a word on the stack - pub unsafe fn dup(&mut self, index: usize) -> Result<[u8; 32], ExecutionError> { + pub fn dup(&mut self, index: usize) -> Result<[u8; 32], ExecutionError> { // Check if the stack is long enough if self.stack.len() < index { return Err(ExecutionError::StackTooSmall); @@ -48,7 +48,7 @@ impl Stack { } // Swap two words on the stack - pub unsafe fn swap(&mut self, index: usize) -> Result<[[u8; 32]; 2], ExecutionError> { + pub fn swap(&mut self, index: usize) -> Result<[[u8; 32]; 2], ExecutionError> { // Check if the stack is long enough if self.stack.len() < index { return Err(ExecutionError::StackTooSmall); From 20b2294ac4a8be5b5deb1206164a07bef7a6d499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20FASANO?= Date: Tue, 18 Jul 2023 12:59:06 +0200 Subject: [PATCH 2/3] fix: replaced raw concat by pad_left utils --- src/core_module/op_codes/comparison.rs | 56 ++++--------------------- src/core_module/op_codes/environment.rs | 24 ++--------- 2 files changed, 11 insertions(+), 69 deletions(-) diff --git a/src/core_module/op_codes/comparison.rs b/src/core_module/op_codes/comparison.rs index 5e68f05..40e8669 100644 --- a/src/core_module/op_codes/comparison.rs +++ b/src/core_module/op_codes/comparison.rs @@ -1,4 +1,4 @@ -use crate::core_module::runner::Runner; +use crate::core_module::{runner::Runner, utils::bytes::pad_left}; use crate::core_module::utils; use crate::core_module::utils::errors::ExecutionError; @@ -15,14 +15,7 @@ pub fn iszero(runner: &mut Runner) -> Result<(), ExecutionError> { let bool = a.is_zero(); - let result_bytes = [ - [0u8; 31].to_vec(), - [if bool { 1u8 } else { 0u8 }; 1].to_vec(), - ] - .concat() - .as_slice() - .try_into() - .unwrap(); + let result_bytes = pad_left(&[if bool { 1u8 } else { 0u8 }; 1]); let result = runner.stack.push(result_bytes); @@ -48,14 +41,7 @@ pub fn eq(runner: &mut Runner) -> Result<(), ExecutionError> { let bool = a.eq(&b); - let result_bytes = [ - [0u8; 31].to_vec(), - [if bool { 1u8 } else { 0u8 }; 1].to_vec(), - ] - .concat() - .as_slice() - .try_into() - .unwrap(); + let result_bytes = pad_left(&[if bool { 1u8 } else { 0u8 }; 1]); let result = runner.stack.push(result_bytes); @@ -81,14 +67,7 @@ pub fn lt(runner: &mut Runner) -> Result<(), ExecutionError> { let bool = a.lt(&b); - let result_bytes = [ - [0u8; 31].to_vec(), - [if bool { 1u8 } else { 0u8 }; 1].to_vec(), - ] - .concat() - .as_slice() - .try_into() - .expect("Wrong length"); + let result_bytes = pad_left(&[if bool { 1u8 } else { 0u8 }; 1]); let result = runner.stack.push(result_bytes); @@ -114,14 +93,7 @@ pub fn gt(runner: &mut Runner) -> Result<(), ExecutionError> { let bool = a.gt(&b); - let result_bytes = [ - [0u8; 31].to_vec(), - [if bool { 1u8 } else { 0u8 }; 1].to_vec(), - ] - .concat() - .as_slice() - .try_into() - .unwrap(); + let result_bytes = pad_left(&[if bool { 1u8 } else { 0u8 }; 1]); let result = runner.stack.push(result_bytes); @@ -147,14 +119,7 @@ pub fn slt(runner: &mut Runner) -> Result<(), ExecutionError> { let bool = a.lt(&b); - let result_bytes = [ - [0u8; 31].to_vec(), - [if bool { 1u8 } else { 0u8 }; 1].to_vec(), - ] - .concat() - .as_slice() - .try_into() - .unwrap(); + let result_bytes = pad_left(&[if bool { 1u8 } else { 0u8 }; 1]); let result = runner.stack.push(result_bytes); @@ -180,14 +145,7 @@ pub fn sgt(runner: &mut Runner) -> Result<(), ExecutionError> { let bool = a.gt(&b); - let result_bytes = [ - [0u8; 31].to_vec(), - [if bool { 1u8 } else { 0u8 }; 1].to_vec(), - ] - .concat() - .as_slice() - .try_into() - .unwrap(); + let result_bytes = pad_left(&[if bool { 1u8 } else { 0u8 }; 1]); let result = runner.stack.push(result_bytes); diff --git a/src/core_module/op_codes/environment.rs b/src/core_module/op_codes/environment.rs index 356d8e9..1af7ba6 100644 --- a/src/core_module/op_codes/environment.rs +++ b/src/core_module/op_codes/environment.rs @@ -13,11 +13,7 @@ use ethers::utils::keccak256; use colored::*; pub fn address(runner: &mut Runner) -> Result<(), ExecutionError> { - let address = [[0; 12].to_vec(), runner.address.to_vec()] - .concat() - .as_slice() - .try_into() - .unwrap(); + let address = pad_left(&runner.address); let result = runner.stack.push(address); @@ -56,11 +52,7 @@ pub fn balance(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn origin(runner: &mut Runner) -> Result<(), ExecutionError> { - let origin = [[0; 12].to_vec(), runner.origin.to_vec()] - .concat() - .as_slice() - .try_into() - .unwrap(); + let origin = pad_left(&runner.origin); let result = runner.stack.push(origin); @@ -78,11 +70,7 @@ pub fn origin(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn caller(runner: &mut Runner) -> Result<(), ExecutionError> { - let caller = [[0; 12].to_vec(), runner.caller.to_vec()] - .concat() - .as_slice() - .try_into() - .unwrap(); + let caller = pad_left(&runner.caller); let result = runner.stack.push(caller); @@ -235,11 +223,7 @@ pub fn codecopy(runner: &mut Runner) -> Result<(), ExecutionError> { } pub fn gasprice(runner: &mut Runner) -> Result<(), ExecutionError> { - let gasprice = [[0; 31].to_vec(), [0xff].to_vec()] - .concat() - .as_slice() - .try_into() - .unwrap(); + let gasprice = pad_left(&[0xff]); let result = runner.stack.push(gasprice); From bfda8d0b86f7551e78e9f7ea09ab36cbdaeba7b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20FASANO?= Date: Tue, 18 Jul 2023 14:21:34 +0200 Subject: [PATCH 3/3] style: improve call logs --- src/core_module/op_codes/system.rs | 12 +++++++----- src/core_module/state.rs | 4 ++-- src/main.rs | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/core_module/op_codes/system.rs b/src/core_module/op_codes/system.rs index 6719be4..4d317d0 100644 --- a/src/core_module/op_codes/system.rs +++ b/src/core_module/op_codes/system.rs @@ -164,7 +164,8 @@ pub fn call(runner: &mut Runner, bypass_static: bool) -> Result<(), ExecutionErr runner .memory .read(calldata_offset.as_usize(), calldata_size.as_usize())? - }; + }; + if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { let address_hex: String = utils::debug::to_hex_address(bytes32_to_address(&to)); @@ -176,7 +177,7 @@ pub fn call(runner: &mut Runner, bypass_static: bool) -> Result<(), ExecutionErr } else { "CALL".yellow() }, - address_hex, + address_hex.blue(), "Calldata".bright_blue(), calldata_hex )); @@ -200,9 +201,10 @@ pub fn call(runner: &mut Runner, bypass_static: bool) -> Result<(), ExecutionErr let return_data = runner.returndata.heap.clone(); if runner.debug_level.is_some() && runner.debug_level.unwrap() >= 1 { + let caller_hex: String = utils::debug::to_hex_address(runner.address); let returndata_hex: String = utils::debug::vec_to_hex_string(return_data.clone()); runner.print_debug(&format!( - "\n{} {} {:?}\n {}: {}\n", + "\n{} {} {}\n {}: {}\n", if call_result.is_err() { if bypass_static { "STATICCALL FAILED".red() @@ -218,9 +220,9 @@ pub fn call(runner: &mut Runner, bypass_static: bool) -> Result<(), ExecutionErr }, if call_result.is_err() { "❌" } else { "✅" }, if call_result.is_err() { - call_result.unwrap_err().to_string() + call_result.unwrap_err().to_string().red() } else { - " ".to_string() + format!("Back to {}", caller_hex.magenta()).white() }, "Returndata".bright_blue(), returndata_hex diff --git a/src/core_module/state.rs b/src/core_module/state.rs index 595b4bf..03d57a0 100644 --- a/src/core_module/state.rs +++ b/src/core_module/state.rs @@ -221,14 +221,14 @@ impl EvmState { } // Load contract code - pub fn get_code(&self, code_hash: [u8; 32]) -> Result<&Vec, ExecutionError> { + fn get_code(&self, code_hash: [u8; 32]) -> Result<&Vec, ExecutionError> { self.codes .get(&code_hash) .ok_or(ExecutionError::CodeNotFound) } // Store contract code and return its hash - pub fn put_code(&mut self, code: Vec) -> Result<[u8; 32], ExecutionError> { + fn put_code(&mut self, code: Vec) -> Result<[u8; 32], ExecutionError> { // Check if static mode is enabled if self.static_mode { return Err(ExecutionError::StaticCallStateChanged); diff --git a/src/main.rs b/src/main.rs index a894523..81a7414 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ fn main() -> Result<(), ExecutionError> { let bytecode = hex::decode(file_content.trim()).expect("Decoding failed"); // Interpret the bytecode - let _ = interpreter.interpret(bytecode, Some(4), true); + let _ = interpreter.interpret(bytecode, Some(3), true); } Err(_) => { return Err(ExecutionError::InvalidFile);