DouglasDwyer / wasm_component_layer

WebAssembly component model implementation for any backend.
73 stars 12 forks source link

resource transfer trampolines are unimplemented #9

Open juntyr opened 9 months ago

juntyr commented 9 months ago

First of all, thank you for your work on this amazing crate - I really hope it becomes a standard API to write runtime-independent code running WASM components :)

I am currently trying to port my WASM modules to components and to switch from a custom runtime abstraction to wasm_runtime_layer. My components use WASI but are composed with a dummy WASI implementation that ensures all calls have deterministic behaviour. Unfortunately, when I try to instantiate the composed component with wasm_component_layer, the crate crashes on a trampoline being unimplemented. I cross-checked with jco, which also doesn't support all trampolines yet but matches on them exhaustively, and was thus able to see that the missing trampoline is ResourceTransferOwn. For reference, wasmtime implements them here: https://github.com/bytecodealliance/wasmtime/blob/main/crates/cranelift/src/compiler/component.rs#L69-L115

How difficult do you think would it be to implement the missing trampolines for the wasm_component_layer?

juntyr commented 9 months ago

Also see https://github.com/bytecodealliance/jco/issues/359 for jco's issue on this topic

DouglasDwyer commented 9 months ago

Hi there. Thanks for reaching out! Yes, trampolines are not currently supported. This poses a challenge for components that have been composed. Not only are the resource transfer trampolines missing, but the string transcoder trampolines as well.

From a glance at the wasmtime implementation, it looks like the resource transfer trampolines are implemented entirely on the host. This means that the implementation shouldn't be too difficult - it would involve extending the GeneratedTrampoline enum in this crate with the new types and creating host-side Funcs to respond to the function calls.

String transcoder trampolines will be much trickier to implement. From what I remember, wasmtime implements string transcoders by using multiple WASM memories to share data directly between components. Multiple memories are not available in other runtimes, (like wasmi or the web) and so a workaround for string transcoders is required. I would need to do more research on this - but ideally an implementation that goes through the host would be ideal.

Regretfully, I do not presently have time to add new features to this crate. I am open to PRs, though, and would be happy to provide additional details about how the implementation should look! :)