Skip to content

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

Fix an inconsistent FuncRefs hole on OOM (#14773)

Alex Crichtond235e33a25b6
diff --git a/crates/fuzzing/tests/oom/caller.rs b/crates/fuzzing/tests/oom/caller.rs
index c15071fa7b..8198b713ba 100644
--- a/crates/fuzzing/tests/oom/caller.rs
+++ b/crates/fuzzing/tests/oom/caller.rs
@@ -5,25 +5,17 @@ use wasmtime_fuzzing::oom::OomTest;
#[test]
fn caller_get_export() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module
- (import "" "f" (func))
- (memory (export "m") 1)
- (func (export "run") (call 0))
- )"#,
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ r#"(module
+ (import "" "f" (func))
+ (memory (export "m") 1)
+ (func (export "run") (call 0))
+ )"#,
+ )?;
OomTest::new().test(|| {
let mut store = Store::try_new(&engine, ())?;
@@ -45,24 +37,16 @@ fn caller_get_export() -> Result<()> {
#[test]
fn caller_data() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module
- (import "" "f" (func))
- (func (export "run") (call 0))
- )"#,
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ r#"(module
+ (import "" "f" (func))
+ (func (export "run") (call 0))
+ )"#,
+ )?;
OomTest::new().test(|| {
let mut store = Store::try_new(&engine, 42u32)?;
@@ -83,24 +67,16 @@ fn caller_data() -> Result<()> {
#[test]
fn caller_engine() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module
- (import "" "f" (func))
- (func (export "run") (call 0))
- )"#,
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ r#"(module
+ (import "" "f" (func))
+ (func (export "run") (call 0))
+ )"#,
+ )?;
OomTest::new().test(|| {
let mut store = Store::try_new(&engine, ())?;
diff --git a/crates/fuzzing/tests/oom/component_component.rs b/crates/fuzzing/tests/oom/component_component.rs
index 617aa476bf..19d33685c3 100644
--- a/crates/fuzzing/tests/oom/component_component.rs
+++ b/crates/fuzzing/tests/oom/component_component.rs
@@ -6,31 +6,23 @@ 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)? };
+ let component = 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"))
+ )
+ )
+ "#,
+ )?;
// Error propagation via anyhow allocates after OOM.
OomTest::new().allow_alloc_after_oom(true).test(|| {
diff --git a/crates/fuzzing/tests/oom/component_func.rs b/crates/fuzzing/tests/oom/component_func.rs
index fcc7beb10f..c3dce29702 100644
--- a/crates/fuzzing/tests/oom/component_func.rs
+++ b/crates/fuzzing/tests/oom/component_func.rs
@@ -6,31 +6,23 @@ use wasmtime_fuzzing::oom::OomTest;
#[tokio::test]
async fn component_instance_pre_instantiate_async() -> 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 component = 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"))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&component)?;
@@ -45,31 +37,23 @@ async fn component_instance_pre_instantiate_async() -> Result<()> {
#[tokio::test]
async fn component_func_call_async() -> 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 component = 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"))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&component)?;
@@ -89,31 +73,23 @@ async fn component_func_call_async() -> Result<()> {
#[tokio::test]
async fn component_typed_func_call_async() -> 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 component = 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"))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&component)?;
@@ -131,31 +107,23 @@ async fn component_typed_func_call_async() -> Result<()> {
#[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 component = 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"))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&component)?;
@@ -168,31 +136,23 @@ fn component_instance_pre_instantiate() -> Result<()> {
#[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 component = 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"))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&component)?;
@@ -209,31 +169,23 @@ fn component_func_call() -> Result<()> {
#[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 component = 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"))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&component)?;
@@ -249,31 +201,23 @@ fn component_typed_func_call() -> Result<()> {
#[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 component = 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"))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&component)?;
diff --git a/crates/fuzzing/tests/oom/component_instance.rs b/crates/fuzzing/tests/oom/component_instance.rs
index de7ca87355..8721fa1392 100644
--- a/crates/fuzzing/tests/oom/component_instance.rs
+++ b/crates/fuzzing/tests/oom/component_instance.rs
@@ -43,31 +43,23 @@ fn instantiate_in_pooling_allocator() -> Result<()> {
#[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 component = 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"))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&component)?;
diff --git a/crates/fuzzing/tests/oom/component_linker.rs b/crates/fuzzing/tests/oom/component_linker.rs
index 8a0caa58e2..1d65d852f4 100644
--- a/crates/fuzzing/tests/oom/component_linker.rs
+++ b/crates/fuzzing/tests/oom/component_linker.rs
@@ -6,31 +6,23 @@ use wasmtime_fuzzing::oom::OomTest;
#[tokio::test]
async fn component_linker_instantiate_async() -> 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 component = 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"))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
OomTest::new()
@@ -44,36 +36,28 @@ async fn component_linker_instantiate_async() -> Result<()> {
#[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
- (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
- (with "" (instance (export "f" (func $f))))
- ))
- (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 component = Component::new(
+ &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
+ (with "" (instance (export "f" (func $f))))
+ ))
+ (func (export "id") (param "x" s32) (result s32)
+ (canon lift (core func $i "id"))
+ )
+ )
+ "#,
+ )?;
let mut linker = Linker::<()>::new(&engine);
linker.root().func_wrap("f", |_, (): ()| Ok(()))?;
@@ -86,31 +70,23 @@ fn component_linker_instantiate() -> Result<()> {
#[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 component = 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"))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
OomTest::new().test(|| {
@@ -121,31 +97,23 @@ fn component_linker_instantiate_pre() -> Result<()> {
#[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 component = 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"))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
OomTest::new().test(|| {
@@ -156,25 +124,17 @@ fn component_linker_substituted_component_type() -> Result<()> {
#[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)? };
+ let component = Component::new(
+ &engine,
+ r#"
+ (component
+ (import "f" (func))
+ )
+ "#,
+ )?;
// Error propagation via anyhow allocates after OOM.
OomTest::new().allow_alloc_after_oom(true).test(|| {
@@ -218,17 +178,10 @@ fn component_linker_instance_func_new() -> Result<()> {
#[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)? };
+ let module = Module::new(&engine, "(module)")?;
// Error propagation via anyhow allocates after OOM.
OomTest::new().allow_alloc_after_oom(true).test(|| {
diff --git a/crates/fuzzing/tests/oom/component_resource.rs b/crates/fuzzing/tests/oom/component_resource.rs
index 7af9446d5a..df92c82b3f 100644
--- a/crates/fuzzing/tests/oom/component_resource.rs
+++ b/crates/fuzzing/tests/oom/component_resource.rs
@@ -6,31 +6,23 @@ use wasmtime_fuzzing::oom::OomTest;
#[tokio::test]
async fn component_resource_any_resource_drop_async() -> 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 component = 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))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&component)?;
@@ -49,31 +41,23 @@ async fn component_resource_any_resource_drop_async() -> Result<()> {
#[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 component = 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))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&component)?;
@@ -92,25 +76,17 @@ 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 component = Component::new(
+ &engine,
+ r#"
+ (component
+ (import "t" (type $t (sub resource)))
+ )
+ "#,
+ )?;
let mut linker = Linker::<()>::new(&engine);
linker
.root()
@@ -130,25 +106,17 @@ fn component_resource_any_try_from_resource() -> Result<()> {
#[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 component = Component::new(
+ &engine,
+ r#"
+ (component
+ (import "t" (type $t (sub resource)))
+ )
+ "#,
+ )?;
let mut linker = Linker::<()>::new(&engine);
linker
.root()
diff --git a/crates/fuzzing/tests/oom/func.rs b/crates/fuzzing/tests/oom/func.rs
index baf64bc933..ff8b524f02 100644
--- a/crates/fuzzing/tests/oom/func.rs
+++ b/crates/fuzzing/tests/oom/func.rs
@@ -1,6 +1,8 @@
#![cfg(arc_try_new)]
-use wasmtime::{Config, Engine, Func, FuncType, Linker, Module, Result, Store, Val, ValType};
+use wasmtime::{
+ Config, Engine, Func, FuncType, Instance, Linker, Module, Result, Store, Val, ValType,
+};
use wasmtime_fuzzing::oom::OomTest;
#[test]
@@ -35,23 +37,40 @@ fn func_new_with_type() -> Result<()> {
})
}
+#[test]
+fn func_new_with_type_then_instantiate() -> Result<()> {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ let module = Module::new(&engine, "(module)")?;
+
+ OomTest::new()
+ .allow_alloc_after_oom(true)
+ .alloc_succeeds_after_oom(true)
+ .test(|| {
+ let mut store = Store::try_new(&engine, ())?;
+ let ty = FuncType::try_new(&engine, [ValType::I32], [ValType::I32])?;
+ let func = Func::try_new(&mut store, ty, |_caller, params, results| {
+ results[0] = params[0].clone();
+ Ok(())
+ });
+ // Whether or not creating the function hit OOM, the store must
+ // still be usable afterwards.
+ Instance::new(&mut store, &module, &[])?;
+ func?;
+ Ok(())
+ })
+}
+
#[test]
fn func_call() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module (func (export "id") (param i32) (result i32) (local.get 0)))"#,
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ r#"(module (func (export "id") (param i32) (result i32) (local.get 0)))"#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
@@ -68,21 +87,13 @@ fn func_call() -> Result<()> {
#[tokio::test]
async fn func_call_async() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module (func (export "id") (param i32) (result i32) (local.get 0)))"#,
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ r#"(module (func (export "id") (param i32) (result i32) (local.get 0)))"#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
@@ -102,21 +113,13 @@ async fn func_call_async() -> Result<()> {
#[tokio::test]
async fn typed_func_call_async() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module (func (export "id") (param i32) (result i32) (local.get 0)))"#,
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ r#"(module (func (export "id") (param i32) (result i32) (local.get 0)))"#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
@@ -134,21 +137,13 @@ async fn typed_func_call_async() -> Result<()> {
#[test]
fn func_typed() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module (func (export "id") (param i32) (result i32) (local.get 0)))"#,
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ r#"(module (func (export "id") (param i32) (result i32) (local.get 0)))"#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
diff --git a/crates/fuzzing/tests/oom/fuzz.rs b/crates/fuzzing/tests/oom/fuzz.rs
index 74d31d0993..d7f7ec38d7 100644
--- a/crates/fuzzing/tests/oom/fuzz.rs
+++ b/crates/fuzzing/tests/oom/fuzz.rs
@@ -3,27 +3,19 @@
use wasmtime::{Config, Engine, Func, Instance, Module, Result, Store};
use wasmtime_fuzzing::oom::OomTest;
-fn make_module_bytes() -> Vec<u8> {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config).unwrap();
+fn make_module(engine: &Engine) -> Result<Module> {
Module::new(
- &engine,
+ engine,
"(module (import \"\" \"f\" (func)) (func (export \"g\") (call 0)))",
)
- .unwrap()
- .serialize()
- .unwrap()
}
#[test]
fn test_fuzz() -> Result<()> {
- let module_bytes = make_module_bytes();
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)? };
+ let module = make_module(&engine)?;
OomTest::new().max_iters(100).fuzz(|| {
let mut store = Store::try_new(&engine, ())?;
@@ -35,12 +27,10 @@ fn test_fuzz() -> Result<()> {
#[tokio::test]
async fn test_fuzz_async() -> Result<()> {
- let module_bytes = make_module_bytes();
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)? };
+ let module = make_module(&engine)?;
OomTest::new()
.max_iters(100)
diff --git a/crates/fuzzing/tests/oom/instance.rs b/crates/fuzzing/tests/oom/instance.rs
index df771460ae..8016aa230c 100644
--- a/crates/fuzzing/tests/oom/instance.rs
+++ b/crates/fuzzing/tests/oom/instance.rs
@@ -8,29 +8,20 @@ use wasmtime_fuzzing::oom::OomTest;
#[test]
fn call_exported_func() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- let module = Module::new(
- &engine,
- r#"
- (module
- (func (export "add") (param i32 i32) (result i32)
- (i32.add (local.get 0) (local.get 1))
- )
- )
- "#,
- )?;
- 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)? };
+ let module = Module::new(
+ &engine,
+ r#"
+ (module
+ (func (export "add") (param i32 i32) (result i32)
+ (i32.add (local.get 0) (local.get 1))
+ )
+ )
+ "#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
@@ -46,21 +37,13 @@ fn call_exported_func() -> Result<()> {
#[test]
fn instance_new() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- "(module (import \"\" \"f\" (func)) (func (export \"g\") (call 0)))",
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ "(module (import \"\" \"f\" (func)) (func (export \"g\") (call 0)))",
+ )?;
OomTest::new().test(|| {
let mut store = Store::try_new(&engine, ())?;
@@ -72,21 +55,13 @@ fn instance_new() -> Result<()> {
#[tokio::test]
async fn instance_new_async() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- "(module (import \"\" \"f\" (func)) (func (export \"g\") (call 0)))",
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ "(module (import \"\" \"f\" (func)) (func (export \"g\") (call 0)))",
+ )?;
OomTest::new()
.test_async(|| async {
@@ -100,17 +75,10 @@ async fn instance_new_async() -> Result<()> {
#[test]
fn instance_get_export() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(&engine, "(module (func (export \"f\")))")?.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)? };
+ let module = Module::new(&engine, "(module (func (export \"f\")))")?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
@@ -124,21 +92,13 @@ fn instance_get_export() -> Result<()> {
#[test]
fn instance_exports() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- "(module (func (export \"f\")) (memory (export \"m\") 1) (global (export \"g\") i32 (i32.const 0)))",
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ "(module (func (export \"f\")) (memory (export \"m\") 1) (global (export \"g\") i32 (i32.const 0)))",
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
@@ -153,21 +113,13 @@ fn instance_exports() -> Result<()> {
#[test]
fn instance_get_func() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- "(module (func (export \"f\") (param i32) (result i32) (local.get 0)))",
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ "(module (func (export \"f\") (param i32) (result i32) (local.get 0)))",
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
@@ -182,21 +134,13 @@ fn instance_get_func() -> Result<()> {
#[test]
fn instance_get_typed_func() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- "(module (func (export \"f\") (param i32) (result i32) (local.get 0)))",
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ "(module (func (export \"f\") (param i32) (result i32) (local.get 0)))",
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
@@ -210,17 +154,10 @@ fn instance_get_typed_func() -> Result<()> {
#[test]
fn instance_get_table() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(&engine, "(module (table (export \"t\") 1 funcref))")?.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)? };
+ let module = Module::new(&engine, "(module (table (export \"t\") 1 funcref))")?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
@@ -235,17 +172,10 @@ fn instance_get_table() -> Result<()> {
#[test]
fn instance_get_memory() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(&engine, "(module (memory (export \"m\") 1))")?.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)? };
+ let module = Module::new(&engine, "(module (memory (export \"m\") 1))")?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
@@ -260,21 +190,13 @@ fn instance_get_memory() -> Result<()> {
#[test]
fn instance_get_global() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- "(module (global (export \"g\") i32 (i32.const 42)))",
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ "(module (global (export \"g\") i32 (i32.const 42)))",
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
@@ -354,25 +276,17 @@ fn pooling_allocator_memory_slot_leak_on_oom() -> Result<()> {
#[test]
fn table_element_segment_init() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module
- (table (export "table") 10 funcref)
- (func $f (result i32) (i32.const 42))
- (elem (i32.const 0) func $f)
- )"#,
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ r#"(module
+ (table (export "table") 10 funcref)
+ (func $f (result i32) (i32.const 42))
+ (elem (i32.const 0) func $f)
+ )"#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
diff --git a/crates/fuzzing/tests/oom/instance_pre.rs b/crates/fuzzing/tests/oom/instance_pre.rs
index 34c2ac63c4..9b1e2ff4d4 100644
--- a/crates/fuzzing/tests/oom/instance_pre.rs
+++ b/crates/fuzzing/tests/oom/instance_pre.rs
@@ -5,33 +5,7 @@ use wasmtime_fuzzing::oom::OomTest;
#[test]
fn instance_pre_instantiate() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- let module = Module::new(
- &engine,
- r#"
- (module
- (import "module" "func" (func (param i32) (result i32)))
-
- (memory (export "memory") 1)
- (data (i32.const 0) "a")
-
- (table (export "table") 1 funcref)
- (elem (i32.const 0) func 1)
-
- (func (export "func") (param i32) (result i32)
- (call 0 (local.get 0))
- )
- )
- "#,
- )?;
- module.serialize()?
- };
-
let mut config = Config::new();
- config.enable_compiler(false);
config.concurrency_support(false);
let engine = Engine::new(&config)?;
@@ -39,7 +13,24 @@ fn instance_pre_instantiate() -> Result<()> {
let mut linker = Linker::<()>::new(&engine);
linker.func_wrap("module", "func", |x: i32| x * 2)?;
- let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
+ let module = Module::new(
+ &engine,
+ r#"
+ (module
+ (import "module" "func" (func (param i32) (result i32)))
+
+ (memory (export "memory") 1)
+ (data (i32.const 0) "a")
+
+ (table (export "table") 1 funcref)
+ (elem (i32.const 0) func 1)
+
+ (func (export "func") (param i32) (result i32)
+ (call 0 (local.get 0))
+ )
+ )
+ "#,
+ )?;
let instance_pre = linker.instantiate_pre(&module)?;
OomTest::new().test(|| {
@@ -51,33 +42,7 @@ fn instance_pre_instantiate() -> Result<()> {
#[tokio::test]
async fn instance_pre_instantiate_async() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- let module = Module::new(
- &engine,
- r#"
- (module
- (import "module" "func" (func (param i32) (result i32)))
-
- (memory (export "memory") 1)
- (data (i32.const 0) "a")
-
- (table (export "table") 1 funcref)
- (elem (i32.const 0) func 1)
-
- (func (export "func") (param i32) (result i32)
- (call 0 (local.get 0))
- )
- )
- "#,
- )?;
- module.serialize()?
- };
-
let mut config = Config::new();
- config.enable_compiler(false);
config.concurrency_support(false);
let engine = Engine::new(&config)?;
@@ -85,7 +50,24 @@ async fn instance_pre_instantiate_async() -> Result<()> {
let mut linker = Linker::<()>::new(&engine);
linker.func_wrap("module", "func", |x: i32| x * 2)?;
- let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
+ let module = Module::new(
+ &engine,
+ r#"
+ (module
+ (import "module" "func" (func (param i32) (result i32)))
+
+ (memory (export "memory") 1)
+ (data (i32.const 0) "a")
+
+ (table (export "table") 1 funcref)
+ (elem (i32.const 0) func 1)
+
+ (func (export "func") (param i32) (result i32)
+ (call 0 (local.get 0))
+ )
+ )
+ "#,
+ )?;
let instance_pre = linker.instantiate_pre(&module)?;
OomTest::new()
diff --git a/crates/fuzzing/tests/oom/linker.rs b/crates/fuzzing/tests/oom/linker.rs
index e04c238331..8f70b86145 100644
--- a/crates/fuzzing/tests/oom/linker.rs
+++ b/crates/fuzzing/tests/oom/linker.rs
@@ -1,6 +1,6 @@
#![cfg(arc_try_new)]
-use wasmtime::{Config, Engine, Func, FuncType, Linker, Module, Result, Store};
+use wasmtime::{Config, Engine, Func, FuncType, Instance, Linker, Module, Result, Store};
use wasmtime_fuzzing::oom::OomTest;
#[test]
@@ -28,33 +28,7 @@ fn linker_func_wrap() -> Result<()> {
#[test]
fn linker_instantiate_pre() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- let module = Module::new(
- &engine,
- r#"
- (module
- (import "module" "func" (func (param i32) (result i32)))
-
- (memory (export "memory") 1)
- (data (i32.const 0) "a")
-
- (table (export "table") 1 funcref)
- (elem (i32.const 0) func 1)
-
- (func (export "func") (param i32) (result i32)
- (call 0 (local.get 0))
- )
- )
- "#,
- )?;
- module.serialize()?
- };
-
let mut config = Config::new();
- config.enable_compiler(false);
config.concurrency_support(false);
let engine = Engine::new(&config)?;
@@ -62,7 +36,24 @@ fn linker_instantiate_pre() -> Result<()> {
let mut linker = Linker::<()>::new(&engine);
linker.func_wrap("module", "func", |x: i32| x * 2)?;
- let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
+ let module = Module::new(
+ &engine,
+ r#"
+ (module
+ (import "module" "func" (func (param i32) (result i32)))
+
+ (memory (export "memory") 1)
+ (data (i32.const 0) "a")
+
+ (table (export "table") 1 funcref)
+ (elem (i32.const 0) func 1)
+
+ (func (export "func") (param i32) (result i32)
+ (call 0 (local.get 0))
+ )
+ )
+ "#,
+ )?;
OomTest::new().test(|| {
let _ = linker.instantiate_pre(&module)?;
@@ -72,33 +63,7 @@ fn linker_instantiate_pre() -> Result<()> {
#[tokio::test]
async fn linker_instantiate_async() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- let module = Module::new(
- &engine,
- r#"
- (module
- (import "module" "func" (func (param i32) (result i32)))
-
- (memory (export "memory") 1)
- (data (i32.const 0) "a")
-
- (table (export "table") 1 funcref)
- (elem (i32.const 0) func 1)
-
- (func (export "func") (param i32) (result i32)
- (call 0 (local.get 0))
- )
- )
- "#,
- )?;
- module.serialize()?
- };
-
let mut config = Config::new();
- config.enable_compiler(false);
config.concurrency_support(false);
let engine = Engine::new(&config)?;
@@ -106,7 +71,24 @@ async fn linker_instantiate_async() -> Result<()> {
let mut linker = Linker::<()>::new(&engine);
linker.func_wrap("module", "func", |x: i32| x * 2)?;
- let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
+ let module = Module::new(
+ &engine,
+ r#"
+ (module
+ (import "module" "func" (func (param i32) (result i32)))
+
+ (memory (export "memory") 1)
+ (data (i32.const 0) "a")
+
+ (table (export "table") 1 funcref)
+ (elem (i32.const 0) func 1)
+
+ (func (export "func") (param i32) (result i32)
+ (call 0 (local.get 0))
+ )
+ )
+ "#,
+ )?;
OomTest::new()
.test_async(|| async {
@@ -154,21 +136,13 @@ fn linker_func_new() -> Result<()> {
#[test]
fn linker_instance() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module (func (export "f")) (memory (export "m") 1))"#,
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ r#"(module (func (export "f")) (memory (export "m") 1))"#,
+ )?;
let pre_linker = Linker::<()>::new(&engine);
let instance_pre = pre_linker.instantiate_pre(&module)?;
@@ -198,19 +172,37 @@ fn linker_get() -> Result<()> {
})
}
+#[test]
+fn linker_get_then_instantiate() -> Result<()> {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ let module = Module::new(&engine, "(module)")?;
+
+ OomTest::new()
+ .allow_alloc_after_oom(true)
+ .alloc_succeeds_after_oom(true)
+ .test(|| {
+ let mut store = Store::try_new(&engine, ())?;
+ let mut linker = Linker::<()>::new(&engine);
+ linker.func_wrap("mod", "func", || {})?;
+ let item = linker.get(&mut store, "mod", "func");
+ // Drop the linker so the store holds the only remaining reference
+ // to the host function, if any. Whether or not `get` hit OOM, the
+ // store must still be usable afterwards.
+ drop(linker);
+ Instance::new(&mut store, &module, &[])?;
+ item?;
+ Ok(())
+ })
+}
+
#[test]
fn linker_get_by_import() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(&engine, r#"(module (import "mod" "func" (func)))"#)?.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)? };
+ let module = Module::new(&engine, r#"(module (import "mod" "func" (func)))"#)?;
let mut linker = Linker::<()>::new(&engine);
linker.func_wrap("mod", "func", || {})?;
@@ -227,17 +219,10 @@ fn linker_get_by_import() -> Result<()> {
#[test]
fn linker_get_default() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(&engine, r#"(module (func (export "_start")))"#)?.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)? };
+ let module = Module::new(&engine, r#"(module (func (export "_start")))"#)?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
@@ -253,21 +238,13 @@ fn linker_get_default() -> Result<()> {
#[tokio::test]
async fn linker_module_async() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module (func (export "f")) (memory (export "m") 1))"#,
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ r#"(module (func (export "f")) (memory (export "m") 1))"#,
+ )?;
OomTest::new()
.test_async(|| async {
diff --git a/crates/fuzzing/tests/oom/memory.rs b/crates/fuzzing/tests/oom/memory.rs
index 8f986cb96d..9e827e5c7f 100644
--- a/crates/fuzzing/tests/oom/memory.rs
+++ b/crates/fuzzing/tests/oom/memory.rs
@@ -169,30 +169,20 @@ fn memory_data_mut() -> Result<()> {
#[test]
fn wasm_memory_grow_relocate() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- config.memory_reservation(65536);
- config.memory_reservation_for_growth(0);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module
- (memory (export "memory") 1)
- (func (export "grow") (param i32) (result i32)
- (memory.grow (local.get 0))
- )
- )"#,
- )?
- .serialize()?
- };
let mut config = Config::new();
- config.enable_compiler(false);
config.concurrency_support(false);
config.memory_reservation(65536);
config.memory_reservation_for_growth(0);
let engine = Engine::new(&config)?;
- let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
+ let module = Module::new(
+ &engine,
+ r#"(module
+ (memory (export "memory") 1)
+ (func (export "grow") (param i32) (result i32)
+ (memory.grow (local.get 0))
+ )
+ )"#,
+ )?;
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&module)?;
diff --git a/crates/fuzzing/tests/oom/module_read.rs b/crates/fuzzing/tests/oom/module_read.rs
index 60cd3f96ae..01b3dcd3f6 100644
--- a/crates/fuzzing/tests/oom/module_read.rs
+++ b/crates/fuzzing/tests/oom/module_read.rs
@@ -5,17 +5,10 @@ use wasmtime_fuzzing::oom::OomTest;
#[test]
fn module_name() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(&engine, r#"(module $my_module (func (export "f")))"#)?.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)? };
+ let module = Module::new(&engine, r#"(module $my_module (func (export "f")))"#)?;
OomTest::new().test(|| {
let _name = module.name();
@@ -25,21 +18,13 @@ fn module_name() -> Result<()> {
#[test]
fn module_imports() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module (import "mod" "func" (func)) (func (export "f")))"#,
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ r#"(module (import "mod" "func" (func)) (func (export "f")))"#,
+ )?;
OomTest::new().test(|| {
let count = module.imports().count();
@@ -50,21 +35,13 @@ fn module_imports() -> Result<()> {
#[test]
fn module_exports() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(
- &engine,
- r#"(module (func (export "f")) (memory (export "m") 1))"#,
- )?
- .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)? };
+ let module = Module::new(
+ &engine,
+ r#"(module (func (export "f")) (memory (export "m") 1))"#,
+ )?;
OomTest::new().test(|| {
let count = module.exports().count();
@@ -75,17 +52,10 @@ fn module_exports() -> Result<()> {
#[test]
fn module_get_export() -> Result<()> {
- let module_bytes = {
- let mut config = Config::new();
- config.concurrency_support(false);
- let engine = Engine::new(&config)?;
- Module::new(&engine, r#"(module (func (export "f")))"#)?.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)? };
+ let module = Module::new(&engine, r#"(module (func (export "f")))"#)?;
OomTest::new().test(|| {
let export = module.get_export("f");
@@ -98,17 +68,10 @@ fn module_get_export() -> Result<()> {
#[test]
fn module_engine() -> 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)? };
+ let module = Module::new(&engine, "(module)")?;
OomTest::new().test(|| {
let _engine = module.engine();
diff --git a/crates/wasmtime/src/runtime/store/func_refs.rs b/crates/wasmtime/src/runtime/store/func_refs.rs
index 1c16ebd756..8f433279b3 100644
--- a/crates/wasmtime/src/runtime/store/func_refs.rs
+++ b/crates/wasmtime/src/runtime/store/func_refs.rs
@@ -159,11 +159,10 @@ impl FuncRefs {
func: Arc<HostFunc>,
modules: &ModuleRegistry,
) -> Result<NonNull<VMFuncRef>, OutOfMemory> {
- debug_assert!(func.func_ref().wasm_call.is_none());
- // SAFETY: the vmctx field in the funcref of `HostFunc` is safe to read.
- let ret = unsafe { self.push(func.func_ref().clone(), modules)? };
- self.storage.push(Storage::ArcHost { func })?;
- Ok(ret)
+ // SAFETY: the `VMFuncRef` here is rooted in `func` and will live as
+ // long as the store. Additionally the `vmctx` field is independent of
+ // all stores for this function.
+ unsafe { self.push_host(func.func_ref().clone(), modules, Storage::ArcHost { func }) }
}
/// Same as `push_arc_host`, but for owned host functions.
@@ -172,10 +171,36 @@ impl FuncRefs {
func: Box<HostFunc>,
modules: &ModuleRegistry,
) -> Result<NonNull<VMFuncRef>, OutOfMemory> {
- debug_assert!(func.func_ref().wasm_call.is_none());
- // SAFETY: the vmctx field in the funcref of `HostFunc` is safe to read.
- let ret = unsafe { self.push(func.func_ref().clone(), modules)? };
- self.storage.push(Storage::BoxHost { func })?;
+ // SAFETY: the `VMFuncRef` here is rooted in `func` and will live as
+ // long as the store. Additionally the `vmctx` field is independent of
+ // all stores for this function.
+ unsafe { self.push_host(func.func_ref().clone(), modules, Storage::BoxHost { func }) }
+ }
+
+ /// Pushes a single host function held in `storage` into this store.
+ ///
+ /// The returned pointer is only valid on the same thread that the store
+ /// exists on.
+ ///
+ /// # Safety
+ ///
+ /// This function requires that `func_ref` has a valid `vmctx` for the
+ /// store. The unsafe contract here is the same as [`FuncRefs::push`] in
+ /// general.
+ unsafe fn push_host(
+ &mut self,
+ func_ref: VMFuncRef,
+ modules: &ModuleRegistry,
+ storage: Storage,
+ ) -> Result<NonNull<VMFuncRef>, OutOfMemory> {
+ debug_assert!(func_ref.wasm_call.is_none());
+ // Reserve space for `storage` to avoid the need to handle a successful
+ // `self.push` followed by a failed `self.storage.push` -- this way we
+ // should be guaranteed that `self.storage.push` succeeds below.
+ self.storage.reserve(1)?;
+ // SAFETY: the unsafe contract of `push` is the same as ours.
+ let ret = unsafe { self.push(func_ref, modules)? };
+ self.storage.push(storage)?;
Ok(ret)
}
}