Skip to content

Commit

Permalink
feat: preserve null refs when returning
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
  • Loading branch information
explodingcamera committed Jan 23, 2024
1 parent 52eda3e commit ec26f4b
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 52 deletions.
4 changes: 2 additions & 2 deletions crates/parser/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> ValType {
F32 => ValType::F32,
F64 => ValType::F64,
V128 => unimplemented!("128-bit values are not supported yet"),
FuncRef => ValType::FuncRef,
ExternRef => ValType::ExternRef,
FuncRef => ValType::RefFunc,
ExternRef => ValType::RefExtern,
}
}

Expand Down
10 changes: 7 additions & 3 deletions crates/tinywasm/src/runtime/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ fn exec_one(
let call_ty = module.func_ty(*type_addr);

let func_idx = stack.values.pop_t::<u32>()?;
let actual_func_addr = table.borrow().get(func_idx as usize)?;
let actual_func_addr = table
.borrow()
.get(func_idx as usize)?
.ok_or_else(|| Trap::UninitializedElement { index: func_idx as usize })?;

let resolved_func_addr = module.resolve_func_addr(actual_func_addr);

// prepare the call frame
Expand Down Expand Up @@ -565,12 +569,12 @@ fn exec_one(
I64TruncF32U => checked_conv_float!(f32, u64, i64, stack),
I64TruncF64U => checked_conv_float!(f64, u64, i64, stack),

// TODO: uninitialized element traps
TableGet(table_index) => {
let table_idx = module.resolve_table_addr(*table_index);
let table = store.get_table(table_idx as usize)?;
let idx = stack.values.pop_t::<i32>()? as usize;
stack.values.push(table.borrow().get(idx)?.into());
let v = table.borrow().get_wasm_val(idx)?;
stack.values.push(v.into());
}

TableSet(table_index) => {
Expand Down
27 changes: 17 additions & 10 deletions crates/tinywasm/src/runtime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ impl RawWasmValue {
ValType::I64 => WasmValue::I64(self.0 as i64),
ValType::F32 => WasmValue::F32(f32::from_bits(self.0 as u32)),
ValType::F64 => WasmValue::F64(f64::from_bits(self.0)),
ValType::ExternRef => WasmValue::RefExtern(match self.0 {
0 => None,
_ => Some(self.0 as u32),
}),
ValType::FuncRef => WasmValue::RefFunc(match self.0 {
0 => None,
_ => Some(self.0 as u32),
}),
ValType::RefExtern => {
if self.0 == -1i64 as u64 {
WasmValue::RefNull(ValType::RefExtern)
} else {
WasmValue::RefExtern(self.0 as u32)
}
}
ValType::RefFunc => {
if self.0 == -1i64 as u64 {
WasmValue::RefNull(ValType::RefFunc)
} else {
WasmValue::RefFunc(self.0 as u32)
}
}
}
}
}
Expand All @@ -46,8 +52,9 @@ impl From<WasmValue> for RawWasmValue {
WasmValue::I64(i) => Self(i as u64),
WasmValue::F32(i) => Self(i.to_bits() as u64),
WasmValue::F64(i) => Self(i.to_bits()),
WasmValue::RefExtern(v) => Self(v.unwrap_or(0) as u64),
WasmValue::RefFunc(v) => Self(v.unwrap_or(0) as u64),
WasmValue::RefExtern(v) => Self(v as i64 as u64),
WasmValue::RefFunc(v) => Self(v as i64 as u64),
WasmValue::RefNull(_) => Self(-1i64 as u64),
}
}
}
Expand Down
27 changes: 18 additions & 9 deletions crates/tinywasm/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl Store {
let val = global.borrow().value;
val
}
RefNull(v) => v.default_value().into(),
RefNull(_) => RawWasmValue::from(0),
RefFunc(idx) => RawWasmValue::from(*idx as i64),
};
Ok(val)
Expand Down Expand Up @@ -414,27 +414,36 @@ impl FunctionInstance {
#[derive(Debug)]
pub(crate) struct TableInstance {
pub(crate) elements: Vec<Option<Addr>>,
pub(crate) _kind: TableType,
pub(crate) kind: TableType,
pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances
}

impl TableInstance {
pub(crate) fn new(kind: TableType, owner: ModuleInstanceAddr) -> Self {
Self { elements: vec![None; kind.size_initial as usize], _kind: kind, _owner: owner }
Self { elements: vec![None; kind.size_initial as usize], kind, _owner: owner }
}

pub(crate) fn get(&self, addr: usize) -> Result<Addr> {
self.elements
.get(addr)
.copied()
.ok_or_else(|| Error::Trap(Trap::UndefinedElement { index: addr }))
.map(|elem| elem.ok_or_else(|| Trap::UninitializedElement { index: addr }.into()))?
pub(crate) fn get_wasm_val(&self, addr: usize) -> Result<WasmValue> {
Ok(match self.kind.element_type {
ValType::RefFunc => {
self.get(addr)?.map(|v| WasmValue::RefFunc(v)).unwrap_or(WasmValue::RefNull(ValType::RefFunc))
}
ValType::RefExtern => {
self.get(addr)?.map(|v| WasmValue::RefExtern(v)).unwrap_or(WasmValue::RefNull(ValType::RefExtern))
}
_ => unimplemented!("unsupported table type: {:?}", self.kind.element_type),
})
}

pub(crate) fn get(&self, addr: usize) -> Result<Option<Addr>> {
self.elements.get(addr).copied().ok_or_else(|| Error::Trap(Trap::UndefinedElement { index: addr }))
}

pub(crate) fn set(&mut self, addr: usize, value: Addr) -> Result<()> {
if addr >= self.elements.len() {
return Err(Error::Other(format!("table element {} not found", addr)));
}

self.elements[addr] = Some(value);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tinywasm/tests/generated/mvp.csv

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions crates/tinywasm/tests/generated/progress-mvp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion crates/tinywasm/tests/testsuite/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl TestSuite {
let mut imports = Imports::new();

let table =
Extern::table(TableType::new(ValType::FuncRef, 10, Some(20)), WasmValue::default_for(ValType::FuncRef));
Extern::table(TableType::new(ValType::RefFunc, 10, Some(20)), WasmValue::default_for(ValType::RefFunc));

let print = Extern::typed_func(|_ctx: tinywasm::FuncContext, _: ()| {
log::debug!("print");
Expand Down Expand Up @@ -464,6 +464,8 @@ impl TestSuite {
));
}

log::debug!("outcomes: {:?}", outcomes);

outcomes.iter().zip(expected).enumerate().try_for_each(|(i, (outcome, exp))| {
(outcome.eq_loose(&exp))
.then_some(())
Expand Down
16 changes: 8 additions & 8 deletions crates/tinywasm/tests/testsuite/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::panic::{self, AssertUnwindSafe};

use eyre::{eyre, Result};
use tinywasm_types::{ModuleInstanceAddr, TinyWasmModule, WasmValue};
use tinywasm_types::{ModuleInstanceAddr, TinyWasmModule, ValType, WasmValue};

pub fn try_downcast_panic(panic: Box<dyn std::any::Any + Send>) -> String {
let info = panic.downcast_ref::<panic::PanicInfo>().or(None).map(|p| p.to_string()).clone();
Expand Down Expand Up @@ -77,10 +77,10 @@ fn wastarg2tinywasmvalue(arg: wast::WastArg) -> Result<tinywasm_types::WasmValue
F64(f) => WasmValue::F64(f64::from_bits(f.bits)),
I32(i) => WasmValue::I32(i),
I64(i) => WasmValue::I64(i),
RefExtern(v) => WasmValue::RefExtern(Some(v)),
RefExtern(v) => WasmValue::RefExtern(v),
RefNull(t) => match t {
wast::core::HeapType::Func => WasmValue::RefFunc(None),
wast::core::HeapType::Extern => WasmValue::RefExtern(None),
wast::core::HeapType::Func => WasmValue::RefNull(ValType::RefFunc),
wast::core::HeapType::Extern => WasmValue::RefNull(ValType::RefExtern),
_ => return Err(eyre!("unsupported arg type: refnull: {:?}", t)),
},
v => return Err(eyre!("unsupported arg type: {:?}", v)),
Expand All @@ -99,16 +99,16 @@ fn wastret2tinywasmvalue(arg: wast::WastRet) -> Result<tinywasm_types::WasmValue
I32(i) => WasmValue::I32(i),
I64(i) => WasmValue::I64(i),
RefNull(t) => match t {
Some(wast::core::HeapType::Func) => WasmValue::RefFunc(None),
Some(wast::core::HeapType::Extern) => WasmValue::RefExtern(None),
Some(wast::core::HeapType::Func) => WasmValue::RefNull(ValType::RefFunc),
Some(wast::core::HeapType::Extern) => WasmValue::RefNull(ValType::RefExtern),
_ => return Err(eyre!("unsupported arg type: refnull: {:?}", t)),
},
RefExtern(v) => match v {
Some(v) => WasmValue::RefExtern(Some(v)),
Some(v) => WasmValue::RefExtern(v),
_ => return Err(eyre!("unsupported arg type: refextern: {:?}", v)),
},
RefFunc(v) => match v {
Some(wast::token::Index::Num(n, _)) => WasmValue::RefFunc(Some(n)),
Some(wast::token::Index::Num(n, _)) => WasmValue::RefFunc(n),
_ => return Err(eyre!("unsupported arg type: reffunc: {:?}", v)),
},
a => return Err(eyre!("unsupported arg type {:?}", a)),
Expand Down
32 changes: 18 additions & 14 deletions crates/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ pub enum WasmValue {
F32(f32),
/// A 64-bit float.
F64(f64),
// Vec types
// V128(i128),
// RefHost(FuncAddr),
RefExtern(Option<ExternAddr>),
RefFunc(Option<FuncAddr>),

RefExtern(ExternAddr),
RefFunc(FuncAddr),
RefNull(ValType),
}

impl WasmValue {
Expand All @@ -97,8 +96,10 @@ impl WasmValue {
Self::I64(i) => ConstInstruction::I64Const(*i),
Self::F32(i) => ConstInstruction::F32Const(*i),
Self::F64(i) => ConstInstruction::F64Const(*i),
Self::RefExtern(None) => ConstInstruction::RefNull(ValType::ExternRef),
Self::RefFunc(None) => ConstInstruction::RefNull(ValType::FuncRef),

Self::RefFunc(i) => ConstInstruction::RefFunc(*i),
Self::RefNull(ty) => ConstInstruction::RefNull(*ty),

// Self::RefExtern(addr) => ConstInstruction::RefExtern(*addr),
_ => unimplemented!("no const_instr for {:?}", self),
}
Expand All @@ -111,15 +112,16 @@ impl WasmValue {
ValType::I64 => Self::I64(0),
ValType::F32 => Self::F32(0.0),
ValType::F64 => Self::F64(0.0),
ValType::FuncRef => Self::RefFunc(None),
ValType::ExternRef => Self::RefExtern(None),
ValType::RefFunc => Self::RefNull(ValType::RefFunc),
ValType::RefExtern => Self::RefNull(ValType::RefExtern),
}
}

pub fn eq_loose(&self, other: &Self) -> bool {
match (self, other) {
(Self::I32(a), Self::I32(b)) => a == b,
(Self::I64(a), Self::I64(b)) => a == b,
(Self::RefNull(v), Self::RefNull(v2)) => v == v2,
(Self::RefExtern(addr), Self::RefExtern(addr2)) => addr == addr2,
(Self::RefFunc(addr), Self::RefFunc(addr2)) => addr == addr2,
(Self::F32(a), Self::F32(b)) => {
Expand Down Expand Up @@ -230,6 +232,7 @@ impl Debug for WasmValue {
WasmValue::F64(i) => write!(f, "f64({})", i),
WasmValue::RefExtern(addr) => write!(f, "ref.extern({:?})", addr),
WasmValue::RefFunc(addr) => write!(f, "ref.func({:?})", addr),
WasmValue::RefNull(ty) => write!(f, "ref.null({:?})", ty),
// WasmValue::V128(i) => write!(f, "v128({})", i),
}
}
Expand All @@ -243,8 +246,9 @@ impl WasmValue {
Self::I64(_) => ValType::I64,
Self::F32(_) => ValType::F32,
Self::F64(_) => ValType::F64,
Self::RefExtern(_) => ValType::ExternRef,
Self::RefFunc(_) => ValType::FuncRef,
Self::RefExtern(_) => ValType::RefExtern,
Self::RefFunc(_) => ValType::RefFunc,
Self::RefNull(ty) => *ty,
}
}
}
Expand All @@ -261,9 +265,9 @@ pub enum ValType {
/// A 64-bit float.
F64,
/// A reference to a function.
FuncRef,
RefFunc,
/// A reference to an external value.
ExternRef,
RefExtern,
}

impl ValType {
Expand Down Expand Up @@ -392,7 +396,7 @@ pub struct TableType {

impl TableType {
pub fn empty() -> Self {
Self { element_type: ValType::FuncRef, size_initial: 0, size_max: None }
Self { element_type: ValType::RefFunc, size_initial: 0, size_max: None }
}

pub fn new(element_type: ValType, size_initial: u32, size_max: Option<u32>) -> Self {
Expand Down

0 comments on commit ec26f4b

Please sign in to comment.