Skip to content

Commit d32f675

Browse files
dhruvinparikhthomasten
authored andcommitted
(feat): add wasmtime sample
1 parent 6ca43ac commit d32f675

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

samples/wasmtime/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Wasmtime sample
2+
3+
This sample shows how to run WebAssembly inside EGo using [Wasmtime](https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go).
4+
5+
By default, *wasmtime-go* comes with a shared library. EGo only supports static linking. To this end, download the wasmtime static library and tell the Go compiler to use it:
6+
```sh
7+
mkdir wasmtime
8+
wget -O- https://github.com/bytecodealliance/wasmtime/releases/download/v15.0.0/wasmtime-v15.0.0-x86_64-linux-c-api.tar.xz | tar xvJf - -C ./wasmtime --strip-components=1
9+
CGO_CFLAGS="-I$PWD/wasmtime/include" CGO_LDFLAGS="$PWD/wasmtime/lib/libwasmtime.a -ldl -lm -static-libgcc" ego-go build
10+
```
11+
12+
Then you can sign and run as usual:
13+
```sh
14+
ego sign wasmtime_sample
15+
ego run wasmtime_sample
16+
```
17+
18+
You should see an output similar to:
19+
```
20+
[erthost] loading enclave ...
21+
[erthost] entering enclave ...
22+
[ego] starting application ...
23+
Results of `sum`: 3
24+
```
25+
26+
Note that `executableHeap` is enabled in `enclave.json` so that Wasmtime can JIT-compile the WebAssembly.

samples/wasmtime/c.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
/*
4+
#cgo LDFLAGS: -static-libgcc
5+
int gnu_get_libc_version() { return 0; }
6+
int __register_atfork() { return 0; }
7+
int __res_init() { return -1; }
8+
*/
9+
import "C"

samples/wasmtime/enclave.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"exe": "wasmtime_sample",
3+
"key": "private.pem",
4+
"debug": true,
5+
"heapSize": 512,
6+
"executableHeap": false,
7+
"productID": 1,
8+
"securityVersion": 1,
9+
"mounts": null,
10+
"env": null,
11+
"files": null
12+
}

samples/wasmtime/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module wasmtime_sample
2+
3+
go 1.17
4+
5+
require github.com/bytecodealliance/wasmtime-go/v15 v15.0.0

samples/wasmtime/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/bytecodealliance/wasmtime-go/v15 v15.0.0 h1:4R2MpSPPbtSxqdsOTvsMn1pnwdEhzbDGMao6LUUSLv4=
2+
github.com/bytecodealliance/wasmtime-go/v15 v15.0.0/go.mod h1:m6vB/SsM+pnJkVHmO1wzHYUeYtciltTKuxuvkR8pYcY=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
8+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

samples/wasmtime/main.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/bytecodealliance/wasmtime-go/v15"
7+
)
8+
9+
// https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go#example-Config-Fuel
10+
func main() {
11+
// Let's assume we don't have WebAssembly bytes at hand. We
12+
// will write WebAssembly manually.
13+
wasmBytes, err := wasmtime.Wat2Wasm(`
14+
(module
15+
(type (func (param i32 i32) (result i32)))
16+
(func (type 0)
17+
local.get 0
18+
local.get 1
19+
i32.add)
20+
(export "sum" (func 0)))
21+
`)
22+
if err != nil {
23+
fmt.Println("Failed to parse WebAssembly code:", err)
24+
return
25+
}
26+
27+
engine := wasmtime.NewEngine()
28+
29+
module, err := wasmtime.NewModule(engine, wasmBytes)
30+
if err != nil {
31+
fmt.Println("failed to compile module ",err)
32+
}
33+
34+
linker := wasmtime.NewLinker(engine)
35+
err = linker.DefineWasi()
36+
if err != nil {
37+
fmt.Println("failed to define wasi ",err)
38+
}
39+
40+
wasiConfig := wasmtime.NewWasiConfig()
41+
42+
store := wasmtime.NewStore(engine)
43+
store.SetWasi(wasiConfig)
44+
45+
instance,err := linker.Instantiate(store, module)
46+
if err != nil {
47+
fmt.Println("failed to instantiate ",err)
48+
}
49+
50+
sumFunc := instance.GetFunc(store,"sum")
51+
if sumFunc != nil {
52+
result ,err := sumFunc.Call(store,1,2)
53+
if err != nil {
54+
fmt.Println("failed to call sum ",err)
55+
}
56+
fmt.Println("Results of `sum`:", result)
57+
}
58+
}

0 commit comments

Comments
 (0)