From c4e4efc694ca043aa3c4200f738e6883c854afba Mon Sep 17 00:00:00 2001 From: Heyang Zhou Date: Sat, 4 May 2019 08:28:13 -0700 Subject: [PATCH] kwasm imports --- lib/kwasm-loader/src/lib.rs | 8 ++++++++ lib/kwasm-loader/src/service.rs | 21 +++++++++++++++++++++ lib/runtime-core/src/backing.rs | 15 +++++++++++---- lib/runtime-core/src/import.rs | 4 ++++ src/bin/wasmer.rs | 3 ++- 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/lib/kwasm-loader/src/lib.rs b/lib/kwasm-loader/src/lib.rs index 74e76ed51..3213a18e7 100644 --- a/lib/kwasm-loader/src/lib.rs +++ b/lib/kwasm-loader/src/lib.rs @@ -34,12 +34,18 @@ impl Loader for KernelLoader { let globals: &[*mut LocalGlobal] = ::std::slice::from_raw_parts(ctx.globals, module.globals.len()); globals.iter().map(|x| (**x).data).collect() }; + let mut import_names: Vec = vec![]; + for (_, import) in &module.imported_functions { + let name = format!("{}##{}", module.namespace_table.get(import.namespace_index), module.name_table.get(import.name_index)); + import_names.push(name); + } Ok(KernelInstance { context: ServiceContext::connect().map_err(|x| format!("{:?}", x))?, code: code.to_vec(), memory: memory, globals: globals, offsets: rm.get_offsets().unwrap(), + import_names: import_names, }) } } @@ -50,6 +56,7 @@ pub struct KernelInstance { memory: Option>, globals: Vec, offsets: Vec, + import_names: Vec, } impl Instance for KernelInstance { @@ -64,6 +71,7 @@ impl Instance for KernelInstance { globals: &self.globals, params: &args, entry_offset: self.offsets[id] as u32, + imports: &self.import_names, }).map_err(|x| format!("{:?}", x))?; Ok(ret as u64) } diff --git a/lib/kwasm-loader/src/service.rs b/lib/kwasm-loader/src/service.rs index 0517ff1c5..194c639e3 100644 --- a/lib/kwasm-loader/src/service.rs +++ b/lib/kwasm-loader/src/service.rs @@ -53,11 +53,19 @@ struct RunCodeRequest { globals: *const u64, global_count: u32, + imported_funcs: *const ImportRequest, + imported_func_count: u32, + entry_offset: u32, params: *const u64, param_count: u32, } +#[repr(C)] +struct ImportRequest { + name: [u8; 64], +} + pub struct RunProfile<'a> { pub code: &'a [u8], pub memory: Option<&'a [u8]>, @@ -65,6 +73,7 @@ pub struct RunProfile<'a> { pub globals: &'a [u64], pub params: &'a [u64], pub entry_offset: u32, + pub imports: &'a [String] } pub struct ServiceContext { @@ -79,6 +88,16 @@ impl ServiceContext { } pub fn run_code(&mut self, run: RunProfile) -> ServiceResult { + let imports: Vec = run.imports.iter().map(|x| { + let mut req: ImportRequest = unsafe { ::std::mem::zeroed() }; + let x = x.as_bytes(); + let mut count = req.name.len() - 1; + if x.len() < count { + count = x.len(); + } + req.name[..count].copy_from_slice(&x[..count]); + req + }).collect(); let req = RunCodeRequest { code: run.code.as_ptr(), code_len: run.code.len() as u32, @@ -89,6 +108,8 @@ impl ServiceContext { table_count: 0, globals: run.globals.as_ptr(), global_count: run.globals.len() as u32, + imported_funcs: imports.as_ptr(), + imported_func_count: imports.len() as u32, params: run.params.as_ptr(), param_count: run.params.len() as u32, entry_offset: run.entry_offset, diff --git a/lib/runtime-core/src/backing.rs b/lib/runtime-core/src/backing.rs index b79180ad6..23aaf2f8a 100644 --- a/lib/runtime-core/src/backing.rs +++ b/lib/runtime-core/src/backing.rs @@ -449,10 +449,17 @@ fn import_functions( }); } None => { - link_errors.push(LinkError::ImportNotFound { - namespace: namespace.to_string(), - name: name.to_string(), - }); + if imports.allow_missing_functions { + functions.push(vm::ImportedFunc { + func: ::std::ptr::null(), + vmctx: ::std::ptr::null_mut(), + }); + } else { + link_errors.push(LinkError::ImportNotFound { + namespace: namespace.to_string(), + name: name.to_string(), + }); + } } } } diff --git a/lib/runtime-core/src/import.rs b/lib/runtime-core/src/import.rs index 14465deed..9522b0829 100644 --- a/lib/runtime-core/src/import.rs +++ b/lib/runtime-core/src/import.rs @@ -47,6 +47,7 @@ impl IsExport for Export { pub struct ImportObject { map: Rc>>>, state_creator: Option (*mut c_void, fn(*mut c_void))>>, + pub allow_missing_functions: bool, } impl ImportObject { @@ -55,6 +56,7 @@ impl ImportObject { Self { map: Rc::new(RefCell::new(HashMap::new())), state_creator: None, + allow_missing_functions: false, } } @@ -65,6 +67,7 @@ impl ImportObject { Self { map: Rc::new(RefCell::new(HashMap::new())), state_creator: Some(Rc::new(state_creator)), + allow_missing_functions: false, } } @@ -116,6 +119,7 @@ impl ImportObject { Self { map: Rc::clone(&self.map), state_creator: self.state_creator.clone(), + allow_missing_functions: false, } } diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index bf891dcd8..f8f6e71c7 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -339,7 +339,8 @@ fn execute_wasm(options: &Run) -> Result<(), String> { }; if let Some(loader) = options.loader { - let import_object = wasmer_runtime_core::import::ImportObject::new(); + let mut import_object = wasmer_runtime_core::import::ImportObject::new(); + import_object.allow_missing_functions = true; // Import initialization might be left to the loader. let instance = module .instantiate(&import_object) .map_err(|e| format!("Can't instantiate module: {:?}", e))?;