Skip to content

Commit

Permalink
Code review feedback.
Browse files Browse the repository at this point in the history
* Add another memory to `test_pooling_allocator_initial_limits_exceeded` to
  ensure a partially created instance is successfully deallocated.
* Update some doc comments for better documentation of `Store` and
  `ResourceLimiter`.
  • Loading branch information
peterhuene committed Apr 16, 2021
1 parent a82f722 commit 17ec96e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
5 changes: 3 additions & 2 deletions crates/wasmtime/src/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ pub(crate) const DEFAULT_MEMORY_LIMIT: usize = 10000;

/// Used by hosts to limit resource consumption of instances at runtime.
///
/// A [`Store`](crate::Store) can be created with a resource limiter so that hosts can take into account
/// non-WebAssembly resource usage to determine if a linear memory or table should grow.
/// [`Store::new_with_limits`](crate::Store::new_with_limits) can be used
/// with a resource limiter to take into account non-WebAssembly resource
/// usage to determine if a linear memory or table should be grown.
pub trait ResourceLimiter {
/// Notifies the resource limiter that an instance's linear memory has been requested to grow.
///
Expand Down
23 changes: 20 additions & 3 deletions crates/wasmtime/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,37 @@ impl Hash for HostInfoKey {
}

impl Store {
/// Creates a new store to be associated with the given [`Engine`].
/// Creates a new [`Store`] to be associated with the given [`Engine`].
///
/// The created [`Store`] will place no additional limits on the size of linear
/// memories or tables at runtime. Linear memories and tables will be allowed to
/// grow to any upper limit specified in their definitions.
///
/// The store will limit the number of instances, linear memories, and tables created to 10,000.
///
/// Use [`Store::new_with_limits`] with a [`StoreLimitsBuilder`](crate::StoreLimitsBuilder) to
/// specify different limits for the store.
pub fn new(engine: &Engine) -> Self {
Self::new_(engine, None)
}

/// Creates a new store to be associated with the given [`Engine`] and using the supplied
/// Creates a new [`Store`] to be associated with the given [`Engine`] and using the supplied
/// resource limiter.
///
/// A [`ResourceLimiter`] can be implemented by hosts to control the size of WebAssembly
/// linear memories and tables when a request is made to grow them.
///
/// [`StoreLimitsBuilder`](crate::StoreLimitsBuilder) can be used to create a
/// [`StoreLimits`](crate::StoreLimits) that implements [`ResourceLimiter`] using
/// static limit values.
///
/// # Example
///
/// ```rust
/// # use wasmtime::{Engine, Store, StoreLimitsBuilder};
/// // Place a limit on linear memories so they cannot grow beyond 1 MiB
/// let engine = Engine::default();
/// let store = Store::new_with_limits(&engine, StoreLimitsBuilder::new().instances(10).build());
/// let store = Store::new_with_limits(&engine, StoreLimitsBuilder::new().memory_pages(16).build());
/// ```
pub fn new_with_limits(engine: &Engine, limiter: impl ResourceLimiter + 'static) -> Self {
Self::new_(engine, Some(Rc::new(ResourceLimiterProxy(limiter))))
Expand Down
11 changes: 9 additions & 2 deletions tests/all/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,24 @@ fn test_initial_table_limits_exceeded() -> Result<()> {
#[test]
fn test_pooling_allocator_initial_limits_exceeded() -> Result<()> {
let mut config = Config::new();
config.wasm_multi_memory(true);
config.allocation_strategy(InstanceAllocationStrategy::Pooling {
strategy: PoolingAllocationStrategy::NextAvailable,
module_limits: ModuleLimits::default(),
module_limits: ModuleLimits {
memories: 2,
..Default::default()
},
instance_limits: InstanceLimits {
count: 1,
..Default::default()
},
});

let engine = Engine::new(&config)?;
let module = Module::new(&engine, r#"(module (memory (export "m") 5))"#)?;
let module = Module::new(
&engine,
r#"(module (memory (export "m1") 2) (memory (export "m2") 5))"#,
)?;

let store = Store::new_with_limits(&engine, StoreLimitsBuilder::new().memory_pages(3).build());

Expand Down

0 comments on commit 17ec96e

Please sign in to comment.