Skip to content

Commit

Permalink
(feat): add wasmtime sample
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvinparikh authored and thomasten committed Jan 21, 2024
1 parent 6ca43ac commit d32f675
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
26 changes: 26 additions & 0 deletions samples/wasmtime/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Wasmtime sample

This sample shows how to run WebAssembly inside EGo using [Wasmtime](https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go).

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:
```sh
mkdir wasmtime
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
CGO_CFLAGS="-I$PWD/wasmtime/include" CGO_LDFLAGS="$PWD/wasmtime/lib/libwasmtime.a -ldl -lm -static-libgcc" ego-go build
```

Then you can sign and run as usual:
```sh
ego sign wasmtime_sample
ego run wasmtime_sample
```

You should see an output similar to:
```
[erthost] loading enclave ...
[erthost] entering enclave ...
[ego] starting application ...
Results of `sum`: 3
```

Note that `executableHeap` is enabled in `enclave.json` so that Wasmtime can JIT-compile the WebAssembly.
9 changes: 9 additions & 0 deletions samples/wasmtime/c.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

/*
#cgo LDFLAGS: -static-libgcc
int gnu_get_libc_version() { return 0; }
int __register_atfork() { return 0; }
int __res_init() { return -1; }
*/
import "C"
12 changes: 12 additions & 0 deletions samples/wasmtime/enclave.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"exe": "wasmtime_sample",
"key": "private.pem",
"debug": true,
"heapSize": 512,
"executableHeap": false,
"productID": 1,
"securityVersion": 1,
"mounts": null,
"env": null,
"files": null
}
5 changes: 5 additions & 0 deletions samples/wasmtime/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module wasmtime_sample

go 1.17

require github.com/bytecodealliance/wasmtime-go/v15 v15.0.0
10 changes: 10 additions & 0 deletions samples/wasmtime/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/bytecodealliance/wasmtime-go/v15 v15.0.0 h1:4R2MpSPPbtSxqdsOTvsMn1pnwdEhzbDGMao6LUUSLv4=
github.com/bytecodealliance/wasmtime-go/v15 v15.0.0/go.mod h1:m6vB/SsM+pnJkVHmO1wzHYUeYtciltTKuxuvkR8pYcY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
58 changes: 58 additions & 0 deletions samples/wasmtime/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"

"github.com/bytecodealliance/wasmtime-go/v15"
)

// https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go#example-Config-Fuel
func main() {
// Let's assume we don't have WebAssembly bytes at hand. We
// will write WebAssembly manually.
wasmBytes, err := wasmtime.Wat2Wasm(`
(module
(type (func (param i32 i32) (result i32)))
(func (type 0)
local.get 0
local.get 1
i32.add)
(export "sum" (func 0)))
`)
if err != nil {
fmt.Println("Failed to parse WebAssembly code:", err)
return
}

engine := wasmtime.NewEngine()

module, err := wasmtime.NewModule(engine, wasmBytes)
if err != nil {
fmt.Println("failed to compile module ",err)
}

linker := wasmtime.NewLinker(engine)
err = linker.DefineWasi()
if err != nil {
fmt.Println("failed to define wasi ",err)
}

wasiConfig := wasmtime.NewWasiConfig()

store := wasmtime.NewStore(engine)
store.SetWasi(wasiConfig)

instance,err := linker.Instantiate(store, module)
if err != nil {
fmt.Println("failed to instantiate ",err)
}

sumFunc := instance.GetFunc(store,"sum")
if sumFunc != nil {
result ,err := sumFunc.Call(store,1,2)
if err != nil {
fmt.Println("failed to call sum ",err)
}
fmt.Println("Results of `sum`:", result)
}
}

0 comments on commit d32f675

Please sign in to comment.