Stream: wasmtime

Topic: wasm-wasi function signature


view this post on Zulip Buster Styren (May 23 2023 at 09:10):

Hi, I'm failing to understand how rust functions translate into wasm functions using the wasm-wasi target. Whenever I try to return multiple values it instead adds another parameter to the signature when I fetch the typed function in wasmtime. Is there a reference somewhere on how this translation is done? I'm failing to see how I can return a fat pointer, for example, since I also fail to see how I can call a wasm function with mutable parameters.

view this post on Zulip Buster Styren (May 23 2023 at 09:13):

For example, this function has a () -> u32 signature when I call it from wasmtime:

extern "C" fn get_pets() -> u32

But this one has a (u32) -> () signature, or at least it doesn't panic when I call it with those params:

extern "C" fn get_pets() -> (u32, u32)

view this post on Zulip bjorn3 (May 23 2023 at 09:29):

The extra argument is the out pointer where the return value should be written. This is defined by the C ABI for Webassembly. See https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md

Conventions supporting interoperatibility between tools working with WebAssembly. - tool-conventions/BasicCABI.md at main · WebAssembly/tool-conventions

view this post on Zulip Buster Styren (May 23 2023 at 12:44):

Alright thanks. So I need to provide a pointer to a region where the return value can be written to? Is there a way for me to allocate this memory from wasmtime, otherwise I'm struggling to figure out how I can supply a valid writable region of the instance memory.

view this post on Zulip bjorn3 (May 23 2023 at 12:49):

You can expose a function from the wasm module to allocate memory and one to free the memory again. Wasmtime doesn't have any knowledge about the memory allocator used by the wasm module, so it can't safely do the allocation for you.


Last updated: Nov 22 2024 at 17:03 UTC