diff --git a/src/main.rs b/src/main.rs index 6554912..d2e259c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use wabt; use wasmi::{ - Error, Externals, FuncInstance, FuncRef, ImportsBuilder, ModuleImportResolver, ModuleInstance, - RuntimeArgs, RuntimeValue, Signature, Trap, ValueType, + Error, Externals, FuncInstance, FuncRef, ImportsBuilder, ModuleImportResolver, ModuleInstance, + RuntimeArgs, RuntimeValue, Signature, Trap, ValueType, }; mod wasm_sys; @@ -11,96 +11,96 @@ pub const KILL: usize = 0; struct HostFunctions; impl HostFunctions { - fn check_signature(&self, index: usize, signature: &Signature) -> bool { - let (params, ret_ty): (&[ValueType], Option) = match index { - *SysCall::KILL => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)), - _ => return false, - }; - signature.params() == params && signature.return_type() == ret_ty - } + fn check_signature(&self, index: usize, signature: &Signature) -> bool { + let (params, ret_ty): (&[ValueType], Option) = match index { + SysCall::KILL => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)), + _ => return false, + }; + signature.params() == params && signature.return_type() == ret_ty + } } impl Externals for HostFunctions { - fn invoke_index( - &mut self, - index: usize, - args: RuntimeArgs, - ) -> Result, Trap> { - match index { - SysCall::KILL => { - let a: u32 = args.nth_checked(0)?; - let b: u32 = args.nth_checked(1)?; - let result = a + b; + fn invoke_index( + &mut self, + index: usize, + args: RuntimeArgs, + ) -> Result, Trap> { + match index { + SysCall::KILL => { + let a: u32 = args.nth_checked(0)?; + let b: u32 = args.nth_checked(1)?; + let result = a + b; - Ok(Some(RuntimeValue::I32(result as i32))) - } - _ => panic!("Unimplemented function at {}", index), - } - } + Ok(Some(RuntimeValue::I32(result as i32))) + } + _ => panic!("Unimplemented function at {}", index), + } + } } impl ModuleImportResolver for HostFunctions { - fn resolve_func(&self, field_name: &str, signature: &Signature) -> Result { - let index = match field_name { - "add" => SysCall::KILL, - _ => { - return Err(Error::Instantiation(format!( - "Export {} not found", - field_name - ))) - } - }; - - if !self.check_signature(index, signature) { + fn resolve_func(&self, field_name: &str, signature: &Signature) -> Result { + let index = match field_name { + "add" => SysCall::KILL, + _ => { return Err(Error::Instantiation(format!( - "Export {} has a bad signature", - field_name - ))); - } + "Export {} not found", + field_name + ))) + } + }; - Ok(FuncInstance::alloc_host( - Signature::new(&[ValueType::I32, ValueType::I32][..], Some(ValueType::I32)), - index, - )) - } + if !self.check_signature(index, signature) { + return Err(Error::Instantiation(format!( + "Export {} has a bad signature", + field_name + ))); + } + + Ok(FuncInstance::alloc_host( + Signature::new(&[ValueType::I32, ValueType::I32][..], Some(ValueType::I32)), + index, + )) + } } fn main() { - // Parse WAT (WebAssembly Text format) into wasm bytecode. - let wasm_binary = wabt::wat2wasm(include_str!("../wasm/test.wat")); + // Parse WAT (WebAssembly Text format) into wasm bytecode. + let wasm_binary = wabt::wat2wasm(include_str!("../wasm/test.wat")); - let wasm_binary = match wasm_binary { - Ok(abc) => abc, - Err(abc) => { - println!("{}", abc); - return; - } - }; + let wasm_binary = match wasm_binary { + Ok(abc) => abc, + Err(abc) => { + println!("{}", abc); + return; + } + }; - // .expect("failed to parse wat"); + // .expect("failed to parse wat"); - // Load wasm binary and prepare it for instantiation. - let module = wasmi::Module::from_buffer(&wasm_binary).expect("failed to load wasm"); + // Load wasm binary and prepare it for instantiation. + let module = wasmi::Module::from_buffer(&wasm_binary).expect("failed to load wasm"); - let imports = ImportsBuilder::new().with_resolver("host", &HostFunctions); + let imports = ImportsBuilder::new().with_resolver("host", &HostFunctions); - // Instantiate a module with empty imports and - // assert that there is no `start` function. - let instance = ModuleInstance::new(&module, &imports) - .expect("failed to instantiate wasm module") - .assert_no_start(); + // Instantiate a module with empty imports and + // assert that there is no `start` function. + let instance = ModuleInstance::new(&module, &imports) + .expect("failed to instantiate wasm module") + .assert_no_start(); - // Finally, invoke the exported function "test" with no parameters - // and empty external function executor. - let result: i32 = instance - .invoke_export("main", &[], &mut HostFunctions) - // .with_resolver(&mut HostFunctions) - .expect("failed to execute export") - .unwrap() - .try_into() - .unwrap(); + // Finally, invoke the exported function "test" with no parameters + // and empty external function executor. + let result: i32 = instance + .invoke_export("main", &[], &mut HostFunctions) + // .with_resolver(&mut HostFunctions) + .expect("failed to execute export") + .unwrap() + .try_into() + .unwrap(); - println!( - "{:?}", - result // .unwrap() - ); + println!( + "{:?}", + result // .unwrap() + ); } diff --git a/src/wasm_sys.rs b/src/wasm_sys.rs index cbe651e..e176b57 100644 --- a/src/wasm_sys.rs +++ b/src/wasm_sys.rs @@ -1,36 +1,66 @@ #[repr(usize)] pub enum SysCall { - KILL = 0, // Provide a PID - CONSOLE_RESET = 1, // Reset the console - CONSOLE_IN = 2, // Console Input - CONSOLE_OUT = 3, // Console output - CONSOLE_GET_TITLE = 4, // Get the console title - CONSOLE_SET_TITLE = 5, // Set the console title + KILL = 0, // Provide a PID + CONSOLE_RESET = 1, // Reset the console + CONSOLE_IN = 2, // Console Input + CONSOLE_OUT = 3, // Console output + CONSOLE_GET_TITLE = 4, // Get the console title + CONSOLE_SET_TITLE = 5, // Set the console title - //scheduler Related Syscals - GET_PRIORITY = 10, // Get scheduler priority - SET_PRIORITY = 11, // Set scheduler priority - // - GET_HOSTNAME = 12, - SET_HOSTNAME = 13, + //scheduler Related Syscals + GET_PRIORITY = 10, // Get scheduler priority + SET_PRIORITY = 11, // Set scheduler priority + // + GET_HOSTNAME = 12, + SET_HOSTNAME = 13, - //File Related syscalls - GET_CONFIG = 20, // Get config - SET_CONFIG = 21, // Set the config - // - MAKE_DIRECTORY = 22, // - DELETE_DIRECTORY = 23, // - RENAME_DIRECTORY = 24, // - SET_DIRECTORY_ACCESS = 25, // - // - MAKE_FILE = 26, // - DELETE_FILE = 27, // - RENAME_FILE = 28, // - SET_FILE_ACCESS = 29, // + //File Related syscalls + GET_CONFIG = 20, // Get config + SET_CONFIG = 21, // Set the config + // + MAKE_DIRECTORY = 22, // + DELETE_DIRECTORY = 23, // + RENAME_DIRECTORY = 24, // + SET_DIRECTORY_ACCESS = 25, // + // + MAKE_FILE = 26, // + DELETE_FILE = 27, // + RENAME_FILE = 28, // + SET_FILE_ACCESS = 29, // - FILE_READ = 30, - FILE_WRITE = 31, + FILE_READ = 30, + FILE_WRITE = 31, - // Security Syscalls - ENCRYPT = 50, + // Security Syscalls + ENCRYPT = 50, +} + +impl From for SysCall { + fn from(n: usize) -> Self { + match n { + 0 => Self::KILL, + 1 => Self::CONSOLE_RESET, + 2 => Self::CONSOLE_IN, + 3 => Self::CONSOLE_OUT, + 4 => Self::CONSOLE_GET_TITLE, + 5 => Self::CONSOLE_SET_TITLE, + 10 => Self::GET_PRIORITY, + 11 => Self::SET_PRIORITY, + 12 => Self::GET_HOSTNAME, + 13 => Self::SET_HOSTNAME, + 20 => Self::GET_CONFIG, + 21 => Self::SET_CONFIG, + 22 => Self::MAKE_DIRECTORY, + 23 => Self::DELETE_DIRECTORY, + 24 => Self::RENAME_DIRECTORY, + 25 => Self::SET_DIRECTORY_ACCESS, + 26 => Self::MAKE_FILE, + 27 => Self::DELETE_FILE, + 28 => Self::RENAME_FILE, + 29 => Self::SET_FILE_ACCESS, + 30 => Self::FILE_READ, + 31 => Self::FILE_WRITE, + 50 => Self::ENCRYPT, + } + } }