A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Handle OOM with resources from the host (#14851)
Alex Crichtoned4a3bc215e9
diff --git a/crates/fuzzing/tests/oom/component_resource.rs b/crates/fuzzing/tests/oom/component_resource.rsindex ed41dce691..126fdca18e 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, Resource, ResourceAny};+use wasmtime::component::{Component, Linker, Resource, ResourceAny, ResourceType};use wasmtime::{Config, Engine, Result, Store};use wasmtime_fuzzing::oom::OomTest;@@ -114,3 +114,42 @@ fn component_resource_any_try_from_resource_after_oom() -> Result<()> {result})}++#[test]+fn component_resource_lift_borrow() -> Result<()> {+ let mut config = Config::new();+ config.concurrency_support(false);+ let engine = Engine::new(&config)?;+ let component = Component::new(+ &engine,+ r#"+ (component+ (import "r" (type $r (sub resource)))+ (core func $drop (canon resource.drop $r))+ (core module $m+ (import "" "drop" (func $drop (param i32)))+ (func (export "f") (param i32) (call $drop (local.get 0)))+ )+ (core instance $i (instantiate $m+ (with "" (instance (export "drop" (func $drop))))+ ))+ (func (export "f") (param "r" (borrow $r))+ (canon lift (core func $i "f"))+ )+ )+ "#,+ )?;+ let mut linker = Linker::<()>::new(&engine);+ linker+ .root()+ .resource("r", ResourceType::host::<MyResource>(), |_, _| Ok(()))?;+ let instance_pre = linker.instantiate_pre(&component)?;++ OomTest::new().allow_alloc_after_oom(true).test(|| {+ let mut store = Store::try_new(&engine, ())?;+ let instance = instance_pre.instantiate(&mut store)?;+ let f = instance.get_typed_func::<(Resource<MyResource>,), ()>(&mut store, "f")?;+ f.call(&mut store, (Resource::new_own(42),))?;+ Ok(())+ })+}diff --git a/crates/wasmtime/src/runtime/vm/component/resources.rs b/crates/wasmtime/src/runtime/vm/component/resources.rsindex b2fbc17257..afb9922fa3 100644--- a/crates/wasmtime/src/runtime/vm/component/resources.rs+++ b/crates/wasmtime/src/runtime/vm/component/resources.rs@@ -196,7 +196,7 @@ impl TypedResourceIndex {/// State related to borrows for a specific call.#[derive(Default)]pub struct CallContext {- lenders: Vec<TypedResourceIndex>,+ lenders: TryVec<TypedResourceIndex>,borrow_count: u32,}@@ -361,8 +361,12 @@ impl ResourceTables<'_> {// tasks. Ideally the `lenders.push` would be skipped for sync tasks but// that requires a bit more plumbing and also appropriately tracking the// `lend_count` and such. For now this treats own/borrow the same way.+ //+ // Space for the lender is reserved first so that recording it can't+ // fail after the lend.+ self.current_scope()?.lenders.reserve(1)?;let rep = self.table_for_index(&index).0.resource_lend(index)?;- self.current_scope()?.lenders.push(index);+ self.current_scope()?.lenders.push(index)?;Ok(rep)}