1
0
Fork 0
forked from AbleOS/ableos

made console_out print the bytes of its arg as ascii

This commit is contained in:
elfein727 2022-01-01 19:07:56 -08:00
parent f2dfad3162
commit 8680e6f470
3 changed files with 144 additions and 134 deletions

Binary file not shown.

View file

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

View file

@ -9,6 +9,7 @@ macro_rules! syscall_enum {
syscall_enum![@get_last $($VariantTail),*] syscall_enum![@get_last $($VariantTail),*]
}; };
($($Variant:ident=$Value:expr,)*) => { ($($Variant:ident=$Value:expr,)*) => {
#[allow(clippy::upper_case_acronyms)]
#[repr(usize)] #[repr(usize)]
pub enum SysCall { pub enum SysCall {
$($Variant = $Value),* $($Variant = $Value),*
@ -28,64 +29,64 @@ macro_rules! syscall_enum {
}; };
} }
syscall_enum! { syscall_enum! {
KILL=0, // Provide a PID KILL=0, // Provide a PID
CONSOLE_RESET=1, // Reset the console CONSOLE_RESET=1, // Reset the console
CONSOLE_IN=2, // Console Input CONSOLE_IN=2, // Console Input
CONSOLE_OUT=3, // Console output CONSOLE_OUT=3, // Console output
CONSOLE_GET_TITLE=4, // Get the console title CONSOLE_GET_TITLE=4, // Get the console title
CONSOLE_SET_TITLE=5, // Set the console title CONSOLE_SET_TITLE=5, // Set the console title
GET_PID=6, // Get the proccess ID GET_PID=6, // Get the proccess ID
PROCESS_INFO=7, // Get information about the process PROCESS_INFO=7, // Get information about the process
//scheduler Related Syscals //scheduler Related Syscals
GET_PRIORITY=8, // Get scheduler priority GET_PRIORITY=8, // Get scheduler priority
SET_PRIORITY=9, // Set scheduler priority SET_PRIORITY=9, // Set scheduler priority
// //
GET_HOSTNAME=10, GET_HOSTNAME=10,
SET_HOSTNAME=11, SET_HOSTNAME=11,
NEW_THREAD=12, NEW_THREAD=12,
NEW_TASK=13, NEW_TASK=13,
KILL_THREAD=14, KILL_THREAD=14,
KILL_TASK=15, KILL_TASK=15,
GET_THREAD=16, GET_THREAD=16,
GET_TASK=17, GET_TASK=17,
SEND= 18, SEND= 18,
RECEIVE = 19, RECEIVE = 19,
RESPOND = 20, RESPOND = 20,
//File Related syscalls //File Related syscalls
// //
MAKE_DIRECTORY=22, // MAKE_DIRECTORY=22, //
DELETE_DIRECTORY=23, // DELETE_DIRECTORY=23, //
RENAME_DIRECTORY=24, // RENAME_DIRECTORY=24, //
SET_DIRECTORY_ACCESS=25, // SET_DIRECTORY_ACCESS=25, //
// //
MAKE_FILE=26, // MAKE_FILE=26, //
DELETE_FILE=27, // DELETE_FILE=27, //
RENAME_FILE=28, // RENAME_FILE=28, //
SET_FILE_ACCESS=29, // SET_FILE_ACCESS=29, //
FILE_READ=30, FILE_READ=30,
FILE_WRITE=31, FILE_WRITE=31,
SLEEP=32, // Sleep in milliseconds SLEEP=32, // Sleep in milliseconds
SLEEP_UNTIL=33, // Sleep until this time in milliseconds (if this is below the current time return) SLEEP_UNTIL=33, // Sleep until this time in milliseconds (if this is below the current time return)
NANOSLEEP=34, // Sleep in nanoseconds NANOSLEEP=34, // Sleep in nanoseconds
NANOSLEEP_UNTIL=35, // Sleep until this time nanoseconds (if this is below the current time return) NANOSLEEP_UNTIL=35, // Sleep until this time nanoseconds (if this is below the current time return)
GET_TIME=36, // Gets the system time (some derivitive of seconds) GET_TIME=36, // Gets the system time (some derivitive of seconds)
SET_TIME=37, // Sets the system time (some derivitive of seconds) SET_TIME=37, // Sets the system time (some derivitive of seconds)
// Socket SysCall // Socket SysCall
SOCKET_BIND=39, // Used by servers to lock a port SOCKET_BIND=39, // Used by servers to lock a port
SOCKET_CONNECT=40, SOCKET_CONNECT=40,
SOCKET_DISCONNECT=41, SOCKET_DISCONNECT=41,
SOCKET_SEND=42, SOCKET_SEND=42,
SOCKET_RECEIVE=43, SOCKET_RECEIVE=43,
EXIT=50, EXIT=50,
EMPTY=0xFFFF, EMPTY=0xFFFF,
} }