From 94183f723c57a434e7f9b5465464357d7fa682cb Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Sun, 31 Jul 2022 20:43:58 -0700 Subject: [PATCH] fixes --- evm/src/cpu/kernel/optimizer.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/evm/src/cpu/kernel/optimizer.rs b/evm/src/cpu/kernel/optimizer.rs index 9683e160..66307aec 100644 --- a/evm/src/cpu/kernel/optimizer.rs +++ b/evm/src/cpu/kernel/optimizer.rs @@ -48,9 +48,9 @@ fn constant_propagation(code: &mut Vec) { replace_windows(code, |window| { if let [Push(Literal(y)), Push(Literal(x)), StandardOp(op)] = window { match op.as_str() { - "ADD" => Some(vec![Push(Literal(x + y))]), - "SUB" => Some(vec![Push(Literal(x - y))]), - "MUL" => Some(vec![Push(Literal(x * y))]), + "ADD" => Some(vec![Push(Literal(x.overflowing_add(y).0))]), + "SUB" => Some(vec![Push(Literal(x.overflowing_sub(y).0))]), + "MUL" => Some(vec![Push(Literal(x.overflowing_mul(y).0))]), "DIV" => Some(vec![Push(Literal( x.checked_div(y).unwrap_or(U256::zero()), ))]), @@ -99,6 +99,28 @@ mod tests { assert_eq!(code, vec![Push(Literal(0.into()))]); } + #[test] + fn test_constant_propagation_add_overflowing() { + let mut code = vec![ + Push(Literal(U256::max_value())), + Push(Literal(U256::max_value())), + StandardOp("ADD".into()), + ]; + constant_propagation(&mut code); + assert_eq!(code, vec![Push(Literal(U256::max_value() - 1))]); + } + + #[test] + fn test_constant_propagation_sub_underflowing() { + let mut code = vec![ + Push(Literal(U256::one())), + Push(Literal(U256::zero())), + StandardOp("SUB".into()), + ]; + constant_propagation(&mut code); + assert_eq!(code, vec![Push(Literal(U256::max_value()))]); + } + #[test] fn test_constant_propagation_mul() { let mut code = vec![