Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a WebAssembly program leak memory?

WebAssembly programs start with a fixed amount of virtual memory, and can request additional memory at runtime. They are also encouraged to discard unused physical pages. Can long running, badly behaved WebAssembly programs leak memory? Will this eventually cause the WebAssembly program to crash?

like image 916
Max Shenfield Avatar asked Feb 06 '26 19:02

Max Shenfield


2 Answers

A WebAssembly program can use up all of its allocated memory, and once it runs out by reaching the memory's declared maximum, or 4GiB, or the browser's limit, then it'll likely crash. Inside that memory C++ programs using toolchains like emscripten have an allocator, and that allocator will decide what it does when out of memory. Likely crash!

Outside that memory, a program can also "leak" by holding onto a bunch of JavaScript objects and preventing the GC from collecting them. This will eventually hit memory limits, leading to crash.

like image 62
JF Bastien Avatar answered Feb 12 '26 11:02

JF Bastien


Yes, we are investigating just such a case here!

https://github.com/emscripten-core/emscripten/issues/14459
(Any emscripten / em++ experts are welcome to help 🕵🏻‍♂️)


The leak starts: before


It exhausts available wasmMemory: after


After this point, all tested calls to the WASM module fail but that does not crash the tab in desktop Chrome, other browsers might behave differently. (The leak happens in all tested browsers.)

like image 41
Cetin Sert Avatar answered Feb 12 '26 10:02

Cetin Sert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!