Skip to content

Commit

Permalink
fix wasmtime sample
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasten committed Jan 21, 2024
1 parent d32f675 commit 1df0b79
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
21 changes: 17 additions & 4 deletions samples/wasmtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@

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:
By default, *wasmtime-go* comes with a library that makes direct syscalls.
EGo only supports syscall via libc.
To this end, build the wasmtime library with libc backend:

```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
git clone -bv15.0.1 --depth=1 https://github.com/bytecodealliance/wasmtime
cd wasmtime
git submodule update --init --depth=1
RUSTFLAGS=--cfg=rustix_use_libc cargo build --release -p wasmtime-c-api
cd ..
```

Tell the Go compiler to use it:

```sh
CGO_LDFLAGS=wasmtime/target/release/libwasmtime.a 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 ...
Expand Down
10 changes: 10 additions & 0 deletions samples/wasmtime/c.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package main

// The wasmtime library links a few symbols that are not available in EGo, but it's sufficient to provide stub implementations of them.

/*
#cgo LDFLAGS: -static-libgcc
int gnu_get_libc_version() { return 0; }
int __register_atfork() { return 0; }
int __res_init() { return -1; }
// TODO remove with next EGo release that supports this syscall
#include <time.h>
int clock_getres(int clockid, struct timespec* res) {
res->tv_sec = 0;
res->tv_nsec = 4000000;
return 0;
}
*/
import "C"
19 changes: 8 additions & 11 deletions samples/wasmtime/enclave.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
{
"exe": "wasmtime_sample",
"key": "private.pem",
"debug": true,
"heapSize": 512,
"executableHeap": false,
"productID": 1,
"securityVersion": 1,
"mounts": null,
"env": null,
"files": null
}
"exe": "wasmtime_sample",
"key": "private.pem",
"debug": true,
"heapSize": 512,
"executableHeap": true,
"productID": 1,
"securityVersion": 1
}
2 changes: 1 addition & 1 deletion samples/wasmtime/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module wasmtime_sample

go 1.17
go 1.18

require github.com/bytecodealliance/wasmtime-go/v15 v15.0.0
4 changes: 0 additions & 4 deletions samples/wasmtime/go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
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=

0 comments on commit 1df0b79

Please sign in to comment.