Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Many more component OOM tests (#13088)

Nick Fitzgerald4426555a23dd
diff --git a/crates/fuzzing/tests/oom/component_component.rs b/crates/fuzzing/tests/oom/component_component.rs
new file mode 100644
index 0000000000..617aa476bf
--- /dev/null
+++ b/crates/fuzzing/tests/oom/component_component.rs
@@ -0,0 +1,40 @@
+#![cfg(arc_try_new)]
+
+use wasmtime::component::Component;
+use wasmtime::{Config, Engine, Result};
+use wasmtime_fuzzing::oom::OomTest;
+
+#[test]
+fn component_serialize() -> Result<()> {
+ let component_bytes = {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ Component::new(
+ &engine,
+ r#"
+ (component
+ (core module $m
+ (func (export "id") (param i32) (result i32) (local.get 0))
+ )
+ (core instance $i (instantiate $m))
+ (func (export "id") (param "x" s32) (result s32)
+ (canon lift (core func $i "id"))
+ )
+ )
+ "#,
+ )?
+ .serialize()?
+ };
+ let mut config = Config::new();
+ config.enable_compiler(false);
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ let component = unsafe { Component::deserialize(&engine, &component_bytes)? };
+
+ // Error propagation via anyhow allocates after OOM.
+ OomTest::new().allow_alloc_after_oom(true).test(|| {
+ let _bytes = component.serialize()?;
+ Ok(())
+ })
+}
diff --git a/crates/fuzzing/tests/oom/component_func.rs b/crates/fuzzing/tests/oom/component_func.rs
index 18922b38ca..fcc7beb10f 100644
--- a/crates/fuzzing/tests/oom/component_func.rs
+++ b/crates/fuzzing/tests/oom/component_func.rs
@@ -246,3 +246,42 @@ fn component_typed_func_call() -> Result<()> {
Ok(())
})
}
+
+#[test]
+fn component_func_typed() -> Result<()> {
+ let component_bytes = {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ Component::new(
+ &engine,
+ r#"
+ (component
+ (core module $m
+ (func (export "id") (param i32) (result i32) (local.get 0))
+ )
+ (core instance $i (instantiate $m))
+ (func (export "id") (param "x" s32) (result s32)
+ (canon lift (core func $i "id"))
+ )
+ )
+ "#,
+ )?
+ .serialize()?
+ };
+ let mut config = Config::new();
+ config.enable_compiler(false);
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ let component = unsafe { Component::deserialize(&engine, &component_bytes)? };
+ let linker = Linker::<()>::new(&engine);
+ let instance_pre = linker.instantiate_pre(&component)?;
+
+ OomTest::new().test(|| {
+ let mut store = Store::try_new(&engine, ())?;
+ let instance = instance_pre.instantiate(&mut store)?;
+ let func = instance.get_func(&mut store, "id").unwrap();
+ let _typed = func.typed::<(i32,), (i32,)>(&store)?;
+ Ok(())
+ })
+}
diff --git a/crates/fuzzing/tests/oom/component_instance.rs b/crates/fuzzing/tests/oom/component_instance.rs
index 36f8b2f953..de7ca87355 100644
--- a/crates/fuzzing/tests/oom/component_instance.rs
+++ b/crates/fuzzing/tests/oom/component_instance.rs
@@ -40,3 +40,41 @@ fn instantiate_in_pooling_allocator() -> Result<()> {
Ok(())
})
}
+
+#[test]
+fn component_instance_get_export() -> Result<()> {
+ let component_bytes = {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ Component::new(
+ &engine,
+ r#"
+ (component
+ (core module $m
+ (func (export "id") (param i32) (result i32) (local.get 0))
+ )
+ (core instance $i (instantiate $m))
+ (func (export "id") (param "x" s32) (result s32)
+ (canon lift (core func $i "id"))
+ )
+ )
+ "#,
+ )?
+ .serialize()?
+ };
+ let mut config = Config::new();
+ config.enable_compiler(false);
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ let component = unsafe { Component::deserialize(&engine, &component_bytes)? };
+ let linker = Linker::<()>::new(&engine);
+ let instance_pre = linker.instantiate_pre(&component)?;
+
+ OomTest::new().test(|| {
+ let mut store = Store::try_new(&engine, ())?;
+ let instance = instance_pre.instantiate(&mut store)?;
+ let _export = instance.get_export(&mut store, None, "id");
+ Ok(())
+ })
+}
diff --git a/crates/fuzzing/tests/oom/component_linker.rs b/crates/fuzzing/tests/oom/component_linker.rs
index 97e00dc98c..e627b5b370 100644
--- a/crates/fuzzing/tests/oom/component_linker.rs
+++ b/crates/fuzzing/tests/oom/component_linker.rs
@@ -1,7 +1,7 @@
#![cfg(arc_try_new)]
-use wasmtime::component::{Component, Linker};
-use wasmtime::{Config, Engine, Result, Store};
+use wasmtime::component::{Component, Linker, ResourceType};
+use wasmtime::{Config, Engine, Module, Result, Store};
use wasmtime_fuzzing::oom::OomTest;
#[tokio::test]
@@ -147,3 +147,103 @@ fn component_linker_substituted_component_type() -> Result<()> {
Ok(())
})
}
+
+#[test]
+fn component_linker_define_unknown_imports_as_traps() -> Result<()> {
+ let component_bytes = {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ Component::new(
+ &engine,
+ r#"
+ (component
+ (import "f" (func))
+ )
+ "#,
+ )?
+ .serialize()?
+ };
+ let mut config = Config::new();
+ config.enable_compiler(false);
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ let component = unsafe { Component::deserialize(&engine, &component_bytes)? };
+
+ // Error propagation via anyhow allocates after OOM.
+ OomTest::new().allow_alloc_after_oom(true).test(|| {
+ let mut linker = Linker::<()>::new(&engine);
+ linker.define_unknown_imports_as_traps(&component)?;
+ Ok(())
+ })
+}
+
+#[test]
+fn component_linker_instance_func_wrap() -> Result<()> {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+
+ // Error propagation via anyhow allocates after OOM.
+ OomTest::new().allow_alloc_after_oom(true).test(|| {
+ let mut linker = Linker::<()>::new(&engine);
+ linker
+ .root()
+ .func_wrap("f", |_cx: wasmtime::StoreContextMut<'_, ()>, (): ()| Ok(()))?;
+ Ok(())
+ })
+}
+
+#[test]
+fn component_linker_instance_func_new() -> Result<()> {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+
+ // Error propagation via anyhow allocates after OOM.
+ OomTest::new().allow_alloc_after_oom(true).test(|| {
+ let mut linker = Linker::<()>::new(&engine);
+ linker
+ .root()
+ .func_new("f", |_cx, _func_ty, _params, _results| Ok(()))?;
+ Ok(())
+ })
+}
+
+#[test]
+fn component_linker_instance_module() -> Result<()> {
+ let module_bytes = {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ Module::new(&engine, "(module)")?.serialize()?
+ };
+ let mut config = Config::new();
+ config.enable_compiler(false);
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
+
+ // Error propagation via anyhow allocates after OOM.
+ OomTest::new().allow_alloc_after_oom(true).test(|| {
+ let mut linker = Linker::<()>::new(&engine);
+ linker.root().module("m", &module)?;
+ Ok(())
+ })
+}
+
+#[test]
+fn component_linker_instance_resource() -> Result<()> {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+
+ // Error propagation from HostFunc::wrap allocates via anyhow.
+ OomTest::new().allow_alloc_after_oom(true).test(|| {
+ let mut linker = Linker::<()>::new(&engine);
+ linker
+ .root()
+ .resource("r", ResourceType::host::<()>(), |_, _| Ok(()))?;
+ Ok(())
+ })
+}
diff --git a/crates/fuzzing/tests/oom/component_resource.rs b/crates/fuzzing/tests/oom/component_resource.rs
index 40786131c2..7af9446d5a 100644
--- a/crates/fuzzing/tests/oom/component_resource.rs
+++ b/crates/fuzzing/tests/oom/component_resource.rs
@@ -1,6 +1,6 @@
#![cfg(arc_try_new)]
-use wasmtime::component::{Component, Linker, ResourceAny};
+use wasmtime::component::{Component, Linker, Resource, ResourceAny, ResourceType};
use wasmtime::{Config, Engine, Result, Store};
use wasmtime_fuzzing::oom::OomTest;
@@ -87,3 +87,81 @@ fn component_resource_any_resource_drop() -> Result<()> {
Ok(())
})
}
+
+struct MyResource;
+
+#[test]
+fn component_resource_any_try_from_resource() -> Result<()> {
+ let component_bytes = {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ Component::new(
+ &engine,
+ r#"
+ (component
+ (import "t" (type $t (sub resource)))
+ )
+ "#,
+ )?
+ .serialize()?
+ };
+ let mut config = Config::new();
+ config.enable_compiler(false);
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ let component = unsafe { Component::deserialize(&engine, &component_bytes)? };
+ let mut linker = Linker::<()>::new(&engine);
+ linker
+ .root()
+ .resource("t", ResourceType::host::<MyResource>(), |_, _| Ok(()))?;
+ let instance_pre = linker.instantiate_pre(&component)?;
+
+ // Error propagation via anyhow allocates after OOM.
+ OomTest::new().allow_alloc_after_oom(true).test(|| {
+ let mut store = Store::try_new(&engine, ())?;
+ let _instance = instance_pre.instantiate(&mut store)?;
+ let resource = Resource::<MyResource>::new_own(42);
+ let any = ResourceAny::try_from_resource(resource, &mut store)?;
+ let _typed: Resource<MyResource> = any.try_into_resource(&mut store)?;
+ Ok(())
+ })
+}
+
+#[test]
+fn component_resource_any_try_into_resource() -> Result<()> {
+ let component_bytes = {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ Component::new(
+ &engine,
+ r#"
+ (component
+ (import "t" (type $t (sub resource)))
+ )
+ "#,
+ )?
+ .serialize()?
+ };
+ let mut config = Config::new();
+ config.enable_compiler(false);
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ let component = unsafe { Component::deserialize(&engine, &component_bytes)? };
+ let mut linker = Linker::<()>::new(&engine);
+ linker
+ .root()
+ .resource("t", ResourceType::host::<MyResource>(), |_, _| Ok(()))?;
+ let instance_pre = linker.instantiate_pre(&component)?;
+
+ // Error propagation via anyhow allocates after OOM.
+ OomTest::new().allow_alloc_after_oom(true).test(|| {
+ let mut store = Store::try_new(&engine, ())?;
+ let _instance = instance_pre.instantiate(&mut store)?;
+ let resource = Resource::<MyResource>::new_own(100);
+ let any = ResourceAny::try_from_resource(resource, &mut store)?;
+ let _back: Resource<MyResource> = any.try_into_resource(&mut store)?;
+ Ok(())
+ })
+}
diff --git a/crates/fuzzing/tests/oom/main.rs b/crates/fuzzing/tests/oom/main.rs
index f904281583..e706f3a009 100644
--- a/crates/fuzzing/tests/oom/main.rs
+++ b/crates/fuzzing/tests/oom/main.rs
@@ -5,6 +5,7 @@ mod bit_set;
mod boxed;
mod btree_map;
mod caller;
+mod component_component;
mod component_func;
mod component_instance;
mod component_linker;
diff --git a/crates/wasmtime/src/runtime/component/component.rs b/crates/wasmtime/src/runtime/component/component.rs
index 9140cc8619..c079c0f812 100644
--- a/crates/wasmtime/src/runtime/component/component.rs
+++ b/crates/wasmtime/src/runtime/component/component.rs
@@ -592,7 +592,11 @@ impl Component {
/// [`Module::serialize`]: crate::Module::serialize
/// [`Module`]: crate::Module
pub fn serialize(&self) -> Result<Vec<u8>> {
- Ok(self.engine_code().image().to_vec())
+ let image = self.engine_code().image();
+ let mut v = TryVec::new();
+ v.reserve(image.len())?;
+ v.try_extend(image.iter().copied())?;
+ Ok(v.into())
}
/// Creates a new `VMFuncRef` with all fields filled out for the destructor
diff --git a/crates/wasmtime/src/runtime/component/linker.rs b/crates/wasmtime/src/runtime/component/linker.rs
index 608473a48c..5f9d2e8bb8 100644
--- a/crates/wasmtime/src/runtime/component/linker.rs
+++ b/crates/wasmtime/src/runtime/component/linker.rs
@@ -757,10 +757,10 @@ impl<T: 'static> LinkerInstance<'_, T> {
ty: ResourceType,
dtor: impl Fn(StoreContextMut<'_, T>, u32) -> Result<()> + Send + Sync + 'static,
) -> Result<()> {
- let dtor = Arc::new(crate::func::HostFunc::wrap(
+ let dtor = try_new::<Arc<_>>(crate::func::HostFunc::wrap(
&self.engine,
move |mut cx: crate::Caller<'_, T>, (param,): (u32,)| dtor(cx.as_context_mut(), param),
- )?);
+ )?)?;
self.insert(name, Definition::Resource(ty, dtor))?;
Ok(())
}
@@ -775,10 +775,10 @@ impl<T: 'static> LinkerInstance<'_, T> {
+ Sync
+ 'static,
{
- let dtor = Arc::new(crate::func::HostFunc::wrap_async(
+ let dtor = try_new::<Arc<_>>(crate::func::HostFunc::wrap_async(
&self.engine,
move |cx: crate::Caller<'_, T>, (param,): (u32,)| dtor(cx.into(), param),
- )?);
+ )?)?;
self.insert(name, Definition::Resource(ty, dtor))?;
Ok(())
}