Skip to content

Issue of wasm memory limit of 2G #493

@0xsyj

Description

@0xsyj

When I was trying to build the AST of a relatively large source code file, I encountered an error:

[-] new Parser and parse failed, try again.
RuntimeError: memory access out of bounds
RuntimeError: memory access out of bounds
    at wasm://wasm/001db6c6:wasm-function[586]:0x20678
    at wasm://wasm/001db6c6:wasm-function[440]:0x1358c
    at wasm://wasm/001db6c6:wasm-function[80]:0x2565
    at wasm://wasm/001db6c6:wasm-function[52]:0x1bb8
    at wasm://wasm/001db6c6:wasm-function[1873]:0x67913
    at wasm://wasm/001db6c6:wasm-function[1874]:0x67a36
    at dynCall (D:\OneDrive\CodeSource\node_modules\cxx-frontend\dist\cjs\index.cjs:904:37)
    at D:\OneDrive\CodeSource\node_modules\cxx-frontend\dist\cjs\index.cjs:907:49
    at Object.createUnit (D:\OneDrive\CodeSource\node_modules\cxx-frontend\dist\cjs\index.cjs:1080:16)
    at new _Parser (D:\OneDrive\CodeSource\node_modules\cxx-frontend\dist\cjs\index.cjs:11389:22)

Later, I checked node_modules\cxx-frontend\dist\cjs\index.cjs and found that the failure occurred at this line:

var base = _malloc(4 + length + 1);

Memory allocation failed. By checking wasmMemory.buffer.byteLength, I noticed that it could not grow beyond 2GB.

After some research, I found the reason: https://emscripten.org/docs/tools_reference/settings_reference.html#maximum-memory. The default maximum memory for WebAssembly is 2GB. This issue can be resolved by specifying ALLOW_MEMORY_GROWTH=1 and MAXIMUM_MEMORY=4GB during the compilation of the WebAssembly module.

I proceeded to recompile the WebAssembly module in the project with these settings. However, I found that the memory was still insufficient because the source code is indeed very large (some open-source projects).

Later, I discovered https://emscripten.org/docs/tools_reference/settings_reference.html#memory64, which allows setting memory beyond 4GB by enabling memory64=1 during compilation and linking. However, when I ran the compiled code, I encountered some strange errors that I couldn't resolve. For example, in the compiled index.cjs, the line wasmTable.get(BigInt(funcPtr)) threw an error: Cannot convert a BigInt value to a number. After converting it to a number, I continued running the code, but then encountered another error: table index out of bounds.

It seems that the project was not designed with the consideration of requiring such a large amount of memory. I hope the development team can address this issue. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions