Add resource limiting to the Wasmtime API. (#2736)
* 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`.
This commit is contained in:
@@ -17,6 +17,7 @@ mod not_for_windows {
|
||||
struct CustomMemory {
|
||||
mem: *mut c_void,
|
||||
size: usize,
|
||||
guard_size: usize,
|
||||
used_wasm_pages: RefCell<u32>,
|
||||
glob_page_counter: Arc<Mutex<u64>>,
|
||||
}
|
||||
@@ -43,6 +44,7 @@ mod not_for_windows {
|
||||
Self {
|
||||
mem,
|
||||
size,
|
||||
guard_size,
|
||||
used_wasm_pages: RefCell::new(num_wasm_pages),
|
||||
glob_page_counter: glob_counter,
|
||||
}
|
||||
@@ -63,6 +65,10 @@ mod not_for_windows {
|
||||
*self.used_wasm_pages.borrow()
|
||||
}
|
||||
|
||||
fn maximum(&self) -> Option<u32> {
|
||||
Some((self.size as u32 - self.guard_size as u32) / WASM_PAGE_SIZE)
|
||||
}
|
||||
|
||||
fn grow(&self, delta: u32) -> Option<u32> {
|
||||
let delta_size = (delta as usize).checked_mul(WASM_PAGE_SIZE as usize)?;
|
||||
|
||||
@@ -70,11 +76,8 @@ mod not_for_windows {
|
||||
let prev_size = (prev_pages as usize).checked_mul(WASM_PAGE_SIZE as usize)?;
|
||||
|
||||
let new_pages = prev_pages.checked_add(delta)?;
|
||||
let new_size = (new_pages as usize).checked_mul(WASM_PAGE_SIZE as usize)?;
|
||||
|
||||
let guard_size = unsafe { sysconf(_SC_PAGESIZE) as usize };
|
||||
|
||||
if new_size > self.size - guard_size {
|
||||
if new_pages > self.maximum().unwrap() {
|
||||
return None;
|
||||
}
|
||||
unsafe {
|
||||
|
||||
Reference in New Issue
Block a user