From b763f60cbc15d109b84239a8a4e7b4fec95e3e73 Mon Sep 17 00:00:00 2001 From: maximopalopoli Date: Fri, 22 Nov 2024 18:28:56 -0300 Subject: [PATCH 1/3] Add edge case test --- crates/vm/levm/tests/edge_case_tests.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/vm/levm/tests/edge_case_tests.rs b/crates/vm/levm/tests/edge_case_tests.rs index 99b198d05..d68189d0c 100644 --- a/crates/vm/levm/tests/edge_case_tests.rs +++ b/crates/vm/levm/tests/edge_case_tests.rs @@ -1,6 +1,7 @@ use bytes::Bytes; use ethrex_core::U256; use ethrex_levm::{ + errors::{TxResult, VMError}, operations::Operation, utils::{new_vm_with_bytecode, new_vm_with_ops}, }; @@ -101,3 +102,12 @@ fn test_is_negative() { let mut current_call_frame = vm.call_frames.pop().unwrap(); vm.execute(&mut current_call_frame); } + +#[test] +fn test_non_compliance_returndatacopy() { + let mut vm = + new_vm_with_bytecode(Bytes::copy_from_slice(&[56, 56, 56, 56, 56, 56, 62, 56])).unwrap(); + let mut current_call_frame = vm.call_frames.pop().unwrap(); + let txreport = vm.execute(&mut current_call_frame); + assert_eq!(txreport.result, TxResult::Revert(VMError::VeryLargeNumber)); +} From bfc37d9a776280509158923a972b608264bdaa32 Mon Sep 17 00:00:00 2001 From: maximopalopoli Date: Fri, 22 Nov 2024 18:29:24 -0300 Subject: [PATCH 2/3] Return error instead of creating a vector with zeros --- crates/vm/levm/src/opcode_handlers/environment.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/vm/levm/src/opcode_handlers/environment.rs b/crates/vm/levm/src/opcode_handlers/environment.rs index f0d7e02c8..ba87015a0 100644 --- a/crates/vm/levm/src/opcode_handlers/environment.rs +++ b/crates/vm/levm/src/opcode_handlers/environment.rs @@ -404,7 +404,7 @@ impl VM { .min(sub_return_data_len), ) } else { - vec![0u8; size].into() + return Err(VMError::VeryLargeNumber); // Maybe can create a new error instead of using this one }; current_call_frame.memory.store_bytes(dest_offset, &data)?; From f185f2137098fdca6c1830716347e127c98edb4c Mon Sep 17 00:00:00 2001 From: maximopalopoli Date: Fri, 22 Nov 2024 18:31:16 -0300 Subject: [PATCH 3/3] Put if with error before the usual case --- .../levm/src/opcode_handlers/environment.rs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/vm/levm/src/opcode_handlers/environment.rs b/crates/vm/levm/src/opcode_handlers/environment.rs index ba87015a0..106317472 100644 --- a/crates/vm/levm/src/opcode_handlers/environment.rs +++ b/crates/vm/levm/src/opcode_handlers/environment.rs @@ -393,19 +393,19 @@ impl VM { } let sub_return_data_len = current_call_frame.sub_return_data.len(); - let data = if returndata_offset < sub_return_data_len { - current_call_frame.sub_return_data.slice( - returndata_offset - ..(returndata_offset - .checked_add(size) - .ok_or(VMError::Internal( - InternalError::ArithmeticOperationOverflow, - ))?) - .min(sub_return_data_len), - ) - } else { + + if returndata_offset >= sub_return_data_len { return Err(VMError::VeryLargeNumber); // Maybe can create a new error instead of using this one - }; + } + let data = current_call_frame.sub_return_data.slice( + returndata_offset + ..(returndata_offset + .checked_add(size) + .ok_or(VMError::Internal( + InternalError::ArithmeticOperationOverflow, + ))?) + .min(sub_return_data_len), + ); current_call_frame.memory.store_bytes(dest_offset, &data)?;