Skip to main content

ResourceLimiter

Trait ResourceLimiter 

Source
pub trait ResourceLimiter: Send + Sync {
    // Provided methods
    fn memory_growing(
        &self,
        _current: usize,
        _desired: usize,
        _maximum: Option<usize>,
    ) -> Result<bool, Trap> { ... }
    fn table_growing(
        &self,
        _current: usize,
        _desired: usize,
        _maximum: Option<usize>,
    ) -> Result<bool, Trap> { ... }
    fn gc_growing(
        &self,
        _current: usize,
        _desired: usize,
        _maximum: Option<usize>,
    ) -> Result<bool, Trap> { ... }
}
Expand description

Controls selected allocation requests from WebAssembly instances.

Configure a limiter with Config::with_resource_limiter. The callbacks cover linear memory, tables, and the logical GC heap. They do not account for stacks, runtime metadata, temporary buffers, backing-capacity overhead, or other host allocations.

Ok(true) allows an allocation attempt, Ok(false) rejects it, and Err returns the supplied trap. Allowing a request does not guarantee that the backing allocation will succeed. Rejected growth uses the operation’s normal failed-growth result. Rejected initial allocation and GC allocation produce Trap::OutOfMemory.

§Example

use tinywasm::engine::Config;
use tinywasm::types::{MemoryArch, MemoryType};
use tinywasm::{Engine, Memory, ResourceLimiter, Store};

struct MemoryLimit(usize);

impl ResourceLimiter for MemoryLimit {
    fn memory_growing(
        &self,
        _current: usize,
        desired: usize,
        _maximum: Option<usize>,
    ) -> Result<bool, tinywasm::Trap> {
        Ok(desired <= self.0)
    }
}

let config = Config::new().with_resource_limiter(MemoryLimit(64 * 1024));
let mut store = Store::new(Engine::new(config));
let memory = Memory::try_new(&mut store, MemoryType::new(MemoryArch::I32, 1, None, None))?;
assert_eq!(memory.grow(&mut store, 1)?, None);

Provided Methods§

Source

fn memory_growing( &self, _current: usize, _desired: usize, _maximum: Option<usize>, ) -> Result<bool, Trap>

Checks a nonzero memory allocation or growth request.

Sizes are in bytes. current is zero for initial allocation. maximum is the declared maximum in bytes, saturated to usize::MAX if needed, or None if no maximum was declared. Growth limits are checked before this callback. The default implementation allows the request.

Source

fn table_growing( &self, _current: usize, _desired: usize, _maximum: Option<usize>, ) -> Result<bool, Trap>

Checks a nonzero table allocation or growth request.

Sizes are in elements. current is zero for initial allocation. maximum is the declared maximum element count when it fits usize, or None otherwise. Table limits are checked before this callback. The default implementation allows the request.

Source

fn gc_growing( &self, _current: usize, _desired: usize, _maximum: Option<usize>, ) -> Result<bool, Trap>

Checks a GC object allocation request.

Sizes are the current TinyWasm-accounted heap bytes and that count plus the requested allocation. They include unreachable objects until collection but not all allocator overhead. maximum is currently always None. This callback runs before threshold-triggered collection.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§