Update wasm-tools crates (#3997)

* Update wasm-tools crates

This commit updates the wasm-tools family of crates as used in Wasmtime.
Notably this brings in the update which removes module linking support
as well as a number of internal refactorings around names and such
within wasmparser itself. This updates all of the wasm translation
support which binds to wasmparser as appropriate.

Other crates all had API-compatible changes for at least what Wasmtime
used so no further changes were necessary beyond updating version
requirements.

* Update a test expectation
This commit is contained in:
Alex Crichton
2022-04-05 14:32:33 -05:00
committed by GitHub
parent 7ac2598009
commit d147802d51
26 changed files with 178 additions and 299 deletions

View File

@@ -596,7 +596,11 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
state.popn(num_args);
state.pushn(inst_results);
}
Operator::CallIndirect { index, table_index } => {
Operator::CallIndirect {
index,
table_index,
table_byte: _,
} => {
// `index` is the index of the function's signature and `table_index` is the index of
// the table to search the function in.
let (sigref, num_args) = state.get_indirect_sig(builder.func, *index, environ)?;

View File

@@ -289,7 +289,7 @@ impl<'dummy_environment> FuncEnvironment for DummyFuncEnvironment<'dummy_environ
WasmType::F32 => ir::types::F32,
WasmType::F64 => ir::types::F64,
WasmType::V128 => ir::types::I8X16,
WasmType::ExnRef | WasmType::FuncRef | WasmType::ExternRef => ir::types::R64,
WasmType::FuncRef | WasmType::ExternRef => ir::types::R64,
},
})
}
@@ -685,7 +685,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
WasmType::F32 => ir::types::F32,
WasmType::F64 => ir::types::F64,
WasmType::V128 => ir::types::I8X16,
WasmType::FuncRef | WasmType::ExternRef | WasmType::ExnRef => reference_type,
WasmType::FuncRef | WasmType::ExternRef => reference_type,
})
};
sig.params.extend(wasm.params().iter().map(&mut cvt));
@@ -698,7 +698,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
&mut self,
index: TypeIndex,
module: &'data str,
field: Option<&'data str>,
field: &'data str,
) -> WasmResult<()> {
assert_eq!(
self.info.functions.len(),
@@ -708,7 +708,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
self.info.functions.push(Exportable::new(index));
self.info
.imported_funcs
.push((String::from(module), String::from(field.unwrap())));
.push((String::from(module), String::from(field)));
Ok(())
}
@@ -726,12 +726,12 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
&mut self,
global: Global,
module: &'data str,
field: Option<&'data str>,
field: &'data str,
) -> WasmResult<()> {
self.info.globals.push(Exportable::new(global));
self.info
.imported_globals
.push((String::from(module), String::from(field.unwrap())));
.push((String::from(module), String::from(field)));
Ok(())
}
@@ -744,12 +744,12 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
&mut self,
table: Table,
module: &'data str,
field: Option<&'data str>,
field: &'data str,
) -> WasmResult<()> {
self.info.tables.push(Exportable::new(table));
self.info
.imported_tables
.push((String::from(module), String::from(field.unwrap())));
.push((String::from(module), String::from(field)));
Ok(())
}
@@ -789,12 +789,12 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
&mut self,
memory: Memory,
module: &'data str,
field: Option<&'data str>,
field: &'data str,
) -> WasmResult<()> {
self.info.memories.push(Exportable::new(memory));
self.info
.imported_memories
.push((String::from(module), String::from(field.unwrap())));
.push((String::from(module), String::from(field)));
Ok(())
}

View File

@@ -8,9 +8,8 @@
use crate::state::FuncTranslationState;
use crate::{
DataIndex, ElemIndex, EntityType, FuncIndex, Global, GlobalIndex, Memory, MemoryIndex,
SignatureIndex, Table, TableIndex, Tag, TagIndex, TypeIndex, WasmError, WasmFuncType,
WasmResult, WasmType,
DataIndex, ElemIndex, FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex,
Table, TableIndex, Tag, TagIndex, TypeIndex, WasmError, WasmFuncType, WasmResult, WasmType,
};
use core::convert::From;
use cranelift_codegen::cursor::FuncCursor;
@@ -531,22 +530,6 @@ pub trait ModuleEnvironment<'data> {
/// Declares a function signature to the environment.
fn declare_type_func(&mut self, wasm_func_type: WasmFuncType) -> WasmResult<()>;
/// Declares a module type signature to the environment.
fn declare_type_module(
&mut self,
imports: &[(&'data str, Option<&'data str>, EntityType)],
exports: &[(&'data str, EntityType)],
) -> WasmResult<()> {
drop((imports, exports));
Err(WasmError::Unsupported("module linking".to_string()))
}
/// Declares an instance type signature to the environment.
fn declare_type_instance(&mut self, exports: &[(&'data str, EntityType)]) -> WasmResult<()> {
drop(exports);
Err(WasmError::Unsupported("module linking".to_string()))
}
/// Translates a type index to its signature index, only called for type
/// indices which point to functions.
fn type_to_signature(&self, index: TypeIndex) -> WasmResult<SignatureIndex> {
@@ -565,7 +548,7 @@ pub trait ModuleEnvironment<'data> {
&mut self,
index: TypeIndex,
module: &'data str,
field: Option<&'data str>,
field: &'data str,
) -> WasmResult<()>;
/// Declares a table import to the environment.
@@ -573,7 +556,7 @@ pub trait ModuleEnvironment<'data> {
&mut self,
table: Table,
module: &'data str,
field: Option<&'data str>,
field: &'data str,
) -> WasmResult<()>;
/// Declares a memory import to the environment.
@@ -581,7 +564,7 @@ pub trait ModuleEnvironment<'data> {
&mut self,
memory: Memory,
module: &'data str,
field: Option<&'data str>,
field: &'data str,
) -> WasmResult<()>;
/// Declares an tag import to the environment.
@@ -589,7 +572,7 @@ pub trait ModuleEnvironment<'data> {
&mut self,
tag: Tag,
module: &'data str,
field: Option<&'data str>,
field: &'data str,
) -> WasmResult<()> {
drop((tag, module, field));
Err(WasmError::Unsupported("wasm tags".to_string()))
@@ -600,20 +583,9 @@ pub trait ModuleEnvironment<'data> {
&mut self,
global: Global,
module: &'data str,
field: Option<&'data str>,
field: &'data str,
) -> WasmResult<()>;
/// Declares a module import to the environment.
fn declare_module_import(
&mut self,
ty_index: TypeIndex,
module: &'data str,
field: Option<&'data str>,
) -> WasmResult<()> {
drop((ty_index, module, field));
Err(WasmError::Unsupported("module linking".to_string()))
}
/// Notifies the implementation that all imports have been declared.
fn finish_imports(&mut self) -> WasmResult<()> {
Ok(())

View File

@@ -8,7 +8,6 @@ use crate::code_translator::{bitcast_arguments, translate_operator, wasm_param_t
use crate::environ::{FuncEnvironment, ReturnMode};
use crate::state::FuncTranslationState;
use crate::translation_utils::get_vmctx_value_label;
use crate::wasm_unsupported;
use crate::WasmResult;
use core::convert::TryInto;
use cranelift_codegen::entity::EntityRef;
@@ -206,7 +205,6 @@ fn declare_locals<FE: FuncEnvironment + ?Sized>(
ExternRef | FuncRef => {
environ.translate_ref_null(builder.cursor(), wasm_type.try_into()?)?
}
ty => return Err(wasm_unsupported!("unsupported local type {:?}", ty)),
};
let ty = builder.func.dfg.value_type(zeroval);

View File

@@ -20,16 +20,19 @@ pub fn translate_module<'data>(
) -> WasmResult<ModuleTranslationState> {
let _tt = timing::wasm_translate_module();
let mut module_translation_state = ModuleTranslationState::new();
let mut validator = Validator::new();
validator.wasm_features(environ.wasm_features());
let mut validator = Validator::new_with_features(environ.wasm_features());
for payload in Parser::new(0).parse_all(data) {
match payload? {
Payload::Version { num, range } => {
validator.version(num, &range)?;
Payload::Version {
num,
encoding,
range,
} => {
validator.version(num, encoding, &range)?;
}
Payload::End => {
validator.end()?;
Payload::End(offset) => {
validator.end(offset)?;
}
Payload::TypeSection(types) => {
@@ -88,7 +91,7 @@ pub fn translate_module<'data>(
}
Payload::CodeSectionEntry(body) => {
let func_validator = validator.code_section_entry()?;
let func_validator = validator.code_section_entry(&body)?;
environ.define_function_body(func_validator, body)?;
}
@@ -104,28 +107,6 @@ pub fn translate_module<'data>(
environ.reserve_passive_data(count)?;
}
Payload::InstanceSection(s) => {
validator.instance_section(&s)?;
unimplemented!();
}
Payload::AliasSection(s) => {
validator.alias_section(&s)?;
unimplemented!();
}
Payload::ModuleSectionStart {
count,
range,
size: _,
} => {
validator.module_section_start(count, &range)?;
unimplemented!();
}
Payload::ModuleSectionEntry { .. } => {
validator.module_section_entry();
unimplemented!();
}
Payload::CustomSection {
name: "name",
data,
@@ -142,9 +123,9 @@ pub fn translate_module<'data>(
Payload::CustomSection { name, data, .. } => environ.custom_section(name, data)?,
Payload::UnknownSection { id, range, .. } => {
validator.unknown_section(id, &range)?;
unreachable!();
other => {
validator.payload(&other)?;
panic!("unimplemented section {:?}", other);
}
}
}

View File

@@ -11,8 +11,8 @@ use crate::environ::ModuleEnvironment;
use crate::state::ModuleTranslationState;
use crate::wasm_unsupported;
use crate::{
DataIndex, ElemIndex, EntityType, FuncIndex, Global, GlobalIndex, GlobalInit, Memory,
MemoryIndex, Table, TableIndex, Tag, TagIndex, TypeIndex, WasmError, WasmResult,
DataIndex, ElemIndex, FuncIndex, Global, GlobalIndex, GlobalInit, Memory, MemoryIndex, Table,
TableIndex, Tag, TagIndex, TypeIndex, WasmError, WasmResult,
};
use core::convert::TryFrom;
use core::convert::TryInto;
@@ -23,31 +23,11 @@ use std::vec::Vec;
use wasmparser::{
self, Data, DataKind, DataSectionReader, Element, ElementItem, ElementItems, ElementKind,
ElementSectionReader, Export, ExportSectionReader, ExternalKind, FunctionSectionReader,
GlobalSectionReader, GlobalType, ImportSectionEntryType, ImportSectionReader,
MemorySectionReader, MemoryType, NameSectionReader, Naming, Operator, TableSectionReader,
TableType, TagSectionReader, TagType, TypeDef, TypeSectionReader,
GlobalSectionReader, GlobalType, ImportSectionReader, MemorySectionReader, MemoryType,
NameSectionReader, Naming, Operator, TableSectionReader, TableType, TagSectionReader, TagType,
TypeDef, TypeRef, TypeSectionReader,
};
fn entity_type(
ty: ImportSectionEntryType,
environ: &mut dyn ModuleEnvironment<'_>,
) -> WasmResult<EntityType> {
Ok(match ty {
ImportSectionEntryType::Function(sig) => {
EntityType::Function(environ.type_to_signature(TypeIndex::from_u32(sig))?)
}
ImportSectionEntryType::Memory(ty) => EntityType::Memory(memory(ty)),
ImportSectionEntryType::Tag(t) => EntityType::Tag(tag(t)),
ImportSectionEntryType::Global(ty) => EntityType::Global(global(ty, GlobalInit::Import)?),
ImportSectionEntryType::Table(ty) => EntityType::Table(table(ty)?),
// doesn't get past validation
ImportSectionEntryType::Module(_) | ImportSectionEntryType::Instance(_) => {
unreachable!()
}
})
}
fn memory(ty: MemoryType) -> Memory {
Memory {
minimum: ty.initial,
@@ -58,8 +38,10 @@ fn memory(ty: MemoryType) -> Memory {
}
fn tag(e: TagType) -> Tag {
Tag {
ty: TypeIndex::from_u32(e.type_index),
match e.kind {
wasmparser::TagKind::Exception => Tag {
ty: TypeIndex::from_u32(e.func_type_idx),
},
}
}
@@ -97,27 +79,6 @@ pub fn parse_type_section<'a>(
.wasm_types
.push((wasm_func_ty.params, wasm_func_ty.returns));
}
TypeDef::Module(t) => {
let imports = t
.imports
.iter()
.map(|i| Ok((i.module, i.field, entity_type(i.ty, environ)?)))
.collect::<WasmResult<Vec<_>>>()?;
let exports = t
.exports
.iter()
.map(|e| Ok((e.name, entity_type(e.ty, environ)?)))
.collect::<WasmResult<Vec<_>>>()?;
environ.declare_type_module(&imports, &exports)?;
}
TypeDef::Instance(t) => {
let exports = t
.exports
.iter()
.map(|e| Ok((e.name, entity_type(e.ty, environ)?)))
.collect::<WasmResult<Vec<_>>>()?;
environ.declare_type_instance(&exports)?;
}
}
}
Ok(())
@@ -133,30 +94,26 @@ pub fn parse_import_section<'data>(
for entry in imports {
let import = entry?;
match import.ty {
ImportSectionEntryType::Function(sig) => {
TypeRef::Func(sig) => {
environ.declare_func_import(
TypeIndex::from_u32(sig),
import.module,
import.field,
import.name,
)?;
}
ImportSectionEntryType::Memory(ty) => {
environ.declare_memory_import(memory(ty), import.module, import.field)?;
TypeRef::Memory(ty) => {
environ.declare_memory_import(memory(ty), import.module, import.name)?;
}
ImportSectionEntryType::Tag(e) => {
environ.declare_tag_import(tag(e), import.module, import.field)?;
TypeRef::Tag(e) => {
environ.declare_tag_import(tag(e), import.module, import.name)?;
}
ImportSectionEntryType::Global(ty) => {
TypeRef::Global(ty) => {
let ty = global(ty, GlobalInit::Import)?;
environ.declare_global_import(ty, import.module, import.field)?;
environ.declare_global_import(ty, import.module, import.name)?;
}
ImportSectionEntryType::Table(ty) => {
TypeRef::Table(ty) => {
let ty = table(ty)?;
environ.declare_table_import(ty, import.module, import.field)?;
}
ImportSectionEntryType::Module(_) | ImportSectionEntryType::Instance(_) => {
unimplemented!()
environ.declare_table_import(ty, import.module, import.name)?;
}
}
}
@@ -279,7 +236,7 @@ pub fn parse_export_section<'data>(
for entry in exports {
let Export {
field,
name,
ref kind,
index,
} = entry?;
@@ -289,18 +246,11 @@ pub fn parse_export_section<'data>(
// becomes a concern here.
let index = index as usize;
match *kind {
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::Tag => environ.declare_tag_export(TagIndex::new(index), field)?,
ExternalKind::Global => {
environ.declare_global_export(GlobalIndex::new(index), field)?
}
// this never gets past validation
ExternalKind::Module | ExternalKind::Instance | ExternalKind::Type => unreachable!(),
ExternalKind::Func => 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::Tag => environ.declare_tag_export(TagIndex::new(index), name)?,
ExternalKind::Global => environ.declare_global_export(GlobalIndex::new(index), name)?,
}
}

View File

@@ -58,7 +58,7 @@ pub enum ControlStackFrame {
num_return_values: usize,
original_stack_size: usize,
exit_is_branched_to: bool,
blocktype: wasmparser::TypeOrFuncType,
blocktype: wasmparser::BlockType,
/// Was the head of the `if` reachable?
head_is_reachable: bool,
/// What was the reachability at the end of the consequent?
@@ -411,7 +411,7 @@ impl FuncTranslationState {
else_data: ElseData,
num_param_types: usize,
num_result_types: usize,
blocktype: wasmparser::TypeOrFuncType,
blocktype: wasmparser::BlockType,
) {
debug_assert!(num_param_types <= self.stack.len());

View File

@@ -1,6 +1,5 @@
//! Helper functions and structures for the translation.
use crate::environ::TargetEnvironment;
use crate::wasm_unsupported;
use crate::WasmResult;
use core::convert::TryInto;
use core::u32;
@@ -34,7 +33,6 @@ pub fn type_to_type<PE: TargetEnvironment + ?Sized>(
wasmparser::Type::ExternRef | wasmparser::Type::FuncRef => {
Ok(environ.reference_type(ty.try_into()?))
}
ty => Err(wasm_unsupported!("type_to_type: wasm type {:?}", ty)),
}
}
@@ -52,17 +50,13 @@ pub fn tabletype_to_type<PE: TargetEnvironment + ?Sized>(
wasmparser::Type::V128 => Ok(Some(ir::types::I8X16)),
wasmparser::Type::ExternRef => Ok(Some(environ.reference_type(ty.try_into()?))),
wasmparser::Type::FuncRef => Ok(None),
ty => Err(wasm_unsupported!(
"tabletype_to_type: table wasm type {:?}",
ty
)),
}
}
/// Get the parameter and result types for the given Wasm blocktype.
pub fn blocktype_params_results<'a, T>(
validator: &'a FuncValidator<T>,
ty_or_ft: wasmparser::TypeOrFuncType,
ty: wasmparser::BlockType,
) -> WasmResult<(
impl ExactSizeIterator<Item = wasmparser::Type> + Clone + 'a,
impl ExactSizeIterator<Item = wasmparser::Type> + Clone + 'a,
@@ -70,26 +64,32 @@ pub fn blocktype_params_results<'a, T>(
where
T: WasmModuleResources,
{
return Ok(match ty_or_ft {
wasmparser::TypeOrFuncType::Type(ty) => {
let (params, results): (&'static [wasmparser::Type], &'static [wasmparser::Type]) =
match ty {
wasmparser::Type::I32 => (&[], &[wasmparser::Type::I32]),
wasmparser::Type::I64 => (&[], &[wasmparser::Type::I64]),
wasmparser::Type::F32 => (&[], &[wasmparser::Type::F32]),
wasmparser::Type::F64 => (&[], &[wasmparser::Type::F64]),
wasmparser::Type::V128 => (&[], &[wasmparser::Type::V128]),
wasmparser::Type::ExternRef => (&[], &[wasmparser::Type::ExternRef]),
wasmparser::Type::FuncRef => (&[], &[wasmparser::Type::FuncRef]),
wasmparser::Type::EmptyBlockType => (&[], &[]),
ty => return Err(wasm_unsupported!("blocktype_params_results: type {:?}", ty)),
};
return Ok(match ty {
wasmparser::BlockType::Empty => {
let params: &'static [wasmparser::Type] = &[];
let results: &'static [wasmparser::Type] = &[];
(
itertools::Either::Left(params.iter().copied()),
itertools::Either::Left(results.iter().copied()),
)
}
wasmparser::TypeOrFuncType::FuncType(ty_index) => {
wasmparser::BlockType::Type(ty) => {
let params: &'static [wasmparser::Type] = &[];
let results: &'static [wasmparser::Type] = match ty {
wasmparser::Type::I32 => &[wasmparser::Type::I32],
wasmparser::Type::I64 => &[wasmparser::Type::I64],
wasmparser::Type::F32 => &[wasmparser::Type::F32],
wasmparser::Type::F64 => &[wasmparser::Type::F64],
wasmparser::Type::V128 => &[wasmparser::Type::V128],
wasmparser::Type::ExternRef => &[wasmparser::Type::ExternRef],
wasmparser::Type::FuncRef => &[wasmparser::Type::FuncRef],
};
(
itertools::Either::Left(params.iter().copied()),
itertools::Either::Left(results.iter().copied()),
)
}
wasmparser::BlockType::FuncType(ty_index) => {
let ty = validator
.resources()
.func_type_at(ty_index)
@@ -129,12 +129,6 @@ pub fn block_with_params<PE: TargetEnvironment + ?Sized>(
wasmparser::Type::V128 => {
builder.append_block_param(block, ir::types::I8X16);
}
ty => {
return Err(wasm_unsupported!(
"block_with_params: type {:?} in multi-value block's signature",
ty
))
}
}
}
Ok(block)