emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.35k stars 3.25k forks source link

3.1.61 ignores wasmMemory when creating module #22129

Closed jozefchutka closed 1 week ago

jozefchutka commented 1 week ago

Using emscripten 3.1.61, and build command:

em++ main.cpp -msimd128 -O3 \
    -sSTACK_SIZE=24MB -sINITIAL_MEMORY=25MB -sMAXIMUM_MEMORY=4gb -sALLOW_MEMORY_GROWTH=1 \
    -sEXPORTED_FUNCTIONS=_malloc,_mfcc -sEXPORTED_RUNTIME_METHODS=ccall \
    -sMODULARIZE=1 -sEXPORT_NAME=createMFCC -sINVOKE_RUN=0 -sEXIT_RUNTIME=1 \
    -sWASM=1 -sENVIRONMENT=worker -o wasm/mfcc.js

When in runtime calling createMFCC({wasmMemory:new WebAssembly.Memory(...)}) my custom memory is being ignored and emscripten somehow creates and uses its own. After a bit of debugging it appears there is no place in generated .js where provided wasmMemory is actually used.

This is a regression as I can see version 3.1.47 nicely accepting provided memory via createMFCC({wasmMemory...

Full build steps can be find on https://github.com/wide-video/mfcc-wasm/blob/7781b888426e17c18d9a4fe3cc9a00839f4e9692/README.md

sbc100 commented 1 week ago

It looks like you are missing -sIMPORTED_MEMORY . IIUC that was required in 3.1.47 but maybe something changed. I will investigate. But -sIMPORTED_MEMORY is what you want.

In fact, if run a debug build (i.e if you remove -O3) you should see the following error on startup:

Use of `wasmMemory` detected.  Use -sIMPORTED_MEMORY to define wasmMemory externally

Do you see this error?

sbc100 commented 1 week ago

(it looks like 3.1.47 is the same in this regard and includes that same error message).

jozefchutka commented 1 week ago

Thanks, indeed this flags fixed it.