add another msize test case

Signed-off-by: xermicus <cyrill@parity.io>
This commit is contained in:
xermicus
2024-03-26 15:11:39 +01:00
parent 17832855e0
commit ec952fd2cb
4 changed files with 42 additions and 4 deletions
+7
View File
@@ -6,4 +6,11 @@ contract MSize {
size := msize() size := msize()
} }
} }
function mStore100() public pure returns (uint size) {
assembly {
mstore(100, msize())
size := msize()
}
}
} }
+30 -2
View File
@@ -163,7 +163,7 @@ mod tests {
} }
#[test] #[test]
fn msize() { fn msize_plain() {
sol!( sol!(
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
contract MSize { contract MSize {
@@ -175,9 +175,9 @@ mod tests {
include_str!("../contracts/MSize.sol"), include_str!("../contracts/MSize.sol"),
false, false,
); );
let (instance, export) = mock_runtime::prepare(&code, None);
let input = MSize::mSizeCall::new(()).abi_encode(); let input = MSize::mSizeCall::new(()).abi_encode();
let (instance, export) = mock_runtime::prepare(&code, None);
let state = crate::mock_runtime::call(State::new(input), &instance, export); let state = crate::mock_runtime::call(State::new(input), &instance, export);
assert_eq!(state.output.flags, 0); assert_eq!(state.output.flags, 0);
@@ -187,4 +187,32 @@ mod tests {
let received = U256::from_be_bytes::<32>(state.output.data.try_into().unwrap()); let received = U256::from_be_bytes::<32>(state.output.data.try_into().unwrap());
assert_eq!(received, expected); assert_eq!(received, expected);
} }
#[test]
fn msize_non_word_sized_access() {
sol!(
#[derive(Debug, PartialEq, Eq)]
contract MSize {
function mStore100() public pure returns (uint);
}
);
let code = crate::compile_blob_with_options(
"MSize",
include_str!("../contracts/MSize.sol"),
false,
);
let (instance, export) = mock_runtime::prepare(&code, None);
let input = MSize::mStore100Call::new(()).abi_encode();
let state = crate::mock_runtime::call(State::new(input), &instance, export);
assert_eq!(state.output.flags, 0);
// https://docs.zksync.io/build/developer-reference/differences-with-ethereum.html#mstore-mload
// "Unlike EVM, where the memory growth is in words, on zkEVM the memory growth is counted in bytes."
// "For example, if you write mstore(100, 0) the msize on zkEVM will be 132, but on the EVM it will be 160."
let expected = U256::try_from(132).unwrap();
let received = U256::from_be_bytes::<32>(state.output.data.try_into().unwrap());
assert_eq!(received, expected);
}
} }
+4 -1
View File
@@ -201,5 +201,8 @@ where
.build_ptr_to_int(heap_start, context.xlen_type(), "heap_start")?, .build_ptr_to_int(heap_start, context.xlen_type(), "heap_start")?,
"heap_size", "heap_size",
)?; )?;
Ok(heap_size.as_basic_value_enum()) Ok(context
.builder()
.build_int_z_extend(heap_size, context.field_type(), "heap_size_extended")?
.as_basic_value_enum())
} }
@@ -89,5 +89,5 @@ where
"return_data_copy_memcpy_from_return_data", "return_data_copy_memcpy_from_return_data",
)?; )?;
Ok(()) todo!("Build heap GEP to allocate if necessary")
} }