Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
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
Removed panics on missing memory
  • Loading branch information
arkpar committed Jan 8, 2019
commit d4141d41dd6699a19dd591699fe8a18497e8bafc
34 changes: 13 additions & 21 deletions core/executor/src/wasm_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,13 +636,20 @@ impl WasmExecutor {
method: &str,
data: &[u8],
) -> Result<Vec<u8>> {
let module = ::wasmi::Module::from_buffer(code)
.expect("all modules compiled with rustc are valid wasm code; qed");
let module = self.prepare_module(ext, heap_pages, &module)
.expect("all modules compiled with rustc are valid wasm code; qed");
let module = ::wasmi::Module::from_buffer(code)?;
let module = self.prepare_module(ext, heap_pages, &module)?;
self.call_in_wasm_module(ext, &module, method, data)
}

fn get_mem_instance(module: &ModuleRef) -> Result<MemoryRef> {
Ok(module
.export_by_name("memory")
.ok_or_else(|| Error::from(ErrorKind::InvalidMemoryReference))?
.as_memory()
.ok_or_else(|| Error::from(ErrorKind::InvalidMemoryReference))?
.clone())
}

/// Call a given method in the given wasm-module runtime.
pub fn call_in_wasm_module<E: Externalities<Blake2Hasher>>(
&self,
Expand All @@ -653,14 +660,7 @@ impl WasmExecutor {
) -> Result<Vec<u8>> {
// extract a reference to a linear memory, optional reference to a table
// and then initialize FunctionExecutor.
let memory = module_instance
.export_by_name("memory")
// TODO: with code coming from the blockchain it isn't strictly been compiled with rustc anymore.
// these assumptions are probably not true anymore
.expect("all modules compiled with rustc should have an export named 'memory'; qed")
.as_memory()
.expect("in module generated by rustc export named 'memory' should be a memory; qed")
.clone();
let memory = Self::get_mem_instance(module_instance)?;
let table: Option<TableRef> = module_instance
.export_by_name("__indirect_function_table")
.and_then(|e| e.as_table().cloned());
Expand Down Expand Up @@ -721,15 +721,7 @@ impl WasmExecutor {

// extract a reference to a linear memory, optional reference to a table
// and then initialize FunctionExecutor.
let memory = intermediate_instance
.not_started_instance()
.export_by_name("memory")
// TODO: with code coming from the blockchain it isn't strictly been compiled with rustc anymore.
// these assumptions are probably not true anymore
.expect("all modules compiled with rustc should have an export named 'memory'; qed")
.as_memory()
.expect("in module generated by rustc export named 'memory' should be a memory; qed")
.clone();
let memory = Self::get_mem_instance(intermediate_instance.not_started_instance())?;
memory.grow(Pages(heap_pages)).map_err(|_| Error::from(ErrorKind::Runtime))?;
let table: Option<TableRef> = intermediate_instance
.not_started_instance()
Expand Down