Write a Rust crate to enable WebAssembly function pass through complex data structure as parameters and result
--
[Updated in Apr 2022] This article is obsoleted. Please click the following links for the detail
- The magic of Wasm link (https://teaproject.org/doc/#/doc_list/%2Fmagic_of_wasm.md)
- a brief introduction link (https://teaproject.org/doc/#/doc_list/get_started.md)
- a video demo of our Web3.0 usage of WASM (https://teaproject.org/doc/#/doc_list/how_to_use_TEA_Party.md)
I am working on a small Proof of Concept on my confidential computing / trusted computing idea. You can read my previous blog posts to get the basic idea. WebAssemly is one of the most important parts of my solution. Due to its high security and isolation, I would like to run crucial logic inside a Wasm module. However, due to the limitation of the data types (i32, i64, f32, f64) and single return value, it makes almost impossible to write any useful logic.
WASI is the “official” solution on this kind of issues, the team even released a demo in Dec 2019 with a String type support. However, they withdraw this feature later due to some reason. I know they will put it back something soon, but I cannot just let my project wait. So I decided to make something on my own. Given I am a newbie of Rust and WASM, the best way to learn is to make something out of it, especially something I (and other developers) may find useful.
The final result is a pair of experimental crates published to Crates.io: https://crates.io/crates/wasi-binio-host and https://crates.io/crates/wasi-binio-wasm
Goals
Terminologies:
- Host: A Rust apps with a WASI runtime so that it can load and run Wasm modules.
- Guest (I call it Wasm in my document): A Wasm module can be loaded by Host and be running inside the WASI runtime.
Guest exports standard functions which can still only take 4 basic value types and one return value. Instead of calling it directly from the Host, we made a call_stub function which indirectly calls the guest functions and also transfers any complex data types from shared linear memory.
Here are some code examples (original code can be found https://github.com/TeaRUST/binio/tree/master/examples )