diff --git a/lib/clif-backend/src/module_env.rs b/lib/clif-backend/src/module_env.rs index a8476f284..cde3b0ed0 100644 --- a/lib/clif-backend/src/module_env.rs +++ b/lib/clif-backend/src/module_env.rs @@ -351,9 +351,7 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa> let mut func = ir::Function::with_name_signature(name, sig); - println!("translating function"); func_translator.translate(body_bytes, &mut func, &mut func_env)?; - println!("done translating function"); func }; diff --git a/lib/runtime/examples/test.rs b/lib/runtime/examples/test.rs index a20965615..903ff76d0 100644 --- a/lib/runtime/examples/test.rs +++ b/lib/runtime/examples/test.rs @@ -4,7 +4,7 @@ use wasmer_runtime::{import::Imports, Instance}; fn main() { let mut instance = create_module_1(); - let result = instance.call("get-0", &[]); + let result = instance.call("call-overwritten-element", &[]); println!("result: {:?}", result); } @@ -20,14 +20,17 @@ fn main() { fn create_module_1() -> Instance { let module_str = r#"(module (type (;0;) (func (result i32))) - (import "spectest" "global_i32" (global (;0;) i32)) + (import "spectest" "table" (table (;0;) 10 anyfunc)) (func (;0;) (type 0) (result i32) - get_global 0) + i32.const 65) (func (;1;) (type 0) (result i32) - get_global 1) - (global (;1;) i32 (get_global 0)) - (export "get-0" (func 0)) - (export "get-0-ref" (func 1))) + i32.const 66) + (func (;2;) (type 0) (result i32) + i32.const 9 + call_indirect (type 0)) + (export "call-overwritten-element" (func 2)) + (elem (;0;) (i32.const 9) 0) + (elem (;1;) (i32.const 9) 1)) "#; let wasm_binary = wat2wasm(module_str.as_bytes()).expect("WAST not valid or malformed"); let module = wasmer_runtime::compile(&wasm_binary[..], &CraneliftCompiler::new()) diff --git a/lib/runtime/src/backing.rs b/lib/runtime/src/backing.rs index 662b00858..095c5b616 100644 --- a/lib/runtime/src/backing.rs +++ b/lib/runtime/src/backing.rs @@ -307,12 +307,43 @@ impl ImportBacking { imports: &mut Imports, vmctx: *mut vm::Ctx, ) -> LinkResult { - Ok(ImportBacking { - functions: import_functions(module, imports, vmctx)?, - memories: import_memories(module, imports, vmctx)?, - tables: import_tables(module, imports, vmctx)?, - globals: import_globals(module, imports)?, - }) + let mut failed = false; + let mut link_errors = vec![]; + + let functions = import_functions(module, imports, vmctx).unwrap_or_else(|le| { + failed = true; + link_errors.extend(le); + Map::new().into_boxed_map() + }); + + let memories = import_memories(module, imports, vmctx).unwrap_or_else(|le| { + failed = true; + link_errors.extend(le); + Map::new().into_boxed_map() + }); + + let tables = import_tables(module, imports, vmctx).unwrap_or_else(|le| { + failed = true; + link_errors.extend(le); + Map::new().into_boxed_map() + }); + + let globals = import_globals(module, imports).unwrap_or_else(|le| { + failed = true; + link_errors.extend(le); + Map::new().into_boxed_map() + }); + + if failed { + Err(link_errors) + } else { + Ok(ImportBacking { + functions, + memories, + tables, + globals, + }) + } } pub fn imported_memory(&self, memory_index: ImportedMemoryIndex) -> vm::ImportedMemory { @@ -325,6 +356,7 @@ fn import_functions( imports: &mut Imports, vmctx: *mut vm::Ctx, ) -> LinkResult> { + let mut link_errors = vec![]; let mut functions = Map::with_capacity(module.imported_functions.len()); for (index, ImportName { namespace, name }) in &module.imported_functions { let sig_index = module.func_assoc[index.convert_up(module)]; @@ -347,12 +379,12 @@ fn import_functions( }, }); } else { - Err(LinkError::IncorrectImportSignature { + link_errors.push(LinkError::IncorrectImportSignature { namespace: namespace.clone(), name: name.clone(), expected: expected_sig.clone(), found: signature.clone(), - })? + }); } } Some(export_type) => { @@ -363,20 +395,27 @@ fn import_functions( Export::Global { .. } => "global", } .to_string(); - Err(LinkError::IncorrectImportType { + link_errors.push(LinkError::IncorrectImportType { namespace: namespace.clone(), name: name.clone(), expected: "function".to_string(), found: export_type_name, - })? + }); + } + None => { + link_errors.push(LinkError::ImportNotFound { + namespace: namespace.clone(), + name: name.clone(), + }); } - None => Err(LinkError::ImportNotFound { - namespace: namespace.clone(), - name: name.clone(), - })?, } } - Ok(functions.into_boxed_map()) + + if link_errors.len() > 0 { + Err(link_errors) + } else { + Ok(functions.into_boxed_map()) + } } fn import_memories( @@ -384,6 +423,7 @@ fn import_memories( imports: &mut Imports, vmctx: *mut vm::Ctx, ) -> LinkResult> { + let mut link_errors = vec![]; let mut memories = Map::with_capacity(module.imported_memories.len()); for (_index, (ImportName { namespace, name }, expected_memory_desc)) in &module.imported_memories @@ -406,12 +446,12 @@ fn import_memories( }, }); } else { - Err(LinkError::IncorrectMemoryDescription { + link_errors.push(LinkError::IncorrectMemoryDescription { namespace: namespace.clone(), name: name.clone(), expected: expected_memory_desc.clone(), found: memory_desc.clone(), - })? + }); } } Some(export_type) => { @@ -422,20 +462,27 @@ fn import_memories( Export::Global { .. } => "global", } .to_string(); - Err(LinkError::IncorrectImportType { + link_errors.push(LinkError::IncorrectImportType { namespace: namespace.clone(), name: name.clone(), expected: "memory".to_string(), found: export_type_name, - })? + }); + } + None => { + link_errors.push(LinkError::ImportNotFound { + namespace: namespace.clone(), + name: name.clone(), + }); } - None => Err(LinkError::ImportNotFound { - namespace: namespace.clone(), - name: name.clone(), - })?, } } - Ok(memories.into_boxed_map()) + + if link_errors.len() > 0 { + Err(link_errors) + } else { + Ok(memories.into_boxed_map()) + } } fn import_tables( @@ -443,6 +490,7 @@ fn import_tables( imports: &mut Imports, vmctx: *mut vm::Ctx, ) -> LinkResult> { + let mut link_errors = vec![]; let mut tables = Map::with_capacity(module.imported_tables.len()); for (_index, (ImportName { namespace, name }, expected_table_desc)) in &module.imported_tables { let table_import = imports @@ -463,12 +511,12 @@ fn import_tables( }, }); } else { - Err(LinkError::IncorrectTableDescription { + link_errors.push(LinkError::IncorrectTableDescription { namespace: namespace.clone(), name: name.clone(), expected: expected_table_desc.clone(), found: table_desc.clone(), - })? + }); } } Some(export_type) => { @@ -479,26 +527,34 @@ fn import_tables( Export::Global { .. } => "global", } .to_string(); - Err(LinkError::IncorrectImportType { + link_errors.push(LinkError::IncorrectImportType { namespace: namespace.clone(), name: name.clone(), expected: "table".to_string(), found: export_type_name, - })? + }); + } + None => { + link_errors.push(LinkError::ImportNotFound { + namespace: namespace.clone(), + name: name.clone(), + }); } - None => Err(LinkError::ImportNotFound { - namespace: namespace.clone(), - name: name.clone(), - })?, } } - Ok(tables.into_boxed_map()) + + if link_errors.len() > 0 { + Err(link_errors) + } else { + Ok(tables.into_boxed_map()) + } } fn import_globals( module: &ModuleInner, imports: &mut Imports, ) -> LinkResult> { + let mut link_errors = vec![]; let mut globals = Map::with_capacity(module.imported_globals.len()); for (_, (ImportName { namespace, name }, imported_global_desc)) in &module.imported_globals { let import = imports @@ -511,12 +567,12 @@ fn import_globals( global: local.inner(), }); } else { - Err(LinkError::IncorrectGlobalDescription { + link_errors.push(LinkError::IncorrectGlobalDescription { namespace: namespace.clone(), name: name.clone(), expected: imported_global_desc.clone(), found: global.clone(), - })? + }); } } Some(export_type) => { @@ -527,18 +583,25 @@ fn import_globals( Export::Global { .. } => "global", } .to_string(); - Err(LinkError::IncorrectImportType { + link_errors.push(LinkError::IncorrectImportType { namespace: namespace.clone(), name: name.clone(), expected: "global".to_string(), found: export_type_name, - })? + }); + } + None => { + link_errors.push(LinkError::ImportNotFound { + namespace: namespace.clone(), + name: name.clone(), + }); } - None => Err(LinkError::ImportNotFound { - namespace: namespace.clone(), - name: name.clone(), - })?, } } - Ok(globals.into_boxed_map()) + + if link_errors.len() > 0 { + Err(link_errors) + } else { + Ok(globals.into_boxed_map()) + } } diff --git a/lib/runtime/src/error.rs b/lib/runtime/src/error.rs index 3a350748b..b86296d69 100644 --- a/lib/runtime/src/error.rs +++ b/lib/runtime/src/error.rs @@ -2,7 +2,7 @@ use crate::types::{FuncSig, GlobalDesc, Memory, MemoryIndex, Table, TableIndex, pub type Result = std::result::Result>; pub type CompileResult = std::result::Result>; -pub type LinkResult = std::result::Result>; +pub type LinkResult = std::result::Result>; pub type RuntimeResult = std::result::Result>; pub type CallResult = std::result::Result>; @@ -120,7 +120,7 @@ impl PartialEq for CallError { #[derive(Debug, Clone)] pub enum Error { CompileError(CompileError), - LinkError(LinkError), + LinkError(Vec), RuntimeError(RuntimeError), CallError(CallError), } @@ -137,9 +137,9 @@ impl From> for Box { } } -impl From> for Box { - fn from(link_err: Box) -> Self { - Box::new(Error::LinkError(*link_err)) +impl From> for Box { + fn from(link_err: Vec) -> Self { + Box::new(Error::LinkError(link_err)) } } diff --git a/lib/runtime/src/instance.rs b/lib/runtime/src/instance.rs index 075d4709b..56f0c10d7 100644 --- a/lib/runtime/src/instance.rs +++ b/lib/runtime/src/instance.rs @@ -248,9 +248,9 @@ impl InstanceInner { ) -> (MemoryPointer, Context, Memory) { match mem_index.local_or_import(module) { LocalOrImport::Local(local_mem_index) => { - let vm_mem = &mut self.backing.memories[local_mem_index]; + let vm_mem = &mut self.backing.vm_memories[local_mem_index]; ( - unsafe { MemoryPointer::new(&mut vm_mem.into_vm_memory(local_mem_index)) }, + unsafe { MemoryPointer::new(vm_mem) }, Context::Internal, *module .memories @@ -313,9 +313,9 @@ impl InstanceInner { ) -> (TablePointer, Context, Table) { match table_index.local_or_import(module) { LocalOrImport::Local(local_table_index) => { - let vm_table = &mut self.backing.tables[local_table_index]; + let vm_table = &mut self.backing.vm_tables[local_table_index]; ( - unsafe { TablePointer::new(&mut vm_table.into_vm_table()) }, + unsafe { TablePointer::new(vm_table) }, Context::Internal, *module .tables diff --git a/lib/runtime/src/structures/slice.rs b/lib/runtime/src/structures/slice.rs index be2227e57..8574b2d5b 100644 --- a/lib/runtime/src/structures/slice.rs +++ b/lib/runtime/src/structures/slice.rs @@ -7,6 +7,7 @@ use std::{ /// This is a dynamically-sized slice /// that can only be indexed by the /// correct index type. +#[derive(Debug)] pub struct SliceMap where K: TypedIndex,