WebAssembly has a WebAssembly.Memory
object, and the binary has a memory section . Thanks to this, the developer gives reasonable guesses about the minimum and maximum memory usage, then the VM allocates at least a minimum (or failure). Then the developer can at runtime request more grow_memory
, which tools, such as Emscripten, will be used under the malloc
hood (this is somewhat similar to sbrk
).
It was difficult for asm.js to understand how ArrayBuffer
will be used, and on some 32-bit platforms you often faced fragmentation of the process, which made it difficult to allocate enough continuous space in the "virtual memory ( ArrayBuffer
should be adjacent to the virtual address space of the browser" process, otherwise you will have a huge perfect). You will try to allocate 256MiB, and sometimes unsuccessfully. It was extremely difficult if the browser was not a multiprocessor, because all the other tabs compete for 32 bits of the virtual address space. Browsers were a little stupid a few years ago, they have improved , but 32 bits are not much to get around.
WebAssembly is supported by WebAssembly.Memory
, which is a special type of ArrayBuffer
. This means that the WebAssembly implementation may be smart with respect to ArrayBuffer
. At the 32-bit level, you have nothing to do: if you finish the adjacent address space, then the VM will not be able to do much. But on 64-bit platforms there is a lot of address space. A browser implementation may prevent you from creating too many instances of WebAssembly.Memory
(virtual memory allocation is almost free, but not quite), but you should be able to get multiple 4GiB allocations. Please note that the browser will simply allocate this space practically and fix the physical addresses for the minimum number of pages that you think you need. Subsequently, it will only be physically allocated when using grow_memory
. This may fail (physical memory is about the same as the amount of RAM that gives or takes up swap space), but it is much more predictable.
An implementation can bring out a similar trick on 32-bit platforms (over-commit, but retain PROT_NONE
and not physically distribute), assuming fragmentation allows, but as for the implementation, and how it thinks it affects ASLR. Itβs really hard to find a memory when youβre not walking that much, but practically and physically.
WebAssembly is currently defined as an ILP32 process: pointers are 32 bits. Therefore, it is difficult for you to limit 4GiB. We can add wasm64 in the future.