Max Shenfield
Max Shenfield

Reputation: 4267

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?

Upvotes: 3

Views: 2052

Answers (2)

Cetin Sert
Cetin Sert

Reputation: 4600

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.)

Upvotes: 1

JF Bastien
JF Bastien

Reputation: 6863

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.

Upvotes: 9

Related Questions