Skip to content

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

More component OOM tests (#13074)

Nick Fitzgerald66239cb94acb
diff --git a/crates/fuzzing/tests/oom/component_func.rs b/crates/fuzzing/tests/oom/component_func.rs
index b0124d067d..18922b38ca 100644
--- a/crates/fuzzing/tests/oom/component_func.rs
+++ b/crates/fuzzing/tests/oom/component_func.rs
@@ -128,3 +128,121 @@ async fn component_typed_func_call_async() -> Result<()> {
})
.await
}
+
+#[test]
+fn component_instance_pre_instantiate() -> 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)?;
+ Ok(())
+ })
+}
+
+#[test]
+fn component_func_call() -> 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 mut results = [Val::S32(0)];
+ func.call(&mut store, &[Val::S32(42)], &mut results)?;
+ assert_eq!(results[0], Val::S32(42));
+ Ok(())
+ })
+}
+
+#[test]
+fn component_typed_func_call() -> 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 id = instance.get_typed_func::<(i32,), (i32,)>(&mut store, "id")?;
+ let result = id.call(&mut store, (42,))?;
+ assert_eq!(result, (42,));
+ Ok(())
+ })
+}
diff --git a/crates/fuzzing/tests/oom/component_linker.rs b/crates/fuzzing/tests/oom/component_linker.rs
index 3da84a4a96..97e00dc98c 100644
--- a/crates/fuzzing/tests/oom/component_linker.rs
+++ b/crates/fuzzing/tests/oom/component_linker.rs
@@ -41,3 +41,109 @@ async fn component_linker_instantiate_async() -> Result<()> {
})
.await
}
+
+#[test]
+fn component_linker_instantiate() -> 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);
+
+ OomTest::new().test(|| {
+ let mut store = Store::try_new(&engine, ())?;
+ let _instance = linker.instantiate(&mut store, &component)?;
+ Ok(())
+ })
+}
+
+#[test]
+fn component_linker_instantiate_pre() -> 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);
+
+ OomTest::new().test(|| {
+ let _instance_pre = linker.instantiate_pre(&component)?;
+ Ok(())
+ })
+}
+
+#[test]
+fn component_linker_substituted_component_type() -> 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);
+
+ OomTest::new().test(|| {
+ let _ty = linker.substituted_component_type(&component)?;
+ Ok(())
+ })
+}
diff --git a/crates/fuzzing/tests/oom/component_resource.rs b/crates/fuzzing/tests/oom/component_resource.rs
index 6f9e844cc7..40786131c2 100644
--- a/crates/fuzzing/tests/oom/component_resource.rs
+++ b/crates/fuzzing/tests/oom/component_resource.rs
@@ -46,3 +46,44 @@ async fn component_resource_any_resource_drop_async() -> Result<()> {
})
.await
}
+
+#[test]
+fn component_resource_any_resource_drop() -> Result<()> {
+ let component_bytes = {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ Component::new(
+ &engine,
+ r#"
+ (component
+ (type $t' (resource (rep i32)))
+ (export $t "t" (type $t'))
+
+ (core func $new (canon resource.new $t))
+ (func (export "mk") (param "r" u32) (result (own $t))
+ (canon lift (core func $new))
+ )
+ )
+ "#,
+ )?
+ .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)?;
+
+ // 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 mk = instance.get_typed_func::<(u32,), (ResourceAny,)>(&mut store, "mk")?;
+ let (resource,) = mk.call(&mut store, (42,))?;
+ resource.resource_drop(&mut store)?;
+ Ok(())
+ })
+}