diff --git a/crates/wasmtime-backend/src/lib.rs b/crates/wasmtime-backend/src/lib.rs index fe8dd922..aff3a6f7 100644 --- a/crates/wasmtime-backend/src/lib.rs +++ b/crates/wasmtime-backend/src/lib.rs @@ -68,7 +68,29 @@ impl WasmBackend for WasmtimeWasmBackend { } } +#[derive(Default)] +struct MyStoreLimiter { + memories: i32 +} + +impl wasmtime::ResourceLimiter for MyStoreLimiter { + fn memory_growing(&mut self, current: usize, desired: usize, maximum: Option<usize>) -> bool { + if current == 0 { + self.memories += 1; + log::debug!("Wasmtime created a memory. Total memories in this Store: {}", self.memories) + } + + true + } + + fn table_growing(&mut self, current: u32, desired: u32, maximum: Option<u32>) -> bool { + true + } +} + + #[derive(Default)] pub struct StoreState { wasi: Vec<WasiCtx>, // wasmtime store does not release memory until drop, so do we + limiter: MyStoreLimiter, } diff --git a/crates/wasmtime-backend/src/store.rs b/crates/wasmtime-backend/src/store.rs index 5dab3bdf..99c9e77b 100644 --- a/crates/wasmtime-backend/src/store.rs +++ b/crates/wasmtime-backend/src/store.rs @@ -19,7 +19,7 @@ use crate::WasmtimeWasmBackend; use marine_wasm_backend_traits::prelude::*; -use wasmtime::StoreContext; +use wasmtime::{ResourceLimiter, StoreContext}; use wasmtime::StoreContextMut; use wasmtime::AsContext as WasmtimeAsContext; use wasmtime::AsContextMut as WasmtimeAsContextMut; @@ -45,9 +45,13 @@ pub struct WasmtimeContextMut<'s> { impl Store<WasmtimeWasmBackend> for WasmtimeStore { fn new(backend: &WasmtimeWasmBackend) -> Self { - Self { + let mut res = Self { inner: wasmtime::Store::new(&backend.engine, <_>::default()), - } + }; + + res.inner.limiter(|ctx| &mut ctx.limiter); + + res } }