A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Share empty `ModuleRuntimeInfo`s across all stores in an engine (#12409)
Nick Fitzgerald17899c88eb39
diff --git a/ci/build-build-matrix.js b/ci/build-build-matrix.jsindex c3624159e7..eb75021abf 100644--- a/ci/build-build-matrix.js+++ b/ci/build-build-matrix.js@@ -44,6 +44,11 @@ const array = [// On OSX all we need to do is configure our deployment target as old as// possible. For now 10.12 is the limit."env": { "MACOSX_DEPLOYMENT_TARGET": "10.12" },+ // Similar to https://github.com/bytecodealliance/wasmtime/pull/12245, we+ // need to avoid a rustc bug that results in linker errors depending on the+ // order and division of code into CGUs. This is fixed on nightly but not+ // stable yet.+ "rust": "wasmtime-ci-pinned-nightly",},{"build": "aarch64-macos",diff --git a/crates/wasmtime/src/engine.rs b/crates/wasmtime/src/engine.rsindex 8b155791cf..c55df6210c 100644--- a/crates/wasmtime/src/engine.rs+++ b/crates/wasmtime/src/engine.rs@@ -5,7 +5,7 @@ pub use crate::runtime::code_memory::CustomCodeMemory;#[cfg(feature = "runtime")]use crate::runtime::type_registry::TypeRegistry;#[cfg(feature = "runtime")]-use crate::runtime::vm::GcRuntime;+use crate::runtime::vm::{GcRuntime, ModuleRuntimeInfo};use alloc::sync::Arc;use core::ptr::NonNull;#[cfg(target_has_atomic = "64")]@@ -66,6 +66,12 @@ struct EngineInner {/// One-time check of whether the compiler's settings, if present, are/// compatible with the native host.compatible_with_native_host: crate::sync::OnceLock<Result<(), String>>,++ /// The canonical empty `ModuleRuntimeInfo`, so that each store doesn't need+ /// allocate its own copy when creating its default caller instance or GC+ /// heap.+ #[cfg(feature = "runtime")]+ empty_module_runtime_info: ModuleRuntimeInfo,}impl core::fmt::Debug for Engine {@@ -122,6 +128,11 @@ impl Engine {#[cfg(not(any(feature = "cranelift", feature = "winch")))]let _ = &mut tunables;+ #[cfg(feature = "runtime")]+ let empty_module_runtime_info = ModuleRuntimeInfo::bare(try_new(+ wasmtime_environ::Module::new(wasmtime_environ::StaticModuleIndex::from_u32(0)),+ )?)?;+Ok(Engine {inner: try_new::<Arc<_>>(EngineInner {#[cfg(any(feature = "cranelift", feature = "winch"))]@@ -150,6 +161,8 @@ impl Engine {config,tunables,features,+ #[cfg(feature = "runtime")]+ empty_module_runtime_info,})?,})}@@ -620,6 +633,11 @@ information about this check\pub fn is_pulley(&self) -> bool {self.target().is_pulley()}++ #[cfg(feature = "runtime")]+ pub(crate) fn empty_module_runtime_info(&self) -> &ModuleRuntimeInfo {+ &self.inner.empty_module_runtime_info+ }}#[cfg(any(feature = "cranelift", feature = "winch"))]diff --git a/crates/wasmtime/src/runtime/store.rs b/crates/wasmtime/src/runtime/store.rsindex 4671bc8f7b..bfdb069e57 100644--- a/crates/wasmtime/src/runtime/store.rs+++ b/crates/wasmtime/src/runtime/store.rs@@ -107,7 +107,6 @@ use crate::{Engine, Module, Val, ValRaw, module::ModuleRegistry};#[cfg(feature = "gc")]use crate::{ExnRef, Rooted};use crate::{Global, Instance, Table};-use alloc::sync::Arc;use core::convert::Infallible;use core::fmt;use core::marker;@@ -116,7 +115,6 @@ use core::num::NonZeroU64;use core::ops::{Deref, DerefMut};use core::pin::Pin;use core::ptr::NonNull;-use wasmtime_environ::StaticModuleIndex;use wasmtime_environ::{DefinedGlobalIndex, DefinedTableIndex, EntityRef, PrimaryMap, TripleExt};mod context;@@ -809,14 +807,10 @@ impl<T> Store<T> {// single "default callee" for the entire `Store`. This is then used as// part of `Func::call` to guarantee that the `callee: *mut VMContext`// is never null.- let module = Arc::new(wasmtime_environ::Module::new(StaticModuleIndex::from_u32(- 0,- )));- let shim = ModuleRuntimeInfo::bare(module);let allocator = OnDemandInstanceAllocator::default();-+ let info = engine.empty_module_runtime_info();allocator- .validate_module(shim.env_module(), shim.offsets())+ .validate_module(info.env_module(), info.offsets()).unwrap();unsafe {@@ -829,7 +823,7 @@ impl<T> Store<T> {AllocateInstanceKind::Dummy {allocator: &allocator,},- &shim,+ info,Default::default(),)).expect("failed to allocate default callee");@@ -1934,7 +1928,7 @@ impl StoreOpaque {store: &mut StoreOpaque,limiter: Option<&mut StoreResourceLimiter<'_>>,) -> Result<GcStore> {- use wasmtime_environ::{StaticModuleIndex, packed_option::ReservedValue};+ use wasmtime_environ::packed_option::ReservedValue;let engine = store.engine();let mem_ty = engine.tunables().gc_heap_memory_type();@@ -1946,9 +1940,7 @@ impl StoreOpaque {// First, allocate the memory that will be our GC heap's storage.let mut request = InstanceAllocationRequest {id: InstanceId::reserved_value(),- runtime_info: &ModuleRuntimeInfo::bare(Arc::new(wasmtime_environ::Module::new(- StaticModuleIndex::from_u32(0),- ))),+ runtime_info: engine.empty_module_runtime_info(),imports: vm::Imports::default(),store,limiter,diff --git a/crates/wasmtime/src/runtime/trampoline/memory.rs b/crates/wasmtime/src/runtime/trampoline/memory.rsindex 21854b314f..b088dfb172 100644--- a/crates/wasmtime/src/runtime/trampoline/memory.rs+++ b/crates/wasmtime/src/runtime/trampoline/memory.rs@@ -41,6 +41,7 @@ pub async fn create_memory(module.exports.insert(String::new(), EntityIndex::Memory(memory_id));+ let info = ModuleRuntimeInfo::bare(Arc::new(module))?;// We create an instance in the on-demand allocator when creating handles// associated with external objects. The configured instance allocator@@ -57,7 +58,7 @@ pub async fn create_memory(AllocateInstanceKind::Dummy {allocator: &allocator,},- &ModuleRuntimeInfo::bare(Arc::new(module)),+ &info,Default::default(),).awaitdiff --git a/crates/wasmtime/src/runtime/trampoline/table.rs b/crates/wasmtime/src/runtime/trampoline/table.rsindex 745fa494a1..6cf885e746 100644--- a/crates/wasmtime/src/runtime/trampoline/table.rs+++ b/crates/wasmtime/src/runtime/trampoline/table.rs@@ -43,7 +43,7 @@ pub async fn create_table(&ModuleRuntimeInfo::bare_with_registered_type(module,table.element().clone().into_registered_type(),- ),+ )?,imports,).awaitdiff --git a/crates/wasmtime/src/runtime/trampoline/tag.rs b/crates/wasmtime/src/runtime/trampoline/tag.rsindex aa4de83766..33678928fd 100644--- a/crates/wasmtime/src/runtime/trampoline/tag.rs+++ b/crates/wasmtime/src/runtime/trampoline/tag.rs@@ -48,7 +48,7 @@ pub fn create_tag(store: &mut StoreOpaque, ty: &TagType) -> Result<InstanceId> {AllocateInstanceKind::Dummy {allocator: &allocator,},- &ModuleRuntimeInfo::bare_with_registered_type(module, Some(func_ty)),+ &ModuleRuntimeInfo::bare_with_registered_type(module, Some(func_ty))?,imports,))}diff --git a/crates/wasmtime/src/runtime/vm.rs b/crates/wasmtime/src/runtime/vm.rsindex fd9ff41e58..37e8fdad05 100644--- a/crates/wasmtime/src/runtime/vm.rs+++ b/crates/wasmtime/src/runtime/vm.rs@@ -44,6 +44,7 @@ use core::pin::pin;use core::ptr::NonNull;use core::sync::atomic::{AtomicUsize, Ordering};use core::task::{Context, Poll, Waker};+use wasmtime_environ::error::OutOfMemory;use wasmtime_environ::{DefinedMemoryIndex, HostPtr, VMOffsets, VMSharedTypeIndex};#[cfg(feature = "gc")]@@ -284,19 +285,8 @@ struct VMStoreRawPtr(pub NonNull<dyn VMStore>);unsafe impl Send for VMStoreRawPtr {}unsafe impl Sync for VMStoreRawPtr {}-/// Functionality required by this crate for a particular module. This-/// is chiefly needed for lazy initialization of various bits of-/// instance state.-///-/// When an instance is created, it holds an `Arc<dyn ModuleRuntimeInfo>`-/// so that it can get to signatures, metadata on functions, memory and-/// funcref-table images, etc. All of these things are ordinarily known-/// by the higher-level layers of Wasmtime. Specifically, the main-/// implementation of this trait is provided by-/// `wasmtime::module::ModuleInner`. Since the runtime crate sits at-/// the bottom of the dependence DAG though, we don't know or care about-/// that; we just need some implementor of this trait for each-/// allocation request.+/// Functionality required by this crate for a particular module. This is+/// chiefly needed for lazy initialization of various bits of instance state.#[derive(Clone)]pub enum ModuleRuntimeInfo {Module(crate::Module),@@ -315,19 +305,20 @@ pub struct BareModuleInfo {}impl ModuleRuntimeInfo {- pub(crate) fn bare(module: Arc<wasmtime_environ::Module>) -> Self {+ pub(crate) fn bare(module: Arc<wasmtime_environ::Module>) -> Result<Self, OutOfMemory> {ModuleRuntimeInfo::bare_with_registered_type(module, None)}pub(crate) fn bare_with_registered_type(module: Arc<wasmtime_environ::Module>,registered_type: Option<RegisteredType>,- ) -> Self {- ModuleRuntimeInfo::Bare(Box::new(BareModuleInfo {+ ) -> Result<Self, OutOfMemory> {+ let info = try_new(BareModuleInfo {offsets: VMOffsets::new(HostPtr, &module),module,_registered_type: registered_type,- }))+ })?;+ Ok(ModuleRuntimeInfo::Bare(info))}/// The underlying Module.