* Add resource limiting to the Wasmtime API. This commit adds a `ResourceLimiter` trait to the Wasmtime API. When used in conjunction with `Store::new_with_limiter`, this can be used to monitor and prevent WebAssembly code from growing linear memories and tables. This is particularly useful when hosts need to take into account host resource usage to determine if WebAssembly code can consume more resources. A simple `StaticResourceLimiter` is also included with these changes that will simply limit the size of linear memories or tables for all instances created in the store based on static values. * Code review feedback. * Implemented `StoreLimits` and `StoreLimitsBuilder`. * Moved `max_instances`, `max_memories`, `max_tables` out of `Config` and into `StoreLimits`. * Moved storage of the limiter in the runtime into `Memory` and `Table`. * Made `InstanceAllocationRequest` use a reference to the limiter. * Updated docs. * Made `ResourceLimiterProxy` generic to remove a level of indirection. * Fixed the limiter not being used for `wasmtime::Memory` and `wasmtime::Table`. * Code review feedback and bug fix. * `Memory::new` now returns `Result<Self>` so that an error can be returned if the initial requested memory exceeds any limits placed on the store. * Changed an `Arc` to `Rc` as the `Arc` wasn't necessary. * Removed `Store` from the `ResourceLimiter` callbacks. Custom resource limiter implementations are free to capture any context they want, so no need to unnecessarily store a weak reference to `Store` from the proxy type. * Fixed a bug in the pooling instance allocator where an instance would be leaked from the pool. Previously, this would only have happened if the OS was unable to make the necessary linear memory available for the instance. With these changes, however, the instance might not be created due to limits placed on the store. We now properly deallocate the instance on error. * Added more tests, including one that covers the fix mentioned above. * Code review feedback. * 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`.
55 lines
1.1 KiB
Rust
55 lines
1.1 KiB
Rust
mod async_functions;
|
|
mod cli_tests;
|
|
mod custom_signal_handler;
|
|
mod debug;
|
|
mod externals;
|
|
mod fuel;
|
|
mod func;
|
|
mod fuzzing;
|
|
mod globals;
|
|
mod host_funcs;
|
|
mod iloop;
|
|
mod import_calling_export;
|
|
mod import_indexes;
|
|
mod instance;
|
|
mod invoke_func_via_table;
|
|
mod limits;
|
|
mod linker;
|
|
mod memory_creator;
|
|
mod module;
|
|
mod module_linking;
|
|
mod module_serialize;
|
|
mod name;
|
|
mod pooling_allocator;
|
|
mod stack_overflow;
|
|
mod table;
|
|
mod traps;
|
|
mod use_after_drop;
|
|
mod wast;
|
|
|
|
// TODO(#1886): Cranelift only supports reference types on x64.
|
|
#[cfg(target_arch = "x86_64")]
|
|
mod funcref;
|
|
#[cfg(target_arch = "x86_64")]
|
|
mod gc;
|
|
|
|
/// A helper to compile a module in a new store with reference types enabled.
|
|
#[cfg(target_arch = "x86_64")]
|
|
pub(crate) fn ref_types_module(
|
|
source: &str,
|
|
) -> anyhow::Result<(wasmtime::Store, wasmtime::Module)> {
|
|
use wasmtime::*;
|
|
|
|
let _ = env_logger::try_init();
|
|
|
|
let mut config = Config::new();
|
|
config.wasm_reference_types(true);
|
|
|
|
let engine = Engine::new(&config)?;
|
|
let store = Store::new(&engine);
|
|
|
|
let module = Module::new(&engine, source)?;
|
|
|
|
Ok((store, module))
|
|
}
|