Skip to content

Commit

Permalink
wip: a lil' more fixes, still not working
Browse files Browse the repository at this point in the history
  • Loading branch information
nerodesu017 committed Feb 18, 2025
1 parent 50bb20c commit f3a614e
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 59 deletions.
16 changes: 16 additions & 0 deletions src/core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ pub enum StoreInstantiationError {
TooManyMemories(usize),
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum LinkerError {
UnmetImport,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Error {
/// The magic number at the very start of the given WASM file is invalid.
Expand Down Expand Up @@ -107,6 +112,7 @@ pub enum Error {
TooManyLocals(usize),
UnsupportedProposal(Proposal),
Overflow,
LinkerError(LinkerError),
}

impl Display for Error {
Expand Down Expand Up @@ -264,6 +270,7 @@ impl Display for Error {
f.write_fmt(format_args!("Unsupported proposal: {:?}", proposal))
}
Error::Overflow => f.write_str("Overflow"),
Error::LinkerError(err) => err.fmt(f)
}
}
}
Expand Down Expand Up @@ -316,6 +323,15 @@ impl Display for StoreInstantiationError {
}
}

impl Display for LinkerError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
use LinkerError::*;
match self {
UnmetImport => f.write_str("Unmet import"),
}
}
}

pub type Result<T> = core::result::Result<T, Error>;

impl From<RuntimeError> for Error {
Expand Down
8 changes: 6 additions & 2 deletions src/execution/function_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use alloc::vec::Vec;
use crate::execution::{hooks::HookSet, value::InteropValueList, RuntimeInstance};
use crate::{RuntimeError, ValType, Value};

use super::store::Store;

pub struct FunctionRef {
pub(crate) module_name: String,
pub(crate) function_name: String,
Expand All @@ -22,17 +24,19 @@ impl FunctionRef {
&self,
runtime: &mut RuntimeInstance<H>,
params: Param,
store: &mut Store,
) -> Result<Returns, RuntimeError> {
runtime.invoke(self, params)
runtime.invoke(self, params, store)
}

pub fn invoke_dynamic<H: HookSet>(
&self,
runtime: &mut RuntimeInstance<H>,
params: Vec<Value>,
ret_types: &[ValType],
store: &mut Store,
) -> Result<Vec<Value>, RuntimeError> {
runtime.invoke_dynamic(self, params, ret_types)
runtime.invoke_dynamic(self, params, ret_types, store)
}

// pub fn get_return_types(&self) -> Vec<Value
Expand Down
3 changes: 2 additions & 1 deletion src/execution/interpreter_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
#[cfg(feature = "hooks")]
use crate::execution::hooks::HookSet;

use super::{execution_info::ExecutionInfo, lut::Lut};
use super::{execution_info::ExecutionInfo, lut::Lut, store::Store};

/// Interprets a functions. Parameters and return values are passed on the stack.
pub(super) fn run<H: HookSet>(
Expand All @@ -42,6 +42,7 @@ pub(super) fn run<H: HookSet>(
lut: &Lut,
stack: &mut Stack,
mut hooks: H,
store: &mut Store,
) -> Result<(), RuntimeError> {
let func_inst = modules[*current_module_idx]
.store
Expand Down
130 changes: 81 additions & 49 deletions src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ where
}

impl<'b> RuntimeInstance<'b, EmptyHookSet> {
pub fn new(validation_info: &'_ ValidationInfo<'b>) -> CustomResult<Self> {
Self::new_with_hooks(DEFAULT_MODULE, validation_info, EmptyHookSet)
pub fn new(validation_info: &'_ ValidationInfo<'b>, store: &mut Store) -> CustomResult<Self> {
Self::new_with_hooks(DEFAULT_MODULE, validation_info, EmptyHookSet, store)
}

pub fn new_named(
module_name: &str,
validation_info: &'_ ValidationInfo<'b>,
store: &mut Store,
) -> CustomResult<Self> {
Self::new_with_hooks(module_name, validation_info, EmptyHookSet)
Self::new_with_hooks(module_name, validation_info, EmptyHookSet, store)
}
}

Expand All @@ -73,6 +74,7 @@ where
module_name: &str,
validation_info: &'_ ValidationInfo<'b>,
hook_set: H,
store: &mut Store,
) -> CustomResult<Self> {
trace!("Starting instantiation of bytecode");

Expand All @@ -95,7 +97,7 @@ where
function_index: start,
exported: false,
};
instance.invoke::<(), ()>(&start_fn, ())?;
instance.invoke::<(), ()>(&start_fn, (), store)?;
}

Ok(instance)
Expand Down Expand Up @@ -173,24 +175,42 @@ where
&mut self,
function_ref: &FunctionRef,
params: Param,
store: &mut Store,
) -> Result<Returns, RuntimeError> {
// First, verify that the function reference is valid
let (module_idx, func_idx) = self.verify_function_ref(function_ref)?;

// -=-= Verification =-=-
trace!("{:?}", self.modules[module_idx].store.funcs);
// trace!("{:?}", self.modules[module_idx].store.funcs);
trace!("{:?}", self.modules[module_idx].functions);

let func_inst = self.modules[module_idx]
.store
.funcs
let func_inst_idx = self.modules[module_idx]
.functions
.get(func_idx)
.ok_or(RuntimeError::FunctionNotFound)?
.try_into_local()
.clone();

let func_inst = store
.functions
.get(func_inst_idx)
.ok_or(RuntimeError::FunctionNotFound)?;
let func_ty = self.modules[module_idx]
.fn_types
.get(func_inst.ty)
.unwrap_validated();

let func_ty = func_inst.ty();

// let func_ty = self.modules

// let func_inst = self.modules[module_idx]
// .store
// .funcs
// .get(func_idx)
// .ok_or(RuntimeError::FunctionNotFound)?
// .try_into_local()
// .ok_or(RuntimeError::FunctionNotFound)?;

// let func_ty = self.modules[module_idx]
// .fn_types
// .get(func_inst.ty_idx)
// .unwrap_validated();

// Check correct function parameters and return types
if func_ty.params.valtypes != Param::TYS {
Expand All @@ -204,15 +224,15 @@ where
let mut stack = Stack::new();
let locals = Locals::new(
params.into_values().into_iter(),
func_inst.locals.iter().cloned(),
func_inst.try_into_local().unwrap().locals.iter().cloned(),
);

// setting `usize::MAX` as return address for the outermost function ensures that we
// observably fail upon errornoeusly continuing execution after that function returns.
stack.push_stackframe(
module_idx,
func_idx,
func_ty,
&func_ty,
locals,
usize::MAX,
usize::MAX,
Expand All @@ -226,6 +246,7 @@ where
self.lut.as_ref().ok_or(RuntimeError::UnmetImport)?,
&mut stack,
EmptyHookSet,
store,
)?;

// Pop return values from stack
Expand All @@ -247,22 +268,24 @@ where
function_ref: &FunctionRef,
params: Vec<Value>,
ret_types: &[ValType],
store: &mut Store,
) -> Result<Vec<Value>, RuntimeError> {
// First, verify that the function reference is valid
let (module_idx, func_idx) = self.verify_function_ref(function_ref)?;

// -=-= Verification =-=-
let func_inst = self.modules[module_idx]
.store
.funcs
let func_inst_idx = self.modules[module_idx]
.functions
.get(func_idx)
.ok_or(RuntimeError::FunctionNotFound)?
.try_into_local()
.clone();

let func_inst = store
.functions
.get(func_inst_idx)
.ok_or(RuntimeError::FunctionNotFound)?;
let func_ty = self.modules[module_idx]
.fn_types
.get(func_inst.ty)
.unwrap_validated();

let func_ty = func_inst.ty();

// Verify that the given parameters match the function parameters
let param_types = params.iter().map(|v| v.to_ty()).collect::<Vec<_>>();
Expand All @@ -278,8 +301,11 @@ where

// Prepare a new stack with the locals for the entry function
let mut stack = Stack::new();
let locals = Locals::new(params.into_iter(), func_inst.locals.iter().cloned());
stack.push_stackframe(module_idx, func_idx, func_ty, locals, 0, 0);
let locals = Locals::new(
params.into_iter(),
func_inst.try_into_local().unwrap().locals.iter().cloned(),
);
stack.push_stackframe(module_idx, func_idx, &func_ty, locals, 0, 0);

let mut currrent_module_idx = module_idx;
// Run the interpreter
Expand All @@ -289,19 +315,21 @@ where
self.lut.as_ref().ok_or(RuntimeError::UnmetImport)?,
&mut stack,
EmptyHookSet,
&mut store,
)?;

let func_inst = self.modules[module_idx]
.store
.funcs
let func_inst_idx = self.modules[module_idx]
.functions
.get(func_idx)
.ok_or(RuntimeError::FunctionNotFound)?
.try_into_local()
.clone();

let func_inst = store
.functions
.get(func_inst_idx)
.ok_or(RuntimeError::FunctionNotFound)?;
let func_ty = self.modules[module_idx]
.fn_types
.get(func_inst.ty)
.unwrap_validated();

let func_ty = func_inst.ty();

// Pop return values from stack
let return_values = func_ty
Expand Down Expand Up @@ -334,22 +362,24 @@ where
&mut self,
function_ref: &FunctionRef,
params: Vec<Value>,
store: &mut Store,
) -> Result<Vec<Value>, RuntimeError> {
// First, verify that the function reference is valid
let (module_idx, func_idx) = self.verify_function_ref(function_ref)?;

// -=-= Verification =-=-
let func_inst = self.modules[module_idx]
.store
.funcs
let func_inst_idx = self.modules[module_idx]
.functions
.get(func_idx)
.ok_or(RuntimeError::FunctionNotFound)?
.try_into_local()
.clone();

let func_inst = store
.functions
.get(func_inst_idx)
.ok_or(RuntimeError::FunctionNotFound)?;
let func_ty = self.modules[module_idx]
.fn_types
.get(func_inst.ty)
.unwrap_validated();

let func_ty = func_inst.ty();

// Verify that the given parameters match the function parameters
let param_types = params.iter().map(|v| v.to_ty()).collect::<Vec<_>>();
Expand All @@ -371,19 +401,21 @@ where
self.lut.as_ref().ok_or(RuntimeError::UnmetImport)?,
&mut stack,
EmptyHookSet,
&mut store,
)?;

let func_inst = self.modules[module_idx]
.store
.funcs
let func_inst_idx = self.modules[module_idx]
.functions
.get(func_idx)
.ok_or(RuntimeError::FunctionNotFound)?
.try_into_local()
.clone();

let func_inst = store
.functions
.get(func_inst_idx)
.ok_or(RuntimeError::FunctionNotFound)?;
let func_ty = self.modules[module_idx]
.fn_types
.get(func_inst.ty)
.unwrap_validated();

let func_ty = func_inst.ty();

// Pop return values from stack
let return_values = func_ty
Expand Down
Loading

0 comments on commit f3a614e

Please sign in to comment.