Emscripten: Wasm memory incompatible using -s TOTAL_MEMORY

Created on 11 Jan 2017  路  5Comments  路  Source: emscripten-core/emscripten

Testing against the incoming branch

I tried to compile a simple c file using emcc test.c -O2 -s TOTAL_MEMORY=149880832 -s WASM=1.
The WebAssembly.Memory is created using Module['wasmMemory'] = new WebAssembly.Memory({ initial: TOTAL_MEMORY / WASM_PAGE_SIZE, maximum: TOTAL_MEMORY / WASM_PAGE_SIZE });.
However, TOTAL_MEMORY has been increased to to be compliant with the asm.js spec
The problem is, the wasm module had been created using 2287 pages and now we are passing a memory object with a min/max of 2304 pages which is invalid according to Linear Memory validation

From what I understand, I can bypass this issue by giving -s ALLOW_MEMORY_GROWTH=1 which will not set a maximum for the memory size and then we can accept a bigger memory.

wontfix

Most helpful comment

Code now has hardcoded minimal possible initial memory value 16MB (initial value = "minimal requirement" for imported Memory). It's not possible to set ~0. That's very confusing for standalone modules. Because when you try to create module instance and pass Memory with < 256 pages, browser (Chrome, FF) will throw linker exception. Content of that message does not help to understand what happened.

All 5 comments

Thanks, we should definitely fix this. Perhaps we could not increase the size for asm.js when in wasm mode, and show an error telling the user to set the proper full size at compile time?

cc @juj, you're working on stuff in this area, like #4918.

Oh, I see we do show an error for this now, I was just not seeing it in the sm shell. In firefox, I get Memory size incompatibility issues may be due to changing TOTAL_MEMORY at runtime to something too large. Use ALLOW_MEMORY_GROWTH to allow any size memory (and also make sure not to set TOTAL_MEMORY at runtime to something smaller than it was at compile time). Good enough or worth improving?

Added a fix for this in the above PR by stopping to adjust the sizes when building, and just validate that they are good. Also added support to pass KB, MB and so on suffixes to command line to make it easy to just say -s TOTAL_MEMORY=142MB when building.

Code now has hardcoded minimal possible initial memory value 16MB (initial value = "minimal requirement" for imported Memory). It's not possible to set ~0. That's very confusing for standalone modules. Because when you try to create module instance and pass Memory with < 256 pages, browser (Chrome, FF) will throw linker exception. Content of that message does not help to understand what happened.

This issue has been automatically marked as stale because there has been no activity in the past 2 years. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.

Was this page helpful?
0 / 5 - 0 ratings