From 339e17d044b44f9a5899824f90243bc78a71d0fe Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Fri, 24 May 2024 10:59:14 +0100 Subject: [PATCH] Allow exclusive access to vm memory --- crates/namada/src/vm/memory.rs | 16 ++++++++-------- crates/namada/src/vm/wasm/memory.rs | 20 ++++++++++++++++---- crates/tests/src/vm_host_env/tx.rs | 2 +- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/crates/namada/src/vm/memory.rs b/crates/namada/src/vm/memory.rs index e55117726c..fe53a6c832 100644 --- a/crates/namada/src/vm/memory.rs +++ b/crates/namada/src/vm/memory.rs @@ -9,28 +9,28 @@ pub trait VmMemory: Clone + Send + Sync { /// Returns bytes read from memory together with the associated gas cost. fn read_bytes( - &self, + &mut self, offset: u64, len: usize, ) -> Result<(Vec, u64), Self::Error>; /// Write bytes to memory. Returns the gas cost. fn write_bytes( - &self, + &mut self, offset: u64, bytes: impl AsRef<[u8]>, ) -> Result; /// Returns string read from memory together with the associated gas cost. fn read_string( - &self, + &mut self, offset: u64, len: usize, ) -> Result<(String, u64), Self::Error>; /// Write string to memory. Returns the gas cost. fn write_string( - &self, + &mut self, offset: u64, string: String, ) -> Result; @@ -55,7 +55,7 @@ pub mod testing { type Error = Infallible; fn read_bytes( - &self, + &mut self, offset: u64, len: usize, ) -> Result<(Vec, u64)> { @@ -64,7 +64,7 @@ pub mod testing { } fn write_bytes( - &self, + &mut self, offset: u64, bytes: impl AsRef<[u8]>, ) -> Result { @@ -77,7 +77,7 @@ pub mod testing { } fn read_string( - &self, + &mut self, offset: u64, len: usize, ) -> Result<(String, u64)> { @@ -88,7 +88,7 @@ pub mod testing { Ok((string, 0)) } - fn write_string(&self, offset: u64, string: String) -> Result { + fn write_string(&mut self, offset: u64, string: String) -> Result { let bytes = string.as_bytes(); let len = bytes.len(); let target = diff --git a/crates/namada/src/vm/wasm/memory.rs b/crates/namada/src/vm/wasm/memory.rs index 89ebcd037f..6cd7bbf924 100644 --- a/crates/namada/src/vm/wasm/memory.rs +++ b/crates/namada/src/vm/wasm/memory.rs @@ -311,7 +311,11 @@ impl VmMemory for WasmMemory { /// Read bytes from memory at the given offset and length, return the bytes /// and the gas cost - fn read_bytes(&self, offset: u64, len: usize) -> Result<(Vec, u64)> { + fn read_bytes( + &mut self, + offset: u64, + len: usize, + ) -> Result<(Vec, u64)> { self.access(|memory| { let mut store = self.store.borrow_mut(); let bytes = read_memory_bytes(&mut *store, memory, offset, len)?; @@ -322,7 +326,11 @@ impl VmMemory for WasmMemory { } /// Write bytes into memory at the given offset and return the gas cost - fn write_bytes(&self, offset: u64, bytes: impl AsRef<[u8]>) -> Result { + fn write_bytes( + &mut self, + offset: u64, + bytes: impl AsRef<[u8]>, + ) -> Result { self.access(|memory| { // No need for a separate gas multiplier for writes since we are // only writing to memory and we already charge gas for @@ -337,7 +345,11 @@ impl VmMemory for WasmMemory { /// Read string from memory at the given offset and bytes length, and return /// the gas cost - fn read_string(&self, offset: u64, len: usize) -> Result<(String, u64)> { + fn read_string( + &mut self, + offset: u64, + len: usize, + ) -> Result<(String, u64)> { let (bytes, gas) = self.read_bytes(offset, len)?; let string = std::str::from_utf8(&bytes) .map_err(Error::InvalidUtf8String)? @@ -347,7 +359,7 @@ impl VmMemory for WasmMemory { /// Write string into memory at the given offset and return the gas cost #[allow(dead_code)] - fn write_string(&self, offset: u64, string: String) -> Result { + fn write_string(&mut self, offset: u64, string: String) -> Result { self.write_bytes(offset, string.as_bytes()) } } diff --git a/crates/tests/src/vm_host_env/tx.rs b/crates/tests/src/vm_host_env/tx.rs index 7df8beb282..594391024d 100644 --- a/crates/tests/src/vm_host_env/tx.rs +++ b/crates/tests/src/vm_host_env/tx.rs @@ -759,7 +759,7 @@ mod tests { batched_tx, } = test_env; - let tx_env = vm::host_env::testing::tx_env_with_wasm_memory( + let mut tx_env = vm::host_env::testing::tx_env_with_wasm_memory( state, iterators, verifiers,