Skip to content

Commit

Permalink
fix: Fix push opcode bug
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Feb 11, 2025
1 parent 774a24d commit 5f1603e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/opcodes/dup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod tests {
#[test]
fn test_opcode_dup() {
test(
vec![],
OpcodeDup(0),
&[
(&[], None),
Expand All @@ -57,6 +58,7 @@ mod tests {
],
);
test(
vec![],
OpcodeDup(1),
&[
(&[], None),
Expand Down
3 changes: 2 additions & 1 deletion src/opcodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ mod tests {
use super::*;

pub fn test<O: OpcodeHandler<U256, MiniEthereum>>(
code: Vec<u8>,
opcode_handler: O,
testcases: &[(&[U256], Option<&[U256]>)],
) {
for (inp, expected_out) in testcases {
let mut gt = GasTracker::new(10000000);
let mut ctx = MiniEthereum::new();
let mut machine = Machine::new(Address::ZERO, vec![], &mut gt, 1024);
let mut machine = Machine::new(Address::ZERO, code.clone(), &mut gt, 1024);
let mut inp_reversed = inp.to_vec();
inp_reversed.reverse();
machine.stack.extend(inp_reversed);
Expand Down
10 changes: 10 additions & 0 deletions src/opcodes/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ mod tests {
#[test]
fn test_opcode_sar() {
test(
vec![],
OpcodeBinaryOp::Sar,
&[
(&[], None),
Expand Down Expand Up @@ -226,6 +227,7 @@ mod tests {
#[test]
fn test_opcode_lt() {
test(
vec![],
OpcodeBinaryOp::Lt,
&[
(&[], None),
Expand All @@ -249,6 +251,7 @@ mod tests {
#[test]
fn test_opcode_gt() {
test(
vec![],
OpcodeBinaryOp::Gt,
&[
(&[], None),
Expand All @@ -272,6 +275,7 @@ mod tests {
#[test]
fn test_opcode_slt() {
test(
vec![],
OpcodeBinaryOp::Slt,
&[
(&[], None),
Expand All @@ -291,6 +295,7 @@ mod tests {
#[test]
fn test_opcode_sgt() {
test(
vec![],
OpcodeBinaryOp::Sgt,
&[
(&[], None),
Expand All @@ -310,6 +315,7 @@ mod tests {
#[test]
fn test_opcode_eq() {
test(
vec![],
OpcodeBinaryOp::Eq,
&[
(&[], None),
Expand All @@ -326,6 +332,7 @@ mod tests {
#[test]
fn test_opcode_is_zero() {
test(
vec![],
OpcodeUnaryOp::IsZero,
&[
(&[], None),
Expand All @@ -340,6 +347,7 @@ mod tests {
#[test]
fn test_opcode_sdiv() {
test(
vec![],
OpcodeBinaryOp::Sdiv,
&[
(&[U256::from(11), U256::from(2)], Some(&[U256::from(5)])),
Expand All @@ -356,6 +364,7 @@ mod tests {
#[test]
fn test_opcode_smod() {
test(
vec![],
OpcodeBinaryOp::Smod,
&[
(&[U256::from(11), U256::from(3)], Some(&[U256::from(2)])),
Expand Down Expand Up @@ -385,6 +394,7 @@ mod tests {
#[test]
fn test_sign_extend() {
test(
vec![],
OpcodeBinaryOp::SignExtend,
&[
(&[U256::from(0), U256::from(0xff)], Some(&[U256::MAX])),
Expand Down
53 changes: 50 additions & 3 deletions src/opcodes/push.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Audited 11 Feb 2025 - Keyvan Kambakhsh */

use super::ExecutionResult;
use super::OpcodeHandler;
use crate::context::Context;
Expand All @@ -16,12 +18,12 @@ impl<W: Word, C: Context<W>> OpcodeHandler<W, C> for OpcodePush {
machine: &mut Machine<W>,
_call_info: &CallInfo<W>,
) -> Result<Option<ExecutionResult>, ExecError> {
if machine.code.len() - machine.pc < self.0 as usize {
return Err(ExecError::Revert(RevertError::NotEnoughBytesInCode));
}
machine.push_stack(if self.0 == 0 {
W::ZERO
} else {
if machine.code.len() < machine.pc + 1 + self.0 as usize {
return Err(ExecError::Revert(RevertError::NotEnoughBytesInCode));
}
W::from_big_endian(&machine.code[machine.pc + 1..machine.pc + 1 + self.0 as usize])
})?;
machine.pc += 1 + self.0 as usize;
Expand All @@ -39,11 +41,56 @@ mod tests {
#[test]
fn test_opcode_push() {
test(
vec![],
OpcodePush(0),
&[
(&[], Some(&[U256::from(0)])),
(&[U256::from(123)], Some(&[U256::from(0), U256::from(123)])),
],
);
test(
vec![0, 0x12],
OpcodePush(1),
&[
(&[], Some(&[U256::from(0x12)])),
(
&[U256::from(123)],
Some(&[U256::from(0x12), U256::from(123)]),
),
],
);
test(
vec![0, 0x12, 0x34],
OpcodePush(2),
&[
(&[], Some(&[U256::from(0x1234)])),
(
&[U256::from(123)],
Some(&[U256::from(0x1234), U256::from(123)]),
),
],
);
test(
vec![0, 0x12, 0x34, 0x0],
OpcodePush(2),
&[
(&[], Some(&[U256::from(0x1234)])),
(
&[U256::from(123)],
Some(&[U256::from(0x1234), U256::from(123)]),
),
],
);
test(
vec![0, 0x12, 0x34, 0x0],
OpcodePush(3),
&[
(&[], Some(&[U256::from(0x123400)])),
(
&[U256::from(123)],
Some(&[U256::from(0x123400), U256::from(123)]),
),
],
);
}
}
2 changes: 2 additions & 0 deletions src/opcodes/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mod tests {
#[test]
fn test_opcode_swap() {
test(
vec![],
OpcodeSwap(0),
&[
(&[], None),
Expand All @@ -56,6 +57,7 @@ mod tests {
],
);
test(
vec![],
OpcodeSwap(1),
&[
(&[], None),
Expand Down

0 comments on commit 5f1603e

Please sign in to comment.