Propagate module-linking types to wasmtime (#2115)

This commit adds lots of plumbing to get the type section from the
module linking proposal plumbed all the way through to the `wasmtime`
crate and the `wasmtime-c-api` crate. This isn't all that useful right
now because Wasmtime doesn't support imported/exported
modules/instances, but this is all necessary groundwork to getting that
exported at some point. I've added some light tests but I suspect the
bulk of the testing will come in a future commit.

One major change in this commit is that `SignatureIndex` no longer
follows type type index space in a wasm module. Instead a new
`TypeIndex` type is used to track that. Function signatures, still
indexed by `SignatureIndex`, are then packed together tightly.
This commit is contained in:
Alex Crichton
2020-11-06 14:48:09 -06:00
committed by GitHub
parent 77827a48a9
commit 73cda83548
27 changed files with 782 additions and 213 deletions

View File

@@ -3,7 +3,7 @@ use crate::{Engine, Export, Extern, Func, Global, Memory, Module, Store, Table,
use anyhow::{anyhow, bail, Context, Error, Result};
use std::any::Any;
use std::mem;
use wasmtime_environ::EntityIndex;
use wasmtime_environ::wasm::EntityIndex;
use wasmtime_jit::CompiledModule;
use wasmtime_runtime::{
Imports, InstantiationError, StackMapRegistry, VMContext, VMExternRefActivationsTable,
@@ -300,6 +300,10 @@ fn with_imports<R>(
}
functions.push(func.vmimport());
}
// FIXME(#2094)
EntityIndex::Module(_i) => unimplemented!(),
EntityIndex::Instance(_i) => unimplemented!(),
}
Ok(())
};

View File

@@ -516,6 +516,10 @@ impl Linker {
ExternType::Global(f) => ImportKind::Global(f),
ExternType::Memory(_) => ImportKind::Memory,
ExternType::Table(_) => ImportKind::Table,
// FIXME(#2094)
ExternType::Module(_) => unimplemented!(),
ExternType::Instance(_) => unimplemented!(),
}
}

View File

@@ -84,7 +84,12 @@ impl Config {
profiler: Arc::new(NullProfilerAgent),
memory_creator: None,
max_wasm_stack: 1 << 20,
features: WasmFeatures::default(),
features: WasmFeatures {
reference_types: true,
bulk_memory: true,
multi_value: true,
..WasmFeatures::default()
},
}
}
@@ -655,6 +660,7 @@ impl fmt::Debug for Config {
.field("wasm_bulk_memory", &self.features.bulk_memory)
.field("wasm_simd", &self.features.simd)
.field("wasm_multi_value", &self.features.multi_value)
.field("wasm_module_linking", &self.features.module_linking)
.field(
"flags",
&settings::Flags::new(self.flags.clone()).to_string(),

View File

@@ -10,7 +10,7 @@ use std::mem;
use std::panic::{self, AssertUnwindSafe};
use wasmtime_environ::entity::PrimaryMap;
use wasmtime_environ::isa::TargetIsa;
use wasmtime_environ::{ir, CompiledFunction, EntityIndex, Module};
use wasmtime_environ::{ir, wasm, CompiledFunction, Module};
use wasmtime_jit::trampoline::ir::{
ExternalName, Function, InstBuilder, MemFlags, StackSlotData, StackSlotKind,
};
@@ -227,7 +227,7 @@ pub fn create_handle_with_function(
let func_id = module.functions.push(sig_id);
module
.exports
.insert(String::new(), EntityIndex::Function(func_id));
.insert(String::new(), wasm::EntityIndex::Function(func_id));
let trampoline = make_trampoline(isa.as_ref(), &mut code_memory, &mut fn_builder_ctx, &sig);
finished_functions.push(trampoline);
@@ -274,7 +274,7 @@ pub unsafe fn create_handle_with_raw_function(
let func_id = module.functions.push(sig_id);
module
.exports
.insert(String::new(), EntityIndex::Function(func_id));
.insert(String::new(), wasm::EntityIndex::Function(func_id));
finished_functions.push(func);
store.signatures().borrow_mut().register(wft, trampoline);

View File

@@ -3,7 +3,7 @@ use crate::trampoline::StoreInstanceHandle;
use crate::{GlobalType, Mutability, Store, Val};
use anyhow::Result;
use wasmtime_environ::entity::PrimaryMap;
use wasmtime_environ::{wasm, EntityIndex, Module};
use wasmtime_environ::{wasm, Module};
use wasmtime_runtime::VMFunctionImport;
pub fn create_global(store: &Store, gt: &GlobalType, val: Val) -> Result<StoreInstanceHandle> {
@@ -43,9 +43,11 @@ pub fn create_global(store: &Store, gt: &GlobalType, val: Val) -> Result<StoreIn
let local_sig_index = module.signatures.push(wasm.clone());
let func_index = module.functions.push(local_sig_index);
module.num_imported_funcs = 1;
module
.imports
.push(("".into(), "".into(), EntityIndex::Function(func_index)));
module.imports.push((
"".into(),
"".into(),
wasm::EntityIndex::Function(func_index),
));
let f = f.caller_checked_anyfunc();
let f = unsafe { f.as_ref() };
@@ -63,7 +65,7 @@ pub fn create_global(store: &Store, gt: &GlobalType, val: Val) -> Result<StoreIn
let global_id = module.globals.push(global);
module
.exports
.insert(String::new(), EntityIndex::Global(global_id));
.insert(String::new(), wasm::EntityIndex::Global(global_id));
let handle = create_handle(
module,
store,

View File

@@ -5,7 +5,7 @@ use crate::Store;
use crate::{Limits, MemoryType};
use anyhow::Result;
use wasmtime_environ::entity::PrimaryMap;
use wasmtime_environ::{wasm, EntityIndex, MemoryPlan, MemoryStyle, Module, WASM_PAGE_SIZE};
use wasmtime_environ::{wasm, MemoryPlan, MemoryStyle, Module, WASM_PAGE_SIZE};
use wasmtime_runtime::{RuntimeLinearMemory, RuntimeMemoryCreator, VMMemoryDefinition};
use std::sync::Arc;
@@ -27,7 +27,7 @@ pub fn create_handle_with_memory(
let memory_id = module.memory_plans.push(memory_plan);
module
.exports
.insert(String::new(), EntityIndex::Memory(memory_id));
.insert(String::new(), wasm::EntityIndex::Memory(memory_id));
create_handle(module, store, PrimaryMap::new(), Box::new(()), &[])
}

View File

@@ -4,7 +4,7 @@ use crate::Store;
use crate::{TableType, ValType};
use anyhow::{bail, Result};
use wasmtime_environ::entity::PrimaryMap;
use wasmtime_environ::{wasm, EntityIndex, Module};
use wasmtime_environ::{wasm, Module};
pub fn create_handle_with_table(store: &Store, table: &TableType) -> Result<StoreInstanceHandle> {
let mut module = Module::new();
@@ -25,7 +25,7 @@ pub fn create_handle_with_table(store: &Store, table: &TableType) -> Result<Stor
let table_id = module.table_plans.push(table_plan);
module
.exports
.insert(String::new(), EntityIndex::Table(table_id));
.insert(String::new(), wasm::EntityIndex::Table(table_id));
create_handle(module, store, PrimaryMap::new(), Box::new(()), &[])
}

View File

@@ -1,6 +1,6 @@
use std::fmt;
use wasmtime_environ::wasm::WasmFuncType;
use wasmtime_environ::{ir, wasm, EntityIndex};
use wasmtime_environ::{ir, wasm};
// Type Representations
@@ -144,7 +144,7 @@ impl ValType {
///
/// This list can be found in [`ImportType`] or [`ExportType`], so these types
/// can either be imported or exported.
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
#[derive(Debug, Clone)]
pub enum ExternType {
/// This external type is the type of a WebAssembly function.
Func(FuncType),
@@ -154,6 +154,10 @@ pub enum ExternType {
Table(TableType),
/// This external type is the type of a WebAssembly memory.
Memory(MemoryType),
/// This external type is the type of a WebAssembly instance.
Instance(InstanceType),
/// This external type is the type of a WebAssembly module.
Module(ModuleType),
}
macro_rules! accessors {
@@ -186,6 +190,39 @@ impl ExternType {
(Global(GlobalType) global unwrap_global)
(Table(TableType) table unwrap_table)
(Memory(MemoryType) memory unwrap_memory)
(Module(ModuleType) module unwrap_module)
(Instance(InstanceType) instance unwrap_instance)
}
fn from_wasmtime(
module: &wasmtime_environ::Module,
ty: &wasmtime_environ::wasm::EntityType,
) -> ExternType {
use wasmtime_environ::wasm::EntityType;
match ty {
EntityType::Function(idx) => {
let sig = module.types[*idx].unwrap_function();
let sig = &module.signatures[sig];
FuncType::from_wasm_func_type(sig).into()
}
EntityType::Global(ty) => GlobalType::from_wasmtime_global(ty).into(),
EntityType::Memory(ty) => MemoryType::from_wasmtime_memory(ty).into(),
EntityType::Table(ty) => TableType::from_wasmtime_table(ty).into(),
EntityType::Module(ty) => {
let (imports, exports) = match &module.types[*ty] {
wasmtime_environ::ModuleType::Module { imports, exports } => (imports, exports),
_ => unreachable!("not possible in valid wasm modules"),
};
ModuleType::from_wasmtime(module, imports, exports).into()
}
EntityType::Instance(ty) => {
let exports = match &module.types[*ty] {
wasmtime_environ::ModuleType::Instance { exports } => exports,
_ => unreachable!("not possible in valid wasm modules"),
};
InstanceType::from_wasmtime(module, exports).into()
}
}
}
}
@@ -213,6 +250,18 @@ impl From<TableType> for ExternType {
}
}
impl From<ModuleType> for ExternType {
fn from(ty: ModuleType) -> ExternType {
ExternType::Module(ty)
}
}
impl From<InstanceType> for ExternType {
fn from(ty: InstanceType) -> ExternType {
ExternType::Instance(ty)
}
}
/// A descriptor for a function in a WebAssembly module.
///
/// WebAssembly functions can have 0 or more parameters and results.
@@ -397,34 +446,198 @@ impl MemoryType {
}
}
// Module Types
/// A descriptor for a WebAssembly module type.
///
/// This is a part of the [WebAssembly module-linking proposal][proposal].
///
/// [proposal]: https://github.com/webassembly/module-linking
#[derive(Debug, Clone)]
pub struct ModuleType {
imports: Vec<(String, Option<String>, ExternType)>,
exports: Vec<(String, ExternType)>,
}
impl ModuleType {
/// Creates a new empty module type.
pub fn new() -> ModuleType {
ModuleType {
imports: Vec::new(),
exports: Vec::new(),
}
}
/// Adds a new export to this `ModuleType`.
pub fn add_named_export(&mut self, name: &str, ty: ExternType) {
self.exports.push((name.to_string(), ty));
}
/// Adds a new import to this `ModuleType`.
pub fn add_named_import(&mut self, module: &str, field: Option<&str>, ty: ExternType) {
self.imports
.push((module.to_string(), field.map(|f| f.to_string()), ty));
}
/// Returns the list of imports associated with this module type.
pub fn imports(&self) -> impl ExactSizeIterator<Item = ImportType<'_>> {
self.imports.iter().map(|(module, name, ty)| {
ImportType {
module,
// FIXME(#2094) should thread through the `Option`
name: name.as_ref().unwrap(),
ty: EntityOrExtern::Extern(ty),
}
})
}
/// Returns the list of exports associated with this module type.
pub fn exports(&self) -> impl ExactSizeIterator<Item = ExportType<'_>> {
self.exports.iter().map(|(name, ty)| ExportType {
name,
ty: EntityOrExtern::Extern(ty),
})
}
pub(crate) fn from_wasmtime(
module: &wasmtime_environ::Module,
imports: &[(String, Option<String>, wasmtime_environ::wasm::EntityType)],
exports: &[(String, wasmtime_environ::wasm::EntityType)],
) -> ModuleType {
ModuleType {
exports: exports
.iter()
.map(|(name, ty)| (name.to_string(), ExternType::from_wasmtime(module, ty)))
.collect(),
imports: imports
.iter()
.map(|(m, name, ty)| {
(
m.to_string(),
name.as_ref().map(|n| n.to_string()),
ExternType::from_wasmtime(module, ty),
)
})
.collect(),
}
}
}
// Instance Types
/// A descriptor for a WebAssembly instance type.
///
/// This is a part of the [WebAssembly module-linking proposal][proposal].
///
/// [proposal]: https://github.com/webassembly/module-linking
#[derive(Debug, Clone)]
pub struct InstanceType {
exports: Vec<(String, ExternType)>,
}
impl InstanceType {
/// Creates a new empty instance type.
pub fn new() -> InstanceType {
InstanceType {
exports: Vec::new(),
}
}
/// Adds a new export to this `ModuleType`.
pub fn add_named_export(&mut self, name: &str, ty: ExternType) {
self.exports.push((name.to_string(), ty));
}
/// Returns the list of exports associated with this module type.
pub fn exports(&self) -> impl ExactSizeIterator<Item = ExportType<'_>> {
self.exports.iter().map(|(name, ty)| ExportType {
name,
ty: EntityOrExtern::Extern(ty),
})
}
pub(crate) fn from_wasmtime(
module: &wasmtime_environ::Module,
exports: &[(String, wasmtime_environ::wasm::EntityType)],
) -> InstanceType {
InstanceType {
exports: exports
.iter()
.map(|(name, ty)| (name.to_string(), ExternType::from_wasmtime(module, ty)))
.collect(),
}
}
}
// Entity Types
#[derive(Clone, Hash, Eq, PartialEq)]
#[derive(Clone)]
pub(crate) enum EntityType<'module> {
Function(&'module wasm::WasmFuncType),
Table(&'module wasm::Table),
Memory(&'module wasm::Memory),
Global(&'module wasm::Global),
Module {
imports: &'module [(String, Option<String>, wasmtime_environ::wasm::EntityType)],
exports: &'module [(String, wasmtime_environ::wasm::EntityType)],
module: &'module wasmtime_environ::Module,
},
Instance {
exports: &'module [(String, wasmtime_environ::wasm::EntityType)],
module: &'module wasmtime_environ::Module,
},
}
impl<'module> EntityType<'module> {
/// Translate from a `EntityIndex` into an `ExternType`.
pub(crate) fn new(
entity_index: &EntityIndex,
entity_index: &wasm::EntityIndex,
module: &'module wasmtime_environ::Module,
) -> EntityType<'module> {
match entity_index {
EntityIndex::Function(func_index) => {
wasm::EntityIndex::Function(func_index) => {
let sig = module.wasm_func_type(*func_index);
EntityType::Function(&sig)
}
EntityIndex::Table(table_index) => {
wasm::EntityIndex::Table(table_index) => {
EntityType::Table(&module.table_plans[*table_index].table)
}
EntityIndex::Memory(memory_index) => {
wasm::EntityIndex::Memory(memory_index) => {
EntityType::Memory(&module.memory_plans[*memory_index].memory)
}
EntityIndex::Global(global_index) => EntityType::Global(&module.globals[*global_index]),
wasm::EntityIndex::Global(global_index) => {
EntityType::Global(&module.globals[*global_index])
}
wasm::EntityIndex::Module(idx) => {
let (imports, exports) = match &module.types[module.modules[*idx]] {
wasmtime_environ::ModuleType::Module { imports, exports } => (imports, exports),
_ => unreachable!("valid modules should never hit this"),
};
EntityType::Module {
imports,
exports,
module,
}
}
wasm::EntityIndex::Instance(idx) => {
// Get the type, either a pointer to an instance for an import
// or a module for an instantiation.
let ty = match module.instances[*idx] {
wasmtime_environ::Instance::Import(ty) => ty,
wasmtime_environ::Instance::Instantiate { module: idx, .. } => {
module.modules[idx]
}
};
// Get the exports of whatever our type specifies, ignoring
// imports in the module case since we're instantiating the
// module.
let exports = match &module.types[ty] {
wasmtime_environ::ModuleType::Instance { exports } => exports,
wasmtime_environ::ModuleType::Module { exports, .. } => exports,
_ => unreachable!("valid modules should never hit this"),
};
EntityType::Instance { exports, module }
}
}
}
@@ -435,6 +648,14 @@ impl<'module> EntityType<'module> {
EntityType::Table(table) => TableType::from_wasmtime_table(table).into(),
EntityType::Memory(memory) => MemoryType::from_wasmtime_memory(memory).into(),
EntityType::Global(global) => GlobalType::from_wasmtime_global(global).into(),
EntityType::Instance { exports, module } => {
InstanceType::from_wasmtime(module, exports).into()
}
EntityType::Module {
imports,
exports,
module,
} => ModuleType::from_wasmtime(module, imports, exports).into(),
}
}
}
@@ -447,7 +668,7 @@ impl<'module> EntityType<'module> {
/// [`Module::imports`](crate::Module::imports) API. Each [`ImportType`]
/// describes an import into the wasm module with the module/name that it's
/// imported from as well as the type of item that's being imported.
#[derive(Clone, Hash, Eq, PartialEq)]
#[derive(Clone)]
pub struct ImportType<'module> {
/// The module of the import.
module: &'module str,
@@ -456,7 +677,13 @@ pub struct ImportType<'module> {
name: &'module str,
/// The type of the import.
ty: EntityType<'module>,
ty: EntityOrExtern<'module>,
}
#[derive(Clone)]
enum EntityOrExtern<'a> {
Entity(EntityType<'a>),
Extern(&'a ExternType),
}
impl<'module> ImportType<'module> {
@@ -467,7 +694,11 @@ impl<'module> ImportType<'module> {
name: &'module str,
ty: EntityType<'module>,
) -> ImportType<'module> {
ImportType { module, name, ty }
ImportType {
module,
name,
ty: EntityOrExtern::Entity(ty),
}
}
/// Returns the module name that this import is expected to come from.
@@ -483,7 +714,10 @@ impl<'module> ImportType<'module> {
/// Returns the expected type of this import.
pub fn ty(&self) -> ExternType {
self.ty.extern_type()
match &self.ty {
EntityOrExtern::Entity(e) => e.extern_type(),
EntityOrExtern::Extern(e) => (*e).clone(),
}
}
}
@@ -505,20 +739,23 @@ impl<'module> fmt::Debug for ImportType<'module> {
/// [`Module::exports`](crate::Module::exports) accessor and describes what
/// names are exported from a wasm module and the type of the item that is
/// exported.
#[derive(Clone, Hash, Eq, PartialEq)]
#[derive(Clone)]
pub struct ExportType<'module> {
/// The name of the export.
name: &'module str,
/// The type of the export.
ty: EntityType<'module>,
ty: EntityOrExtern<'module>,
}
impl<'module> ExportType<'module> {
/// Creates a new export which is exported with the given `name` and has the
/// given `ty`.
pub(crate) fn new(name: &'module str, ty: EntityType<'module>) -> ExportType<'module> {
ExportType { name, ty }
ExportType {
name,
ty: EntityOrExtern::Entity(ty),
}
}
/// Returns the name by which this export is known.
@@ -528,7 +765,10 @@ impl<'module> ExportType<'module> {
/// Returns the type of this export.
pub fn ty(&self) -> ExternType {
self.ty.extern_type()
match &self.ty {
EntityOrExtern::Entity(e) => e.extern_type(),
EntityOrExtern::Extern(e) => (*e).clone(),
}
}
}