Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce flag to prevent reallocation of shared memory #173

Open
ghost opened this issue Jan 6, 2021 · 0 comments
Open

Introduce flag to prevent reallocation of shared memory #173

ghost opened this issue Jan 6, 2021 · 0 comments

Comments

@ghost
Copy link

ghost commented Jan 6, 2021

Instantiating a module more than once allocates new memory for each instance, what if, instead, there could be a flag that would cause all instances to refer to the same memory?
Instead of them being thread-local memories, they would be "global."

This allows Wasm to remain completely self-contained, encapsulated, and in control of its own memory.
Currently, shared memory is completely useless if not importing host shared memory, but every step in between the module getting its memory, and the host passing it to the module, leaves room for mutation, and unexpected change at runtime, as the host has control over the array buffer too.

Nothing more would need to be changed, it would just change the lifetime of the memory.

This could be implemented as a simple cache associated with the module.

Current loading (JS):

// main.mjs:
const memory = new WebAssembly.Memory({ ..., shared: true });

const {
    instance,
    module
} = await WebAssembly.instantiateStreaming(..., ...memory...);

const thread = new Worker("./worker.mjs", ...);

worker.postMessage({ memory, module });

// worker.mjs:
... // await .postMessage({ memory, module })
const instance = await WebAssembly.instantiate(module, ...memory...);

With cached memory:

// main.mjs:
const {
    instance,
    module
} = await WebAssembly.instantiateStreaming(...);

const thread = new Worker("./worker.mjs", ...);

worker.postMessage(module);

// worker.mjs:
... // await .postMessage(memory)
const instance = await WebAssembly.instantiate(module);

In the former example, the WebAssembly module's memory may be corrupted later at the will of the host, breaking encapsulation, but the way that multi-threaded Wasm loading works, it leaves no alternative.

Would this break any of the core motivations/ideas/philosophies behind WebAssembly?

@ghost ghost changed the title Suggestion: flag to prevent reallocation of shared memory Introduce flag to prevent reallocation of shared memory Jan 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants