Some attempts to combine WASM and gVisor
- Dominant language
- Go
- Stars
- 19.3k
- Forks
- 2k
- Avg merge
- 3d 5h
- Merged PRs (30d)
- 264
Description
When I tried to apply gVisor to more scenarios in production area, I found that gVisor has the potential to be the guest user space kernel of WASM runtime.
In addition, when I attended the CNCF runtime conference, I found that WASM became more and more popular as the CNCF runtime.
So, I made a simple POC to verify my idea:
https://github.com/lubinszARM/gvisor/tree/pr_platform_wasm_poc
In my simple poc, I added a WASM platform and a simple wasm syscall interface support(fd_write).
Full wasm syscall interface list is here: https://docs.rs/wasi/0.10.2+wasi-snapshot-preview1/wasi/wasi_snapshot_preview1/index.html
We can follow the steps below to verify my poc:
1, add a new runtime in /etc/docker/daemon.json
"myrunsc": {
"path": "/usr/local/bin/runsc",
"runtimeArgs": [
"--platform=wasm"
]
},
2, docker run --rm --runtime=myrunsc younglook/wasi-hello
3, verify the result
```
# docker run --rm --runtime=runsc younglook/wasi-hello
hello world
```
4, hello.wat is like following:
```
(module
;; Import the required fd_write WASI function which will write the given io vectors to stdout
;; The function signature for fd_write is:
;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
(import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
(memory 1)
(export "memory" (memory 0))
;; Write 'hello world\n' to memory at an offset of 8 bytes
;; Note the trailing newline which is required for the text to appear
(data (i32.const 8) "hello world\n")
(func $main (export "_start")
;; Creating a new io vector within linear memory
(i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
(i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string
(call $fd_write
(i32.const 1) ;; file_descriptor - 1 for stdout
(i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
(i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
(i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
)
drop ;; Discard the number of bytes written from the top of the stack
)
)
```
After communicating with my colleagues from the wasm team, we summarized the benefits of this approach:
1, Can meet the requirements of OCI
https://github.com/bytecodealliance/wasmtime/issues/358
2, Convert system call into function call to get better performance, the specific WASI link method is as follows:
https://github.com/lubinszARM/gvisor/blob/pr_platform_wasm_poc/pkg/sentry/wasmvm/wasmtimevm.go#L39
There are some similar projects that use this method, such as:
https://github.com/kenny-ngo/wasmjit
https://github.com/wasmerio/kernel-wasm
3, Introduce a high-performance network, such as DPDK
4, Introduce a user mode kernel for wasm runtime to improve security
Contributor guide
Assessment
This issue has not been assessed yet.