diff --git a/samples/wasmtime/README.md b/samples/wasmtime/README.md index 1b4803c..6f048ca 100644 --- a/samples/wasmtime/README.md +++ b/samples/wasmtime/README.md @@ -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 ... diff --git a/samples/wasmtime/c.go b/samples/wasmtime/c.go index c5d8dfa..d7f8ff1 100644 --- a/samples/wasmtime/c.go +++ b/samples/wasmtime/c.go @@ -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 +int clock_getres(int clockid, struct timespec* res) { + res->tv_sec = 0; + res->tv_nsec = 4000000; + return 0; +} */ import "C" diff --git a/samples/wasmtime/enclave.json b/samples/wasmtime/enclave.json index 097127c..07f6406 100644 --- a/samples/wasmtime/enclave.json +++ b/samples/wasmtime/enclave.json @@ -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 -} \ No newline at end of file + "exe": "wasmtime_sample", + "key": "private.pem", + "debug": true, + "heapSize": 512, + "executableHeap": true, + "productID": 1, + "securityVersion": 1 +} diff --git a/samples/wasmtime/go.mod b/samples/wasmtime/go.mod index f0c6c44..3dcc4c2 100644 --- a/samples/wasmtime/go.mod +++ b/samples/wasmtime/go.mod @@ -1,5 +1,5 @@ module wasmtime_sample -go 1.17 +go 1.18 require github.com/bytecodealliance/wasmtime-go/v15 v15.0.0 diff --git a/samples/wasmtime/go.sum b/samples/wasmtime/go.sum index 3d135d0..43bf921 100644 --- a/samples/wasmtime/go.sum +++ b/samples/wasmtime/go.sum @@ -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=