Skip to content

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

Track memory of self-borrow handles too (#15075)

Alex Crichtona4788b8676c9
diff --git a/crates/wasmtime/src/runtime/vm/component/handle_table.rs b/crates/wasmtime/src/runtime/vm/component/handle_table.rs
index e856c8f128..8fae63c855 100644
--- a/crates/wasmtime/src/runtime/vm/component/handle_table.rs
+++ b/crates/wasmtime/src/runtime/vm/component/handle_table.rs
@@ -11,10 +11,19 @@ const MAX_HANDLE: u32 = 1 << 28;
/// Count of allocated slots across all `HandleTable`s in a store.
///
-/// This counts slots available to get a proxy for memory allocated on the host
-/// for this table. New slots added by `insert` increment the `used` count, and
-/// then that never gets decremented for the lifetime of the store as tables are
-/// never shrunk.
+/// This count and limit is intended to roughly correspond with the amount of
+/// host memory being used to track handles within a store. This includes
+/// operations such as:
+///
+/// * Creating a handle (via `insert`) retains space for this. This is never
+/// decremented since the tables are never shrunk.
+/// * Tracking lends to create a `borrow` (via `resource_lend` and in
+/// `resource_lift_borrow` elsewhere allocates memory). This is later
+/// decremented in `resource_undo_lend` because the backing vector is indeed
+/// dropped when the frame goes out of scope.
+///
+/// This means that sometimes a handle is double-counted (e.g. borrows in some
+/// situations), but this is intended to still reflect live active memory use.
pub struct HandleCounter {
used: usize,
limit: usize,
@@ -285,10 +294,14 @@ impl HandleTable {
///
/// This will increase `lend_count` for both `own` and `borrow` handles and
/// must be paired with a `resource_undo_lend` below later on (managed by
- /// `CallContexts`).
+ /// `CallContexts`). Each lend is also charged to `counter` until then.
///
/// Upon success returns the "rep" of the resource.
- pub fn resource_lend(&mut self, idx: TypedResourceIndex) -> Result<u32> {
+ pub fn resource_lend(
+ &mut self,
+ counter: &mut HandleCounter,
+ idx: TypedResourceIndex,
+ ) -> Result<u32> {
match self.get_mut(idx.raw_index())? {
Slot::ResourceOwn {
resource,
@@ -300,7 +313,14 @@ impl HandleTable {
..
} => {
let rep = resource.rep(&idx)?;
- *lend_count = lend_count.checked_add(1).unwrap();
+ let Some(new_lend_count) = lend_count.checked_add(1) else {
+ bail!("cannot lend resource: too many active lends")
+ };
+ if counter.used >= counter.limit {
+ bail!("cannot lend resource: store handle limit exceeded");
+ }
+ counter.used += 1;
+ *lend_count = new_lend_count;
Ok(rep)
}
_ => bail!("index {} is not a resource", idx.raw_index()),
@@ -308,10 +328,15 @@ impl HandleTable {
}
/// Undoes a lending operation previously performed by `resource_lend`.
- pub fn resource_undo_lend(&mut self, idx: TypedResourceIndex) -> Result<()> {
+ pub fn resource_undo_lend(
+ &mut self,
+ counter: &mut HandleCounter,
+ idx: TypedResourceIndex,
+ ) -> Result<()> {
match self.get_mut(idx.raw_index())? {
Slot::ResourceOwn { lend_count, .. } | Slot::ResourceBorrow { lend_count, .. } => {
*lend_count -= 1;
+ counter.used -= 1;
Ok(())
}
_ => bail!("index {} is not a resource", idx.raw_index()),
diff --git a/crates/wasmtime/src/runtime/vm/component/resources.rs b/crates/wasmtime/src/runtime/vm/component/resources.rs
index 0c40fe43d0..c76566f3bf 100644
--- a/crates/wasmtime/src/runtime/vm/component/resources.rs
+++ b/crates/wasmtime/src/runtime/vm/component/resources.rs
@@ -363,7 +363,8 @@ impl ResourceTables<'_> {
// 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)?;
+ let (table, counter) = self.table_for_index(&index);
+ let rep = table.resource_lend(counter, index)?;
self.current_scope()?.lenders.push(index)?;
Ok(rep)
}
@@ -403,10 +404,8 @@ impl ResourceTables<'_> {
// Note the panics here which should never get triggered in theory
// due to the dynamic tracking of borrows and such employed for
// resources.
- self.table_for_index(&lender)
- .0
- .resource_undo_lend(lender)
- .unwrap();
+ let (table, counter) = self.table_for_index(&lender);
+ table.resource_undo_lend(counter, lender).unwrap();
}
Ok(())
}
diff --git a/tests/all/component_model/handle_table_limit.rs b/tests/all/component_model/handle_table_limit.rs
index bc31bd0a9a..b576a659fe 100644
--- a/tests/all/component_model/handle_table_limit.rs
+++ b/tests/all/component_model/handle_table_limit.rs
@@ -182,3 +182,93 @@ fn freed_slots_charged() -> Result<()> {
Ok(())
}
+
+/// Borrows lent to the resource's owner count against the limit while the
+/// call is active, and return their budget when it returns.
+#[test]
+fn lends_to_owner_count_against_the_limit() -> Result<()> {
+ // `$D` defines a resource and `$U` passes a `list<borrow<R>>` of `n` copies
+ // of one handle to `$D`'s `take`, `k` times. `$D` receives reps, so no
+ // handles are created for the borrows, but each lend still counts against
+ // the limit until `take` returns.
+ const LEND_COMPONENT: &str = r#"
+(component
+ (component $D
+ (type $R (resource (rep i32)))
+ (core func $new (canon resource.new $R))
+ (core module $m
+ (import "" "new" (func $new (param i32) (result i32)))
+ (memory (export "mem") 1)
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32) (i32.const 16))
+ (func (export "make") (result i32) (call $new (i32.const 42)))
+ (func (export "take") (param i32 i32) (result i32) (local.get 1)))
+ (core instance $i (instantiate $m (with "" (instance (export "new" (func $new))))))
+ (export $Rx "r" (type $R))
+ (func (export "make") (result (own $Rx)) (canon lift (core func $i "make")))
+ (func (export "take") (param "xs" (list (borrow $Rx))) (result u32)
+ (canon lift (core func $i "take") (memory (core memory $i "mem"))
+ (realloc (core func $i "realloc")))))
+ (instance $d (instantiate $D))
+
+ (component $U
+ (import "r" (type $R (sub resource)))
+ (import "make" (func $make (result (own $R))))
+ (import "take" (func $take (param "xs" (list (borrow $R))) (result u32)))
+ (core module $mm (memory (export "mem") 1))
+ (core instance $mi (instantiate $mm))
+ (core func $make (canon lower (func $make)))
+ (core func $take (canon lower (func $take) (memory (core memory $mi "mem"))))
+ (core module $m
+ (import "" "mem" (memory 1))
+ (import "" "make" (func $make (result i32)))
+ (import "" "take" (func $take (param i32 i32) (result i32)))
+ (func (export "run") (param $n i32) (param $k i32) (result i32)
+ (local $h i32) (local $i i32)
+ (local.set $h (call $make))
+ (loop $l
+ (if (i32.lt_u (local.get $i) (local.get $n)) (then
+ (i32.store (i32.shl (local.get $i) (i32.const 2)) (local.get $h))
+ (local.set $i (i32.add (local.get $i) (i32.const 1)))
+ (br $l))))
+ (local.set $i (i32.const 0))
+ (loop $l
+ (if (i32.lt_u (local.get $i) (local.get $k)) (then
+ (drop (call $take (i32.const 0) (local.get $n)))
+ (local.set $i (i32.add (local.get $i) (i32.const 1)))
+ (br $l))))
+ (local.get $k)))
+ (core instance $i (instantiate $m (with "" (instance
+ (export "mem" (memory $mi "mem"))
+ (export "make" (func $make)) (export "take" (func $take))))))
+ (func (export "run") (param "n" u32) (param "k" u32) (result u32)
+ (canon lift (core func $i "run"))))
+ (instance $u (instantiate $U (with "r" (type $d "r"))
+ (with "make" (func $d "make")) (with "take" (func $d "take"))))
+ (export "run" (func $u "run"))
+)
+"#;
+ let engine = engine();
+ let component = Component::new(&engine, LEND_COMPONENT)?;
+ let linker = Linker::new(&engine);
+
+ let mut store = Store::new(&engine, ());
+ store.set_max_component_handles(20);
+ let instance = linker.instantiate(&mut store, &component)?;
+ let run = instance.get_typed_func::<(u32, u32), (u32,)>(&mut store, "run")?;
+
+ // Ten lends at a time, many times over, fit.
+ let (n,) = run.call(&mut store, (10, 100))?;
+ assert_eq!(n, 100);
+
+ let mut store = Store::new(&engine, ());
+ store.set_max_component_handles(20);
+ let instance = linker.instantiate(&mut store, &component)?;
+ let run = instance.get_typed_func::<(u32, u32), (u32,)>(&mut store, "run")?;
+
+ // Fifty lends in one call don't.
+ run.call(&mut store, (50, 1))
+ .unwrap_err()
+ .assert_contains("store handle limit exceeded");
+
+ Ok(())
+}