Hei there! I am currently trying to run wasm on embedded devices, therefore memory constraints are quite a thing. When I compile the following rust code:
What I don't understand in the output is the memory section, that is initialized with 16(!) pages, each 64kB big. Is this my fault or why does the compiler add the section?
Ahh I See, but if I delete the memory section and run it inside a runtime (in my case wasmi), I don't get any error as wasmi allocates it's own stack when a program starts.
Do you know if I could have an influence on the size of it (if it's the stack)?
Your host functions will use that memory export for accessing anything inside the WebAssembly module's address space - so that includes the heap, static variables, or values on the stack. I don't think the stack is allocated separately because whenever I've written host functions I've been able to read values using the same export regardless of whether the value lives on the stack or the heap.
I'm not sure where the number 16 comes from (16 pages = 16 * 64kb = 1024 kb), though.
I am almost certainly sure that wasmi allocates on it's own, but I will have a look. I am also using their StackRecycler in order to keep a lower memory footprint.
Looking at the number 16 there I'm quite sure this is the page number, quoting MDN:
The 1 (in (memory 1) ) indicates that the imported memory must have at least 1 page of memory (WebAssembly defines a page to be 64KB.)
The wasm stack. Instructions can only push and pop. Statically checked that every instruction manipulates it correctly. (right types, no stack underflow, ...)
The stack in the linear memory. This one is used when the address of a value on the stack needs to be taken or if the value is larger than a single wasm primitive value as stored on the wasm stack. This one needs the memory section.