Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
More review comments and minor extra comments
  • Loading branch information
koute committed Nov 22, 2021
commit e63c7a1ea60d65a67ee9818b9f39ae55372ac7aa
18 changes: 12 additions & 6 deletions client/executor/wasmtime/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,17 @@ pub(crate) struct HostContext<'a, 'b> {

impl<'a, 'b> HostContext<'a, 'b> {
fn host_state(&self) -> &HostState {
self.caller.data().host_state().expect("host state cannot be empty")
self.caller
.data()
.host_state()
.expect("host state is not empty when calling a function in wasm; qed")
}

fn host_state_mut(&mut self) -> &mut HostState {
self.caller.data_mut().host_state_mut().expect("host state cannot be empty")
self.caller
.data_mut()
.host_state_mut()
.expect("host state is not empty when calling a function in wasm; qed")
}

fn sandbox_store(&self) -> &sandbox::Store<Func> {
Expand Down Expand Up @@ -109,7 +115,7 @@ impl<'a, 'b> sp_wasm_interface::FunctionContext for HostContext<'a, 'b> {
let memory = self.caller.data().memory();
let (memory, data) = memory.data_and_store_mut(&mut self.caller);
data.host_state_mut()
.expect("host state cannot be empty")
.expect("host state is not empty when calling a function in wasm; qed")
.allocator
.allocate(memory, size)
.map_err(|e| e.to_string())
Expand All @@ -119,7 +125,7 @@ impl<'a, 'b> sp_wasm_interface::FunctionContext for HostContext<'a, 'b> {
let memory = self.caller.data().memory();
let (memory, data) = memory.data_and_store_mut(&mut self.caller);
data.host_state_mut()
.expect("host state cannot be empty")
.expect("host state is not empty when calling a function in wasm; qed")
.allocator
.deallocate(memory, ptr)
.map_err(|e| e.to_string())
Expand Down Expand Up @@ -147,7 +153,7 @@ impl<'a, 'b> Sandbox for HostContext<'a, 'b> {
Ok(buffer) => buffer,
};

if let Err(_) = util::write_memory_from(&mut self.caller, buf_ptr, &buffer) {
if util::write_memory_from(&mut self.caller, buf_ptr, &buffer).is_err() {
return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS)
}

Expand All @@ -170,7 +176,7 @@ impl<'a, 'b> Sandbox for HostContext<'a, 'b> {
Ok(buffer) => buffer,
};

if let Err(_) = sandboxed_memory.write_from(Pointer::new(offset as u32), &buffer) {
if sandboxed_memory.write_from(Pointer::new(offset as u32), &buffer).is_err() {
return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS)
}

Expand Down
8 changes: 5 additions & 3 deletions client/executor/wasmtime/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ use std::{
use wasmtime::{Engine, Memory, StoreLimits, Table};

pub(crate) struct StoreData {
/// The limits we aply to the store. We need to store it here to return a reference to this
/// The limits we apply to the store. We need to store it here to return a reference to this
/// object when we have the limits enabled.
pub(crate) limits: StoreLimits,
/// This will only be set when we call into the runtime.
pub(crate) host_state: Option<HostState>,
/// This will be always set once the store is initialized.
pub(crate) memory: Option<Memory>,
/// This will be set only if the runtime actually contains a table.
pub(crate) table: Option<Table>,
}

Expand All @@ -64,12 +66,12 @@ impl StoreData {
self.host_state.as_mut()
}

/// Returns a reference to the host memory.
/// Returns the host memory.
pub fn memory(&self) -> Memory {
self.memory.expect("memory is always set; qed")
}

/// Returns a reference to the host table.
/// Returns the host table.
pub fn table(&self) -> Option<Table> {
self.table
}
Expand Down