diff options
| -rw-r--r-- | crates/tinywasm/benches/tinywasm.rs | 2 | ||||
| -rw-r--r-- | crates/tinywasm/src/func.rs | 13 | ||||
| -rw-r--r-- | crates/tinywasm/src/interpreter/executor.rs | 31 | ||||
| -rw-r--r-- | crates/tinywasm/src/interpreter/simd/mod.rs | 7 | ||||
| -rw-r--r-- | crates/tinywasm/src/interpreter/stack/value_stack.rs | 21 | ||||
| -rw-r--r-- | crates/tinywasm/src/interpreter/values.rs | 52 | ||||
| -rw-r--r-- | crates/tinywasm/src/reference.rs | 35 | ||||
| -rw-r--r-- | crates/tinywasm/src/store/memory/mod.rs | 40 | ||||
| -rw-r--r-- | crates/tinywasm/src/store/memory/vec.rs | 4 | ||||
| -rw-r--r-- | crates/tinywasm/src/store/mod.rs | 13 | ||||
| -rw-r--r-- | crates/tinywasm/src/store/state.rs | 61 | ||||
| -rw-r--r-- | crates/tinywasm/src/store/types.rs | 9 | ||||
| -rw-r--r-- | crates/types/src/types.rs | 6 |
13 files changed, 160 insertions, 134 deletions
diff --git a/crates/tinywasm/benches/tinywasm.rs b/crates/tinywasm/benches/tinywasm.rs index 9a4de80..67db886 100644 --- a/crates/tinywasm/benches/tinywasm.rs +++ b/crates/tinywasm/benches/tinywasm.rs @@ -23,7 +23,7 @@ fn tinywasm_from_twasm(twasm: &[u8]) -> Result<Module> { } fn tinywasm_run(module: &Module) -> Result<()> { - let engine = Engine::new(Config::default().with_memory_backend(MemoryBackend::paged(64 * 1024))); + let engine = Engine::new(Config::default()); let mut store = Store::new(engine); let mut imports = Imports::default(); imports.define("env", "printi32", HostFunction::from(&mut store, |_: FuncContext<'_>, _: i32| Ok(()))); diff --git a/crates/tinywasm/src/func.rs b/crates/tinywasm/src/func.rs index 732cafc..c4409cd 100644 --- a/crates/tinywasm/src/func.rs +++ b/crates/tinywasm/src/func.rs @@ -278,6 +278,7 @@ impl FuncContext<'_> { self.store .get_module_instance(self.module_id) .unwrap_or_else(|| unreachable!("invalid module instance id in host function context: {}", self.module_id)) + .clone() } /// Get a memory export. @@ -566,13 +567,11 @@ fn collect_call_results( func_ty: &FuncType, ) -> Result<Vec<WasmValue>> { debug_assert!(value_stack.len() >= func_ty.results().len()); // m values are on the top of the stack (Ensured by validation) - let mut res: Vec<_> = func_ty - .results() - .iter() - .rev() - .map(|&ty| state.attach_value(value_stack.pop_tinyvalue(ty), ty).expect("validated function result")) - .collect(); - res.reverse(); // reverse to get the original order + let results = func_ty.results(); + let mut res = vec![WasmValue::I32(0); results.len()]; + for (i, &ty) in results.iter().enumerate().rev() { + res[i] = value_stack.pop_wasmvalue(state, ty); + } Ok(res) } diff --git a/crates/tinywasm/src/interpreter/executor.rs b/crates/tinywasm/src/interpreter/executor.rs index f864972..1c80dac 100644 --- a/crates/tinywasm/src/interpreter/executor.rs +++ b/crates/tinywasm/src/interpreter/executor.rs @@ -46,7 +46,10 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> { pub(crate) fn new(store: &'store mut Store, cf: CallFrame, call_stack_base: u32) -> Self { let wasm_func = store.state.get_wasm_func(cf.func_addr); - let module = store.get_module_instance_internal(wasm_func.owner); + let module = store + .get_module_instance(wasm_func.owner) + .unwrap_or_else(|| unreachable!("invalid module instance")) + .clone(); Self { module, cf, func: wasm_func.func.clone(), store, call_stack_base } } @@ -1004,7 +1007,11 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> { self.store.call_stack.push(self.cf)?; self.cf = CallFrame::new(func_addr, locals_base, wasm_func.func.locals); if wasm_func.owner != self.module.id() { - self.module = self.store.get_module_instance_internal(wasm_func.owner); + self.module = self + .store + .get_module_instance(wasm_func.owner) + .unwrap_or_else(|| unreachable!("invalid module instance")) + .clone(); } Ok(()) @@ -1023,7 +1030,11 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> { }; self.cf = CallFrame::new(func_addr, locals_base, wasm_func.func.locals); if wasm_func.owner != self.module.id() { - self.module = self.store.get_module_instance_internal(wasm_func.owner); + self.module = self + .store + .get_module_instance(wasm_func.owner) + .unwrap_or_else(|| unreachable!("invalid module instance")) + .clone(); } Ok(()) @@ -1039,8 +1050,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> { let mut params = Vec::new(); params.try_reserve_exact(param_types.len()).map_err(|_| Trap::OutOfMemory)?; for &ty in param_types.iter().rev() { - let value = self.store.value_stack.pop_tinyvalue(ty); - params.push(self.store.state.attach_value(value, ty).expect("validated host argument")); + params.push(self.store.value_stack.pop_wasmvalue(&self.store.state, ty)); } params.reverse(); if params_may_gc { @@ -1094,13 +1104,12 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> { fn exec_call_self(&mut self) -> Result<(), Trap> { self.charge_call_fuel(FUEL_COST_CALL_TOTAL); - - self.store.call_stack.push(self.cf)?; let Ok(locals_base) = self.store.value_stack.enter_locals(&self.func.params, &self.func.locals) else { cold_path(); return Err(Trap::CallStackOverflow); }; - self.cf = CallFrame::new(self.cf.func_addr, locals_base, self.func.locals); + let new = CallFrame::new(self.cf.func_addr, locals_base, self.func.locals); + self.store.call_stack.push(core::mem::replace(&mut self.cf, new))?; Ok(()) } @@ -1195,7 +1204,11 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> { let wasm_func = self.store.state.get_wasm_func(caller.func_addr); self.func = wasm_func.func.clone(); if wasm_func.owner != self.module.id() { - self.module = self.store.get_module_instance_internal(wasm_func.owner); + self.module = self + .store + .get_module_instance(wasm_func.owner) + .unwrap_or_else(|| unreachable!("invalid module instance")) + .clone(); } self.cf = caller; false diff --git a/crates/tinywasm/src/interpreter/simd/mod.rs b/crates/tinywasm/src/interpreter/simd/mod.rs index b05769c..d54268f 100644 --- a/crates/tinywasm/src/interpreter/simd/mod.rs +++ b/crates/tinywasm/src/interpreter/simd/mod.rs @@ -13,8 +13,9 @@ use core::arch::wasm32 as wasm; use core::arch::wasm64 as wasm; #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +#[repr(transparent)] /// A 128-bit SIMD value -pub struct Value128(pub(super) [u8; 16]); +pub struct Value128(pub(crate) [u8; 16]); impl From<[u8; 16]> for Value128 { fn from(bytes: [u8; 16]) -> Self { @@ -24,12 +25,12 @@ impl From<[u8; 16]> for Value128 { impl Value128 { #[inline(always)] - pub fn from_le_bytes(bytes: [u8; 16]) -> Self { + pub const fn from_le_bytes(bytes: [u8; 16]) -> Self { Self(bytes) } #[inline(always)] - pub fn to_le_bytes(self) -> [u8; 16] { + pub const fn to_le_bytes(self) -> [u8; 16] { self.0 } } diff --git a/crates/tinywasm/src/interpreter/stack/value_stack.rs b/crates/tinywasm/src/interpreter/stack/value_stack.rs index 3b5dfe2..852618d 100644 --- a/crates/tinywasm/src/interpreter/stack/value_stack.rs +++ b/crates/tinywasm/src/interpreter/stack/value_stack.rs @@ -244,20 +244,21 @@ impl ValueStack { pub(crate) fn push_dyn(&mut self, value: TinyWasmValue) -> Result<(), Trap> { match value { - TinyWasmValue::Value32(v) => self.stack_32.push(v)?, - TinyWasmValue::Value64(v) => self.stack_64.push(v)?, - TinyWasmValue::Value128(v) => self.stack_128.push(v)?, - TinyWasmValue::ValueRef(v) => self.stack_32.push(v.raw())?, + TinyWasmValue::Value32(v) => self.stack_32.push(v), + TinyWasmValue::Value64(v) => self.stack_64.push(v), + TinyWasmValue::Value128(v) => self.stack_128.push(v), + TinyWasmValue::ValueRef(v) => self.stack_32.push(v.raw()), } - Ok(()) } - pub(crate) fn pop_tinyvalue(&mut self, val_type: WasmType) -> TinyWasmValue { + pub(crate) fn pop_wasmvalue(&mut self, state: &crate::store::State, val_type: WasmType) -> WasmValue { match val_type { - WasmType::I32 | WasmType::F32 => TinyWasmValue::Value32(Value32::stack_pop(self)), - WasmType::I64 | WasmType::F64 => TinyWasmValue::Value64(Value64::stack_pop(self)), - WasmType::Ref(_) => TinyWasmValue::ValueRef(ValueRef::stack_pop(self)), - WasmType::V128 => TinyWasmValue::Value128(Value128::stack_pop(self)), + WasmType::I32 => WasmValue::I32(self.stack_32.pop() as i32), + WasmType::I64 => WasmValue::I64(self.stack_64.pop() as i64), + WasmType::F32 => WasmValue::F32(f32::from_bits(self.stack_32.pop())), + WasmType::F64 => WasmValue::F64(f64::from_bits(self.stack_64.pop())), + WasmType::Ref(ty) => WasmValue::Ref(state.to_ref_value(ValueRef::from_raw(self.stack_32.pop()), ty)), + WasmType::V128 => WasmValue::V128(self.stack_128.pop().0), } } diff --git a/crates/tinywasm/src/interpreter/values.rs b/crates/tinywasm/src/interpreter/values.rs index fae7876..6913605 100644 --- a/crates/tinywasm/src/interpreter/values.rs +++ b/crates/tinywasm/src/interpreter/values.rs @@ -1,12 +1,16 @@ use super::stack::{CallFrame, ValueStack}; use crate::store::Globals; use crate::{Result, interpreter::simd::Value128}; -use tinywasm_types::{AnyRef, ExnRef, ExternRef, FuncRef, GlobalAddr, LocalAddr, RefValue, WasmType, WasmValue}; +use tinywasm_types::{GlobalAddr, LocalAddr, RefValue, WasmValue}; pub(crate) type Value32 = u32; pub(crate) type Value64 = u64; #[derive(Debug, Clone, Copy, PartialEq, Eq)] +/// Packed internal representation of a WebAssembly reference. +/// +/// Unlike the public [`RefValue`], this stores no explicit reference category. +/// Converting it back therefore requires the value's canonical reference type. pub(crate) struct ValueRef(u32); impl Default for ValueRef { @@ -71,6 +75,18 @@ impl ValueRef { } } +impl From<RefValue> for ValueRef { + fn from(value: RefValue) -> Self { + match value { + RefValue::Null => Self::NULL, + RefValue::Func(value) => Self::from_category_addr(value.addr()), + RefValue::Extern(value) => Self::from_raw(value.raw()), + RefValue::Exn(value) => Self::from_category_addr(value.addr()), + RefValue::Any(value) => Self::from_raw(value.raw()), + } + } +} + #[allow(private_interfaces)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] /// A untyped WebAssembly value @@ -118,32 +134,6 @@ impl TinyWasmValue { _ => None, } } - - /// Converts this internal value to the requested public WebAssembly type. - pub fn attach_type(self, ty: WasmType) -> Option<WasmValue> { - match (self, ty) { - (Self::Value32(v), WasmType::I32) => Some(WasmValue::I32(v as i32)), - (Self::Value64(v), WasmType::I64) => Some(WasmValue::I64(v as i64)), - (Self::Value32(v), WasmType::F32) => Some(WasmValue::F32(f32::from_bits(v))), - (Self::Value64(v), WasmType::F64) => Some(WasmValue::F64(f64::from_bits(v))), - (Self::ValueRef(v), WasmType::Ref(_)) if v.is_null() => Some(RefValue::Null.into()), - (Self::ValueRef(v), WasmType::Ref(ty)) if ty.is_func() => { - Some(WasmValue::Ref(RefValue::Func(FuncRef::new(v.addr()?)))) - } - (Self::ValueRef(v), WasmType::Ref(ty)) if ty.is_extern() => { - Some(WasmValue::Ref(RefValue::Extern(ExternRef::from_raw(v.raw())))) - } - (Self::ValueRef(v), WasmType::Ref(ty)) if ty.is_exn() => { - Some(WasmValue::Ref(RefValue::Exn(ExnRef::new(v.addr()?)))) - } - (Self::ValueRef(v), WasmType::Ref(_)) => Some(WasmValue::Ref(RefValue::Any(AnyRef::from_raw(v.raw())))), - (Self::Value128(v), WasmType::V128) => Some(WasmValue::V128(v.0)), - (_, WasmType::I32 | WasmType::F32) => None, - (_, WasmType::I64 | WasmType::F64) => None, - (_, WasmType::Ref(_)) => None, - (_, WasmType::V128) => None, - } - } } impl From<&WasmValue> for TinyWasmValue { @@ -153,11 +143,7 @@ impl From<&WasmValue> for TinyWasmValue { WasmValue::I64(v) => Self::Value64(*v as u64), WasmValue::F32(v) => Self::Value32(v.to_bits()), WasmValue::F64(v) => Self::Value64(v.to_bits()), - WasmValue::Ref(RefValue::Null) => Self::ValueRef(ValueRef::NULL), - WasmValue::Ref(RefValue::Func(value)) => Self::ValueRef(ValueRef::from_category_addr(value.addr())), - WasmValue::Ref(RefValue::Extern(value)) => Self::ValueRef(ValueRef::from_raw(value.raw())), - WasmValue::Ref(RefValue::Exn(value)) => Self::ValueRef(ValueRef::from_category_addr(value.addr())), - WasmValue::Ref(RefValue::Any(value)) => Self::ValueRef(ValueRef::from_raw(value.raw())), + WasmValue::Ref(value) => Self::ValueRef((*value).into()), WasmValue::V128(v) => Self::Value128((*v).into()), } } @@ -190,9 +176,7 @@ pub(crate) trait InternalValue: sealed::Sealed + Copy + Default { fn local_set(stack: &mut ValueStack, frame: &CallFrame, index: LocalAddr, value: Self); fn local_update(stack: &mut ValueStack, frame: &CallFrame, index: LocalAddr, f: impl FnOnce(Self) -> Self); fn local_copy(stack: &mut ValueStack, frame: &CallFrame, from: LocalAddr, to: LocalAddr); - /// Gets a value from its physical global lane. fn global_get(globals: &Globals, addr: GlobalAddr) -> Self; - /// Sets a value in its physical global lane. fn global_set(globals: &mut Globals, addr: GlobalAddr, value: Self); } diff --git a/crates/tinywasm/src/reference.rs b/crates/tinywasm/src/reference.rs index ad9cfd9..00f966e 100644 --- a/crates/tinywasm/src/reference.rs +++ b/crates/tinywasm/src/reference.rs @@ -5,7 +5,7 @@ use alloc::format; use alloc::string::{String, ToString}; use alloc::vec::Vec; -use crate::interpreter::TinyWasmValue; +use crate::interpreter::ValueRef; use crate::store::TableInstance; use crate::{Error, MemoryInstance, Result, Store, Trap}; use tinywasm_types::{Addr, GlobalType, MemAddr, MemoryType, TableAddr, TableType, WasmType, WasmValue}; @@ -347,13 +347,14 @@ fn table_value_to_element( state: &crate::store::State, element_type: tinywasm_types::RefType, value: WasmValue, -) -> Result<crate::interpreter::ValueRef, Trap> { - if !state.value_matches_type(value, WasmType::Ref(element_type)) { +) -> Result<ValueRef, Trap> { + let WasmValue::Ref(value) = value else { + return Err(Trap::Other("invalid table value type")); + }; + if !state.value_matches_type(WasmValue::Ref(value), WasmType::Ref(element_type)) { return Err(Trap::Other("invalid table value type")); } - let WasmValue::Ref(value) = value else { unreachable!() }; - let TinyWasmValue::ValueRef(value) = TinyWasmValue::from(WasmValue::Ref(value)) else { unreachable!() }; - Ok(value) + Ok(value.into()) } impl Table { @@ -393,14 +394,9 @@ impl Table { /// Get a table element as a wasm reference value. pub fn get(&self, store: &Store, index: TableAddr) -> Result<WasmValue> { let table = self.instance(store)?; - let value = store - .state - .attach_value(TinyWasmValue::ValueRef(*table.get(index as usize)?), WasmType::Ref(table.kind.element_type)) - .expect("table value matches its element type"); - if let WasmValue::Ref(value) = value { - store.state.pin_host_ref(value); - } - Ok(value) + let value = store.state.to_ref_value(*table.get(index as usize)?, table.kind.element_type); + store.state.pin_host_ref(value); + Ok(WasmValue::Ref(value)) } /// Load a range of table elements and iterate over wasm reference values. @@ -413,15 +409,12 @@ impl Table { let table = self.instance(store)?; let element_type = table.kind.element_type; let elements = table.load(offset, len)?; - let may_contain_gc = store.state.type_may_contain_gc(WasmType::Ref(element_type)); + let may_contain_gc = store.state.type_may_contain_gc(&WasmType::Ref(element_type)); Ok(elements.iter().copied().map(move |value| { if may_contain_gc { store.state.gc.pin(value); } - store - .state - .attach_value(TinyWasmValue::ValueRef(value), WasmType::Ref(element_type)) - .expect("table value matches its element type") + WasmValue::Ref(store.state.to_ref_value(value, element_type)) })) } @@ -473,7 +466,7 @@ impl Global { /// Get the current value of the global. pub fn get(&self, store: &Store) -> Result<WasmValue> { self.0.validate_store(store)?; - let value = store.state.global_wasm_value(self.0.addr); + let value = store.state.get_global_wasmvalue(self.0.addr); if let WasmValue::Ref(value) = value { store.state.pin_host_ref(value); } @@ -483,6 +476,6 @@ impl Global { /// Set the current value of the global. pub fn set(&self, store: &mut Store, value: WasmValue) -> Result<()> { self.0.validate_store(store)?; - store.state.set_global_wasm_value(self.0.addr, value) + store.state.set_global_wasmvalue(self.0.addr, value) } } diff --git a/crates/tinywasm/src/store/memory/mod.rs b/crates/tinywasm/src/store/memory/mod.rs index a3f0053..5805c86 100644 --- a/crates/tinywasm/src/store/memory/mod.rs +++ b/crates/tinywasm/src/store/memory/mod.rs @@ -377,13 +377,13 @@ macro_rules! impl_mem_traits { #[inline(always)] fn load_at(mem: &dyn LinearMemory, addr: usize) -> core::result::Result<Self, crate::Trap> { - Ok(Self::from_le_bytes(match mem.$read(addr) { - Ok(bytes) => bytes, + match mem.$read(addr) { + Ok(bytes) => Ok(Self::from_le_bytes(bytes)), Err(trap) => { cold_path(); - return Err(trap); + Err(trap) } - })) + } } #[inline(always)] @@ -402,10 +402,38 @@ macro_rules! impl_mem_traits { impl_mem_traits!( u8, 1, read_8, write_8, i8, 1, read_8, write_8, u16, 2, read_16, write_16, i16, 2, read_16, write_16, u32, 4, read_32, write_32, i32, 4, read_32, write_32, f32, 4, read_32, write_32, u64, 8, read_64, write_64, i64, 8, - read_64, write_64, f64, 8, read_64, write_64, Value128, 16, read_128, write_128 + read_64, write_64, f64, 8, read_64, write_64 ); -fn memory_oob(offset: usize, len: usize, max: usize) -> crate::Trap { +impl MemValue<16> for Value128 { + #[inline(always)] + fn to_mem_bytes(self) -> [u8; 16] { + self.0 + } + + #[inline(always)] + fn from_mem_bytes(bytes: [u8; 16]) -> Self { + Self(bytes) + } + + #[inline(always)] + fn load_at(mem: &dyn LinearMemory, addr: usize) -> core::result::Result<Self, crate::Trap> { + match mem.read_128(addr) { + Ok(bytes) => Ok(Self(bytes)), + Err(trap) => { + cold_path(); + Err(trap) + } + } + } + + #[inline(always)] + fn store_at(self, mem: &mut dyn LinearMemory, addr: usize) -> core::result::Result<(), crate::Trap> { + mem.write_128(addr, &self.0) + } +} + +const fn memory_oob(offset: usize, len: usize, max: usize) -> crate::Trap { crate::Trap::MemoryOutOfBounds { offset, len, max } } diff --git a/crates/tinywasm/src/store/memory/vec.rs b/crates/tinywasm/src/store/memory/vec.rs index 2737159..aa468ec 100644 --- a/crates/tinywasm/src/store/memory/vec.rs +++ b/crates/tinywasm/src/store/memory/vec.rs @@ -35,7 +35,9 @@ impl VecMemory { #[inline(always)] fn read_fixed<const N: usize>(&self, addr: usize) -> Result<[u8; N], crate::Trap> { self.check_fixed_addr::<N>(addr)?; - Ok(self.data[addr..addr + N].try_into().unwrap_or_else(|_| unreachable!("slice length should be {N}"))) + let mut bytes = [0u8; N]; + bytes.copy_from_slice(&self.data[addr..addr + N]); + Ok(bytes) } #[inline(always)] diff --git a/crates/tinywasm/src/store/mod.rs b/crates/tinywasm/src/store/mod.rs index 3c80e91..9d5622a 100644 --- a/crates/tinywasm/src/store/mod.rs +++ b/crates/tinywasm/src/store/mod.rs @@ -87,13 +87,8 @@ impl Store { } /// Get a module instance by the internal id - pub fn get_module_instance(&self, id: ModuleInstanceId) -> Option<ModuleInstance> { - self.module_instances.get(id as usize).cloned() - } - - #[inline] - pub(crate) fn get_module_instance_internal(&self, id: ModuleInstanceId) -> ModuleInstance { - self.module_instances.get(id as usize).unwrap_or_else(|| unreachable!("invalid module instance: {id}")).clone() + pub fn get_module_instance(&self, id: ModuleInstanceId) -> Option<&ModuleInstance> { + self.module_instances.get(id as usize) } pub(crate) fn enter_execution(&mut self) -> Result<()> { @@ -168,11 +163,11 @@ impl Store { ) -> impl ExactSizeIterator<Item = FuncAddr> { let start = self.state.funcs.len() as FuncAddr; debug_assert_eq!(funcs.len(), type_addrs.len()); - for (func, &type_addr) in funcs.iter().zip(type_addrs) { + for (func, &type_addr) in funcs.iter().cloned().zip(type_addrs) { self.state.funcs.push(FunctionInstance { type_addr, gc: self.state.func_gc_metadata(type_addr), - kind: FunctionKind::Wasm(WasmFunctionInstance { func: func.clone(), owner }), + kind: FunctionKind::Wasm(WasmFunctionInstance { func, owner }), }); } start..start + funcs.len() as FuncAddr diff --git a/crates/tinywasm/src/store/state.rs b/crates/tinywasm/src/store/state.rs index 392035f..5cb561a 100644 --- a/crates/tinywasm/src/store/state.rs +++ b/crates/tinywasm/src/store/state.rs @@ -22,11 +22,11 @@ pub(crate) struct State { impl State { /// Returns whether values of this type can contain a managed GC object. - pub(crate) fn type_may_contain_gc(&self, ty: WasmType) -> bool { + pub(crate) fn type_may_contain_gc(&self, ty: &WasmType) -> bool { Self::type_may_contain_gc_in(&self.canonical_types, ty) } - fn type_may_contain_gc_in(types: &[SubType], ty: WasmType) -> bool { + fn type_may_contain_gc_in(types: &[SubType], ty: &WasmType) -> bool { let WasmType::Ref(ty) = ty else { return false }; if let Some(type_addr) = ty.type_index() { return matches!(types[type_addr as usize].composite, CompositeType::Struct(_) | CompositeType::Array(_)); @@ -47,8 +47,8 @@ impl State { pub(crate) fn func_gc_metadata(&self, type_addr: TypeAddr) -> FunctionGcMetadata { let ty = self.get_canonical_func_type(type_addr); FunctionGcMetadata { - params: ty.params().iter().copied().any(|ty| self.type_may_contain_gc(ty)), - results: ty.results().iter().copied().any(|ty| self.type_may_contain_gc(ty)), + params: ty.params().iter().any(|ty| self.type_may_contain_gc(ty)), + results: ty.results().iter().any(|ty| self.type_may_contain_gc(ty)), } } @@ -74,21 +74,21 @@ impl State { .chain( self.globals .globals_32() - .filter(|(_, ty)| Self::type_may_contain_gc_in(canonical_types, ty.ty)) + .filter(|(_, ty)| Self::type_may_contain_gc_in(canonical_types, &ty.ty)) .map(|(value, _)| ValueRef::from_raw(value)), ) .chain( self.tables .iter() .filter(|table| { - Self::type_may_contain_gc_in(canonical_types, WasmType::Ref(table.kind.element_type)) + Self::type_may_contain_gc_in(canonical_types, &WasmType::Ref(table.kind.element_type)) }) .flat_map(|table| table.elements.iter().copied()), ) .chain( self.elements .iter() - .filter(|element| Self::type_may_contain_gc_in(canonical_types, WasmType::Ref(element.ty))) + .filter(|element| Self::type_may_contain_gc_in(canonical_types, &WasmType::Ref(element.ty))) .flat_map(|element| element.items.iter().flatten().copied()), ) .chain(values.iter().filter_map(|value| value.as_ref())); @@ -132,22 +132,30 @@ impl State { Ok(object) } - /// Converts an internal value using canonical reference type information. - pub(crate) fn attach_value(&self, value: TinyWasmValue, ty: WasmType) -> Option<WasmValue> { - if let (TinyWasmValue::ValueRef(value), WasmType::Ref(reference_type)) = (value, ty) - && let Some(type_addr) = reference_type.type_index() - { - if value.is_null() { - return Some(RefValue::Null.into()); - } + /// Converts an internal reference using canonical heap type information. + pub(crate) fn to_ref_value(&self, value: ValueRef, ty: RefType) -> RefValue { + if value.is_null() { + return RefValue::Null; + } + + if let Some(type_addr) = ty.type_index() { return match &self.get_type(type_addr).composite { - CompositeType::Func(_) => Some(RefValue::Func(FuncRef::new(value.addr()?)).into()), - CompositeType::Struct(_) | CompositeType::Array(_) => { - Some(RefValue::Any(AnyRef::from_raw(value.raw())).into()) + CompositeType::Func(_) => { + RefValue::Func(FuncRef::new(value.addr().expect("non-null reference has an address"))) } + CompositeType::Struct(_) | CompositeType::Array(_) => RefValue::Any(AnyRef::from_raw(value.raw())), }; } - value.attach_type(ty) + if ty.is_func() { + return RefValue::Func(FuncRef::new(value.addr().expect("non-null reference has an address"))); + } + if ty.is_extern() { + return RefValue::Extern(ExternRef::from_raw(value.raw())); + } + if ty.is_exn() { + return RefValue::Exn(ExnRef::new(value.addr().expect("non-null reference has an address"))); + } + RefValue::Any(AnyRef::from_raw(value.raw())) } /// Returns whether one canonical type is a subtype of another. @@ -364,22 +372,20 @@ impl State { } /// Converts a global directly to its public value representation. - pub(crate) fn global_wasm_value(&self, addr: GlobalAddr) -> WasmValue { + pub(crate) fn get_global_wasmvalue(&self, addr: GlobalAddr) -> WasmValue { let ty = self.globals.ty(addr).ty; match ty { WasmType::I32 => WasmValue::I32(self.globals.get_32(addr) as i32), WasmType::I64 => WasmValue::I64(self.globals.get_64(addr) as i64), WasmType::F32 => WasmValue::F32(f32::from_bits(self.globals.get_32(addr))), WasmType::F64 => WasmValue::F64(f64::from_bits(self.globals.get_64(addr))), - WasmType::Ref(_) => self - .attach_value(TinyWasmValue::ValueRef(ValueRef::from_raw(self.globals.get_32(addr))), ty) - .unwrap_or_else(|| unreachable!("global value does not match its type")), + WasmType::Ref(ty) => WasmValue::Ref(self.to_ref_value(ValueRef::from_raw(self.globals.get_32(addr)), ty)), WasmType::V128 => WasmValue::V128(self.globals.get_128(addr).to_le_bytes()), } } /// Validates and sets a global from its public value representation. - pub(crate) fn set_global_wasm_value(&mut self, addr: GlobalAddr, value: WasmValue) -> Result<()> { + pub(crate) fn set_global_wasmvalue(&mut self, addr: GlobalAddr, value: WasmValue) -> Result<()> { let ty = self.globals.ty(addr); if !ty.mutable { cold_path(); @@ -394,12 +400,7 @@ impl State { WasmValue::I64(value) => self.globals.set_64(addr, value as u64), WasmValue::F32(value) => self.globals.set_32(addr, value.to_bits()), WasmValue::F64(value) => self.globals.set_64(addr, value.to_bits()), - WasmValue::Ref(value) => { - let TinyWasmValue::ValueRef(value) = TinyWasmValue::from(WasmValue::Ref(value)) else { - unreachable!("reference value did not convert to an internal reference") - }; - self.globals.set_32(addr, value.raw()); - } + WasmValue::Ref(value) => self.globals.set_32(addr, ValueRef::from(value).raw()), WasmValue::V128(value) => self.globals.set_128(addr, value.into()), } Ok(()) diff --git a/crates/tinywasm/src/store/types.rs b/crates/tinywasm/src/store/types.rs index 7cee56b..4954bba 100644 --- a/crates/tinywasm/src/store/types.rs +++ b/crates/tinywasm/src/store/types.rs @@ -39,6 +39,15 @@ fn map_field_type(field: FieldType, resolve: &mut impl FnMut(TypeAddr) -> TypeAd fn map_subtype(ty: &SubType, mut resolve: impl FnMut(TypeAddr) -> TypeAddr) -> SubType { let supertype = ty.supertype.map(&mut resolve); let composite = match &ty.composite { + CompositeType::Func(ty) + if !ty + .params() + .iter() + .chain(ty.results()) + .any(|ty| matches!(ty, WasmType::Ref(ty) if ty.is_concrete())) => + { + CompositeType::Func(ty.clone()) + } CompositeType::Func(ty) => { let params = ty.params().iter().copied().map(|ty| map_value_type(ty, &mut resolve)).collect::<Vec<_>>(); let results = ty.results().iter().copied().map(|ty| map_value_type(ty, &mut resolve)).collect::<Vec<_>>(); diff --git a/crates/types/src/types.rs b/crates/types/src/types.rs index 214ada6..0801849 100644 --- a/crates/types/src/types.rs +++ b/crates/types/src/types.rs @@ -1,4 +1,4 @@ -use alloc::boxed::Box; +use alloc::{boxed::Box, sync::Arc}; use crate::{TypeAddr, WasmType}; @@ -100,7 +100,7 @@ impl CompositeType { #[cfg_attr(feature = "debug", derive(Debug))] #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] pub struct FuncType { - data: Box<[WasmType]>, + data: Arc<[WasmType]>, param_count: u16, } @@ -108,7 +108,7 @@ impl FuncType { /// Create a new function type. pub fn new(params: &[WasmType], results: &[WasmType]) -> Self { let data: Box<[WasmType]> = params.iter().cloned().chain(results.iter().cloned()).collect(); - Self { data, param_count: params.len() as u16 } + Self { data: data.into(), param_count: params.len() as u16 } } /// Get the parameter types of this function type. |
