summaryrefslogtreecommitdiff
path: root/crates/parser/src/conversion.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/parser/src/conversion.rs')
-rw-r--r--crates/parser/src/conversion.rs318
1 files changed, 244 insertions, 74 deletions
diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs
index db8d2aa..36455ee 100644
--- a/crates/parser/src/conversion.rs
+++ b/crates/parser/src/conversion.rs
@@ -1,16 +1,38 @@
-use crate::{Result, module::FunctionCode, visit::process_operators_and_validate};
-use alloc::{boxed::Box, format, string::ToString, vec::Vec};
+use crate::validation::{FuncValidator, FuncValidatorAllocations, ValidatorResources};
+#[cfg(feature = "validate")]
+use crate::visit::process_operators_and_validate;
+use crate::{ParserOptions, Result, module::FunctionCode, visit::process_operators};
+use alloc::{boxed::Box, format, vec::Vec};
use tinywasm_types::*;
-use wasmparser::{
- CompositeInnerType, FuncValidator, FuncValidatorAllocations, OperatorsReader, OperatorsReaderAllocations,
- ValidatorResources,
-};
+use wasmparser::{CompositeInnerType, OperatorsReader, OperatorsReaderAllocations, UnpackedIndex};
-pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result<tinywasm_types::Element> {
+/// Function identity and result layout shared by lowering and finalization.
+#[derive(Clone, Copy)]
+pub(crate) struct FunctionLoweringContext {
+ /// Module type index of the function signature.
+ pub(crate) ty_idx: u32,
+ /// Module function index, including imported functions.
+ pub(crate) self_func_addr: u32,
+ /// Result counts in each physical value lane.
+ pub(crate) function_results: ValueCounts,
+}
+
+pub(crate) fn value_lane(ty: wasmparser::ValType) -> ValueLane {
+ match ty {
+ wasmparser::ValType::I32 | wasmparser::ValType::F32 | wasmparser::ValType::Ref(_) => ValueLane::S32,
+ wasmparser::ValType::I64 | wasmparser::ValType::F64 => ValueLane::S64,
+ wasmparser::ValType::V128 => ValueLane::S128,
+ }
+}
+
+pub(crate) fn convert_module_element(
+ element: wasmparser::Element<'_>,
+ global_types: &[WasmType],
+) -> Result<tinywasm_types::Element> {
let kind = match element.kind {
wasmparser::ElementKind::Active { table_index, offset_expr } => tinywasm_types::ElementKind::Active {
table: table_index.unwrap_or(0),
- offset: process_const_operators(offset_expr.get_operators_reader())?,
+ offset: process_const_operators(offset_expr.get_operators_reader(), global_types)?,
},
wasmparser::ElementKind::Passive => tinywasm_types::ElementKind::Passive,
wasmparser::ElementKind::Declared => tinywasm_types::ElementKind::Declared,
@@ -30,7 +52,7 @@ pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result
wasmparser::ElementItems::Expressions(ty, exprs) => {
let items = exprs
.into_iter()
- .map(|expr| Ok(ElementItem::Expr(process_const_operators(expr?.get_operators_reader())?)))
+ .map(|expr| Ok(ElementItem::Expr(process_const_operators(expr?.get_operators_reader(), global_types)?)))
.collect::<Result<Vec<_>>>()?
.into_boxed_slice();
@@ -39,13 +61,16 @@ pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result
}
}
-pub(crate) fn convert_module_data(data: wasmparser::Data<'_>) -> Result<tinywasm_types::Data> {
+pub(crate) fn convert_module_data(
+ data: wasmparser::Data<'_>,
+ global_types: &[WasmType],
+) -> Result<tinywasm_types::Data> {
Ok(tinywasm_types::Data {
- data: data.data.to_vec().into_boxed_slice(),
+ data: data.data.into(),
range: data.range,
kind: match data.kind {
wasmparser::DataKind::Active { memory_index, offset_expr } => {
- let offset = process_const_operators(offset_expr.get_operators_reader())?;
+ let offset = process_const_operators(offset_expr.get_operators_reader(), global_types)?;
tinywasm_types::DataKind::Active { mem: memory_index, offset }
}
wasmparser::DataKind::Passive => tinywasm_types::DataKind::Passive,
@@ -68,9 +93,7 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result<Im
wasmparser::TypeRef::Global(ty) => {
ImportKind::Global(GlobalType::new(convert_valtype(&ty.content_type)?, ty.mutable))
}
- wasmparser::TypeRef::Tag(ty) => {
- return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported import kind: {ty:?}")));
- }
+ wasmparser::TypeRef::Tag(ty) => ImportKind::Tag(convert_tag_type(ty)),
_ => {
return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported import kind: {:?}", import.ty)));
}
@@ -90,16 +113,17 @@ pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> MemoryTyp
pub(crate) fn convert_module_globals(
globals: wasmparser::SectionLimited<'_, wasmparser::Global<'_>>,
+ global_types: &mut Vec<WasmType>,
) -> Result<Box<[Global]>> {
- globals
- .into_iter()
- .map(|global| {
- let global = global?;
- let ty = convert_valtype(&global.ty.content_type)?;
- let ops = global.init_expr.get_operators_reader();
- Ok(Global { init: process_const_operators(ops)?, ty: GlobalType::new(ty, global.ty.mutable) })
- })
- .collect::<Result<Box<_>>>()
+ let mut out = Vec::with_capacity(globals.count() as usize);
+ for global in globals {
+ let global = global?;
+ let ty = convert_valtype(&global.ty.content_type)?;
+ let init = process_const_operators(global.init_expr.get_operators_reader(), global_types)?;
+ global_types.push(ty);
+ out.push(Global { init, ty: GlobalType::new(ty, global.ty.mutable) });
+ }
+ Ok(out.into_boxed_slice())
}
pub(crate) fn convert_module_export(export: wasmparser::Export<'_>) -> Result<Export> {
@@ -108,7 +132,8 @@ pub(crate) fn convert_module_export(export: wasmparser::Export<'_>) -> Result<Ex
wasmparser::ExternalKind::Table => ExternalKind::Table,
wasmparser::ExternalKind::Memory => ExternalKind::Memory,
wasmparser::ExternalKind::Global => ExternalKind::Global,
- wasmparser::ExternalKind::Tag | wasmparser::ExternalKind::FuncExact => {
+ wasmparser::ExternalKind::Tag => ExternalKind::Tag,
+ wasmparser::ExternalKind::FuncExact => {
return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported export kind: {:?}", export.kind)));
}
};
@@ -116,72 +141,166 @@ pub(crate) fn convert_module_export(export: wasmparser::Export<'_>) -> Result<Ex
Ok(Export { index: export.index, name: Box::from(export.name), kind })
}
+pub(crate) const fn convert_tag_type(ty: wasmparser::TagType) -> TagType {
+ TagType::new(ty.func_type_idx)
+}
+
+fn extend_local_types(local_types: &mut Vec<ValueLane>, count: u32, ty: wasmparser::ValType) -> Result<()> {
+ let size = value_lane(ty);
+ let count =
+ usize::try_from(count).map_err(|_| crate::ParseError::Other("local declaration count is too large".into()))?;
+ local_types.reserve(count);
+ local_types.extend(core::iter::repeat_n(size, count));
+ Ok(())
+}
+
pub(crate) fn convert_module_code(
func: wasmparser::FunctionBody<'_>,
- mut validator: Option<FuncValidator<ValidatorResources>>,
+ validator: Option<FuncValidator<ValidatorResources>>,
reader_allocs: OperatorsReaderAllocations,
metadata: &crate::visit::ModuleMetadata,
- ty_idx: u32,
+ context: FunctionLoweringContext,
+ options: &ParserOptions,
) -> Result<(FunctionCode, Option<FuncValidatorAllocations>, OperatorsReaderAllocations)> {
- let locals_reader = func.get_locals_reader()?;
- let pos = locals_reader.original_position();
- let signature = metadata.signature(ty_idx)?.clone();
- let mut local_types = signature.params.clone();
+ let mut locals_reader = func.get_locals_reader()?;
+ let mut local_types = metadata.signature(context.ty_idx)?.params.clone();
- for (i, local) in locals_reader.into_iter().enumerate() {
- let local = local?;
+ #[cfg(feature = "validate")]
+ let mut validator = validator;
+
+ for _ in 0..locals_reader.get_count() {
+ #[cfg(feature = "validate")]
+ let position = locals_reader.original_position();
+ let local = locals_reader.read()?;
+ #[cfg(feature = "validate")]
if let Some(validator) = validator.as_mut() {
- validator.define_locals(pos + i, local.0, local.1)?;
+ validator.define_locals(position, local.0, local.1)?;
}
- let size = crate::visit::OperandSize::from(local.1);
- let count = usize::try_from(local.0)
- .map_err(|_| crate::ParseError::Other("local declaration count is too large".into()))?;
- local_types.reserve(count);
- local_types.extend(core::iter::repeat_n(size, count));
+ extend_local_types(&mut local_types, local.0, local.1)?;
}
+ #[cfg(not(feature = "validate"))]
+ let _ = validator;
+
// maps a local's address to the index in the type's locals array
let mut local_addr_map = Vec::with_capacity(local_types.len());
let mut local_counts = ValueCounts::default();
for ty in &local_types {
let (count, error) = match ty {
- crate::visit::OperandSize::S32 => (&mut local_counts.c32, "too many 32-bit locals"),
- crate::visit::OperandSize::S64 => (&mut local_counts.c64, "too many 64-bit locals"),
- crate::visit::OperandSize::S128 => (&mut local_counts.c128, "too many 128-bit locals"),
+ ValueLane::S32 => (&mut local_counts.c32, "too many 32-bit locals"),
+ ValueLane::S64 => (&mut local_counts.c64, "too many 64-bit locals"),
+ ValueLane::S128 => (&mut local_counts.c128, "too many 128-bit locals"),
};
local_addr_map.push(*count);
*count = count.checked_add(1).ok_or_else(|| crate::ParseError::Other(error.into()))?;
}
- let (body, data, validator_allocs, reader_allocs) =
- process_operators_and_validate(validator, func, local_types, local_addr_map, metadata, ty_idx, reader_allocs)?;
+ #[cfg(feature = "validate")]
+ let (body, data, uses_local_memory, validator_allocs, reader_allocs) = match validator {
+ Some(validator) => {
+ let (body, data, uses_local_memory, validator_allocs, reader_allocs) = process_operators_and_validate(
+ validator,
+ func,
+ (local_types, local_addr_map),
+ metadata,
+ context,
+ reader_allocs,
+ options,
+ )?;
+ (body, data, uses_local_memory, Some(validator_allocs), reader_allocs)
+ }
+ None => {
+ let (body, data, uses_local_memory, reader_allocs) =
+ process_operators(func, (local_types, local_addr_map), metadata, context, reader_allocs, options)?;
+ (body, data, uses_local_memory, None, reader_allocs)
+ }
+ };
+ #[cfg(not(feature = "validate"))]
+ let (body, data, uses_local_memory, validator_allocs, reader_allocs) = {
+ let _ = validator;
+ let (body, data, uses_local_memory, reader_allocs) =
+ process_operators(func, (local_types, local_addr_map), metadata, context, reader_allocs, options)?;
+ (body, data, uses_local_memory, None, reader_allocs)
+ };
Ok((
- FunctionCode { instructions: body, data, locals: local_counts, uses_local_memory: false },
+ FunctionCode { instructions: body, data: data.finish(), locals: local_counts, uses_local_memory },
validator_allocs,
reader_allocs,
))
}
-pub(crate) fn convert_module_type(ty: wasmparser::RecGroup) -> Result<FuncType> {
- let mut types = ty.types();
- // TODO(wasm3): Preserve recursive groups and non-function composite types instead of flattening singleton funcs.
- if types.len() != 1 {
- return Err(crate::ParseError::UnsupportedOperator(
- "Expected exactly one type in the type section".to_string(),
- ));
+pub(crate) fn convert_rec_group(ty: wasmparser::RecGroup, group_start: u32, types: &mut Vec<SubType>) -> Result<u32> {
+ let group_len = u32::try_from(ty.types().len())
+ .map_err(|_| crate::ParseError::Other("recursive type group is too large".into()))?;
+ types.reserve(group_len as usize);
+ for ty in ty.into_types() {
+ let composite = &ty.composite_type;
+ if composite.shared {
+ return Err(crate::ParseError::UnsupportedOperator("shared composite types are unsupported".into()));
+ }
+ if composite.descriptor_idx.is_some() || composite.describes_idx.is_some() {
+ return Err(crate::ParseError::UnsupportedOperator("descriptor types are unsupported".into()));
+ }
+
+ let supertype =
+ ty.supertype_idx.map(|idx| convert_type_index(idx.unpack(), group_start, group_len)).transpose()?;
+ let composite = match &composite.inner {
+ CompositeInnerType::Func(ty) => {
+ let params = ty
+ .params()
+ .iter()
+ .map(|ty| convert_valtype_in_group(ty, group_start, group_len))
+ .collect::<Result<Vec<_>>>()?;
+ let results = ty
+ .results()
+ .iter()
+ .map(|ty| convert_valtype_in_group(ty, group_start, group_len))
+ .collect::<Result<Vec<_>>>()?;
+ CompositeType::Func(FuncType::new(&params, &results))
+ }
+ CompositeInnerType::Struct(ty) => CompositeType::Struct(StructType {
+ fields: ty
+ .fields
+ .iter()
+ .map(|field| convert_field_type(field, group_start, group_len))
+ .collect::<Result<_>>()?,
+ }),
+ CompositeInnerType::Array(ty) => {
+ CompositeType::Array(ArrayType { field: convert_field_type(&ty.0, group_start, group_len)? })
+ }
+ CompositeInnerType::Cont(_) => {
+ return Err(crate::ParseError::UnsupportedOperator("continuation types are unsupported".into()));
+ }
+ };
+ types.push(SubType { is_final: ty.is_final, supertype, composite });
+ }
+ Ok(group_len)
+}
+
+fn convert_type_index(index: UnpackedIndex, group_start: u32, group_len: u32) -> Result<TypeAddr> {
+ match index {
+ UnpackedIndex::Module(index) => Ok(index),
+ UnpackedIndex::RecGroup(index) if index < group_len => {
+ group_start.checked_add(index).ok_or_else(|| crate::ParseError::Other("type index is too large".into()))
+ }
+ UnpackedIndex::RecGroup(index) => {
+ Err(crate::ParseError::Other(format!("recursive group type index out of bounds: {index}")))
+ }
+ #[cfg(feature = "validate")]
+ UnpackedIndex::Id(_) => {
+ Err(crate::ParseError::UnsupportedOperator(format!("unsupported canonical type index: {index}")))
+ }
}
+}
- let ty = types.next().unwrap();
- let CompositeInnerType::Func(ty) = &ty.composite_type.inner else {
- return Err(crate::ParseError::UnsupportedOperator(format!(
- "Unsupported non-function type in type section: {}",
- ty.composite_type
- )));
+fn convert_field_type(field: &wasmparser::FieldType, group_start: u32, group_len: u32) -> Result<FieldType> {
+ let storage = match &field.element_type {
+ wasmparser::StorageType::I8 => StorageType::I8,
+ wasmparser::StorageType::I16 => StorageType::I16,
+ wasmparser::StorageType::Val(ty) => StorageType::Value(convert_valtype_in_group(ty, group_start, group_len)?),
};
- let params = ty.params().iter().map(convert_valtype).collect::<Result<Vec<_>>>()?;
- let results = ty.results().iter().map(convert_valtype).collect::<Result<Vec<_>>>()?;
- Ok(FuncType::new(&params, &results))
+ Ok(FieldType { storage, mutable: field.mutable })
}
pub(crate) fn convert_ref_type(ty: wasmparser::RefType) -> Result<RefType> {
@@ -189,17 +308,30 @@ pub(crate) fn convert_ref_type(ty: wasmparser::RefType) -> Result<RefType> {
}
pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> Result<WasmType> {
+ convert_valtype_with_group(valtype, None)
+}
+
+fn convert_valtype_in_group(valtype: &wasmparser::ValType, group_start: u32, group_len: u32) -> Result<WasmType> {
+ convert_valtype_with_group(valtype, Some((group_start, group_len)))
+}
+
+fn convert_valtype_with_group(valtype: &wasmparser::ValType, group: Option<(u32, u32)>) -> Result<WasmType> {
match valtype {
wasmparser::ValType::I32 => Ok(WasmType::I32),
wasmparser::ValType::I64 => Ok(WasmType::I64),
wasmparser::ValType::F32 => Ok(WasmType::F32),
wasmparser::ValType::F64 => Ok(WasmType::F64),
wasmparser::ValType::V128 => Ok(WasmType::V128),
- wasmparser::ValType::Ref(r) => Ok(WasmType::Ref(convert_ref_type(*r)?)),
+ wasmparser::ValType::Ref(r) => {
+ Ok(WasmType::Ref(convert_heap_type_with_group(r.heap_type(), r.is_nullable(), group)?))
+ }
}
}
-pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<Box<[ConstInstruction]>> {
+pub(crate) fn process_const_operators(
+ ops: OperatorsReader<'_>,
+ global_types: &[WasmType],
+) -> Result<Box<[ConstInstruction]>> {
let mut out = Vec::new();
let mut operator_count = 0;
let mut end_reached = false;
@@ -213,19 +345,36 @@ pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<Box<[C
}
let instr = match op {
- wasmparser::Operator::RefNull { hty } => {
- convert_heap_type(hty, false)?;
- ConstInstruction::Ref(RefValue::Null)
- }
+ wasmparser::Operator::RefNull { hty } => ConstInstruction::RefNull(convert_heap_type(hty, true)?),
wasmparser::Operator::RefFunc { function_index } => {
- ConstInstruction::Ref(RefValue::Func(FuncRef::new(function_index)))
+ ConstInstruction::RefFunc(ModuleFuncIdx::new(function_index))
+ }
+ wasmparser::Operator::RefI31 => ConstInstruction::RefI31,
+ wasmparser::Operator::AnyConvertExtern => ConstInstruction::AnyConvertExtern,
+ wasmparser::Operator::ExternConvertAny => ConstInstruction::ExternConvertAny,
+ wasmparser::Operator::StructNew { struct_type_index } => ConstInstruction::StructNew(struct_type_index),
+ wasmparser::Operator::StructNewDefault { struct_type_index } => {
+ ConstInstruction::StructNewDefault(struct_type_index)
+ }
+ wasmparser::Operator::ArrayNew { array_type_index } => ConstInstruction::ArrayNew(array_type_index),
+ wasmparser::Operator::ArrayNewDefault { array_type_index } => {
+ ConstInstruction::ArrayNewDefault(array_type_index)
+ }
+ wasmparser::Operator::ArrayNewFixed { array_type_index, array_size } => {
+ ConstInstruction::ArrayNewFixed(array_type_index, array_size)
}
wasmparser::Operator::I32Const { value } => ConstInstruction::I32Const(value),
wasmparser::Operator::I64Const { value } => ConstInstruction::I64Const(value),
wasmparser::Operator::F32Const { value } => ConstInstruction::F32Const(f32::from_bits(value.bits())),
wasmparser::Operator::F64Const { value } => ConstInstruction::F64Const(f64::from_bits(value.bits())),
wasmparser::Operator::V128Const { value } => ConstInstruction::V128Const(*value.bytes()),
- wasmparser::Operator::GlobalGet { global_index } => ConstInstruction::GlobalGet(global_index),
+ wasmparser::Operator::GlobalGet { global_index } => match global_types.get(global_index as usize) {
+ Some(WasmType::I32 | WasmType::F32) => ConstInstruction::GlobalGet32(global_index),
+ Some(WasmType::I64 | WasmType::F64) => ConstInstruction::GlobalGet64(global_index),
+ Some(WasmType::V128) => ConstInstruction::GlobalGet128(global_index),
+ Some(WasmType::Ref(_)) => ConstInstruction::GlobalGetRef(global_index),
+ None => return Err(crate::ParseError::Other(format!("global index out of bounds: {global_index}"))),
+ },
wasmparser::Operator::I32Add => ConstInstruction::I32Add,
wasmparser::Operator::I32Sub => ConstInstruction::I32Sub,
wasmparser::Operator::I32Mul => ConstInstruction::I32Mul,
@@ -249,6 +398,14 @@ pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<Box<[C
}
pub(crate) fn convert_heap_type(heap: wasmparser::HeapType, nullable: bool) -> Result<RefType> {
+ convert_heap_type_with_group(heap, nullable, None)
+}
+
+fn convert_heap_type_with_group(
+ heap: wasmparser::HeapType,
+ nullable: bool,
+ group: Option<(u32, u32)>,
+) -> Result<RefType> {
match heap {
wasmparser::HeapType::Abstract { shared: false, ty } => Ok(RefType::new_abstract(
nullable,
@@ -271,11 +428,24 @@ pub(crate) fn convert_heap_type(heap: wasmparser::HeapType, nullable: bool) -> R
},
)),
wasmparser::HeapType::Concrete(index) => {
- let index = index.as_module_index().ok_or_else(|| {
- crate::ParseError::UnsupportedOperator(format!("Unsupported non-module heap type index: {index:?}"))
- })?;
- RefType::new_concrete(nullable, index)
- .ok_or_else(|| crate::ParseError::Other(format!("heap type index is too large: {index}")))
+ let index = match index {
+ UnpackedIndex::Module(index) => index,
+ index @ UnpackedIndex::RecGroup(_) => {
+ let (group_start, group_len) = group.ok_or_else(|| {
+ crate::ParseError::UnsupportedOperator(format!(
+ "recursive-group heap type outside a type group: {index}"
+ ))
+ })?;
+ convert_type_index(index, group_start, group_len)?
+ }
+ #[cfg(feature = "validate")]
+ index @ UnpackedIndex::Id(_) => {
+ return Err(crate::ParseError::UnsupportedOperator(format!(
+ "unsupported canonical heap type index: {index}"
+ )));
+ }
+ };
+ Ok(RefType::new_concrete(nullable, index))
}
wasmparser::HeapType::Abstract { shared: true, .. } | wasmparser::HeapType::Exact(_) => {
Err(crate::ParseError::UnsupportedOperator(format!("Unsupported heap type: {heap:?}")))