Move the signature registry into Engine.
This commit moves the shared signature registry out of `Store` and into `Engine`. This helps eliminate work that was performed whenever a `Module` was instantiated into a `Store`. Now a `Module` is registered with the shared signature registry upon creation, storing the mapping from the module's signature index space to the shared index space. This also refactors the "frame info" registry into a general purpose "module registry" that is used to look up trap information, signature information, and (soon) stack map information.
This commit is contained in:
@@ -753,7 +753,7 @@ pub struct StackMapRegistry {
|
|||||||
struct StackMapRegistryInner {
|
struct StackMapRegistryInner {
|
||||||
/// A map from the highest pc in a module, to its stack maps.
|
/// A map from the highest pc in a module, to its stack maps.
|
||||||
///
|
///
|
||||||
/// For details, see the comment above `GlobalFrameInfo::ranges`.
|
/// For details, see the comment above `GlobalModuleRegistry`.
|
||||||
ranges: BTreeMap<usize, ModuleStackMaps>,
|
ranges: BTreeMap<usize, ModuleStackMaps>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
//! use a thread-local to store information about how to unwind. Additionally
|
//! use a thread-local to store information about how to unwind. Additionally
|
||||||
//! this requires that the check of whether a pc is a wasm trap or not is a
|
//! this requires that the check of whether a pc is a wasm trap or not is a
|
||||||
//! global check rather than a per-thread check. This necessitates the existence
|
//! global check rather than a per-thread check. This necessitates the existence
|
||||||
//! of `GlobalFrameInfo` in the `wasmtime` crate.
|
//! of `GlobalModuleRegistry` in the `wasmtime` crate.
|
||||||
//!
|
//!
|
||||||
//! Otherwise this file heavily uses the `mach` Rust crate for type and
|
//! Otherwise this file heavily uses the `mach` Rust crate for type and
|
||||||
//! function declarations. Many bits and pieces are copied or translated from
|
//! function declarations. Many bits and pieces are copied or translated from
|
||||||
|
|||||||
@@ -319,6 +319,10 @@ impl HostFuncMap {
|
|||||||
fn async_required(&self) -> bool {
|
fn async_required(&self) -> bool {
|
||||||
self.funcs.values().any(|f| f.1)
|
self.funcs.values().any(|f| f.1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn iter(&self) -> impl Iterator<Item = &HostFunc> {
|
||||||
|
self.funcs.values().map(|v| &*v.0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! generate_wrap_async_host_func {
|
macro_rules! generate_wrap_async_host_func {
|
||||||
@@ -1318,6 +1322,10 @@ impl Config {
|
|||||||
|
|
||||||
for_each_function_signature!(generate_wrap_async_host_func);
|
for_each_function_signature!(generate_wrap_async_host_func);
|
||||||
|
|
||||||
|
pub(crate) fn host_funcs(&self) -> impl Iterator<Item = &HostFunc> {
|
||||||
|
self.host_funcs.iter()
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn get_host_func(&self, module: &str, name: &str) -> Option<&HostFunc> {
|
pub(crate) fn get_host_func(&self, module: &str, name: &str) -> Option<&HostFunc> {
|
||||||
self.host_funcs.get(module, name)
|
self.host_funcs.get(module, name)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,31 @@
|
|||||||
|
use crate::signatures::{SharedSignatures, SignatureRegistry, TrampolineMap};
|
||||||
use crate::Config;
|
use crate::Config;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::sync::Arc;
|
use std::collections::HashMap;
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
#[cfg(feature = "cache")]
|
#[cfg(feature = "cache")]
|
||||||
use wasmtime_cache::CacheConfig;
|
use wasmtime_cache::CacheConfig;
|
||||||
|
use wasmtime_environ::{
|
||||||
|
entity::PrimaryMap,
|
||||||
|
wasm::{SignatureIndex, WasmFuncType},
|
||||||
|
};
|
||||||
use wasmtime_jit::Compiler;
|
use wasmtime_jit::Compiler;
|
||||||
use wasmtime_runtime::{debug_builtins, InstanceAllocator};
|
use wasmtime_runtime::{
|
||||||
|
debug_builtins, InstanceAllocator, InstanceHandle, VMCallerCheckedAnyfunc,
|
||||||
|
VMSharedSignatureIndex, VMTrampoline,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct EngineHostFuncs {
|
||||||
|
anyfuncs: HashMap<InstanceHandle, Box<VMCallerCheckedAnyfunc>>,
|
||||||
|
trampolines: TrampolineMap,
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is safe for send and sync as it is read-only once the
|
||||||
|
// engine is constructed and the host functions live with the config,
|
||||||
|
// which the engine keeps a strong reference to.
|
||||||
|
unsafe impl Send for EngineHostFuncs {}
|
||||||
|
unsafe impl Sync for EngineHostFuncs {}
|
||||||
|
|
||||||
/// An `Engine` which is a global context for compilation and management of wasm
|
/// An `Engine` which is a global context for compilation and management of wasm
|
||||||
/// modules.
|
/// modules.
|
||||||
@@ -37,6 +58,16 @@ struct EngineInner {
|
|||||||
config: Config,
|
config: Config,
|
||||||
compiler: Compiler,
|
compiler: Compiler,
|
||||||
allocator: Box<dyn InstanceAllocator>,
|
allocator: Box<dyn InstanceAllocator>,
|
||||||
|
signatures: RwLock<SignatureRegistry>,
|
||||||
|
host_funcs: EngineHostFuncs,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for EngineInner {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let mut signatures = self.signatures.write().unwrap();
|
||||||
|
signatures.unregister(self.host_funcs.trampolines.indexes());
|
||||||
|
assert!(signatures.is_empty());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Engine {
|
impl Engine {
|
||||||
@@ -46,11 +77,29 @@ impl Engine {
|
|||||||
debug_builtins::ensure_exported();
|
debug_builtins::ensure_exported();
|
||||||
config.validate()?;
|
config.validate()?;
|
||||||
let allocator = config.build_allocator()?;
|
let allocator = config.build_allocator()?;
|
||||||
|
let mut signatures = SignatureRegistry::default();
|
||||||
|
let mut host_funcs = EngineHostFuncs::default();
|
||||||
|
|
||||||
|
// Register all the host function signatures
|
||||||
|
for func in config.host_funcs() {
|
||||||
|
let sig = signatures.register(func.ty.as_wasm_func_type());
|
||||||
|
|
||||||
|
// Cloning the instance handle is safe as host functions outlive the engine
|
||||||
|
host_funcs.anyfuncs.insert(
|
||||||
|
unsafe { func.instance.clone() },
|
||||||
|
Box::new(func.anyfunc(sig)),
|
||||||
|
);
|
||||||
|
|
||||||
|
host_funcs.trampolines.insert(sig, func.trampoline);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Engine {
|
Ok(Engine {
|
||||||
inner: Arc::new(EngineInner {
|
inner: Arc::new(EngineInner {
|
||||||
config: config.clone(),
|
config: config.clone(),
|
||||||
compiler: config.build_compiler(allocator.as_ref()),
|
compiler: config.build_compiler(allocator.as_ref()),
|
||||||
allocator,
|
allocator,
|
||||||
|
signatures: RwLock::new(signatures),
|
||||||
|
host_funcs,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -79,6 +128,53 @@ impl Engine {
|
|||||||
Arc::ptr_eq(&a.inner, &b.inner)
|
Arc::ptr_eq(&a.inner, &b.inner)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn register_module_signatures(
|
||||||
|
&self,
|
||||||
|
signatures: &PrimaryMap<SignatureIndex, WasmFuncType>,
|
||||||
|
trampolines: impl Iterator<Item = (SignatureIndex, VMTrampoline)>,
|
||||||
|
) -> (SharedSignatures, TrampolineMap) {
|
||||||
|
self.inner
|
||||||
|
.signatures
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.register_module(signatures, trampolines)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn register_signature(&self, ty: &WasmFuncType) -> VMSharedSignatureIndex {
|
||||||
|
self.inner.signatures.write().unwrap().register(ty)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn unregister_signatures(
|
||||||
|
&self,
|
||||||
|
indexes: impl Iterator<Item = VMSharedSignatureIndex>,
|
||||||
|
) {
|
||||||
|
self.inner.signatures.write().unwrap().unregister(indexes);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn lookup_func_type(&self, index: VMSharedSignatureIndex) -> Option<WasmFuncType> {
|
||||||
|
self.inner
|
||||||
|
.signatures
|
||||||
|
.read()
|
||||||
|
.unwrap()
|
||||||
|
.lookup_type(index)
|
||||||
|
.cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn host_func_trampolines(&self) -> &TrampolineMap {
|
||||||
|
&self.inner.host_funcs.trampolines
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn host_func_anyfunc(
|
||||||
|
&self,
|
||||||
|
instance: &InstanceHandle,
|
||||||
|
) -> Option<&VMCallerCheckedAnyfunc> {
|
||||||
|
self.inner
|
||||||
|
.host_funcs
|
||||||
|
.anyfuncs
|
||||||
|
.get(instance)
|
||||||
|
.map(AsRef::as_ref)
|
||||||
|
}
|
||||||
|
|
||||||
/// Ahead-of-time (AOT) compiles a WebAssembly module.
|
/// Ahead-of-time (AOT) compiles a WebAssembly module.
|
||||||
///
|
///
|
||||||
/// The `bytes` provided must be in one of two formats:
|
/// The `bytes` provided must be in one of two formats:
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::{sig_registry::SignatureRegistry, trampoline::StoreInstanceHandle};
|
use crate::trampoline::StoreInstanceHandle;
|
||||||
use crate::{Config, Extern, FuncType, Store, Trap, Val, ValType};
|
use crate::{Config, Extern, FuncType, Store, Trap, Val, ValType};
|
||||||
use anyhow::{bail, Context as _, Result};
|
use anyhow::{bail, Context as _, Result};
|
||||||
use smallvec::{smallvec, SmallVec};
|
use smallvec::{smallvec, SmallVec};
|
||||||
@@ -22,9 +22,9 @@ use wasmtime_runtime::{
|
|||||||
/// This differs from `Func` in that it is not associated with a `Store`.
|
/// This differs from `Func` in that it is not associated with a `Store`.
|
||||||
/// Host functions are associated with a `Config`.
|
/// Host functions are associated with a `Config`.
|
||||||
pub(crate) struct HostFunc {
|
pub(crate) struct HostFunc {
|
||||||
ty: FuncType,
|
pub ty: FuncType,
|
||||||
instance: InstanceHandle,
|
pub instance: InstanceHandle,
|
||||||
trampoline: VMTrampoline,
|
pub trampoline: VMTrampoline,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HostFunc {
|
impl HostFunc {
|
||||||
@@ -73,6 +73,23 @@ impl HostFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets a caller-checked anyfunc for this host function given a shared signature index.
|
||||||
|
///
|
||||||
|
/// The shared signature index must have been registered for the signature of
|
||||||
|
/// this host function.
|
||||||
|
pub fn anyfunc(&self, sig: VMSharedSignatureIndex) -> VMCallerCheckedAnyfunc {
|
||||||
|
let mut anyfunc = match self
|
||||||
|
.instance
|
||||||
|
.lookup_by_declaration(&EntityIndex::Function(FuncIndex::from_u32(0)))
|
||||||
|
{
|
||||||
|
wasmtime_runtime::Export::Function(f) => unsafe { f.anyfunc.as_ref() }.clone(),
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
anyfunc.type_index = sig;
|
||||||
|
anyfunc
|
||||||
|
}
|
||||||
|
|
||||||
/// Converts a `HostFunc` to a `Func`.
|
/// Converts a `HostFunc` to a `Func`.
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
@@ -88,11 +105,11 @@ impl HostFunc {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let export = ExportFunction {
|
let export = ExportFunction {
|
||||||
anyfunc: std::ptr::NonNull::new_unchecked(store.get_host_anyfunc(
|
anyfunc: store
|
||||||
&self.instance,
|
.engine()
|
||||||
&self.ty,
|
.host_func_anyfunc(&self.instance)
|
||||||
self.trampoline,
|
.unwrap()
|
||||||
)),
|
.into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
Func {
|
Func {
|
||||||
@@ -408,13 +425,9 @@ impl Func {
|
|||||||
Func::invoke(&store, &ty_clone, caller_vmctx, values_vec, &func)
|
Func::invoke(&store, &ty_clone, caller_vmctx, values_vec, &func)
|
||||||
});
|
});
|
||||||
|
|
||||||
let (instance, trampoline) = crate::trampoline::create_function(
|
let (instance, trampoline) =
|
||||||
&ty,
|
crate::trampoline::create_function(&ty, func, store.engine().config(), Some(store))
|
||||||
func,
|
.expect("failed to create function");
|
||||||
store.engine().config(),
|
|
||||||
Some(&mut store.signatures().borrow_mut()),
|
|
||||||
)
|
|
||||||
.expect("failed to create function");
|
|
||||||
|
|
||||||
let idx = EntityIndex::Function(FuncIndex::from_u32(0));
|
let idx = EntityIndex::Function(FuncIndex::from_u32(0));
|
||||||
let (instance, export) = match instance.lookup_by_declaration(&idx) {
|
let (instance, export) = match instance.lookup_by_declaration(&idx) {
|
||||||
@@ -734,7 +747,7 @@ impl Func {
|
|||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn wrap<Params, Results>(store: &Store, func: impl IntoFunc<Params, Results>) -> Func {
|
pub fn wrap<Params, Results>(store: &Store, func: impl IntoFunc<Params, Results>) -> Func {
|
||||||
let (_, instance, trampoline) = func.into_func(Some(&mut store.signatures().borrow_mut()));
|
let (_, instance, trampoline) = func.into_func(Some(store));
|
||||||
|
|
||||||
let (instance, export) = unsafe {
|
let (instance, export) = unsafe {
|
||||||
let idx = EntityIndex::Function(FuncIndex::from_u32(0));
|
let idx = EntityIndex::Function(FuncIndex::from_u32(0));
|
||||||
@@ -759,33 +772,35 @@ impl Func {
|
|||||||
|
|
||||||
/// Returns the underlying wasm type that this `Func` has.
|
/// Returns the underlying wasm type that this `Func` has.
|
||||||
pub fn ty(&self) -> FuncType {
|
pub fn ty(&self) -> FuncType {
|
||||||
// Signatures should always be registered in the store's registry of
|
// Signatures should always be registered in the engine's registry of
|
||||||
// shared signatures, so we should be able to unwrap safely here.
|
// shared signatures, so we should be able to unwrap safely here.
|
||||||
let signatures = self.instance.store.signatures().borrow();
|
FuncType::from_wasm_func_type(
|
||||||
let (wft, _) = signatures
|
self.instance
|
||||||
.lookup_shared(self.sig_index())
|
.store
|
||||||
.expect("signature should be registered");
|
.engine()
|
||||||
|
.lookup_func_type(self.sig_index())
|
||||||
// This is only called with `Export::Function`, and since it's coming
|
.expect("signature should be registered"),
|
||||||
// from wasmtime_runtime itself we should support all the types coming
|
)
|
||||||
// out of it, so assert such here.
|
|
||||||
FuncType::from_wasm_func_type(&wft)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of parameters that this function takes.
|
/// Returns the number of parameters that this function takes.
|
||||||
pub fn param_arity(&self) -> usize {
|
pub fn param_arity(&self) -> usize {
|
||||||
let signatures = self.instance.store.signatures().borrow();
|
let sig = self
|
||||||
let (sig, _) = signatures
|
.instance
|
||||||
.lookup_shared(self.sig_index())
|
.store
|
||||||
|
.engine()
|
||||||
|
.lookup_func_type(self.sig_index())
|
||||||
.expect("signature should be registered");
|
.expect("signature should be registered");
|
||||||
sig.params.len()
|
sig.params.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of results this function produces.
|
/// Returns the number of results this function produces.
|
||||||
pub fn result_arity(&self) -> usize {
|
pub fn result_arity(&self) -> usize {
|
||||||
let signatures = self.instance.store.signatures().borrow();
|
let sig = self
|
||||||
let (sig, _) = signatures
|
.instance
|
||||||
.lookup_shared(self.sig_index())
|
.store
|
||||||
|
.engine()
|
||||||
|
.lookup_func_type(self.sig_index())
|
||||||
.expect("signature should be registered");
|
.expect("signature should be registered");
|
||||||
sig.returns.len()
|
sig.returns.len()
|
||||||
}
|
}
|
||||||
@@ -907,21 +922,12 @@ impl Func {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) unsafe fn from_wasmtime_function(export: &ExportFunction, store: &Store) -> Self {
|
pub(crate) unsafe fn from_wasmtime_function(export: &ExportFunction, store: &Store) -> Self {
|
||||||
// Each function signature in a module should have a trampoline stored
|
|
||||||
// on that module as well, so unwrap the result here since otherwise
|
|
||||||
// it's a bug in wasmtime.
|
|
||||||
let anyfunc = export.anyfunc.as_ref();
|
let anyfunc = export.anyfunc.as_ref();
|
||||||
let trampoline = store
|
|
||||||
.signatures()
|
|
||||||
.borrow()
|
|
||||||
.lookup_shared(anyfunc.type_index)
|
|
||||||
.expect("failed to retrieve trampoline from module")
|
|
||||||
.1;
|
|
||||||
|
|
||||||
Func {
|
Func {
|
||||||
instance: store.existing_vmctx(anyfunc.vmctx),
|
instance: store.existing_vmctx(anyfunc.vmctx),
|
||||||
export: export.clone(),
|
export: export.clone(),
|
||||||
trampoline,
|
trampoline: store.lookup_trampoline(anyfunc.type_index),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1542,10 +1548,7 @@ for_each_function_signature!(impl_host_abi);
|
|||||||
/// as an implementation detail of this crate.
|
/// as an implementation detail of this crate.
|
||||||
pub trait IntoFunc<Params, Results> {
|
pub trait IntoFunc<Params, Results> {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn into_func(
|
fn into_func(self, store: Option<&Store>) -> (FuncType, InstanceHandle, VMTrampoline);
|
||||||
self,
|
|
||||||
registry: Option<&mut SignatureRegistry>,
|
|
||||||
) -> (FuncType, InstanceHandle, VMTrampoline);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A structure representing the *caller's* context when creating a function
|
/// A structure representing the *caller's* context when creating a function
|
||||||
@@ -1658,12 +1661,12 @@ macro_rules! impl_into_func {
|
|||||||
$($args: WasmTy,)*
|
$($args: WasmTy,)*
|
||||||
R: WasmRet,
|
R: WasmRet,
|
||||||
{
|
{
|
||||||
fn into_func(self, registry: Option<&mut SignatureRegistry>) -> (FuncType, InstanceHandle, VMTrampoline) {
|
fn into_func(self, store: Option<&Store>) -> (FuncType, InstanceHandle, VMTrampoline) {
|
||||||
let f = move |_: Caller<'_>, $($args:$args),*| {
|
let f = move |_: Caller<'_>, $($args:$args),*| {
|
||||||
self($($args),*)
|
self($($args),*)
|
||||||
};
|
};
|
||||||
|
|
||||||
f.into_func(registry)
|
f.into_func(store)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1674,7 +1677,7 @@ macro_rules! impl_into_func {
|
|||||||
$($args: WasmTy,)*
|
$($args: WasmTy,)*
|
||||||
R: WasmRet,
|
R: WasmRet,
|
||||||
{
|
{
|
||||||
fn into_func(self, registry: Option<&mut SignatureRegistry>) -> (FuncType, InstanceHandle, VMTrampoline) {
|
fn into_func(self, store: Option<&Store>) -> (FuncType, InstanceHandle, VMTrampoline) {
|
||||||
/// This shim is called by Wasm code, constructs a `Caller`,
|
/// This shim is called by Wasm code, constructs a `Caller`,
|
||||||
/// calls the wrapped host function, and returns the translated
|
/// calls the wrapped host function, and returns the translated
|
||||||
/// result back to Wasm.
|
/// result back to Wasm.
|
||||||
@@ -1807,10 +1810,10 @@ macro_rules! impl_into_func {
|
|||||||
|
|
||||||
let trampoline = host_trampoline::<$($args,)* R>;
|
let trampoline = host_trampoline::<$($args,)* R>;
|
||||||
|
|
||||||
// If not given a registry, use a default signature index that is guaranteed to trap
|
// If not given a store, use a default signature index that is guaranteed to trap.
|
||||||
// if the function is called indirectly without first being associated with a store (a bug condition).
|
// If the function is called indirectly without first being associated with a store (a bug condition).
|
||||||
let shared_signature_id = registry
|
let shared_signature_id = store
|
||||||
.map(|r| r.register(ty.as_wasm_func_type(), trampoline))
|
.map(|s| s.register_signature(ty.as_wasm_func_type(), trampoline))
|
||||||
.unwrap_or(VMSharedSignatureIndex::default());
|
.unwrap_or(VMSharedSignatureIndex::default());
|
||||||
|
|
||||||
let instance = unsafe {
|
let instance = unsafe {
|
||||||
|
|||||||
@@ -362,6 +362,7 @@ impl<'a> Instantiator<'a> {
|
|||||||
let expected_ty =
|
let expected_ty =
|
||||||
self.cur.module.compiled_module().module().type_of(*index);
|
self.cur.module.compiled_module().module().type_of(*index);
|
||||||
matching::MatchCx {
|
matching::MatchCx {
|
||||||
|
signatures: self.cur.module.signatures(),
|
||||||
types: self.cur.module.types(),
|
types: self.cur.module.types(),
|
||||||
store: self.store,
|
store: self.store,
|
||||||
}
|
}
|
||||||
@@ -513,14 +514,12 @@ impl<'a> Instantiator<'a> {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let engine = self.store.engine();
|
let engine = self.store.engine();
|
||||||
let allocator = engine.allocator();
|
let allocator = engine.allocator();
|
||||||
let signatures = self.store.signatures().borrow();
|
|
||||||
let signatures = signatures.lookup_table(&self.cur.module);
|
|
||||||
|
|
||||||
let instance = allocator.allocate(InstanceAllocationRequest {
|
let instance = allocator.allocate(InstanceAllocationRequest {
|
||||||
module: compiled_module.module().clone(),
|
module: compiled_module.module().clone(),
|
||||||
finished_functions: compiled_module.finished_functions(),
|
finished_functions: compiled_module.finished_functions(),
|
||||||
imports: self.cur.build(),
|
imports: self.cur.build(),
|
||||||
shared_signatures: (&signatures).into(),
|
shared_signatures: self.cur.module.signatures().into(),
|
||||||
host_state: Box::new(()),
|
host_state: Box::new(()),
|
||||||
interrupts: self.store.interrupts(),
|
interrupts: self.store.interrupts(),
|
||||||
externref_activations_table: self.store.externref_activations_table()
|
externref_activations_table: self.store.externref_activations_table()
|
||||||
|
|||||||
@@ -282,13 +282,12 @@ mod func;
|
|||||||
mod config;
|
mod config;
|
||||||
mod engine;
|
mod engine;
|
||||||
mod externals;
|
mod externals;
|
||||||
mod frame_info;
|
|
||||||
mod instance;
|
mod instance;
|
||||||
mod linker;
|
mod linker;
|
||||||
mod memory;
|
mod memory;
|
||||||
mod module;
|
mod module;
|
||||||
mod r#ref;
|
mod r#ref;
|
||||||
mod sig_registry;
|
mod signatures;
|
||||||
mod store;
|
mod store;
|
||||||
mod trampoline;
|
mod trampoline;
|
||||||
mod trap;
|
mod trap;
|
||||||
@@ -298,12 +297,11 @@ mod values;
|
|||||||
pub use crate::config::*;
|
pub use crate::config::*;
|
||||||
pub use crate::engine::*;
|
pub use crate::engine::*;
|
||||||
pub use crate::externals::*;
|
pub use crate::externals::*;
|
||||||
pub use crate::frame_info::{FrameInfo, FrameSymbol};
|
|
||||||
pub use crate::func::*;
|
pub use crate::func::*;
|
||||||
pub use crate::instance::Instance;
|
pub use crate::instance::Instance;
|
||||||
pub use crate::linker::*;
|
pub use crate::linker::*;
|
||||||
pub use crate::memory::*;
|
pub use crate::memory::*;
|
||||||
pub use crate::module::Module;
|
pub use crate::module::{FrameInfo, FrameSymbol, Module};
|
||||||
pub use crate::r#ref::ExternRef;
|
pub use crate::r#ref::ExternRef;
|
||||||
pub use crate::store::*;
|
pub use crate::store::*;
|
||||||
pub use crate::trap::*;
|
pub use crate::trap::*;
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
use crate::types::{ExportType, ExternType, ImportType};
|
use crate::{
|
||||||
|
signatures::{SharedSignatures, TrampolineMap},
|
||||||
|
types::{ExportType, ExternType, ImportType},
|
||||||
|
};
|
||||||
use crate::{Engine, ModuleType};
|
use crate::{Engine, ModuleType};
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@@ -8,13 +11,36 @@ use wasmparser::Validator;
|
|||||||
#[cfg(feature = "cache")]
|
#[cfg(feature = "cache")]
|
||||||
use wasmtime_cache::ModuleCacheEntry;
|
use wasmtime_cache::ModuleCacheEntry;
|
||||||
use wasmtime_environ::entity::PrimaryMap;
|
use wasmtime_environ::entity::PrimaryMap;
|
||||||
use wasmtime_environ::wasm::ModuleIndex;
|
use wasmtime_environ::wasm::{ModuleIndex, SignatureIndex};
|
||||||
use wasmtime_jit::{CompilationArtifacts, CompiledModule, TypeTables};
|
use wasmtime_jit::{CompilationArtifacts, CompiledModule, TypeTables};
|
||||||
|
use wasmtime_runtime::VMSharedSignatureIndex;
|
||||||
|
|
||||||
|
mod registry;
|
||||||
mod serialization;
|
mod serialization;
|
||||||
|
|
||||||
|
pub use registry::{FrameInfo, FrameSymbol, GlobalModuleRegistry, ModuleRegistry};
|
||||||
pub use serialization::SerializedModule;
|
pub use serialization::SerializedModule;
|
||||||
|
|
||||||
|
// A wrapper around registered signatures and trampolines that will automatically
|
||||||
|
/// unregister the signatures when dropped.
|
||||||
|
pub(crate) struct ModuleSharedSignatures {
|
||||||
|
engine: Engine,
|
||||||
|
signatures: SharedSignatures,
|
||||||
|
trampolines: TrampolineMap,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for ModuleSharedSignatures {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if !self.signatures.is_empty() {
|
||||||
|
// Use the shared signatures map to unregister as not every registered
|
||||||
|
// signature will have a trampoline, but every index in the trampoline map
|
||||||
|
// will be present in the shared signatures map.
|
||||||
|
self.engine
|
||||||
|
.unregister_signatures(self.signatures.values().cloned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A compiled WebAssembly module, ready to be instantiated.
|
/// A compiled WebAssembly module, ready to be instantiated.
|
||||||
///
|
///
|
||||||
/// A `Module` is a compiled in-memory representation of an input WebAssembly
|
/// A `Module` is a compiled in-memory representation of an input WebAssembly
|
||||||
@@ -102,6 +128,8 @@ struct ModuleInner {
|
|||||||
/// Type information of this module and all `artifact_upvars` compiled
|
/// Type information of this module and all `artifact_upvars` compiled
|
||||||
/// modules.
|
/// modules.
|
||||||
types: Arc<TypeTables>,
|
types: Arc<TypeTables>,
|
||||||
|
/// Registered shared signature for the module.
|
||||||
|
signatures: Arc<ModuleSharedSignatures>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Module {
|
impl Module {
|
||||||
@@ -318,10 +346,16 @@ impl Module {
|
|||||||
engine.compiler().isa(),
|
engine.compiler().isa(),
|
||||||
&*engine.config().profiler,
|
&*engine.config().profiler,
|
||||||
)?;
|
)?;
|
||||||
let module = modules.remove(main_module);
|
|
||||||
|
|
||||||
// Validate the module can be used with the current allocator
|
// Validate the module can be used with the current allocator
|
||||||
engine.allocator().validate(module.module())?;
|
engine.allocator().validate(modules[main_module].module())?;
|
||||||
|
|
||||||
|
let (signatures, trampolines) = engine.register_module_signatures(
|
||||||
|
&types.wasm_signatures,
|
||||||
|
modules.iter().flat_map(|m| m.trampolines().iter().cloned()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let module = modules.remove(main_module);
|
||||||
|
|
||||||
Ok(Module {
|
Ok(Module {
|
||||||
inner: Arc::new(ModuleInner {
|
inner: Arc::new(ModuleInner {
|
||||||
@@ -330,6 +364,11 @@ impl Module {
|
|||||||
types: Arc::new(types),
|
types: Arc::new(types),
|
||||||
artifact_upvars: modules,
|
artifact_upvars: modules,
|
||||||
module_upvars: Vec::new(),
|
module_upvars: Vec::new(),
|
||||||
|
signatures: Arc::new(ModuleSharedSignatures {
|
||||||
|
engine: engine.clone(),
|
||||||
|
signatures,
|
||||||
|
trampolines,
|
||||||
|
}),
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -416,8 +455,8 @@ impl Module {
|
|||||||
) -> Module {
|
) -> Module {
|
||||||
Module {
|
Module {
|
||||||
inner: Arc::new(ModuleInner {
|
inner: Arc::new(ModuleInner {
|
||||||
types: self.types().clone(),
|
types: self.inner.types.clone(),
|
||||||
engine: self.engine().clone(),
|
engine: self.inner.engine.clone(),
|
||||||
module: self.inner.artifact_upvars[artifact_index].clone(),
|
module: self.inner.artifact_upvars[artifact_index].clone(),
|
||||||
artifact_upvars: artifact_upvars
|
artifact_upvars: artifact_upvars
|
||||||
.iter()
|
.iter()
|
||||||
@@ -432,6 +471,7 @@ impl Module {
|
|||||||
wasmtime_environ::ModuleUpvar::Local(i) => modules[i].clone(),
|
wasmtime_environ::ModuleUpvar::Local(i) => modules[i].clone(),
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
|
signatures: self.inner.signatures.clone(),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -448,6 +488,14 @@ impl Module {
|
|||||||
&self.inner.types
|
&self.inner.types
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn signatures(&self) -> &PrimaryMap<SignatureIndex, VMSharedSignatureIndex> {
|
||||||
|
&self.inner.signatures.signatures
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn shared_signatures(&self) -> &Arc<ModuleSharedSignatures> {
|
||||||
|
&self.inner.signatures
|
||||||
|
}
|
||||||
|
|
||||||
/// Looks up the module upvar value at the `index` specified.
|
/// Looks up the module upvar value at the `index` specified.
|
||||||
///
|
///
|
||||||
/// Note that this panics if `index` is out of bounds since this should
|
/// Note that this panics if `index` is out of bounds since this should
|
||||||
|
|||||||
@@ -1,35 +1,30 @@
|
|||||||
use std::collections::BTreeMap;
|
//! Implements a registry of modules for a store.
|
||||||
use std::sync::Arc;
|
|
||||||
use std::sync::Mutex;
|
|
||||||
use wasmtime_environ::entity::EntityRef;
|
|
||||||
use wasmtime_environ::ir;
|
|
||||||
use wasmtime_environ::wasm::DefinedFuncIndex;
|
|
||||||
use wasmtime_environ::{FunctionAddressMap, TrapInformation};
|
|
||||||
use wasmtime_jit::CompiledModule;
|
|
||||||
|
|
||||||
/// This is a structure that lives within a `Store` and retains information
|
use crate::{module::ModuleSharedSignatures, Module};
|
||||||
/// about all modules registered with the `Store` via instantiation.
|
use std::{
|
||||||
///
|
collections::BTreeMap,
|
||||||
/// "frame information" here refers to things like determining whether a
|
sync::{Arc, Mutex},
|
||||||
/// program counter is a wasm program counter, and additionally mapping program
|
};
|
||||||
/// counters to wasm filenames, modules, line numbers, etc. This store of
|
use wasmtime_environ::{
|
||||||
/// information lives as long as a `Store` lives since modules are never
|
entity::EntityRef, ir, wasm::DefinedFuncIndex, FunctionAddressMap, TrapInformation,
|
||||||
/// unloaded today.
|
};
|
||||||
#[derive(Default)]
|
use wasmtime_jit::CompiledModule;
|
||||||
pub struct StoreFrameInfo {
|
use wasmtime_runtime::{VMSharedSignatureIndex, VMTrampoline};
|
||||||
/// An internal map that keeps track of backtrace frame information for
|
|
||||||
/// each module.
|
lazy_static::lazy_static! {
|
||||||
///
|
static ref GLOBAL_MODULES: Mutex<GlobalModuleRegistry> = Default::default();
|
||||||
/// This map is morally a map of ranges to a map of information for that
|
|
||||||
/// module. Each module is expected to reside in a disjoint section of
|
|
||||||
/// contiguous memory. No modules can overlap.
|
|
||||||
///
|
|
||||||
/// The key of this map is the highest address in the module and the value
|
|
||||||
/// is the module's information, which also contains the start address.
|
|
||||||
ranges: BTreeMap<usize, ModuleFrameInfo>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StoreFrameInfo {
|
/// Used for registering modules with a store.
|
||||||
|
///
|
||||||
|
/// The map is from the ending (exclusive) address for the module code to
|
||||||
|
/// the registered module.
|
||||||
|
///
|
||||||
|
/// The `BTreeMap` is used to quickly locate a module based on a program counter value.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct ModuleRegistry(BTreeMap<usize, RegisteredModule>);
|
||||||
|
|
||||||
|
impl ModuleRegistry {
|
||||||
/// Fetches frame information about a program counter in a backtrace.
|
/// Fetches frame information about a program counter in a backtrace.
|
||||||
///
|
///
|
||||||
/// Returns an object if this `pc` is known to some previously registered
|
/// Returns an object if this `pc` is known to some previously registered
|
||||||
@@ -48,8 +43,8 @@ impl StoreFrameInfo {
|
|||||||
self.module(pc)?.lookup_trap_info(pc)
|
self.module(pc)?.lookup_trap_info(pc)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn module(&self, pc: usize) -> Option<&ModuleFrameInfo> {
|
fn module(&self, pc: usize) -> Option<&RegisteredModule> {
|
||||||
let (end, info) = self.ranges.range(pc..).next()?;
|
let (end, info) = self.0.range(pc..).next()?;
|
||||||
if pc < info.start || *end < pc {
|
if pc < info.start || *end < pc {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
@@ -57,57 +52,78 @@ impl StoreFrameInfo {
|
|||||||
Some(info)
|
Some(info)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Registers a new compiled module's frame information.
|
/// Registers a new module with the registry.
|
||||||
pub fn register(&mut self, module: &Arc<CompiledModule>) {
|
pub fn register(&mut self, module: &Module) -> bool {
|
||||||
let (start, end) = module.code().range();
|
let compiled_module = module.compiled_module();
|
||||||
|
let (start, end) = compiled_module.code().range();
|
||||||
|
|
||||||
// Ignore modules with no code or finished functions
|
// Ignore modules with no code, finished functions, or if the module is already registered
|
||||||
if start == end || module.finished_functions().is_empty() {
|
if start == end || compiled_module.finished_functions().is_empty() {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The module code range is exclusive for end, so make it inclusive as it
|
// The module code range is exclusive for end, so make it inclusive as it
|
||||||
// may be a valid PC value
|
// may be a valid PC value
|
||||||
let end = end - 1;
|
let end = end - 1;
|
||||||
|
|
||||||
|
if self.0.get(&end).is_some() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Assert that this module's code doesn't collide with any other registered modules
|
// Assert that this module's code doesn't collide with any other registered modules
|
||||||
if let Some((_, prev)) = self.ranges.range(end..).next() {
|
if let Some((_, prev)) = self.0.range(end..).next() {
|
||||||
assert!(prev.start > end);
|
assert!(prev.start > end);
|
||||||
}
|
}
|
||||||
if let Some((prev_end, _)) = self.ranges.range(..=start).next_back() {
|
|
||||||
|
if let Some((prev_end, _)) = self.0.range(..=start).next_back() {
|
||||||
assert!(*prev_end < start);
|
assert!(*prev_end < start);
|
||||||
}
|
}
|
||||||
|
|
||||||
let prev = self.ranges.insert(
|
let prev = self.0.insert(
|
||||||
end,
|
end,
|
||||||
ModuleFrameInfo {
|
RegisteredModule {
|
||||||
start,
|
start,
|
||||||
module: module.clone(),
|
module: compiled_module.clone(),
|
||||||
|
signatures: module.shared_signatures().clone(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
assert!(prev.is_none());
|
assert!(prev.is_none());
|
||||||
|
|
||||||
GLOBAL_INFO.lock().unwrap().register(start, end, module);
|
GLOBAL_MODULES.lock().unwrap().register(start, end, module);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Looks up a trampoline from a shared signature index.
|
||||||
|
///
|
||||||
|
/// This will search all modules associated with the store for a suitable trampoline
|
||||||
|
/// given the shared signature index.
|
||||||
|
pub fn lookup_trampoline(&self, index: VMSharedSignatureIndex) -> Option<VMTrampoline> {
|
||||||
|
for (_, m) in &self.0 {
|
||||||
|
if let Some(trampoline) = m.signatures.trampolines.get(index) {
|
||||||
|
return Some(trampoline);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for StoreFrameInfo {
|
impl Drop for ModuleRegistry {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
let mut info = GLOBAL_INFO.lock().unwrap();
|
let mut info = GLOBAL_MODULES.lock().unwrap();
|
||||||
for end in self.ranges.keys() {
|
for end in self.0.keys() {
|
||||||
info.unregister(*end);
|
info.unregister(*end);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents a module's frame information.
|
struct RegisteredModule {
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ModuleFrameInfo {
|
|
||||||
start: usize,
|
start: usize,
|
||||||
module: Arc<CompiledModule>,
|
module: Arc<CompiledModule>,
|
||||||
|
signatures: Arc<ModuleSharedSignatures>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModuleFrameInfo {
|
impl RegisteredModule {
|
||||||
/// Determines if the related module has unparsed debug information.
|
/// Determines if the related module has unparsed debug information.
|
||||||
pub fn has_unparsed_debuginfo(&self) -> bool {
|
pub fn has_unparsed_debuginfo(&self) -> bool {
|
||||||
self.module.has_unparsed_debuginfo()
|
self.module.has_unparsed_debuginfo()
|
||||||
@@ -214,47 +230,29 @@ impl ModuleFrameInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This is the dual of `StoreFrameInfo` and is stored globally (as the name
|
/// This is the global module registry that stores information for all modules
|
||||||
/// implies) rather than simply in one `Store`.
|
/// that are currently in use by any `Store`.
|
||||||
///
|
///
|
||||||
/// The purpose of this map is to be called from signal handlers to determine
|
/// The purpose of this map is to be called from signal handlers to determine
|
||||||
/// whether a program counter is a wasm trap or not. Specifically macOS has
|
/// whether a program counter is a wasm trap or not. Specifically macOS has
|
||||||
/// no contextual information about the thread available, hence the necessity
|
/// no contextual information about the thread available, hence the necessity
|
||||||
/// for global state rather than using thread local state.
|
/// for global state rather than using thread local state.
|
||||||
///
|
///
|
||||||
/// This is similar to `StoreFrameInfo` except that it has less information and
|
/// This is similar to `ModuleRegistry` except that it has less information and
|
||||||
/// supports removal. Any time anything is registered with a `StoreFrameInfo`
|
/// supports removal. Any time anything is registered with a `ModuleRegistry`
|
||||||
/// it is also automatically registered with the singleton global frame
|
/// it is also automatically registered with the singleton global module
|
||||||
/// information. When a `StoreFrameInfo` is destroyed then all of its entries
|
/// registry. When a `ModuleRegistry` is destroyed then all of its entries
|
||||||
/// are removed from the global frame information.
|
/// are removed from the global module registry.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct GlobalFrameInfo {
|
pub struct GlobalModuleRegistry(BTreeMap<usize, GlobalRegisteredModule>);
|
||||||
// The map here behaves the same way as `StoreFrameInfo`.
|
|
||||||
ranges: BTreeMap<usize, GlobalModuleFrameInfo>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This is the equivalent of `ModuleFrameInfo` except it keeps a reference count.
|
impl GlobalModuleRegistry {
|
||||||
struct GlobalModuleFrameInfo {
|
|
||||||
module: ModuleFrameInfo,
|
|
||||||
|
|
||||||
/// Note that modules can be instantiated in many stores, so the purpose of
|
|
||||||
/// this field is to keep track of how many stores have registered a
|
|
||||||
/// module. Information is only removed from the global store when this
|
|
||||||
/// reference count reaches 0.
|
|
||||||
references: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
|
||||||
static ref GLOBAL_INFO: Mutex<GlobalFrameInfo> = Default::default();
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GlobalFrameInfo {
|
|
||||||
/// Returns whether the `pc`, according to globally registered information,
|
/// Returns whether the `pc`, according to globally registered information,
|
||||||
/// is a wasm trap or not.
|
/// is a wasm trap or not.
|
||||||
pub(crate) fn is_wasm_pc(pc: usize) -> bool {
|
pub(crate) fn is_wasm_pc(pc: usize) -> bool {
|
||||||
let info = GLOBAL_INFO.lock().unwrap();
|
let info = GLOBAL_MODULES.lock().unwrap();
|
||||||
|
|
||||||
match info.ranges.range(pc..).next() {
|
match info.0.range(pc..).next() {
|
||||||
Some((end, info)) => {
|
Some((end, info)) => {
|
||||||
if pc < info.module.start || *end < pc {
|
if pc < info.module.start || *end < pc {
|
||||||
return false;
|
return false;
|
||||||
@@ -263,7 +261,7 @@ impl GlobalFrameInfo {
|
|||||||
match info.module.func(pc) {
|
match info.module.func(pc) {
|
||||||
Some((index, offset)) => {
|
Some((index, offset)) => {
|
||||||
let (addr_map, _) = info.module.module.func_info(index);
|
let (addr_map, _) = info.module.module.func_info(index);
|
||||||
ModuleFrameInfo::instr_pos(offset, addr_map).is_some()
|
RegisteredModule::instr_pos(offset, addr_map).is_some()
|
||||||
}
|
}
|
||||||
None => false,
|
None => false,
|
||||||
}
|
}
|
||||||
@@ -274,17 +272,15 @@ impl GlobalFrameInfo {
|
|||||||
|
|
||||||
/// Registers a new region of code, described by `(start, end)` and with
|
/// Registers a new region of code, described by `(start, end)` and with
|
||||||
/// the given function information, with the global information.
|
/// the given function information, with the global information.
|
||||||
fn register(&mut self, start: usize, end: usize, module: &Arc<CompiledModule>) {
|
fn register(&mut self, start: usize, end: usize, module: &Module) {
|
||||||
let info = self
|
let info = self.0.entry(end).or_insert_with(|| GlobalRegisteredModule {
|
||||||
.ranges
|
module: RegisteredModule {
|
||||||
.entry(end)
|
start,
|
||||||
.or_insert_with(|| GlobalModuleFrameInfo {
|
module: module.compiled_module().clone(),
|
||||||
module: ModuleFrameInfo {
|
signatures: module.shared_signatures().clone(),
|
||||||
start,
|
},
|
||||||
module: module.clone(),
|
references: 0,
|
||||||
},
|
});
|
||||||
references: 0,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Note that ideally we'd debug_assert that the information previously
|
// Note that ideally we'd debug_assert that the information previously
|
||||||
// stored, if any, matches the `functions` we were given, but for now we
|
// stored, if any, matches the `functions` we were given, but for now we
|
||||||
@@ -293,17 +289,28 @@ impl GlobalFrameInfo {
|
|||||||
info.references += 1;
|
info.references += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unregisters a region of code (keyed by the `end` address) from this
|
/// Unregisters a region of code (keyed by the `end` address) from the
|
||||||
/// global information.
|
/// global information.
|
||||||
fn unregister(&mut self, end: usize) {
|
fn unregister(&mut self, end: usize) {
|
||||||
let info = self.ranges.get_mut(&end).unwrap();
|
let info = self.0.get_mut(&end).unwrap();
|
||||||
info.references -= 1;
|
info.references -= 1;
|
||||||
if info.references == 0 {
|
if info.references == 0 {
|
||||||
self.ranges.remove(&end);
|
self.0.remove(&end);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This is the equivalent of `RegisteredModule` except it keeps a reference count.
|
||||||
|
struct GlobalRegisteredModule {
|
||||||
|
module: RegisteredModule,
|
||||||
|
|
||||||
|
/// Note that modules can be instantiated in many stores, so the purpose of
|
||||||
|
/// this field is to keep track of how many stores have registered a
|
||||||
|
/// module. Information is only removed from the global registry when this
|
||||||
|
/// reference count reaches 0.
|
||||||
|
references: usize,
|
||||||
|
}
|
||||||
|
|
||||||
/// Description of a frame in a backtrace for a [`Trap`].
|
/// Description of a frame in a backtrace for a [`Trap`].
|
||||||
///
|
///
|
||||||
/// Whenever a WebAssembly trap occurs an instance of [`Trap`] is created. Each
|
/// Whenever a WebAssembly trap occurs an instance of [`Trap`] is created. Each
|
||||||
@@ -321,19 +328,6 @@ pub struct FrameInfo {
|
|||||||
symbols: Vec<FrameSymbol>,
|
symbols: Vec<FrameSymbol>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Debug information for a symbol that is attached to a [`FrameInfo`].
|
|
||||||
///
|
|
||||||
/// When DWARF debug information is present in a wasm file then this structure
|
|
||||||
/// can be found on a [`FrameInfo`] and can be used to learn about filenames,
|
|
||||||
/// line numbers, etc, which are the origin of a function in a stack trace.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct FrameSymbol {
|
|
||||||
name: Option<String>,
|
|
||||||
file: Option<String>,
|
|
||||||
line: Option<u32>,
|
|
||||||
column: Option<u32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FrameInfo {
|
impl FrameInfo {
|
||||||
/// Returns the WebAssembly function index for this frame.
|
/// Returns the WebAssembly function index for this frame.
|
||||||
///
|
///
|
||||||
@@ -405,6 +399,19 @@ impl FrameInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Debug information for a symbol that is attached to a [`FrameInfo`].
|
||||||
|
///
|
||||||
|
/// When DWARF debug information is present in a wasm file then this structure
|
||||||
|
/// can be found on a [`FrameInfo`] and can be used to learn about filenames,
|
||||||
|
/// line numbers, etc, which are the origin of a function in a stack trace.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct FrameSymbol {
|
||||||
|
name: Option<String>,
|
||||||
|
file: Option<String>,
|
||||||
|
line: Option<u32>,
|
||||||
|
column: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
impl FrameSymbol {
|
impl FrameSymbol {
|
||||||
/// Returns the function name associated with this symbol.
|
/// Returns the function name associated with this symbol.
|
||||||
///
|
///
|
||||||
@@ -463,7 +470,7 @@ fn test_frame_info() -> Result<(), anyhow::Error> {
|
|||||||
)?;
|
)?;
|
||||||
// Create an instance to ensure the frame information is registered.
|
// Create an instance to ensure the frame information is registered.
|
||||||
Instance::new(&store, &module, &[])?;
|
Instance::new(&store, &module, &[])?;
|
||||||
let info = store.frame_info().borrow();
|
let modules = store.modules().borrow();
|
||||||
for (i, alloc) in module.compiled_module().finished_functions() {
|
for (i, alloc) in module.compiled_module().finished_functions() {
|
||||||
let (start, end) = unsafe {
|
let (start, end) = unsafe {
|
||||||
let ptr = (**alloc).as_ptr();
|
let ptr = (**alloc).as_ptr();
|
||||||
@@ -471,7 +478,7 @@ fn test_frame_info() -> Result<(), anyhow::Error> {
|
|||||||
(ptr as usize, ptr as usize + len)
|
(ptr as usize, ptr as usize + len)
|
||||||
};
|
};
|
||||||
for pc in start..end {
|
for pc in start..end {
|
||||||
let (frame, _) = info.lookup_frame_info(pc).unwrap();
|
let (frame, _) = modules.lookup_frame_info(pc).unwrap();
|
||||||
assert!(frame.func_index() == i.as_u32());
|
assert!(frame.func_index() == i.as_u32());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
//! Implements module serialization.
|
//! Implements module serialization.
|
||||||
|
|
||||||
use super::ModuleInner;
|
use super::{ModuleInner, ModuleSharedSignatures};
|
||||||
use crate::{Engine, Module, OptLevel};
|
use crate::{Engine, Module, OptLevel};
|
||||||
use anyhow::{anyhow, bail, Context, Result};
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
use bincode::Options;
|
use bincode::Options;
|
||||||
@@ -10,8 +10,7 @@ use std::fmt;
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::{collections::HashMap, fmt::Display};
|
use std::{collections::HashMap, fmt::Display};
|
||||||
use wasmtime_environ::Tunables;
|
use wasmtime_environ::{isa::TargetIsa, settings, Tunables};
|
||||||
use wasmtime_environ::{isa::TargetIsa, settings};
|
|
||||||
use wasmtime_jit::{
|
use wasmtime_jit::{
|
||||||
CompilationArtifacts, CompilationStrategy, CompiledModule, Compiler, TypeTables,
|
CompilationArtifacts, CompilationStrategy, CompiledModule, Compiler, TypeTables,
|
||||||
};
|
};
|
||||||
@@ -123,55 +122,44 @@ impl From<settings::OptLevel> for OptLevel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A small helper struct which defines modules are serialized.
|
/// A small helper struct for serialized module upvars.
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct SerializedModuleData<'a> {
|
struct SerializedModuleUpvar {
|
||||||
/// All compiled artifacts needed by this module, where the last entry in
|
/// The module's index into the compilation artifact.
|
||||||
/// this list is the artifacts for the module itself.
|
index: usize,
|
||||||
artifacts: Vec<MyCow<'a, CompilationArtifacts>>,
|
/// Indexes into the list of all compilation artifacts for this module.
|
||||||
|
artifact_upvars: Vec<usize>,
|
||||||
/// Closed-over module values that are also needed for this module.
|
/// Closed-over module values that are also needed for this module.
|
||||||
modules: Vec<SerializedModuleData<'a>>,
|
module_upvars: Vec<SerializedModuleUpvar>,
|
||||||
/// The index into the list of type tables that are used for this module's
|
|
||||||
/// type tables.
|
|
||||||
type_tables: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SerializedModuleData<'a> {
|
impl SerializedModuleUpvar {
|
||||||
pub fn new(module: &'a Module) -> (Self, Vec<MyCow<'a, TypeTables>>) {
|
pub fn new(module: &Module, artifacts: &[Arc<CompiledModule>]) -> Self {
|
||||||
let mut pushed = HashMap::new();
|
// TODO: improve upon the linear searches in the artifact list
|
||||||
let mut tables = Vec::new();
|
let index = artifacts
|
||||||
return (module_data(module, &mut pushed, &mut tables), tables);
|
.iter()
|
||||||
|
.position(|a| Arc::as_ptr(a) == Arc::as_ptr(&module.inner.module))
|
||||||
|
.expect("module should be in artifacts list");
|
||||||
|
|
||||||
fn module_data<'a>(
|
SerializedModuleUpvar {
|
||||||
module: &'a Module,
|
index,
|
||||||
type_tables_pushed: &mut HashMap<usize, usize>,
|
artifact_upvars: module
|
||||||
type_tables: &mut Vec<MyCow<'a, TypeTables>>,
|
.inner
|
||||||
) -> SerializedModuleData<'a> {
|
.artifact_upvars
|
||||||
// Deduplicate `Arc<TypeTables>` using our two parameters to ensure we
|
.iter()
|
||||||
// serialize type tables as little as possible.
|
.map(|m| {
|
||||||
let ptr = Arc::as_ptr(module.types());
|
artifacts
|
||||||
let type_tables_idx = *type_tables_pushed.entry(ptr as usize).or_insert_with(|| {
|
.iter()
|
||||||
type_tables.push(MyCow::Borrowed(module.types()));
|
.position(|a| Arc::as_ptr(a) == Arc::as_ptr(m))
|
||||||
type_tables.len() - 1
|
.expect("artifact should be in artifacts list")
|
||||||
});
|
})
|
||||||
SerializedModuleData {
|
.collect(),
|
||||||
artifacts: module
|
module_upvars: module
|
||||||
.inner
|
.inner
|
||||||
.artifact_upvars
|
.module_upvars
|
||||||
.iter()
|
.iter()
|
||||||
.map(|i| MyCow::Borrowed(i.compilation_artifacts()))
|
.map(|m| SerializedModuleUpvar::new(m, artifacts))
|
||||||
.chain(Some(MyCow::Borrowed(
|
.collect(),
|
||||||
module.compiled_module().compilation_artifacts(),
|
|
||||||
)))
|
|
||||||
.collect(),
|
|
||||||
modules: module
|
|
||||||
.inner
|
|
||||||
.module_upvars
|
|
||||||
.iter()
|
|
||||||
.map(|i| module_data(i, type_tables_pushed, type_tables))
|
|
||||||
.collect(),
|
|
||||||
type_tables: type_tables_idx,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -212,14 +200,36 @@ pub struct SerializedModule<'a> {
|
|||||||
strategy: CompilationStrategy,
|
strategy: CompilationStrategy,
|
||||||
tunables: Tunables,
|
tunables: Tunables,
|
||||||
features: WasmFeatures,
|
features: WasmFeatures,
|
||||||
data: SerializedModuleData<'a>,
|
artifacts: Vec<MyCow<'a, CompilationArtifacts>>,
|
||||||
tables: Vec<MyCow<'a, TypeTables>>,
|
module_upvars: Vec<SerializedModuleUpvar>,
|
||||||
|
types: MyCow<'a, TypeTables>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SerializedModule<'a> {
|
impl<'a> SerializedModule<'a> {
|
||||||
pub fn new(module: &'a Module) -> Self {
|
pub fn new(module: &'a Module) -> Self {
|
||||||
let (data, tables) = SerializedModuleData::new(module);
|
let compiler = module.engine().compiler();
|
||||||
Self::with_data(module.engine().compiler(), data, tables)
|
let artifacts = module
|
||||||
|
.inner
|
||||||
|
.artifact_upvars
|
||||||
|
.iter()
|
||||||
|
.map(|m| MyCow::Borrowed(m.compilation_artifacts()))
|
||||||
|
.chain(Some(MyCow::Borrowed(
|
||||||
|
module.inner.module.compilation_artifacts(),
|
||||||
|
)))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let module_upvars = module
|
||||||
|
.inner
|
||||||
|
.module_upvars
|
||||||
|
.iter()
|
||||||
|
.map(|m| SerializedModuleUpvar::new(m, &module.inner.artifact_upvars))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
Self::with_data(
|
||||||
|
compiler,
|
||||||
|
artifacts,
|
||||||
|
module_upvars,
|
||||||
|
MyCow::Borrowed(module.types()),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_artifacts(
|
pub fn from_artifacts(
|
||||||
@@ -229,19 +239,17 @@ impl<'a> SerializedModule<'a> {
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
Self::with_data(
|
Self::with_data(
|
||||||
compiler,
|
compiler,
|
||||||
SerializedModuleData {
|
artifacts.iter().map(MyCow::Borrowed).collect(),
|
||||||
artifacts: artifacts.iter().map(MyCow::Borrowed).collect(),
|
Vec::new(),
|
||||||
modules: Vec::new(),
|
MyCow::Borrowed(types),
|
||||||
type_tables: 0,
|
|
||||||
},
|
|
||||||
vec![MyCow::Borrowed(types)],
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_data(
|
fn with_data(
|
||||||
compiler: &Compiler,
|
compiler: &Compiler,
|
||||||
data: SerializedModuleData<'a>,
|
artifacts: Vec<MyCow<'a, CompilationArtifacts>>,
|
||||||
tables: Vec<MyCow<'a, TypeTables>>,
|
module_upvars: Vec<SerializedModuleUpvar>,
|
||||||
|
types: MyCow<'a, TypeTables>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let isa = compiler.isa();
|
let isa = compiler.isa();
|
||||||
|
|
||||||
@@ -260,8 +268,9 @@ impl<'a> SerializedModule<'a> {
|
|||||||
strategy: compiler.strategy(),
|
strategy: compiler.strategy(),
|
||||||
tunables: compiler.tunables().clone(),
|
tunables: compiler.tunables().clone(),
|
||||||
features: compiler.features().into(),
|
features: compiler.features().into(),
|
||||||
data,
|
artifacts,
|
||||||
tables,
|
module_upvars,
|
||||||
|
types,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,45 +285,95 @@ impl<'a> SerializedModule<'a> {
|
|||||||
self.check_tunables(compiler)?;
|
self.check_tunables(compiler)?;
|
||||||
self.check_features(compiler)?;
|
self.check_features(compiler)?;
|
||||||
|
|
||||||
let types = self
|
let types = Arc::new(self.types.unwrap_owned());
|
||||||
.tables
|
let mut modules = CompiledModule::from_artifacts_list(
|
||||||
.into_iter()
|
self.artifacts
|
||||||
.map(|t| Arc::new(t.unwrap_owned()))
|
.into_iter()
|
||||||
.collect::<Vec<_>>();
|
.map(|i| i.unwrap_owned())
|
||||||
let module = mk(engine, &types, self.data)?;
|
.collect(),
|
||||||
|
engine.compiler().isa(),
|
||||||
|
&*engine.config().profiler,
|
||||||
|
)?;
|
||||||
|
|
||||||
// Validate the module can be used with the current allocator
|
// Validate the module can be used with the current allocator
|
||||||
engine.allocator().validate(module.inner.module.module())?;
|
engine
|
||||||
|
.allocator()
|
||||||
|
.validate(modules.last().unwrap().module())?;
|
||||||
|
|
||||||
return Ok(module);
|
let (signatures, trampolines) = engine.register_module_signatures(
|
||||||
|
&types.wasm_signatures,
|
||||||
|
modules.iter().flat_map(|m| m.trampolines().iter().cloned()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let signatures = Arc::new(ModuleSharedSignatures {
|
||||||
|
engine: engine.clone(),
|
||||||
|
signatures,
|
||||||
|
trampolines,
|
||||||
|
});
|
||||||
|
|
||||||
|
let module = modules.pop().unwrap();
|
||||||
|
|
||||||
|
let module_upvars = self
|
||||||
|
.module_upvars
|
||||||
|
.iter()
|
||||||
|
.map(|m| {
|
||||||
|
mk(
|
||||||
|
engine,
|
||||||
|
&modules,
|
||||||
|
&types,
|
||||||
|
m.index,
|
||||||
|
&m.artifact_upvars,
|
||||||
|
&m.module_upvars,
|
||||||
|
&signatures,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect::<Result<Vec<_>>>()?;
|
||||||
|
|
||||||
|
return Ok(Module {
|
||||||
|
inner: Arc::new(ModuleInner {
|
||||||
|
engine: engine.clone(),
|
||||||
|
types,
|
||||||
|
module,
|
||||||
|
artifact_upvars: modules,
|
||||||
|
module_upvars,
|
||||||
|
signatures,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
fn mk(
|
fn mk(
|
||||||
engine: &Engine,
|
engine: &Engine,
|
||||||
types: &Vec<Arc<TypeTables>>,
|
artifacts: &[Arc<CompiledModule>],
|
||||||
data: SerializedModuleData<'_>,
|
types: &Arc<TypeTables>,
|
||||||
|
module_index: usize,
|
||||||
|
artifact_upvars: &[usize],
|
||||||
|
module_upvars: &[SerializedModuleUpvar],
|
||||||
|
signatures: &Arc<ModuleSharedSignatures>,
|
||||||
) -> Result<Module> {
|
) -> Result<Module> {
|
||||||
let mut artifacts = CompiledModule::from_artifacts_list(
|
|
||||||
data.artifacts
|
|
||||||
.into_iter()
|
|
||||||
.map(|i| i.unwrap_owned())
|
|
||||||
.collect(),
|
|
||||||
engine.compiler().isa(),
|
|
||||||
&*engine.config().profiler,
|
|
||||||
)?;
|
|
||||||
let inner = ModuleInner {
|
|
||||||
engine: engine.clone(),
|
|
||||||
types: types[data.type_tables].clone(),
|
|
||||||
module: artifacts.pop().unwrap(),
|
|
||||||
artifact_upvars: artifacts,
|
|
||||||
module_upvars: data
|
|
||||||
.modules
|
|
||||||
.into_iter()
|
|
||||||
.map(|m| mk(engine, types, m))
|
|
||||||
.collect::<Result<Vec<_>>>()?,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Module {
|
Ok(Module {
|
||||||
inner: Arc::new(inner),
|
inner: Arc::new(ModuleInner {
|
||||||
|
engine: engine.clone(),
|
||||||
|
types: types.clone(),
|
||||||
|
module: artifacts[module_index].clone(),
|
||||||
|
artifact_upvars: artifact_upvars
|
||||||
|
.iter()
|
||||||
|
.map(|i| artifacts[*i].clone())
|
||||||
|
.collect(),
|
||||||
|
module_upvars: module_upvars
|
||||||
|
.into_iter()
|
||||||
|
.map(|m| {
|
||||||
|
mk(
|
||||||
|
engine,
|
||||||
|
artifacts,
|
||||||
|
types,
|
||||||
|
m.index,
|
||||||
|
&m.artifact_upvars,
|
||||||
|
&m.module_upvars,
|
||||||
|
signatures,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect::<Result<Vec<_>>>()?,
|
||||||
|
signatures: signatures.clone(),
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,155 +0,0 @@
|
|||||||
//! Implement a registry of function signatures, for fast indirect call
|
|
||||||
//! signature checking.
|
|
||||||
|
|
||||||
use crate::Module;
|
|
||||||
use std::collections::{hash_map, HashMap};
|
|
||||||
use std::convert::TryFrom;
|
|
||||||
use wasmtime_environ::entity::PrimaryMap;
|
|
||||||
use wasmtime_environ::wasm::{SignatureIndex, WasmFuncType};
|
|
||||||
use wasmtime_runtime::{VMSharedSignatureIndex, VMTrampoline};
|
|
||||||
|
|
||||||
/// WebAssembly requires that the caller and callee signatures in an indirect
|
|
||||||
/// call must match. To implement this efficiently, keep a registry of all
|
|
||||||
/// signatures, shared by all instances, so that call sites can just do an
|
|
||||||
/// index comparison.
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct SignatureRegistry {
|
|
||||||
// Map from a wasm actual function type to the index that it is assigned,
|
|
||||||
// shared amongst all wasm modules.
|
|
||||||
wasm2index: HashMap<WasmFuncType, VMSharedSignatureIndex>,
|
|
||||||
|
|
||||||
// Map of all known wasm function signatures in this registry. This is
|
|
||||||
// keyed by `VMSharedSignatureIndex` above.
|
|
||||||
index_map: Vec<Entry>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct Entry {
|
|
||||||
// The WebAssembly type signature, using wasm types.
|
|
||||||
wasm: WasmFuncType,
|
|
||||||
// The native trampoline used to invoke this type signature from `Func`.
|
|
||||||
// Note that the code memory for this trampoline is not owned by this
|
|
||||||
// type, but instead it's expected to be owned by the store that this
|
|
||||||
// registry lives within.
|
|
||||||
trampoline: Option<VMTrampoline>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SignatureRegistry {
|
|
||||||
/// Registers all signatures within a module into this registry all at once.
|
|
||||||
///
|
|
||||||
/// This will also internally register trampolines compiled in the module.
|
|
||||||
pub fn register_module(&mut self, module: &Module) {
|
|
||||||
// Register a unique index for all types in this module, even if they
|
|
||||||
// don't have a trampoline.
|
|
||||||
let signatures = &module.types().wasm_signatures;
|
|
||||||
for ty in module.compiled_module().module().types.values() {
|
|
||||||
if let wasmtime_environ::ModuleType::Function(index) = ty {
|
|
||||||
self.register_one(&signatures[*index], None);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Once we've got a shared index for all types used then also fill in
|
|
||||||
// any trampolines that the module has compiled as well.
|
|
||||||
for (index, trampoline) in module.compiled_module().trampolines() {
|
|
||||||
let shared = self.wasm2index[&signatures[*index]];
|
|
||||||
let entry = &mut self.index_map[shared.bits() as usize];
|
|
||||||
if entry.trampoline.is_none() {
|
|
||||||
entry.trampoline = Some(*trampoline);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Register a signature and return its unique index.
|
|
||||||
pub fn register(
|
|
||||||
&mut self,
|
|
||||||
wasm: &WasmFuncType,
|
|
||||||
trampoline: VMTrampoline,
|
|
||||||
) -> VMSharedSignatureIndex {
|
|
||||||
self.register_one(wasm, Some(trampoline))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn register_one(
|
|
||||||
&mut self,
|
|
||||||
wasm: &WasmFuncType,
|
|
||||||
trampoline: Option<VMTrampoline>,
|
|
||||||
) -> VMSharedSignatureIndex {
|
|
||||||
let len = self.wasm2index.len();
|
|
||||||
|
|
||||||
match self.wasm2index.entry(wasm.clone()) {
|
|
||||||
hash_map::Entry::Occupied(entry) => {
|
|
||||||
let ret = *entry.get();
|
|
||||||
let entry = &mut self.index_map[ret.bits() as usize];
|
|
||||||
// If the entry does not previously have a trampoline, then
|
|
||||||
// overwrite it with whatever was specified by this function.
|
|
||||||
if entry.trampoline.is_none() {
|
|
||||||
entry.trampoline = trampoline;
|
|
||||||
}
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
hash_map::Entry::Vacant(entry) => {
|
|
||||||
// Keep `signature_hash` len under 2**32 -- VMSharedSignatureIndex::new(std::u32::MAX)
|
|
||||||
// is reserved for VMSharedSignatureIndex::default().
|
|
||||||
assert!(
|
|
||||||
len < std::u32::MAX as usize,
|
|
||||||
"Invariant check: signature_hash.len() < std::u32::MAX"
|
|
||||||
);
|
|
||||||
debug_assert_eq!(len, self.index_map.len());
|
|
||||||
let index = VMSharedSignatureIndex::new(u32::try_from(len).unwrap());
|
|
||||||
self.index_map.push(Entry {
|
|
||||||
wasm: wasm.clone(),
|
|
||||||
trampoline,
|
|
||||||
});
|
|
||||||
entry.insert(index);
|
|
||||||
index
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Looks up a shared index from the wasm signature itself.
|
|
||||||
pub fn lookup(&self, wasm: &WasmFuncType) -> Option<VMSharedSignatureIndex> {
|
|
||||||
self.wasm2index.get(wasm).cloned()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Builds a lookup table for a module from the possible module's signature
|
|
||||||
/// indices to the shared signature index within this registry.
|
|
||||||
pub fn lookup_table(
|
|
||||||
&self,
|
|
||||||
module: &Module,
|
|
||||||
) -> PrimaryMap<SignatureIndex, VMSharedSignatureIndex> {
|
|
||||||
// For module-linking using modules this builds up a map that is
|
|
||||||
// too large. This builds up a map for everything in `TypeTables` but
|
|
||||||
// that's all the types for all modules in a whole module linking graph,
|
|
||||||
// which our `module` may not be using.
|
|
||||||
//
|
|
||||||
// For all non-module-linking-using modules, though, this is not an
|
|
||||||
// issue. This is optimizing for the non-module-linking case right now
|
|
||||||
// and it seems like module linking will likely change to the point that
|
|
||||||
// this will no longer be an issue in the future.
|
|
||||||
let signatures = &module.types().wasm_signatures;
|
|
||||||
let mut map = PrimaryMap::with_capacity(signatures.len());
|
|
||||||
for wasm in signatures.values() {
|
|
||||||
map.push(
|
|
||||||
self.wasm2index
|
|
||||||
.get(wasm)
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or(VMSharedSignatureIndex::new(u32::MAX)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
map
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Looks up information known about a shared signature index.
|
|
||||||
///
|
|
||||||
/// Note that for this operation to be semantically correct the `idx` must
|
|
||||||
/// have previously come from a call to `register` of this same object.
|
|
||||||
pub fn lookup_shared(
|
|
||||||
&self,
|
|
||||||
idx: VMSharedSignatureIndex,
|
|
||||||
) -> Option<(&WasmFuncType, VMTrampoline)> {
|
|
||||||
let (wasm, trampoline) = self
|
|
||||||
.index_map
|
|
||||||
.get(idx.bits() as usize)
|
|
||||||
.map(|e| (&e.wasm, e.trampoline))?;
|
|
||||||
Some((wasm, trampoline?))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
185
crates/wasmtime/src/signatures.rs
Normal file
185
crates/wasmtime/src/signatures.rs
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
//! Implement a registry of function signatures, for fast indirect call
|
||||||
|
//! signature checking.
|
||||||
|
|
||||||
|
use std::collections::{hash_map::Entry, HashMap};
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
use wasmtime_environ::entity::PrimaryMap;
|
||||||
|
use wasmtime_environ::wasm::{SignatureIndex, WasmFuncType};
|
||||||
|
use wasmtime_runtime::{VMSharedSignatureIndex, VMTrampoline};
|
||||||
|
|
||||||
|
/// Represents a mapping of shared signature index to trampolines.
|
||||||
|
///
|
||||||
|
/// This is used in various places to store trampolines associated with shared
|
||||||
|
/// signature indexes.
|
||||||
|
///
|
||||||
|
/// As multiple trampolines may exist for a single signature, the map entries
|
||||||
|
/// are internally reference counted.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct TrampolineMap(HashMap<VMSharedSignatureIndex, (usize, VMTrampoline)>);
|
||||||
|
|
||||||
|
impl TrampolineMap {
|
||||||
|
/// Inserts a trampoline into the map.
|
||||||
|
pub fn insert(&mut self, index: VMSharedSignatureIndex, trampoline: VMTrampoline) {
|
||||||
|
let entry = match self.0.entry(index) {
|
||||||
|
Entry::Occupied(e) => e.into_mut(),
|
||||||
|
Entry::Vacant(e) => e.insert((0, trampoline)),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Increment the ref count
|
||||||
|
entry.0 += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets a trampoline from the map.
|
||||||
|
pub fn get(&self, index: VMSharedSignatureIndex) -> Option<VMTrampoline> {
|
||||||
|
self.0.get(&index).map(|(_, trampoline)| *trampoline)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Iterates the shared signature indexes stored in the map.
|
||||||
|
///
|
||||||
|
/// A shared signature index will be returned by the iterator for every
|
||||||
|
/// trampoline registered for that index, so duplicates may be present.
|
||||||
|
///
|
||||||
|
/// This iterator can be used for deregistering signatures with the
|
||||||
|
/// signature registry.
|
||||||
|
pub fn indexes<'a>(&'a self) -> impl Iterator<Item = VMSharedSignatureIndex> + 'a {
|
||||||
|
self.0
|
||||||
|
.iter()
|
||||||
|
.flat_map(|(index, (count, _))| std::iter::repeat(*index).take(*count))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Determines if the trampoline map is empty.
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.0.is_empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents a map between module signature indexes and
|
||||||
|
/// shared signature indexes.
|
||||||
|
pub type SharedSignatures = PrimaryMap<SignatureIndex, VMSharedSignatureIndex>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct RegistryEntry {
|
||||||
|
references: usize,
|
||||||
|
ty: WasmFuncType,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// WebAssembly requires that the caller and callee signatures in an indirect
|
||||||
|
/// call must match. To implement this efficiently, keep a registry of all
|
||||||
|
/// signatures, shared by all instances, so that call sites can just do an
|
||||||
|
/// index comparison.
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct SignatureRegistry {
|
||||||
|
map: HashMap<WasmFuncType, VMSharedSignatureIndex>,
|
||||||
|
entries: Vec<Option<RegistryEntry>>,
|
||||||
|
free: Vec<VMSharedSignatureIndex>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SignatureRegistry {
|
||||||
|
/// Registers a module with the signature registry from the collection of
|
||||||
|
/// all signatures and trampolines in the module.
|
||||||
|
pub fn register_module(
|
||||||
|
&mut self,
|
||||||
|
signatures: &PrimaryMap<SignatureIndex, WasmFuncType>,
|
||||||
|
trampolines: impl Iterator<Item = (SignatureIndex, VMTrampoline)>,
|
||||||
|
) -> (SharedSignatures, TrampolineMap) {
|
||||||
|
let mut sigs = SharedSignatures::default();
|
||||||
|
let mut map = TrampolineMap::default();
|
||||||
|
|
||||||
|
for (_, ty) in signatures.iter() {
|
||||||
|
sigs.push(self.register(ty));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (index, trampoline) in trampolines {
|
||||||
|
let index = self.map[&signatures[index]];
|
||||||
|
map.insert(index, trampoline);
|
||||||
|
}
|
||||||
|
|
||||||
|
(sigs, map)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Registers a single signature with the registry.
|
||||||
|
///
|
||||||
|
/// This is used for registering host functions created with the Wasmtime API.
|
||||||
|
pub fn register(&mut self, ty: &WasmFuncType) -> VMSharedSignatureIndex {
|
||||||
|
let len = self.map.len();
|
||||||
|
|
||||||
|
let index = match self.map.entry(ty.clone()) {
|
||||||
|
Entry::Occupied(e) => *e.get(),
|
||||||
|
Entry::Vacant(e) => {
|
||||||
|
let (index, entry) = match self.free.pop() {
|
||||||
|
Some(index) => (index, &mut self.entries[index.bits() as usize]),
|
||||||
|
None => {
|
||||||
|
// Keep `index_map` len under 2**32 -- VMSharedSignatureIndex::new(std::u32::MAX)
|
||||||
|
// is reserved for VMSharedSignatureIndex::default().
|
||||||
|
assert!(
|
||||||
|
len < std::u32::MAX as usize,
|
||||||
|
"Invariant check: index_map.len() < std::u32::MAX"
|
||||||
|
);
|
||||||
|
debug_assert_eq!(len, self.entries.len());
|
||||||
|
|
||||||
|
let index = VMSharedSignatureIndex::new(u32::try_from(len).unwrap());
|
||||||
|
self.entries.push(None);
|
||||||
|
|
||||||
|
(index, self.entries.last_mut().unwrap())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
*entry = Some(RegistryEntry {
|
||||||
|
references: 0,
|
||||||
|
ty: ty.clone(),
|
||||||
|
});
|
||||||
|
|
||||||
|
*e.insert(index)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.entries[index.bits() as usize]
|
||||||
|
.as_mut()
|
||||||
|
.unwrap()
|
||||||
|
.references += 1;
|
||||||
|
|
||||||
|
index
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unregisters a collection of shared indexes from the registry.
|
||||||
|
pub fn unregister(&mut self, indexes: impl Iterator<Item = VMSharedSignatureIndex>) {
|
||||||
|
for index in indexes {
|
||||||
|
let removed = {
|
||||||
|
let entry = self.entries[index.bits() as usize].as_mut().unwrap();
|
||||||
|
|
||||||
|
debug_assert!(entry.references > 0);
|
||||||
|
entry.references -= 1;
|
||||||
|
|
||||||
|
if entry.references == 0 {
|
||||||
|
self.map.remove(&entry.ty);
|
||||||
|
self.free.push(index);
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if removed {
|
||||||
|
self.entries[index.bits() as usize] = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Looks up a function type from a shared signature index.
|
||||||
|
pub fn lookup_type(&self, index: VMSharedSignatureIndex) -> Option<&WasmFuncType> {
|
||||||
|
self.entries
|
||||||
|
.get(index.bits() as usize)
|
||||||
|
.and_then(|e| e.as_ref().map(|e| &e.ty))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Determines if the registry is semantically empty.
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
// If the map is empty, assert that all remaining entries are "free"
|
||||||
|
if self.map.is_empty() {
|
||||||
|
assert!(self.free.len() == self.entries.len());
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
use crate::frame_info;
|
use crate::{
|
||||||
use crate::frame_info::StoreFrameInfo;
|
module::ModuleRegistry, signatures::TrampolineMap, trampoline::StoreInstanceHandle, Engine,
|
||||||
use crate::sig_registry::SignatureRegistry;
|
Func, Module, Trap,
|
||||||
use crate::trampoline::StoreInstanceHandle;
|
};
|
||||||
use crate::{Engine, Func, FuncType, Module, Trap};
|
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use std::any::{Any, TypeId};
|
use std::any::{Any, TypeId};
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
use std::collections::{hash_map::Entry, HashMap, HashSet};
|
use std::collections::{hash_map::Entry, HashMap};
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
@@ -16,12 +15,12 @@ use std::ptr;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
use wasmtime_environ::wasm;
|
use wasmtime_environ::wasm::WasmFuncType;
|
||||||
use wasmtime_jit::{CompiledModule, ModuleCode};
|
use wasmtime_jit::{CompiledModule, ModuleCode};
|
||||||
use wasmtime_runtime::{
|
use wasmtime_runtime::{
|
||||||
Export, InstanceAllocator, InstanceHandle, OnDemandInstanceAllocator, SignalHandler,
|
InstanceAllocator, InstanceHandle, OnDemandInstanceAllocator, SignalHandler, StackMapRegistry,
|
||||||
StackMapRegistry, TrapInfo, VMCallerCheckedAnyfunc, VMContext, VMExternRef,
|
TrapInfo, VMContext, VMExternRef, VMExternRefActivationsTable, VMInterrupts,
|
||||||
VMExternRefActivationsTable, VMInterrupts, VMTrampoline,
|
VMSharedSignatureIndex, VMTrampoline,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Used to associate instances with the store.
|
/// Used to associate instances with the store.
|
||||||
@@ -72,20 +71,13 @@ pub struct Store {
|
|||||||
|
|
||||||
pub(crate) struct StoreInner {
|
pub(crate) struct StoreInner {
|
||||||
engine: Engine,
|
engine: Engine,
|
||||||
/// The map of all host functions registered with this store's signature registry
|
|
||||||
host_funcs: RefCell<HashMap<InstanceHandle, Box<VMCallerCheckedAnyfunc>>>,
|
|
||||||
interrupts: Arc<VMInterrupts>,
|
interrupts: Arc<VMInterrupts>,
|
||||||
signatures: RefCell<SignatureRegistry>,
|
|
||||||
instances: RefCell<Vec<StoreInstance>>,
|
instances: RefCell<Vec<StoreInstance>>,
|
||||||
signal_handler: RefCell<Option<Box<SignalHandler<'static>>>>,
|
signal_handler: RefCell<Option<Box<SignalHandler<'static>>>>,
|
||||||
externref_activations_table: VMExternRefActivationsTable,
|
externref_activations_table: VMExternRefActivationsTable,
|
||||||
stack_map_registry: StackMapRegistry,
|
stack_map_registry: StackMapRegistry,
|
||||||
/// Information about JIT code which allows us to test if a program counter
|
modules: RefCell<ModuleRegistry>,
|
||||||
/// is in JIT code, lookup trap information, etc.
|
trampolines: RefCell<TrampolineMap>,
|
||||||
frame_info: RefCell<StoreFrameInfo>,
|
|
||||||
/// Set of all compiled modules that we're holding a strong reference to
|
|
||||||
/// the module's code for. This includes JIT functions, trampolines, etc.
|
|
||||||
modules: RefCell<HashSet<ArcModuleCode>>,
|
|
||||||
// Numbers of resources instantiated in this store.
|
// Numbers of resources instantiated in this store.
|
||||||
instance_count: Cell<usize>,
|
instance_count: Cell<usize>,
|
||||||
memory_count: Cell<usize>,
|
memory_count: Cell<usize>,
|
||||||
@@ -137,21 +129,19 @@ impl Store {
|
|||||||
// once-per-thread. Platforms like Unix, however, only require this
|
// once-per-thread. Platforms like Unix, however, only require this
|
||||||
// once-per-program. In any case this is safe to call many times and
|
// once-per-program. In any case this is safe to call many times and
|
||||||
// each one that's not relevant just won't do anything.
|
// each one that's not relevant just won't do anything.
|
||||||
wasmtime_runtime::init_traps(frame_info::GlobalFrameInfo::is_wasm_pc)
|
wasmtime_runtime::init_traps(crate::module::GlobalModuleRegistry::is_wasm_pc)
|
||||||
.expect("failed to initialize trap handling");
|
.expect("failed to initialize trap handling");
|
||||||
|
|
||||||
Store {
|
Store {
|
||||||
inner: Rc::new(StoreInner {
|
inner: Rc::new(StoreInner {
|
||||||
engine: engine.clone(),
|
engine: engine.clone(),
|
||||||
host_funcs: RefCell::new(HashMap::new()),
|
|
||||||
interrupts: Arc::new(Default::default()),
|
interrupts: Arc::new(Default::default()),
|
||||||
signatures: RefCell::new(Default::default()),
|
|
||||||
instances: RefCell::new(Vec::new()),
|
instances: RefCell::new(Vec::new()),
|
||||||
signal_handler: RefCell::new(None),
|
signal_handler: RefCell::new(None),
|
||||||
externref_activations_table: VMExternRefActivationsTable::new(),
|
externref_activations_table: VMExternRefActivationsTable::new(),
|
||||||
stack_map_registry: StackMapRegistry::default(),
|
stack_map_registry: StackMapRegistry::default(),
|
||||||
frame_info: Default::default(),
|
modules: RefCell::new(ModuleRegistry::default()),
|
||||||
modules: Default::default(),
|
trampolines: RefCell::new(TrampolineMap::default()),
|
||||||
instance_count: Default::default(),
|
instance_count: Default::default(),
|
||||||
memory_count: Default::default(),
|
memory_count: Default::default(),
|
||||||
table_count: Default::default(),
|
table_count: Default::default(),
|
||||||
@@ -181,35 +171,6 @@ impl Store {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_host_anyfunc(
|
|
||||||
&self,
|
|
||||||
instance: &InstanceHandle,
|
|
||||||
ty: &FuncType,
|
|
||||||
trampoline: VMTrampoline,
|
|
||||||
) -> *mut VMCallerCheckedAnyfunc {
|
|
||||||
let mut funcs = self.inner.host_funcs.borrow_mut();
|
|
||||||
|
|
||||||
let anyfunc = funcs.entry(unsafe { instance.clone() }).or_insert_with(|| {
|
|
||||||
let mut anyfunc = match instance
|
|
||||||
.lookup_by_declaration(&wasm::EntityIndex::Function(wasm::FuncIndex::from_u32(0)))
|
|
||||||
{
|
|
||||||
Export::Function(f) => unsafe { f.anyfunc.as_ref() }.clone(),
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Register the function with this store's signature registry
|
|
||||||
anyfunc.type_index = self
|
|
||||||
.inner
|
|
||||||
.signatures
|
|
||||||
.borrow_mut()
|
|
||||||
.register(ty.as_wasm_func_type(), trampoline);
|
|
||||||
|
|
||||||
Box::new(anyfunc)
|
|
||||||
});
|
|
||||||
|
|
||||||
&mut **anyfunc
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the [`Engine`] that this store is associated with.
|
/// Returns the [`Engine`] that this store is associated with.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn engine(&self) -> &Engine {
|
pub fn engine(&self) -> &Engine {
|
||||||
@@ -244,52 +205,16 @@ impl Store {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn signatures(&self) -> &RefCell<SignatureRegistry> {
|
|
||||||
&self.inner.signatures
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn register_module(&self, module: &Module) {
|
pub(crate) fn register_module(&self, module: &Module) {
|
||||||
// With a module being instantiated into this `Store` we need to
|
// Register the module with the registry
|
||||||
// preserve its jit-code. References to this module's code and
|
if !self.inner.modules.borrow_mut().register(module) {
|
||||||
// trampolines are not owning-references so it's our responsibility to
|
|
||||||
// keep it all alive within the `Store`.
|
|
||||||
//
|
|
||||||
// If this module is already present in the store then we skip all
|
|
||||||
// further registration steps.
|
|
||||||
let first = self
|
|
||||||
.inner
|
|
||||||
.modules
|
|
||||||
.borrow_mut()
|
|
||||||
.insert(ArcModuleCode(module.compiled_module().code().clone()));
|
|
||||||
if !first {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// All modules register their JIT code in a store for two reasons
|
|
||||||
// currently:
|
|
||||||
//
|
|
||||||
// * First we only catch signals/traps if the program counter falls
|
|
||||||
// within the jit code of an instantiated wasm module. This ensures
|
|
||||||
// we don't catch accidental Rust/host segfaults.
|
|
||||||
//
|
|
||||||
// * Second when generating a backtrace we'll use this mapping to
|
|
||||||
// only generate wasm frames for instruction pointers that fall
|
|
||||||
// within jit code.
|
|
||||||
self.inner
|
|
||||||
.frame_info
|
|
||||||
.borrow_mut()
|
|
||||||
.register(module.compiled_module());
|
|
||||||
|
|
||||||
// We need to know about all the stack maps of all instantiated modules
|
// We need to know about all the stack maps of all instantiated modules
|
||||||
// so when performing a GC we know about all wasm frames that we find
|
// so when performing a GC we know about all wasm frames that we find
|
||||||
// on the stack.
|
// on the stack.
|
||||||
self.register_stack_maps(module.compiled_module());
|
self.register_stack_maps(module.compiled_module());
|
||||||
|
|
||||||
// Signatures are loaded into our `SignatureRegistry` here
|
|
||||||
// once-per-module (and once-per-signature). This allows us to create
|
|
||||||
// a `Func` wrapper for any function in the module, which requires that
|
|
||||||
// we know about the signature and trampoline for all instances.
|
|
||||||
self.signatures().borrow_mut().register_module(module);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn register_stack_maps(&self, module: &CompiledModule) {
|
fn register_stack_maps(&self, module: &CompiledModule) {
|
||||||
@@ -304,6 +229,39 @@ impl Store {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is used to register a `Func` with the store
|
||||||
|
pub(crate) fn register_signature(
|
||||||
|
&self,
|
||||||
|
ty: &WasmFuncType,
|
||||||
|
trampoline: VMTrampoline,
|
||||||
|
) -> VMSharedSignatureIndex {
|
||||||
|
let index = self.inner.engine.register_signature(ty);
|
||||||
|
self.inner
|
||||||
|
.trampolines
|
||||||
|
.borrow_mut()
|
||||||
|
.insert(index, trampoline);
|
||||||
|
index
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn lookup_trampoline(&self, index: VMSharedSignatureIndex) -> VMTrampoline {
|
||||||
|
// Look up the trampoline with the store's trampolines (from `Func`).
|
||||||
|
if let Some(trampoline) = self.inner.trampolines.borrow().get(index) {
|
||||||
|
return trampoline;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look up the trampoline with the registered modules
|
||||||
|
if let Some(trampoline) = self.inner.modules.borrow().lookup_trampoline(index) {
|
||||||
|
return trampoline;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lastly, check with the engine (for `HostFunc`)
|
||||||
|
self.inner
|
||||||
|
.engine
|
||||||
|
.host_func_trampolines()
|
||||||
|
.get(index)
|
||||||
|
.expect("trampoline missing")
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn bump_resource_counts(&self, module: &Module) -> Result<()> {
|
pub(crate) fn bump_resource_counts(&self, module: &Module) -> Result<()> {
|
||||||
let config = self.engine().config();
|
let config = self.engine().config();
|
||||||
|
|
||||||
@@ -363,7 +321,7 @@ impl Store {
|
|||||||
.borrow()
|
.borrow()
|
||||||
.iter()
|
.iter()
|
||||||
.any(|i| i.handle.vmctx_ptr() == handle.vmctx_ptr())
|
.any(|i| i.handle.vmctx_ptr() == handle.vmctx_ptr())
|
||||||
|| self.inner.host_funcs.borrow().get(&handle).is_some()
|
|| self.inner.engine.host_func_anyfunc(&handle).is_some()
|
||||||
);
|
);
|
||||||
StoreInstanceHandle {
|
StoreInstanceHandle {
|
||||||
store: self.clone(),
|
store: self.clone(),
|
||||||
@@ -494,8 +452,8 @@ impl Store {
|
|||||||
&self.inner.stack_map_registry
|
&self.inner.stack_map_registry
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn frame_info(&self) -> &RefCell<StoreFrameInfo> {
|
pub(crate) fn modules(&self) -> &RefCell<ModuleRegistry> {
|
||||||
&self.inner.frame_info
|
&self.inner.modules
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Notifies that the current Store (and all referenced entities) has been moved over to a
|
/// Notifies that the current Store (and all referenced entities) has been moved over to a
|
||||||
@@ -984,6 +942,12 @@ impl Drop for StoreInner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let trampolines = self.trampolines.borrow();
|
||||||
|
|
||||||
|
if !trampolines.is_empty() {
|
||||||
|
self.engine.unregister_signatures(trampolines.indexes());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
//! Support for a calling of an imported function.
|
//! Support for a calling of an imported function.
|
||||||
|
|
||||||
use crate::{sig_registry::SignatureRegistry, Config, FuncType, Trap};
|
use crate::{Config, FuncType, Store, Trap};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
@@ -262,15 +262,15 @@ pub fn create_function(
|
|||||||
ft: &FuncType,
|
ft: &FuncType,
|
||||||
func: Box<dyn Fn(*mut VMContext, *mut u128) -> Result<(), Trap>>,
|
func: Box<dyn Fn(*mut VMContext, *mut u128) -> Result<(), Trap>>,
|
||||||
config: &Config,
|
config: &Config,
|
||||||
registry: Option<&mut SignatureRegistry>,
|
store: Option<&Store>,
|
||||||
) -> Result<(InstanceHandle, VMTrampoline)> {
|
) -> Result<(InstanceHandle, VMTrampoline)> {
|
||||||
let (module, finished_functions, trampoline, trampoline_state) =
|
let (module, finished_functions, trampoline, trampoline_state) =
|
||||||
create_function_trampoline(config, ft, func)?;
|
create_function_trampoline(config, ft, func)?;
|
||||||
|
|
||||||
// If there is no signature registry, use the default signature index which is
|
// If there is no store, use the default signature index which is
|
||||||
// guaranteed to trap if there is ever an indirect call on the function (should not happen)
|
// guaranteed to trap if there is ever an indirect call on the function (should not happen)
|
||||||
let shared_signature_id = registry
|
let shared_signature_id = store
|
||||||
.map(|r| r.register(ft.as_wasm_func_type(), trampoline))
|
.map(|s| s.register_signature(ft.as_wasm_func_type(), trampoline))
|
||||||
.unwrap_or(VMSharedSignatureIndex::default());
|
.unwrap_or(VMSharedSignatureIndex::default());
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|||||||
@@ -161,7 +161,7 @@ impl Trap {
|
|||||||
maybe_interrupted,
|
maybe_interrupted,
|
||||||
} => {
|
} => {
|
||||||
let mut code = store
|
let mut code = store
|
||||||
.frame_info()
|
.modules()
|
||||||
.borrow()
|
.borrow()
|
||||||
.lookup_trap_info(pc)
|
.lookup_trap_info(pc)
|
||||||
.map(|info| info.trap_code)
|
.map(|info| info.trap_code)
|
||||||
@@ -239,7 +239,7 @@ impl Trap {
|
|||||||
// (the call instruction) so we subtract one as the lookup.
|
// (the call instruction) so we subtract one as the lookup.
|
||||||
let pc_to_lookup = if Some(pc) == trap_pc { pc } else { pc - 1 };
|
let pc_to_lookup = if Some(pc) == trap_pc { pc } else { pc - 1 };
|
||||||
if let Some((info, has_unparsed_debuginfo)) =
|
if let Some((info, has_unparsed_debuginfo)) =
|
||||||
store.frame_info().borrow().lookup_frame_info(pc_to_lookup)
|
store.modules().borrow().lookup_frame_info(pc_to_lookup)
|
||||||
{
|
{
|
||||||
wasm_trace.push(info);
|
wasm_trace.push(info);
|
||||||
|
|
||||||
|
|||||||
@@ -204,8 +204,7 @@ impl ExternType {
|
|||||||
) -> ExternType {
|
) -> ExternType {
|
||||||
match ty {
|
match ty {
|
||||||
EntityType::Function(idx) => {
|
EntityType::Function(idx) => {
|
||||||
let sig = &types.wasm_signatures[*idx];
|
FuncType::from_wasm_func_type(types.wasm_signatures[*idx].clone()).into()
|
||||||
FuncType::from_wasm_func_type(sig).into()
|
|
||||||
}
|
}
|
||||||
EntityType::Global(ty) => GlobalType::from_wasmtime_global(ty).into(),
|
EntityType::Global(ty) => GlobalType::from_wasmtime_global(ty).into(),
|
||||||
EntityType::Memory(ty) => MemoryType::from_wasmtime_memory(ty).into(),
|
EntityType::Memory(ty) => MemoryType::from_wasmtime_memory(ty).into(),
|
||||||
@@ -298,8 +297,8 @@ impl FuncType {
|
|||||||
&self.sig
|
&self.sig
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn from_wasm_func_type(sig: &wasm::WasmFuncType) -> FuncType {
|
pub(crate) fn from_wasm_func_type(sig: wasm::WasmFuncType) -> FuncType {
|
||||||
FuncType { sig: sig.clone() }
|
Self { sig }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::{Extern, Store};
|
use crate::{signatures::SharedSignatures, Extern, Store};
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use wasmtime_environ::wasm::{
|
use wasmtime_environ::wasm::{
|
||||||
EntityType, Global, InstanceTypeIndex, Memory, ModuleTypeIndex, SignatureIndex, Table,
|
EntityType, Global, InstanceTypeIndex, Memory, ModuleTypeIndex, SignatureIndex, Table,
|
||||||
@@ -6,6 +6,7 @@ use wasmtime_environ::wasm::{
|
|||||||
use wasmtime_jit::TypeTables;
|
use wasmtime_jit::TypeTables;
|
||||||
|
|
||||||
pub struct MatchCx<'a> {
|
pub struct MatchCx<'a> {
|
||||||
|
pub signatures: &'a SharedSignatures,
|
||||||
pub types: &'a TypeTables,
|
pub types: &'a TypeTables,
|
||||||
pub store: &'a Store,
|
pub store: &'a Store,
|
||||||
}
|
}
|
||||||
@@ -70,13 +71,8 @@ impl MatchCx<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn func(&self, expected: SignatureIndex, actual: &crate::Func) -> Result<()> {
|
pub fn func(&self, expected: SignatureIndex, actual: &crate::Func) -> Result<()> {
|
||||||
let matches = match self
|
let matches = match self.signatures.get(expected) {
|
||||||
.store
|
Some(idx) => actual.sig_index() == *idx,
|
||||||
.signatures()
|
|
||||||
.borrow()
|
|
||||||
.lookup(&self.types.wasm_signatures[expected])
|
|
||||||
{
|
|
||||||
Some(idx) => actual.sig_index() == idx,
|
|
||||||
// If our expected signature isn't registered, then there's no way
|
// If our expected signature isn't registered, then there's no way
|
||||||
// that `actual` can match it.
|
// that `actual` can match it.
|
||||||
None => false,
|
None => false,
|
||||||
@@ -114,15 +110,19 @@ impl MatchCx<'_> {
|
|||||||
let module = actual.compiled_module().module();
|
let module = actual.compiled_module().module();
|
||||||
self.imports_match(
|
self.imports_match(
|
||||||
expected,
|
expected,
|
||||||
|
actual.signatures(),
|
||||||
actual.types(),
|
actual.types(),
|
||||||
module.imports().map(|(name, field, ty)| {
|
module.imports().map(|(name, field, ty)| {
|
||||||
assert!(field.is_none()); // should be true if module linking is enabled
|
assert!(field.is_none()); // should be true if module linking is enabled
|
||||||
(name, ty)
|
(name, ty)
|
||||||
}),
|
}),
|
||||||
)?;
|
)?;
|
||||||
self.exports_match(expected_sig.exports, actual.types(), |name| {
|
self.exports_match(
|
||||||
module.exports.get(name).map(|idx| module.type_of(*idx))
|
expected_sig.exports,
|
||||||
})?;
|
actual.signatures(),
|
||||||
|
actual.types(),
|
||||||
|
|name| module.exports.get(name).map(|idx| module.type_of(*idx)),
|
||||||
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,6 +133,7 @@ impl MatchCx<'_> {
|
|||||||
fn imports_match<'a>(
|
fn imports_match<'a>(
|
||||||
&self,
|
&self,
|
||||||
expected: ModuleTypeIndex,
|
expected: ModuleTypeIndex,
|
||||||
|
actual_signatures: &SharedSignatures,
|
||||||
actual_types: &TypeTables,
|
actual_types: &TypeTables,
|
||||||
actual_imports: impl Iterator<Item = (&'a str, EntityType)>,
|
actual_imports: impl Iterator<Item = (&'a str, EntityType)>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
@@ -146,10 +147,11 @@ impl MatchCx<'_> {
|
|||||||
None => bail!("expected type doesn't import {:?}", name),
|
None => bail!("expected type doesn't import {:?}", name),
|
||||||
};
|
};
|
||||||
MatchCx {
|
MatchCx {
|
||||||
|
signatures: actual_signatures,
|
||||||
types: actual_types,
|
types: actual_types,
|
||||||
store: self.store,
|
store: self.store,
|
||||||
}
|
}
|
||||||
.extern_ty_matches(&actual_ty, expected_ty, self.types)
|
.extern_ty_matches(&actual_ty, expected_ty, self.signatures, self.types)
|
||||||
.with_context(|| format!("module import {:?} incompatible", name))?;
|
.with_context(|| format!("module import {:?} incompatible", name))?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -160,6 +162,7 @@ impl MatchCx<'_> {
|
|||||||
fn exports_match(
|
fn exports_match(
|
||||||
&self,
|
&self,
|
||||||
expected: InstanceTypeIndex,
|
expected: InstanceTypeIndex,
|
||||||
|
actual_signatures: &SharedSignatures,
|
||||||
actual_types: &TypeTables,
|
actual_types: &TypeTables,
|
||||||
lookup: impl Fn(&str) -> Option<EntityType>,
|
lookup: impl Fn(&str) -> Option<EntityType>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
@@ -169,7 +172,7 @@ impl MatchCx<'_> {
|
|||||||
for (name, expected) in self.types.instance_signatures[expected].exports.iter() {
|
for (name, expected) in self.types.instance_signatures[expected].exports.iter() {
|
||||||
match lookup(name) {
|
match lookup(name) {
|
||||||
Some(ty) => self
|
Some(ty) => self
|
||||||
.extern_ty_matches(expected, &ty, actual_types)
|
.extern_ty_matches(expected, &ty, actual_signatures, actual_types)
|
||||||
.with_context(|| format!("export {:?} incompatible", name))?,
|
.with_context(|| format!("export {:?} incompatible", name))?,
|
||||||
None => bail!("failed to find export {:?}", name),
|
None => bail!("failed to find export {:?}", name),
|
||||||
}
|
}
|
||||||
@@ -183,6 +186,7 @@ impl MatchCx<'_> {
|
|||||||
&self,
|
&self,
|
||||||
expected: &EntityType,
|
expected: &EntityType,
|
||||||
actual_ty: &EntityType,
|
actual_ty: &EntityType,
|
||||||
|
actual_signatures: &SharedSignatures,
|
||||||
actual_types: &TypeTables,
|
actual_types: &TypeTables,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let actual_desc = match actual_ty {
|
let actual_desc = match actual_ty {
|
||||||
@@ -221,7 +225,7 @@ impl MatchCx<'_> {
|
|||||||
EntityType::Instance(expected) => match actual_ty {
|
EntityType::Instance(expected) => match actual_ty {
|
||||||
EntityType::Instance(actual) => {
|
EntityType::Instance(actual) => {
|
||||||
let sig = &actual_types.instance_signatures[*actual];
|
let sig = &actual_types.instance_signatures[*actual];
|
||||||
self.exports_match(*expected, actual_types, |name| {
|
self.exports_match(*expected, actual_signatures, actual_types, |name| {
|
||||||
sig.exports.get(name).cloned()
|
sig.exports.get(name).cloned()
|
||||||
})?;
|
})?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -237,15 +241,19 @@ impl MatchCx<'_> {
|
|||||||
|
|
||||||
self.imports_match(
|
self.imports_match(
|
||||||
*expected,
|
*expected,
|
||||||
|
actual_signatures,
|
||||||
actual_types,
|
actual_types,
|
||||||
actual_module_sig
|
actual_module_sig
|
||||||
.imports
|
.imports
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(module, ty)| (module.as_str(), ty.clone())),
|
.map(|(module, ty)| (module.as_str(), ty.clone())),
|
||||||
)?;
|
)?;
|
||||||
self.exports_match(expected_module_sig.exports, actual_types, |name| {
|
self.exports_match(
|
||||||
actual_instance_sig.exports.get(name).cloned()
|
expected_module_sig.exports,
|
||||||
})?;
|
actual_signatures,
|
||||||
|
actual_types,
|
||||||
|
|name| actual_instance_sig.exports.get(name).cloned(),
|
||||||
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
_ => bail!("expected module, but found {}", actual_desc),
|
_ => bail!("expected module, but found {}", actual_desc),
|
||||||
|
|||||||
Reference in New Issue
Block a user