wasm-micro-runtime icon indicating copy to clipboard operation
wasm-micro-runtime copied to clipboard

Passing buffer betwen Multiple Modules

Open niekunhit opened this issue 3 years ago • 1 comments

Hi wamr experts. I just study the source code under the samples/multi-modules directory. It seems like there are no parameters passing when using the imported functions.

image Regarding my use case, I would like to pass a buffer when I use the imported function. image You can get the information as the figure, function call_A pass the *buffer to function A. Because module A and the main module are in the different module instances, so the pointer address of buffer is invalid in module A. Are there some methods to resolve this?

niekunhit avatar Jun 23 '22 02:06 niekunhit

Theoretically speaking, yes, there are several ways to pass buffers between modules now and more ways in the future.

The first question about passing buffers is shall we pass buffers by values or by references?

Passing by values is not a stupid idea. On the opposite, it might be the only way when passing buffers across language bounds. Because we can almost guarantee that different languages won't share the same memory layouts for one object. Actually, they won't even use the same implementation. https://stackoverflow.com/a/33983491/1967781 gives us a good example of the difference between c-style string and std::string of c++.

interface type is an ongoing proposal that tries to answer how. The basic idea of it is to transfer a buffer into a third layout and copy it. Yes, the whole progress kinds similar with an inter-process communication.

The core of "passing by references" is how to let modules see each other's memory. import is a good choice but is lack of appropriate tooling support. wasm-ld only provide to import a memory from the environment(like runtime). https://lld.llvm.org/WebAssembly.html#cmdoption-import-memory.

So, here we are. Currently the most doable solution is transfer with host-functions. Host functions are able to access modules' memory. It is allowed to use one module as the malloc() provider and let both host functions and other modules to access. envoy is using such a way to let its WASM filters to process incoming packages.

lum1n0us avatar Jun 29 '22 00:06 lum1n0us