pub mod service; use wasmer_runtime_core::{ loader::{self, Loader, Instance}, backend::RunnableModule, vm::{Ctx, LocalGlobal, SigId, Anyfunc}, module::ModuleInfo, types::{Value, LocalMemoryIndex, LocalTableIndex, ImportedMemoryIndex, ImportedTableIndex}, structures::TypedIndex, }; use service::{ServiceContext, LoadProfile, RunProfile, TableEntryRequest}; pub struct KernelLoader; impl Loader for KernelLoader { type Instance = KernelInstance; type Error = String; fn load(&self, rm: &dyn RunnableModule, module: &ModuleInfo, full_ctx: &Ctx) -> Result { let ctx = &full_ctx.internal; let code = rm.get_code().unwrap(); let memory = if let Some(_) = module.memories.get(LocalMemoryIndex::new(0)) { Some(unsafe { ::std::slice::from_raw_parts((**ctx.memories).base, (**ctx.memories).bound) }) } else if let Some(_) = module.imported_memories.get(ImportedMemoryIndex::new(0)) { return Err("imported memory is not supported".into()); } else { None }; let table: Option> = if let Some(_) = module.tables.get(LocalTableIndex::new(0)) { Some(unsafe { let table = &**ctx.tables; let elements: &[Anyfunc] = ::std::slice::from_raw_parts(table.base as *const Anyfunc, table.count); let base_addr = code.as_ptr() as usize; let end_addr = base_addr + code.len(); elements.iter().map(|x| { let func_addr = x.func as usize; TableEntryRequest { offset: if x.func.is_null() || func_addr < base_addr || func_addr >= end_addr { ::std::usize::MAX } else { x.func as usize - base_addr }, sig_id: x.sig_id.0, } }).collect() }) } else if let Some(_) = module.imported_tables.get(ImportedTableIndex::new(0)) { return Err("imported table is not supported".into()); } else { None }; if module.imported_globals.len() > 0 { return Err("imported globals are not supported".into()); } let globals: Vec = unsafe { 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); } let dynamic_sigindices: &[u32] = unsafe { ::std::mem::transmute::<&[SigId], &[u32]>( ::std::slice::from_raw_parts(ctx.dynamic_sigindices, full_ctx.dynamic_sigindice_count()) ) }; let profile = LoadProfile { code: code, memory: memory, memory_max: 0, globals: &globals, imports: &import_names, dynamic_sigindices: dynamic_sigindices, table: table.as_ref().map(|x| x.as_slice()), }; let sc = ServiceContext::new(profile).map_err(|x| format!("{:?}", x))?; Ok(KernelInstance { context: sc, offsets: rm.get_offsets().unwrap(), }) } } pub struct KernelInstance { context: ServiceContext, offsets: Vec, } impl Instance for KernelInstance { type Error = String; fn call(&mut self, id: usize, args: &[Value]) -> Result { let args: Vec = args.iter().map(|x| x.to_u64()).collect(); let ret = self.context.run_code(RunProfile { entry_offset: self.offsets[id] as u32, params: &args, }).map_err(|x| format!("{:?}", x))?; Ok(ret) } fn read_memory(&mut self, offset: u32, len: u32) -> Result, String> { self.context.read_memory(offset, len).map_err(|x| format!("{:?}", x)) } fn write_memory(&mut self, offset: u32, len: u32, buf: &[u8]) -> Result<(), String> { self.context.write_memory(offset, len, buf).map_err(|x| format!("{:?}", x)) } }