diff --git a/cranelift/wasm/Cargo.toml b/cranelift/wasm/Cargo.toml index 0462fc8a68..a0ed9fb37c 100644 --- a/cranelift/wasm/Cargo.toml +++ b/cranelift/wasm/Cargo.toml @@ -11,7 +11,7 @@ keywords = ["webassembly", "wasm"] edition = "2018" [dependencies] -wasmparser = { version = "0.23.0", default-features = false } +wasmparser = { version = "0.29.2", default-features = false } cranelift-codegen = { path = "../cranelift-codegen", version = "0.29.0", default-features = false } cranelift-entity = { path = "../cranelift-entity", version = "0.29.0", default-features = false } cranelift-frontend = { path = "../cranelift-frontend", version = "0.29.0", default-features = false } diff --git a/cranelift/wasm/src/code_translator.rs b/cranelift/wasm/src/code_translator.rs index ad0d75e2da..f83b462cef 100644 --- a/cranelift/wasm/src/code_translator.rs +++ b/cranelift/wasm/src/code_translator.rs @@ -886,6 +886,161 @@ pub fn translate_operator( Operator::RefNull | Operator::RefIsNull { .. } => { return Err(WasmError::Unsupported("proposed reference-type operators")); } + Operator::MemoryInit { .. } + | Operator::DataDrop { .. } + | Operator::MemoryCopy + | Operator::MemoryFill + | Operator::TableInit { .. } + | Operator::ElemDrop { .. } + | Operator::TableCopy + | Operator::TableGet { .. } + | Operator::TableSet { .. } + | Operator::TableGrow { .. } + | Operator::TableSize { .. } => { + return Err(WasmError::Unsupported("proposed bulk memory operators")); + } + Operator::V128Load { .. } + | Operator::V128Store { .. } + | Operator::V128Const { .. } + | Operator::V8x16Shuffle { .. } + | Operator::I8x16Splat + | Operator::I8x16ExtractLaneS { .. } + | Operator::I8x16ExtractLaneU { .. } + | Operator::I8x16ReplaceLane { .. } + | Operator::I16x8Splat + | Operator::I16x8ExtractLaneS { .. } + | Operator::I16x8ExtractLaneU { .. } + | Operator::I16x8ReplaceLane { .. } + | Operator::I32x4Splat + | Operator::I32x4ExtractLane { .. } + | Operator::I32x4ReplaceLane { .. } + | Operator::I64x2Splat + | Operator::I64x2ExtractLane { .. } + | Operator::I64x2ReplaceLane { .. } + | Operator::F32x4Splat + | Operator::F32x4ExtractLane { .. } + | Operator::F32x4ReplaceLane { .. } + | Operator::F64x2Splat + | Operator::F64x2ExtractLane { .. } + | Operator::F64x2ReplaceLane { .. } + | Operator::I8x16Eq + | Operator::I8x16Ne + | Operator::I8x16LtS + | Operator::I8x16LtU + | Operator::I8x16GtS + | Operator::I8x16GtU + | Operator::I8x16LeS + | Operator::I8x16LeU + | Operator::I8x16GeS + | Operator::I8x16GeU + | Operator::I16x8Eq + | Operator::I16x8Ne + | Operator::I16x8LtS + | Operator::I16x8LtU + | Operator::I16x8GtS + | Operator::I16x8GtU + | Operator::I16x8LeS + | Operator::I16x8LeU + | Operator::I16x8GeS + | Operator::I16x8GeU + | Operator::I32x4Eq + | Operator::I32x4Ne + | Operator::I32x4LtS + | Operator::I32x4LtU + | Operator::I32x4GtS + | Operator::I32x4GtU + | Operator::I32x4LeS + | Operator::I32x4LeU + | Operator::I32x4GeS + | Operator::I32x4GeU + | Operator::F32x4Eq + | Operator::F32x4Ne + | Operator::F32x4Lt + | Operator::F32x4Gt + | Operator::F32x4Le + | Operator::F32x4Ge + | Operator::F64x2Eq + | Operator::F64x2Ne + | Operator::F64x2Lt + | Operator::F64x2Gt + | Operator::F64x2Le + | Operator::F64x2Ge + | Operator::V128Not + | Operator::V128And + | Operator::V128Or + | Operator::V128Xor + | Operator::V128Bitselect + | Operator::I8x16Neg + | Operator::I8x16AnyTrue + | Operator::I8x16AllTrue + | Operator::I8x16Shl + | Operator::I8x16ShrS + | Operator::I8x16ShrU + | Operator::I8x16Add + | Operator::I8x16AddSaturateS + | Operator::I8x16AddSaturateU + | Operator::I8x16Sub + | Operator::I8x16SubSaturateS + | Operator::I8x16SubSaturateU + | Operator::I8x16Mul + | Operator::I16x8Neg + | Operator::I16x8AnyTrue + | Operator::I16x8AllTrue + | Operator::I16x8Shl + | Operator::I16x8ShrS + | Operator::I16x8ShrU + | Operator::I16x8Add + | Operator::I16x8AddSaturateS + | Operator::I16x8AddSaturateU + | Operator::I16x8Sub + | Operator::I16x8SubSaturateS + | Operator::I16x8SubSaturateU + | Operator::I16x8Mul + | Operator::I32x4Neg + | Operator::I32x4AnyTrue + | Operator::I32x4AllTrue + | Operator::I32x4Shl + | Operator::I32x4ShrS + | Operator::I32x4ShrU + | Operator::I32x4Add + | Operator::I32x4Sub + | Operator::I32x4Mul + | Operator::I64x2Neg + | Operator::I64x2AnyTrue + | Operator::I64x2AllTrue + | Operator::I64x2Shl + | Operator::I64x2ShrS + | Operator::I64x2ShrU + | Operator::I64x2Add + | Operator::I64x2Sub + | Operator::F32x4Abs + | Operator::F32x4Neg + | Operator::F32x4Sqrt + | Operator::F32x4Add + | Operator::F32x4Sub + | Operator::F32x4Mul + | Operator::F32x4Div + | Operator::F32x4Min + | Operator::F32x4Max + | Operator::F64x2Abs + | Operator::F64x2Neg + | Operator::F64x2Sqrt + | Operator::F64x2Add + | Operator::F64x2Sub + | Operator::F64x2Mul + | Operator::F64x2Div + | Operator::F64x2Min + | Operator::F64x2Max + | Operator::I32x4TruncSF32x4Sat + | Operator::I32x4TruncUF32x4Sat + | Operator::I64x2TruncSF64x2Sat + | Operator::I64x2TruncUF64x2Sat + | Operator::F32x4ConvertSI32x4 + | Operator::F32x4ConvertUI32x4 + | Operator::F64x2ConvertSI64x2 + | Operator::F64x2ConvertUI64x2 => { + return Err(WasmError::Unsupported("proposed SIMD operators")); + } }; Ok(()) } diff --git a/cranelift/wasm/src/sections_translator.rs b/cranelift/wasm/src/sections_translator.rs index 37c5d34672..e406ec3eee 100644 --- a/cranelift/wasm/src/sections_translator.rs +++ b/cranelift/wasm/src/sections_translator.rs @@ -12,15 +12,15 @@ use crate::translation_utils::{ type_to_type, FuncIndex, Global, GlobalIndex, GlobalInit, Memory, MemoryIndex, SignatureIndex, Table, TableElementType, TableIndex, }; -use core::str::from_utf8; use cranelift_codegen::ir::{self, AbiParam, Signature}; use cranelift_entity::EntityRef; use std::vec::Vec; use wasmparser::{ - self, CodeSectionReader, Data, DataSectionReader, Element, ElementSectionReader, Export, - ExportSectionReader, ExternalKind, FuncType, FunctionSectionReader, GlobalSectionReader, - GlobalType, ImportSectionEntryType, ImportSectionReader, MemorySectionReader, MemoryType, - Operator, TableSectionReader, TypeSectionReader, + self, CodeSectionReader, Data, DataKind, DataSectionReader, Element, ElementKind, + ElementSectionReader, Export, ExportSectionReader, ExternalKind, FuncType, + FunctionSectionReader, GlobalSectionReader, GlobalType, ImportSectionEntryType, + ImportSectionReader, MemorySectionReader, MemoryType, Operator, TableSectionReader, + TypeSectionReader, }; /// Parses the Type section of the wasm module. @@ -65,12 +65,8 @@ pub fn parse_import_section<'data>( for entry in imports { let import = entry?; - - // The input has already been validated, so we should be able to - // assume valid UTF-8 and use `from_utf8_unchecked` if performance - // becomes a concern here. - let module_name = from_utf8(import.module).unwrap(); - let field_name = from_utf8(import.field).unwrap(); + let module_name = import.module; + let field_name = import.field; match import.ty { ImportSectionEntryType::Function(sig) => { @@ -232,13 +228,12 @@ pub fn parse_export_section<'data>( // The input has already been validated, so we should be able to // assume valid UTF-8 and use `from_utf8_unchecked` if performance // becomes a concern here. - let name = from_utf8(field).unwrap(); let index = index as usize; match *kind { - ExternalKind::Function => environ.declare_func_export(FuncIndex::new(index), name), - ExternalKind::Table => environ.declare_table_export(TableIndex::new(index), name), - ExternalKind::Memory => environ.declare_memory_export(MemoryIndex::new(index), name), - ExternalKind::Global => environ.declare_global_export(GlobalIndex::new(index), name), + ExternalKind::Function => environ.declare_func_export(FuncIndex::new(index), field), + ExternalKind::Table => environ.declare_table_export(TableIndex::new(index), field), + ExternalKind::Memory => environ.declare_memory_export(MemoryIndex::new(index), field), + ExternalKind::Global => environ.declare_global_export(GlobalIndex::new(index), field), } } @@ -260,29 +255,35 @@ pub fn parse_element_section<'data>( environ.reserve_table_elements(elements.get_count()); for entry in elements { - let Element { + let Element { kind, items } = entry?; + if let ElementKind::Active { table_index, init_expr, - items, - } = entry?; - let mut init_expr_reader = init_expr.get_binary_reader(); - let (base, offset) = match init_expr_reader.read_operator()? { - Operator::I32Const { value } => (None, value as u32 as usize), - Operator::GetGlobal { global_index } => (Some(GlobalIndex::from_u32(global_index)), 0), - ref s => panic!("unsupported init expr in element section: {:?}", s), - }; - let items_reader = items.get_items_reader()?; - let mut elems = Vec::with_capacity(cast::usize(items_reader.get_count())); - for item in items_reader { - let x = item?; - elems.push(FuncIndex::from_u32(x)); + } = kind + { + let mut init_expr_reader = init_expr.get_binary_reader(); + let (base, offset) = match init_expr_reader.read_operator()? { + Operator::I32Const { value } => (None, value as u32 as usize), + Operator::GetGlobal { global_index } => { + (Some(GlobalIndex::from_u32(global_index)), 0) + } + ref s => panic!("unsupported init expr in element section: {:?}", s), + }; + let items_reader = items.get_items_reader()?; + let mut elems = Vec::with_capacity(cast::usize(items_reader.get_count())); + for item in items_reader { + let x = item?; + elems.push(FuncIndex::from_u32(x)); + } + environ.declare_table_elements( + TableIndex::from_u32(table_index), + base, + offset, + elems.into_boxed_slice(), + ) + } else { + panic!("unsupported passive elements section"); } - environ.declare_table_elements( - TableIndex::from_u32(table_index), - base, - offset, - elems.into_boxed_slice(), - ) } Ok(()) } @@ -309,23 +310,29 @@ pub fn parse_data_section<'data>( environ.reserve_data_initializers(data.get_count()); for entry in data { - let Data { + let Data { kind, data } = entry?; + if let DataKind::Active { memory_index, init_expr, - data, - } = entry?; - let mut init_expr_reader = init_expr.get_binary_reader(); - let (base, offset) = match init_expr_reader.read_operator()? { - Operator::I32Const { value } => (None, value as u32 as usize), - Operator::GetGlobal { global_index } => (Some(GlobalIndex::from_u32(global_index)), 0), - ref s => panic!("unsupported init expr in data section: {:?}", s), - }; - environ.declare_data_initialization( - MemoryIndex::from_u32(memory_index), - base, - offset, - data, - ); + } = kind + { + let mut init_expr_reader = init_expr.get_binary_reader(); + let (base, offset) = match init_expr_reader.read_operator()? { + Operator::I32Const { value } => (None, value as u32 as usize), + Operator::GetGlobal { global_index } => { + (Some(GlobalIndex::from_u32(global_index)), 0) + } + ref s => panic!("unsupported init expr in data section: {:?}", s), + }; + environ.declare_data_initialization( + MemoryIndex::from_u32(memory_index), + base, + offset, + data, + ); + } else { + panic!("unsupported passive data section"); + } } Ok(())