1
0
Fork 0
forked from AbleOS/ableos
ableos-idl/ableos/src/wasm/mod.rs

118 lines
3.8 KiB
Rust
Raw Normal View History

use alloc::string::String;
use wasmi::TrapKind;
2021-12-24 03:30:27 -06:00
use {
alloc::format,
wasm_sys::SysCall,
wasmi::{
Error, Externals, FuncInstance, FuncRef, ImportsBuilder, ModuleImportResolver,
ModuleInstance, RuntimeArgs, RuntimeValue, Signature, Trap, ValueType,
},
2021-11-28 14:50:14 -06:00
};
2021-11-28 14:50:14 -06:00
mod wasm_sys;
struct HostFunctions;
impl HostFunctions {
fn check_signature(&self, index: usize, signature: &Signature) -> bool {
let (params, ret_ty): (&[ValueType], Option<ValueType>) = match index.into() {
SysCall::KILL => (&[], None),
SysCall::EMPTY => (&[], None),
_ => return false,
};
signature.params() == params && signature.return_type() == ret_ty
}
2021-11-28 14:50:14 -06:00
}
impl Externals for HostFunctions {
fn invoke_index(
&mut self,
index: usize,
args: RuntimeArgs,
) -> Result<Option<RuntimeValue>, Trap> {
match index.into() {
// Take in one arg discard the rest
SysCall::KILL => {
info!("Program run at runtime called a system call");
debug!("Runtime arguments: {:?}", args);
Ok(None)
}
// Do nothing
SysCall::EMPTY => Ok(None),
SysCall::EXIT => Ok(None),
SysCall::CONSOLE_RESET => Ok(None),
SysCall::CONSOLE_IN => {
info!("In");
Ok(None)
}
2022-01-01 18:06:46 -06:00
SysCall::CONSOLE_OUT => {
// Eventually change this to 2- ptr and len
if args.len() != 1 {
return Err(Trap::new(TrapKind::UnexpectedSignature));
}
let arg: u64 = args.nth(0);
let buf = unsafe { String::from_utf8_unchecked(arg.to_le_bytes().to_vec()) };
println!["helllooooooo{}", buf];
Ok(None)
}
SysCall::CONSOLE_GET_TITLE => Ok(None),
SysCall::CONSOLE_SET_TITLE => Ok(None),
_ => panic!("Unimplemented function at {}", index),
}
}
2021-11-28 14:50:14 -06:00
}
impl ModuleImportResolver for HostFunctions {
fn resolve_func(&self, field_name: &str, signature: &Signature) -> Result<FuncRef, Error> {
let index = match field_name {
"kill" => SysCall::KILL as usize,
"empty" => SysCall::EMPTY as usize,
"exit" => SysCall::EXIT as usize,
"console_out" => SysCall::CONSOLE_OUT as usize,
_ => {
return Err(Error::Instantiation(format!(
"Export {} not found",
field_name
)))
}
};
2021-11-28 14:50:14 -06:00
if !self.check_signature(index, signature) {
return Err(Error::Instantiation(format!(
"Export {} has a bad signature",
field_name
)));
}
2021-11-28 14:50:14 -06:00
Ok(FuncInstance::alloc_host(
Signature::new(&[][..], None),
index,
))
}
2021-11-28 14:50:14 -06:00
}
pub fn evaluate() {
let wasm_binary = include_bytes!("bin/console_out_test.wasm");
2021-11-28 14:50:14 -06:00
// Load wasm binary and prepare it for instantiation.
let module = wasmi::Module::from_buffer(&wasm_binary).expect("failed to load wasm");
2021-11-28 14:50:14 -06:00
let imports = ImportsBuilder::new().with_resolver("env", &HostFunctions);
2021-11-28 14:50:14 -06:00
// 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")
.unwrap()
.assert_no_start();
2021-11-28 14:50:14 -06:00
// Finally, invoke the exported function "test" with no parameters
// and empty external function executor.
let result: i32 = instance
.invoke_export("_start", &[], &mut HostFunctions)
.expect("failed to execute export")
.unwrap()
.try_into()
.unwrap();
2021-11-28 14:50:14 -06:00
println!("{:?}", result);
2021-11-28 14:50:14 -06:00
}