zicklag opened issue #5329:
I have a use-case where I have multiple WASM modules that I all want to have the _same_ default WASM memory.
Each one would obviously only be able to be running one at a time, but I need them all to be able to modify the same WASM memory without having to copy the memory between each module.
Is this possible in Wasmtime?
bjorn3 commented on issue #5329:
If the wasm modules import the linear memory rather than export it, this is possible by providing the same linear memory when instantiating them. Note that wasi requires the linear memory to be exported, so if you want wasi you can't share the linear memory. Also the wasm modules themself need to be aware that the linear memory is shared to avoid corruption.
zicklag commented on issue #5329:
If the wasm modules import the linear memory rather than export it
Is there a way to generate a WASM binary from Rust that will import it's memory instead of exporting it? Or is it essentially a matter of simply transforming the WASM output from Rust from an export to an import?
Note that wasi requires the linear memory to be exported, so if you want wasi you can't share the linear memory.
Luckily I don't need wasi, so that shouldn't be a problem.
Also the wasm modules themself need to be aware that the linear memory is shared to avoid corruption.
This is just from a logic standpoint correct? Like each module needing to understand that it shares a heap with other modules and therefore must ensure their allocators don't overlap each-other.
bjorn3 commented on issue #5329:
If the wasm modules import the linear memory rather than export it
Is there a way to generate a WASM binary from Rust that will import it's memory instead of exporting it? Or is it essentially a matter of simply transforming the WASM output from Rust from an export to an import?
Passing
--import-memory
to the linker should work for importing the memory, but I don't think it is enough to avoid corruption.Also the wasm modules themself need to be aware that the linear memory is shared to avoid corruption.
This is just from a logic standpoint correct? Like each module needing to understand that it shares a heap with other modules and therefore must ensure their allocators don't overlap each-other.
Changing the allocator is not enough. The stack and the data segment must also not overlap. I'm not sure if lld (the linker used by rustc for wasm) supports this. Emscripten does support it afaik when linking as dynamic libraries, but afaik lld doesn't support dynamic libraries on wasm.
alexcrichton commented on issue #5329:
Sharing memory can be done, as mentioned, by having modules import their linear memory. This is implemented and available for use in Wasmtime. Creating a module that imports memory to simultaneously share with others, however, is not a trivial operation to do. There is no standard at this time for such a set of modules, and everything must be manually set up (which may be unlikely to be what you're looking for).
Given though that Wasmtime implements everything necessary here and the remaining part of the question is a toolchain issue I'm going to close this since I don't think there's an issue for Wasmtime to handle here.
alexcrichton closed issue #5329:
I have a use-case where I have multiple WASM modules that I all want to have the _same_ default WASM memory.
Each one would obviously only be able to be running one at a time, but I need them all to be able to modify the same WASM memory without having to copy the memory between each module.
Is this possible in Wasmtime?
Last updated: Dec 23 2024 at 12:05 UTC