Expose wasmtime_store_limiter in the c-api (#5761)

This commit is contained in:
Lukas Forst
2023-02-13 18:18:37 +01:00
committed by GitHub
parent a0a97f5e8f
commit 6cddc923f3
2 changed files with 75 additions and 1 deletions

View File

@@ -2,7 +2,10 @@ use crate::{wasm_engine_t, wasmtime_error_t, wasmtime_val_t, ForeignData};
use std::cell::UnsafeCell;
use std::ffi::c_void;
use std::sync::Arc;
use wasmtime::{AsContext, AsContextMut, Store, StoreContext, StoreContextMut, Val};
use wasmtime::{
AsContext, AsContextMut, Store, StoreContext, StoreContextMut, StoreLimits, StoreLimitsBuilder,
Val,
};
/// This representation of a `Store` is used to implement the `wasm.h` API.
///
@@ -77,6 +80,9 @@ pub struct StoreData {
/// Temporary storage for usage during host->wasm calls, same as above but
/// for a different direction.
pub wasm_val_storage: Vec<Val>,
/// Limits for the store.
pub store_limits: StoreLimits,
}
#[no_mangle]
@@ -94,6 +100,7 @@ pub extern "C" fn wasmtime_store_new(
wasi: None,
hostcall_val_storage: Vec::new(),
wasm_val_storage: Vec::new(),
store_limits: StoreLimits::default(),
},
),
})
@@ -104,6 +111,35 @@ pub extern "C" fn wasmtime_store_context(store: &mut wasmtime_store_t) -> CStore
store.store.as_context_mut()
}
#[no_mangle]
pub extern "C" fn wasmtime_store_limiter(
store: &mut wasmtime_store_t,
memory_size: i64,
table_elements: i64,
instances: i64,
tables: i64,
memories: i64,
) {
let mut limiter = StoreLimitsBuilder::new();
if memory_size >= 0 {
limiter = limiter.memory_size(memory_size as usize);
}
if table_elements >= 0 {
limiter = limiter.table_elements(table_elements as u32);
}
if instances >= 0 {
limiter = limiter.instances(instances as usize);
}
if tables >= 0 {
limiter = limiter.tables(tables as usize);
}
if memories >= 0 {
limiter = limiter.memories(memories as usize);
}
store.store.data_mut().store_limits = limiter.build();
store.store.limiter(|data| &mut data.store_limits);
}
#[no_mangle]
pub extern "C" fn wasmtime_context_get_data(store: CStoreContext<'_>) -> *mut c_void {
store.data().foreign.data