ableos/kernel/src/interp.rs

203 lines
5.2 KiB
Rust
Raw Normal View History

2023-04-10 01:16:30 -05:00
use alloc::string::String;
use alloc::vec::Vec;
2023-04-05 12:29:20 -05:00
use log::trace;
2023-04-10 01:16:30 -05:00
use wasmi::Instance;
2023-04-05 12:29:20 -05:00
use wasmi::{Caller, Func, Linker, Module, Store};
2023-04-07 16:44:33 -05:00
use xml::XMLElement;
2023-04-10 01:16:30 -05:00
use crate::handle;
#[derive(Debug)]
pub struct WasmContext {
pub proc_id: Option<u64>,
pub instance: Instance,
pub store: Store<HostState>,
}
2023-04-05 12:29:20 -05:00
pub fn wasm() -> Result<(), wasmi::Error> {
use wasmi::Config;
use wasmi::Engine;
2023-04-07 16:44:33 -05:00
let mut conf = Config::default();
conf.wasm_bulk_memory(true);
// conf.,
2023-04-05 12:29:20 -05:00
let engine = Engine::new(&conf);
2023-04-07 16:44:33 -05:00
// trace!("Engine constructed");
2023-04-05 12:29:20 -05:00
2023-04-07 16:44:33 -05:00
// let wasm = include_bytes!("../../wasm_syscall_test.wasm");
let wasm = include_bytes!("../../test.wasm");
2023-04-05 12:29:20 -05:00
2023-04-07 16:44:33 -05:00
// trace!("Loading WASM binary");
2023-04-05 12:29:20 -05:00
let module = Module::new(&engine, &wasm[..]).unwrap();
2023-04-07 16:44:33 -05:00
// trace!("Constructing wasm module");
2023-04-10 01:16:30 -05:00
let hs = HostState {};
2023-04-07 16:44:33 -05:00
let mut store = Store::new(&engine, hs);
// trace!("constructing host store");
let read_mem_addr = Func::wrap(
&mut store,
|caller: Caller<'_, HostState>, param: i32| -> i32 { read_memory_address(caller, param) },
);
2023-04-05 12:29:20 -05:00
2023-04-07 16:44:33 -05:00
let mut linker = <Linker<HostState>>::new(&engine);
linker.define(
"host",
"read_mem_addr",
Func::wrap(
&mut store,
|caller: Caller<'_, HostState>, param: i32| -> i32 {
read_memory_address(caller, param)
},
),
)?;
2023-04-05 12:29:20 -05:00
2023-04-07 16:44:33 -05:00
linker.define(
"host",
"read_object_attribute",
Func::wrap(&mut store, host_read_object_attribute),
)?;
2023-04-05 12:29:20 -05:00
2023-04-07 16:44:33 -05:00
linker.define(
"host",
"create_object",
Func::wrap(&mut store, host_make_object),
)?;
2023-04-05 12:29:20 -05:00
2023-04-07 16:44:33 -05:00
let instance = linker
.instantiate(&mut store, &module)?
.ensure_no_start(&mut store)?;
2023-04-05 12:29:20 -05:00
2023-04-07 16:44:33 -05:00
let version = instance.get_global(&store, "VERSION");
2023-04-05 12:29:20 -05:00
2023-04-07 16:44:33 -05:00
// trace!("Version: {:?}", version);
let hello = instance.get_typed_func::<(), (i32, i32)>(&store, "start")?;
let ret = hello.call(&mut store, ())?;
trace!("Called _start got return of {:?}", ret);
2023-04-05 12:29:20 -05:00
Ok(())
}
2023-04-10 01:16:30 -05:00
#[derive(Clone, Debug)]
pub struct HostState {}
2023-04-07 16:44:33 -05:00
pub fn read_memory_address(caller: Caller<'_, HostState>, address: i32) -> i32 {
2023-04-05 12:29:20 -05:00
trace!("Address: {}", address);
2023-04-07 16:44:33 -05:00
// let obj = host_make_object(caller, 16, 23);
2023-04-05 12:29:20 -05:00
0
}
2023-04-07 16:44:33 -05:00
use crate::kmain::OBJECTS;
pub fn host_make_object(
caller: Caller<'_, HostState>,
address_start: i32,
length_of_string: i32,
) -> i64 {
trace!(
"Called with addr {{ start {} length {} }}",
address_start,
length_of_string
);
let mem = caller.get_export("memory").unwrap().into_memory().unwrap();
let mem_array = mem.data(&caller);
let mut name = String::new();
for i in address_start..(address_start + length_of_string) {
let ch = mem_array[i as usize] as char;
name.push(ch);
}
trace!("Object Name {}", name);
let hand = handle::Handle::new();
{
let binding = OBJECTS;
let mut olock = binding.lock();
let obj = XMLElement::new(name);
olock.insert(handle::Handle::new(), obj);
}
hand.into()
}
pub fn host_read_object_attribute(
caller: Caller<'_, HostState>,
handle: i64,
address_start: i32,
length_of_string: i32,
) -> (i32, i32) {
{
let binding = OBJECTS;
let mut olock = binding.lock();
// olock.get(&handle);
}
let mem = caller.get_export("memory").unwrap().into_memory().unwrap();
let mem_array = mem.data(&caller);
let mut name = String::new();
for i in address_start..(address_start + length_of_string) {
let ch = mem_array[i as usize] as char;
name.push(ch);
}
(0, 0)
}
2023-04-10 01:16:30 -05:00
pub fn build_wasm_context(bytes: Vec<u8>) -> Result<WasmContext, wasmi::Error> {
use wasmi::Config;
use wasmi::Engine;
let mut conf = Config::default();
conf.wasm_bulk_memory(true);
// conf.,
let engine = Engine::new(&conf);
// trace!("Engine constructed");
// let wasm = include_bytes!("../../wasm_syscall_test.wasm");
let wasm = include_bytes!("../../test.wasm");
// trace!("Loading WASM binary");
let module = Module::new(&engine, &wasm[..]).unwrap();
// trace!("Constructing wasm module");
let hs = HostState {};
let mut store = Store::new(&engine, hs);
// trace!("constructing host store");
let read_mem_addr = Func::wrap(
&mut store,
|caller: Caller<'_, HostState>, param: i32| -> i32 { read_memory_address(caller, param) },
);
let mut linker = <Linker<HostState>>::new(&engine);
linker.define(
"host",
"read_mem_addr",
Func::wrap(
&mut store,
|caller: Caller<'_, HostState>, param: i32| -> i32 {
read_memory_address(caller, param)
},
),
)?;
linker.define(
"host",
"read_object_attribute",
Func::wrap(&mut store, host_read_object_attribute),
)?;
linker.define(
"host",
"create_object",
Func::wrap(&mut store, host_make_object),
)?;
let instance = linker
.instantiate(&mut store, &module)?
.ensure_no_start(&mut store)?;
let wc = WasmContext {
instance,
store,
proc_id: None,
};
Ok(wc)
}