Hello !
I am trying to limit the maximum amount of memory that a WASM module can use.
For this I experimented with StoreLimit, but I am encountering a problem.
Here you can see some part of my code that are relevant to this limitation.
const MAX_MEMORY_SIZE: usize = 3145728; /* 3 Mo */
let my_state = StoreState {
limits: StoreLimitsBuilder::new()
.memory_size(MAX_MEMORY_SIZE)
.instances(1)
.build(),
wasi: ...
};
let mut store = Store::new(&engine, my_state);
store.limiter(|state| &mut state.limits);
wasmtime_wasi::add_to_linker(&mut linker, |state: &mut StoreState| &mut state.wasi)?;
I wanted to test that my solution was working properly with the following module that allocates 2Mo of memory, but it ends up being killed even though it should not, since I set MAX_MEMORY_SIZE to 3Mo.
// Allocate 2Mo - 2097152
let mut vec: Vec<u64> = Vec::with_capacity(262144);
for i in 0..262144 {
vec.push(i);
}
let ret_str = format!("The useful size of `vec` is {} - Diff: {}", size_of_val(&*vec), size_of_val(&*vec) - 2097152);
let res = JsonResult { result: Some(ret_str) };
print!("{}", serde_json::to_string(&res).unwrap());
}
Can somebody help me understand why this is happening please ?
If I'm not mistaken you are the one who implemented this feature @Peter Huene :)
Rustc by default sets a 1MB stack, together with your Vec::with_capacity
this would come out to exactly 3MB. However statics are also stored in the linear memory and there is some allocator overhead. As such the total used memory is more than 3MB, hence causing the wasm module to get killed.
Okay that make more sens, thank you for the answer bjorn !
Last updated: Nov 26 2024 at 00:12 UTC