Skip to content

Commit 2d443ea

Browse files
authored
Add OOM test for component Linker::instantiate_async (#13049)
Use try_new for Arc allocations in Linker::typecheck and Linker::instantiate_pre to avoid infallible allocations during OOM testing.
1 parent a591e3b commit 2d443ea

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#![cfg(arc_try_new)]
2+
3+
use wasmtime::component::{Component, Linker};
4+
use wasmtime::{Config, Engine, Result, Store};
5+
use wasmtime_fuzzing::oom::OomTest;
6+
7+
#[tokio::test]
8+
async fn component_linker_instantiate_async() -> Result<()> {
9+
let component_bytes = {
10+
let mut config = Config::new();
11+
config.concurrency_support(false);
12+
let engine = Engine::new(&config)?;
13+
Component::new(
14+
&engine,
15+
r#"
16+
(component
17+
(core module $m
18+
(func (export "id") (param i32) (result i32) (local.get 0))
19+
)
20+
(core instance $i (instantiate $m))
21+
(func (export "id") (param "x" s32) (result s32)
22+
(canon lift (core func $i "id"))
23+
)
24+
)
25+
"#,
26+
)?
27+
.serialize()?
28+
};
29+
let mut config = Config::new();
30+
config.enable_compiler(false);
31+
config.concurrency_support(false);
32+
let engine = Engine::new(&config)?;
33+
let component = unsafe { Component::deserialize(&engine, &component_bytes)? };
34+
let linker = Linker::<()>::new(&engine);
35+
36+
OomTest::new()
37+
.test_async(|| async {
38+
let mut store = Store::try_new(&engine, ())?;
39+
let _instance = linker.instantiate_async(&mut store, &component).await?;
40+
Ok(())
41+
})
42+
.await
43+
}

crates/fuzzing/tests/oom/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod boxed;
66
mod btree_map;
77
mod caller;
88
mod component_func;
9+
mod component_linker;
910
mod config;
1011
mod engine;
1112
mod entity_set;

crates/wasmtime/src/runtime/component/linker.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<T: 'static> Linker<T> {
177177
engine: &self.engine,
178178
types: component.types(),
179179
strings: &self.strings,
180-
imported_resources: Default::default(),
180+
imported_resources: try_new::<Arc<_>>(Default::default())?,
181181
};
182182

183183
// Walk over the component's list of import names and use that to lookup
@@ -267,7 +267,11 @@ impl<T: 'static> Linker<T> {
267267
assert_eq!(i, idx);
268268
}
269269
Ok(unsafe {
270-
InstancePre::new_unchecked(component.clone(), Arc::new(imports), imported_resources)
270+
InstancePre::new_unchecked(
271+
component.clone(),
272+
try_new::<Arc<_>>(imports)?,
273+
imported_resources,
274+
)
271275
})
272276
}
273277

0 commit comments

Comments
 (0)