Skip to content

Commit

Permalink
Allow exclusive access to vm memory
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed May 24, 2024
1 parent 134fbc0 commit 339e17d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
16 changes: 8 additions & 8 deletions crates/namada/src/vm/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>, u64), Self::Error>;

/// Write bytes to memory. Returns the gas cost.
fn write_bytes(
&self,
&mut self,
offset: u64,
bytes: impl AsRef<[u8]>,
) -> Result<u64, Self::Error>;

/// 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<u64, Self::Error>;
Expand All @@ -55,7 +55,7 @@ pub mod testing {
type Error = Infallible;

fn read_bytes(
&self,
&mut self,
offset: u64,
len: usize,
) -> Result<(Vec<u8>, u64)> {
Expand All @@ -64,7 +64,7 @@ pub mod testing {
}

fn write_bytes(
&self,
&mut self,
offset: u64,
bytes: impl AsRef<[u8]>,
) -> Result<u64> {
Expand All @@ -77,7 +77,7 @@ pub mod testing {
}

fn read_string(
&self,
&mut self,
offset: u64,
len: usize,
) -> Result<(String, u64)> {
Expand All @@ -88,7 +88,7 @@ pub mod testing {
Ok((string, 0))
}

fn write_string(&self, offset: u64, string: String) -> Result<u64> {
fn write_string(&mut self, offset: u64, string: String) -> Result<u64> {
let bytes = string.as_bytes();
let len = bytes.len();
let target =
Expand Down
20 changes: 16 additions & 4 deletions crates/namada/src/vm/wasm/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>, u64)> {
fn read_bytes(
&mut self,
offset: u64,
len: usize,
) -> Result<(Vec<u8>, u64)> {
self.access(|memory| {
let mut store = self.store.borrow_mut();
let bytes = read_memory_bytes(&mut *store, memory, offset, len)?;
Expand All @@ -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<u64> {
fn write_bytes(
&mut self,
offset: u64,
bytes: impl AsRef<[u8]>,
) -> Result<u64> {
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
Expand All @@ -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)?
Expand All @@ -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<u64> {
fn write_string(&mut self, offset: u64, string: String) -> Result<u64> {
self.write_bytes(offset, string.as_bytes())
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/src/vm_host_env/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 339e17d

Please sign in to comment.