diff options
Diffstat (limited to 'crates/types')
| -rw-r--r-- | crates/types/README.md | 4 | ||||
| -rw-r--r-- | crates/types/src/archive.rs | 45 | ||||
| -rw-r--r-- | crates/types/src/lib.rs | 236 | ||||
| -rw-r--r-- | crates/types/src/value.rs | 175 |
4 files changed, 214 insertions, 246 deletions
diff --git a/crates/types/README.md b/crates/types/README.md index f2d048b..5a4431e 100644 --- a/crates/types/README.md +++ b/crates/types/README.md @@ -1,3 +1,3 @@ -# `tinywasm_types` +# `tinywasm-types` -This crate contains the types used by the [`tinywasm`](https://crates.io/crates/tinywasm) crate. It is also used by the [`tinywasm_parser`](https://crates.io/crates/tinywasm_parser) crate to parse WebAssembly binaries. +This crate contains the types used by the [`tinywasm`](https://crates.io/crates/tinywasm) crate. It is also used by the [`tinywasm-parser`](https://crates.io/crates/tinywasm-parser) crate to parse WebAssembly binaries. diff --git a/crates/types/src/archive.rs b/crates/types/src/archive.rs index 00a9911..bbd2206 100644 --- a/crates/types/src/archive.rs +++ b/crates/types/src/archive.rs @@ -1,3 +1,5 @@ +use core::fmt::{Display, Formatter}; + use crate::TinyWasmModule; use rkyv::{ check_archived_root, @@ -14,30 +16,49 @@ const TWASM_MAGIC: [u8; 16] = [ TWASM_MAGIC_PREFIX[0], TWASM_MAGIC_PREFIX[1], TW pub use rkyv::AlignedVec; -fn validate_magic(wasm: &[u8]) -> Result<usize, &str> { - if wasm.len() < TWASM_MAGIC.len() { - return Err("Invalid twasm: too short"); - } - if &wasm[..TWASM_MAGIC_PREFIX.len()] != TWASM_MAGIC_PREFIX { - return Err("Invalid twasm: invalid magic number"); +fn validate_magic(wasm: &[u8]) -> Result<usize, TwasmError> { + if wasm.len() < TWASM_MAGIC.len() || &wasm[..TWASM_MAGIC_PREFIX.len()] != TWASM_MAGIC_PREFIX { + return Err(TwasmError::InvalidMagic); } if &wasm[TWASM_MAGIC_PREFIX.len()..TWASM_MAGIC_PREFIX.len() + TWASM_VERSION.len()] != TWASM_VERSION { - return Err("Invalid twasm: invalid version"); + return Err(TwasmError::InvalidVersion); } if wasm[TWASM_MAGIC_PREFIX.len() + TWASM_VERSION.len()..TWASM_MAGIC.len()] != [0; 10] { - return Err("Invalid twasm: invalid padding"); + return Err(TwasmError::InvalidPadding); } Ok(TWASM_MAGIC.len()) } +#[derive(Debug)] +pub enum TwasmError { + InvalidMagic, + InvalidVersion, + InvalidPadding, + InvalidArchive, +} + +impl Display for TwasmError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + match self { + TwasmError::InvalidMagic => write!(f, "Invalid twasm: invalid magic number"), + TwasmError::InvalidVersion => write!(f, "Invalid twasm: invalid version"), + TwasmError::InvalidPadding => write!(f, "Invalid twasm: invalid padding"), + TwasmError::InvalidArchive => write!(f, "Invalid twasm: invalid archive"), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for TwasmError {} + impl TinyWasmModule { /// Creates a TinyWasmModule from a slice of bytes. - pub fn from_twasm(wasm: &[u8]) -> Result<TinyWasmModule, &str> { + pub fn from_twasm(wasm: &[u8]) -> Result<TinyWasmModule, TwasmError> { let len = validate_magic(wasm)?; - let root = check_archived_root::<Self>(&wasm[len..]).map_err(|e| { - log::error!("Error checking archived root: {}", e); - "Error checking archived root" + let root = check_archived_root::<Self>(&wasm[len..]).map_err(|_e| { + crate::log::error!("Invalid archive: {}", _e); + TwasmError::InvalidArchive })?; Ok(root.deserialize(&mut rkyv::Infallible).unwrap()) diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index 547379c..205ec5a 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -10,6 +10,8 @@ //! Types used by [`tinywasm`](https://docs.rs/tinywasm) and [`tinywasm_parser`](https://docs.rs/tinywasm_parser). extern crate alloc; +use alloc::boxed::Box; +use core::{fmt::Debug, ops::Range}; // log for logging (optional). #[cfg(feature = "logging")] @@ -24,10 +26,9 @@ pub(crate) mod log { } mod instructions; -use core::{fmt::Debug, ops::Range}; - -use alloc::boxed::Box; +mod value; pub use instructions::*; +pub use value::*; #[cfg(feature = "archive")] pub mod archive; @@ -73,235 +74,6 @@ pub struct TinyWasmModule { pub elements: Box<[Element]>, } -/// A WebAssembly value. -/// -/// See <https://webassembly.github.io/spec/core/syntax/types.html#value-types> -#[derive(Clone, Copy)] -pub enum WasmValue { - // Num types - /// A 32-bit integer. - I32(i32), - /// A 64-bit integer. - I64(i64), - /// A 32-bit float. - F32(f32), - /// A 64-bit float. - F64(f64), - - RefExtern(ExternAddr), - RefFunc(FuncAddr), - RefNull(ValType), -} - -impl WasmValue { - #[inline] - pub fn const_instr(&self) -> ConstInstruction { - match self { - Self::I32(i) => ConstInstruction::I32Const(*i), - Self::I64(i) => ConstInstruction::I64Const(*i), - Self::F32(i) => ConstInstruction::F32Const(*i), - Self::F64(i) => ConstInstruction::F64Const(*i), - - Self::RefFunc(i) => ConstInstruction::RefFunc(*i), - Self::RefNull(ty) => ConstInstruction::RefNull(*ty), - - // Self::RefExtern(addr) => ConstInstruction::RefExtern(*addr), - _ => unimplemented!("no const_instr for {:?}", self), - } - } - - /// Get the default value for a given type. - #[inline] - pub fn default_for(ty: ValType) -> Self { - match ty { - ValType::I32 => Self::I32(0), - ValType::I64 => Self::I64(0), - ValType::F32 => Self::F32(0.0), - ValType::F64 => Self::F64(0.0), - ValType::RefFunc => Self::RefNull(ValType::RefFunc), - ValType::RefExtern => Self::RefNull(ValType::RefExtern), - } - } - - #[inline] - 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)) => { - if a.is_nan() && b.is_nan() { - true // Both are NaN, treat them as equal - } else { - a.to_bits() == b.to_bits() - } - } - (Self::F64(a), Self::F64(b)) => { - if a.is_nan() && b.is_nan() { - true // Both are NaN, treat them as equal - } else { - a.to_bits() == b.to_bits() - } - } - _ => false, - } - } -} - -impl From<i32> for WasmValue { - #[inline] - fn from(i: i32) -> Self { - Self::I32(i) - } -} - -impl From<i64> for WasmValue { - #[inline] - fn from(i: i64) -> Self { - Self::I64(i) - } -} - -impl From<f32> for WasmValue { - #[inline] - fn from(i: f32) -> Self { - Self::F32(i) - } -} - -impl From<f64> for WasmValue { - #[inline] - fn from(i: f64) -> Self { - Self::F64(i) - } -} - -#[cold] -fn cold() {} - -impl TryFrom<WasmValue> for i32 { - type Error = (); - - #[inline] - fn try_from(value: WasmValue) -> Result<Self, Self::Error> { - match value { - WasmValue::I32(i) => Ok(i), - _ => { - cold(); - crate::log::error!("i32: try_from failed: {:?}", value); - Err(()) - } - } - } -} - -impl TryFrom<WasmValue> for i64 { - type Error = (); - - #[inline] - fn try_from(value: WasmValue) -> Result<Self, Self::Error> { - match value { - WasmValue::I64(i) => Ok(i), - _ => { - cold(); - crate::log::error!("i64: try_from failed: {:?}", value); - Err(()) - } - } - } -} - -impl TryFrom<WasmValue> for f32 { - type Error = (); - - #[inline] - fn try_from(value: WasmValue) -> Result<Self, Self::Error> { - match value { - WasmValue::F32(i) => Ok(i), - _ => { - cold(); - crate::log::error!("f32: try_from failed: {:?}", value); - Err(()) - } - } - } -} - -impl TryFrom<WasmValue> for f64 { - type Error = (); - - #[inline] - fn try_from(value: WasmValue) -> Result<Self, Self::Error> { - match value { - WasmValue::F64(i) => Ok(i), - _ => { - cold(); - crate::log::error!("f64: try_from failed: {:?}", value); - Err(()) - } - } - } -} - -impl Debug for WasmValue { - fn fmt(&self, f: &mut alloc::fmt::Formatter<'_>) -> alloc::fmt::Result { - match self { - WasmValue::I32(i) => write!(f, "i32({})", i), - WasmValue::I64(i) => write!(f, "i64({})", i), - WasmValue::F32(i) => write!(f, "f32({})", i), - 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), - } - } -} - -impl WasmValue { - /// Get the type of a [`WasmValue`] - #[inline] - pub fn val_type(&self) -> ValType { - match self { - Self::I32(_) => ValType::I32, - Self::I64(_) => ValType::I64, - Self::F32(_) => ValType::F32, - Self::F64(_) => ValType::F64, - Self::RefExtern(_) => ValType::RefExtern, - Self::RefFunc(_) => ValType::RefFunc, - Self::RefNull(ty) => *ty, - } - } -} - -/// Type of a WebAssembly value. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] -#[cfg_attr(feature = "archive", archive(check_bytes))] -pub enum ValType { - /// A 32-bit integer. - I32, - /// A 64-bit integer. - I64, - /// A 32-bit float. - F32, - /// A 64-bit float. - F64, - /// A reference to a function. - RefFunc, - /// A reference to an external value. - RefExtern, -} - -impl ValType { - #[inline] - pub fn default_value(&self) -> WasmValue { - WasmValue::default_for(*self) - } -} - /// A WebAssembly External Kind. /// /// See <https://webassembly.github.io/spec/core/syntax/types.html#external-types> diff --git a/crates/types/src/value.rs b/crates/types/src/value.rs new file mode 100644 index 0000000..e46092b --- /dev/null +++ b/crates/types/src/value.rs @@ -0,0 +1,175 @@ +use core::fmt::Debug; + +use crate::{ConstInstruction, ExternAddr, FuncAddr}; + +/// A WebAssembly value. +/// +/// See <https://webassembly.github.io/spec/core/syntax/types.html#value-types> +#[derive(Clone, Copy)] +pub enum WasmValue { + // Num types + /// A 32-bit integer. + I32(i32), + /// A 64-bit integer. + I64(i64), + /// A 32-bit float. + F32(f32), + /// A 64-bit float. + F64(f64), + + RefExtern(ExternAddr), + RefFunc(FuncAddr), + RefNull(ValType), +} + +impl WasmValue { + #[inline] + pub fn const_instr(&self) -> ConstInstruction { + match self { + Self::I32(i) => ConstInstruction::I32Const(*i), + Self::I64(i) => ConstInstruction::I64Const(*i), + Self::F32(i) => ConstInstruction::F32Const(*i), + Self::F64(i) => ConstInstruction::F64Const(*i), + + Self::RefFunc(i) => ConstInstruction::RefFunc(*i), + Self::RefNull(ty) => ConstInstruction::RefNull(*ty), + + // Self::RefExtern(addr) => ConstInstruction::RefExtern(*addr), + _ => unimplemented!("no const_instr for {:?}", self), + } + } + + /// Get the default value for a given type. + #[inline] + pub fn default_for(ty: ValType) -> Self { + match ty { + ValType::I32 => Self::I32(0), + ValType::I64 => Self::I64(0), + ValType::F32 => Self::F32(0.0), + ValType::F64 => Self::F64(0.0), + ValType::RefFunc => Self::RefNull(ValType::RefFunc), + ValType::RefExtern => Self::RefNull(ValType::RefExtern), + } + } + + #[inline] + 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)) => { + if a.is_nan() && b.is_nan() { + true // Both are NaN, treat them as equal + } else { + a.to_bits() == b.to_bits() + } + } + (Self::F64(a), Self::F64(b)) => { + if a.is_nan() && b.is_nan() { + true // Both are NaN, treat them as equal + } else { + a.to_bits() == b.to_bits() + } + } + _ => false, + } + } +} + +#[cold] +fn cold() {} + +impl Debug for WasmValue { + fn fmt(&self, f: &mut alloc::fmt::Formatter<'_>) -> alloc::fmt::Result { + match self { + WasmValue::I32(i) => write!(f, "i32({})", i), + WasmValue::I64(i) => write!(f, "i64({})", i), + WasmValue::F32(i) => write!(f, "f32({})", i), + 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), + } + } +} + +impl WasmValue { + /// Get the type of a [`WasmValue`] + #[inline] + pub fn val_type(&self) -> ValType { + match self { + Self::I32(_) => ValType::I32, + Self::I64(_) => ValType::I64, + Self::F32(_) => ValType::F32, + Self::F64(_) => ValType::F64, + Self::RefExtern(_) => ValType::RefExtern, + Self::RefFunc(_) => ValType::RefFunc, + Self::RefNull(ty) => *ty, + } + } +} + +/// Type of a WebAssembly value. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] +pub enum ValType { + /// A 32-bit integer. + I32, + /// A 64-bit integer. + I64, + /// A 32-bit float. + F32, + /// A 64-bit float. + F64, + /// A reference to a function. + RefFunc, + /// A reference to an external value. + RefExtern, +} + +impl ValType { + #[inline] + pub fn default_value(&self) -> WasmValue { + WasmValue::default_for(*self) + } +} + +macro_rules! impl_conversion_for_wasmvalue { + ($($t:ty => $variant:ident),*) => { + $( + // Implementing From<$t> for WasmValue + impl From<$t> for WasmValue { + #[inline] + fn from(i: $t) -> Self { + Self::$variant(i) + } + } + + // Implementing TryFrom<WasmValue> for $t + impl TryFrom<WasmValue> for $t { + type Error = (); + + #[inline] + fn try_from(value: WasmValue) -> Result<Self, Self::Error> { + if let WasmValue::$variant(i) = value { + Ok(i) + } else { + cold(); + Err(()) + } + } + } + )* + } +} + +impl_conversion_for_wasmvalue! { + i32 => I32, + i64 => I64, + f32 => F32, + f64 => F64 +} |
