A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Handle OOM in component `instantiate_pre` more (#14759)
Alex Crichton04226ad1e843
diff --git a/crates/core/src/error/oom.rs b/crates/core/src/error/oom.rsindex 138de94435..e100d0d567 100644--- a/crates/core/src/error/oom.rs+++ b/crates/core/src/error/oom.rs@@ -57,7 +57,7 @@ use core::{fmt, mem, ptr::NonNull};/// |  `wasmtime::Func::call` | Yes |/// |  Wasm execution | Yes |/// |  Garbage collector and related APIs | Not yet |-/// |  Component Model concurrency/async APIs | Not yet |+/// |  Component Model with `concurrency_support` | Not yet |/// |  Other instantiation and execution APIs... | Yes |/// | **WASI Implementations and Host APIs** | **Depends** |/// |  `wasmtime_wasi` | No |diff --git a/crates/environ/src/collections/primary_map.rs b/crates/environ/src/collections/primary_map.rsindex a44dd4b8b7..6ed542091f 100644--- a/crates/environ/src/collections/primary_map.rs+++ b/crates/environ/src/collections/primary_map.rs@@ -289,3 +289,21 @@ whereself.iter_mut()}}++impl<K, V> From<cranelift_entity::PrimaryMap<K, V>> for TryPrimaryMap<K, V>+where+ K: EntityRef,+{+ fn from(inner: cranelift_entity::PrimaryMap<K, V>) -> Self {+ Self { inner }+ }+}++impl<K, V> From<TryPrimaryMap<K, V>> for cranelift_entity::PrimaryMap<K, V>+where+ K: EntityRef,+{+ fn from(try_map: TryPrimaryMap<K, V>) -> Self {+ try_map.inner+ }+}diff --git a/crates/fuzzing/tests/oom/component_linker.rs b/crates/fuzzing/tests/oom/component_linker.rsindex e627b5b370..8a0caa58e2 100644--- a/crates/fuzzing/tests/oom/component_linker.rs+++ b/crates/fuzzing/tests/oom/component_linker.rs@@ -52,10 +52,15 @@ fn component_linker_instantiate() -> Result<()> {&engine,r#"(component+ (import "f" (func $f))+ (core func $f (canon lower (func $f)))(core module $m+ (import "" "f" (func))(func (export "id") (param i32) (result i32) (local.get 0)))- (core instance $i (instantiate $m))+ (core instance $i (instantiate $m+ (with "" (instance (export "f" (func $f))))+ ))(func (export "id") (param "x" s32) (result s32)(canon lift (core func $i "id")))@@ -69,7 +74,8 @@ fn component_linker_instantiate() -> Result<()> {config.concurrency_support(false);let engine = Engine::new(&config)?;let component = unsafe { Component::deserialize(&engine, &component_bytes)? };- let linker = Linker::<()>::new(&engine);+ let mut linker = Linker::<()>::new(&engine);+ linker.root().func_wrap("f", |_, (): ()| Ok(()))?;OomTest::new().test(|| {let mut store = Store::try_new(&engine, ())?;diff --git a/crates/wasmtime/src/runtime/component/linker.rs b/crates/wasmtime/src/runtime/component/linker.rsindex 89cf2fd7f1..bdfdd21ed3 100644--- a/crates/wasmtime/src/runtime/component/linker.rs+++ b/crates/wasmtime/src/runtime/component/linker.rs@@ -250,7 +250,7 @@ impl<T: 'static> Linker<T> {// using the import map within the component created at// component-compile-time.let env_component = component.env_component();- let mut imports = PrimaryMap::with_capacity(env_component.imports.len());+ let mut imports = TryPrimaryMap::with_capacity(env_component.imports.len())?;for (idx, (import, names)) in env_component.imports.iter() {let (root, _) = &env_component.import_types[*import];@@ -277,13 +277,13 @@ impl<T: 'static> Linker<T> {// runtime imports are never instances.Definition::Instance(_) => unreachable!(),};- let i = imports.push(import);+ let i = imports.push(import)?;assert_eq!(i, idx);}Ok(unsafe {InstancePre::new_unchecked(component.clone(),- try_new::<Arc<_>>(imports)?,+ try_new::<Arc<_>>(PrimaryMap::from(imports))?,imported_resources,)})