emscripten-core / emscripten-core/emscripten

How to share memory between two modules?

Open
#18,356 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

In my application I have two separate modules, built with `-sMODULARIZE=1` and `USE_PTHREADS=1`. Now I'm trying to make them share the same block of memory by initializing them this way:
```js
var wasmMemory = new WebAssembly.Memory({
"initial": 65536e3 / 65536,
"maximum": 2147483648 / 65536,
"shared": true
});

const moduleMain = await ModuleMain({
noInitialRun: true,
wasmMemory: wasmMemory,
canvas: (function () {
return document.getElementById('canvas');
})()
})

const moduleInterpreter = await ModuleInterpreter({
noInitialRun: true,
wasmMemory: wasmMemory,
})

console.log("moduleInterpreter loaded")
```
`noItinitialRun` is set to `true` so I can do a step-by-step experimentation.

The problem I'm facing is that everything blows up during the runtime initialization of `ModuleInterpreter`:

![Schermata del 2022-12-12 22-19-51](https://user-images.githubusercontent.com/8508972/207157015-d9af686d-1e7f-4803-a50d-a8bb587021d9.png)
Looks like the memory is being shared, but the second module is tripping up on memory of the first one, during the call of static constructors.

So, is there an "official" way of doing this? I suppose I should telll `ModuleInterpreter` to start using the `wasmMemory` at a certain offset, past the memory used by the first module.

I guess I'll run into other problems with the dynamic memory allocator after solving this, but they should be easier to deal with since I already use a custom one

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.