Skip to content

Commit

Permalink
Update to TinyGo 0.26 (envoyproxy#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Oct 6, 2022
1 parent 7a94d69 commit bc11806
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 131 deletions.
61 changes: 0 additions & 61 deletions .github/workflows/buildtools-tinygo.yaml

This file was deleted.

10 changes: 3 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ on:
workflow_dispatch:

env:
GO_VERSION: 1.18
TINYGO_VERSION: 0.25.0
GO_VERSION: 1.19
TINYGO_VERSION: 0.26.0
WABT_VERSION: 1.0.29
# The SHA of the envoy version comes from https://github.com/istio/proxy/blob/master/WORKSPACE#L42
# Here we aim to support:
Expand Down Expand Up @@ -39,15 +39,11 @@ jobs:
go-version: ${{ env.GO_VERSION }}
cache: true

- name: Install TinyGo and wabt
- name: Install TinyGo
run: |
gh release download v${TINYGO_VERSION} -p '*.linux-amd64.tar.gz' -D ~ -R github.com/tinygo-org/tinygo
tar -xf ~/tinygo${TINYGO_VERSION}.linux-amd64.tar.gz -C $HOME
echo "$HOME/tinygo/bin" >> $GITHUB_PATH
gh release download ${WABT_VERSION} -p '*-ubuntu.tar.gz' -D ~ -R github.com/WebAssembly/wabt
tar -xf ~/wabt-${WABT_VERSION}-ubuntu.tar.gz -C $HOME
echo "$HOME/wabt-${WABT_VERSION}/bin" >> $GITHUB_PATH
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ Targets:

### Building the filter

>Note: The build of the Wasm filter currently relies on Go `1.18.*`. On MacOS, you can install it with `brew install [email protected]` and then use
```
PATH=/opt/homebrew/Cellar/[email protected]/1.18.6/bin:$PATH GOROOT=/opt/homebrew/Cellar/[email protected]/1.18.6/libexec go run mage.go build
go run mage.go build
```
You will find the WASM plugin under `./build/main.wasm`.

Expand Down
20 changes: 0 additions & 20 deletions buildtools/tinygo/Dockerfile

This file was deleted.

13 changes: 0 additions & 13 deletions buildtools/tinygo/wasi-libc.Dockerfile

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/magefile/mage v1.14.0
github.com/stretchr/testify v1.8.0
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220922045757-132ee0a06ac2
github.com/tetratelabs/wabin v0.0.0-20220927005300-3b0fbf39a46a
github.com/tidwall/gjson v1.14.3
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220922045757-132ee0a06ac2 h1:Dd0c7ZCOStoWSnBtfcoF7mXC0MPA9YfbrCHkE4qexZs=
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220922045757-132ee0a06ac2/go.mod h1:YVEdbjpYmWRaTXSHuIK6O1kibi313mcQpTbcuBTd4Mg=
github.com/tetratelabs/wabin v0.0.0-20220927005300-3b0fbf39a46a h1:P0R3+CTAT7daT8ig5gh9GEd/eDQ5md1xl4pkYMcwOqg=
github.com/tetratelabs/wabin v0.0.0-20220927005300-3b0fbf39a46a/go.mod h1:m9ymHTgNSEjuxvw8E7WWe4Pl4hZQHXONY8wE6dMLaRk=
github.com/tetratelabs/wazero v1.0.0-beta.2 h1:Qa1R1oizAYHcmy8PljgINdXUZ/nRQkxUBbYfGSb4IBE=
github.com/tetratelabs/wazero v1.0.0-beta.2/go.mod h1:CD5smBN5rGZo7UNe8aUiWyYE3bDWED/CQSonog9NSEg=
github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw=
Expand Down
61 changes: 33 additions & 28 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
"io"
"os"
"path/filepath"
"strings"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/tetratelabs/wabin/binary"
"github.com/tetratelabs/wabin/wasm"
)

var addLicenseVersion = "04bfe4ee9ca5764577b029acc6a1957fd1997153" // https://github.com/google/addlicense
Expand Down Expand Up @@ -95,41 +96,17 @@ func Build() error {
if err := os.MkdirAll("build", 0755); err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}

timingBuildTag := ""
if os.Getenv("TIMING") == "true" {
timingBuildTag = "-tags 'timing proxywasm_timing'"
timingBuildTag = "-tags='timing proxywasm_timing'"
}

script := fmt.Sprintf(`
cd /src && \
tinygo build -opt 2 -o build/mainraw.wasm -scheduler=none -target=wasi %s . && \
wasm-opt -Os -c build/mainraw.wasm -o build/mainopt.wasm && \
wasm2wat --enable-all build/mainopt.wasm -o build/mainopt.wat
`, timingBuildTag)

if err := sh.RunV("docker", "run", "--pull", "always", "--rm", "-v", fmt.Sprintf("%s:/src", wd), "ghcr.io/corazawaf/coraza-proxy-wasm/buildtools-tinygo:main", "bash", "-c",
strings.TrimSpace(script)); err != nil {
if err := sh.RunV("tinygo", "build", "-opt=2", "-o", filepath.Join("build", "mainraw.wasm"), "-scheduler=none", "-target=wasi", timingBuildTag); err != nil {
return err
}

watBytes, err := os.ReadFile(filepath.Join("build", "mainopt.wat"))
if err != nil {
return err
}
wat := string(watBytes)
wat = strings.ReplaceAll(wat, "fd_filestat_get", "fd_fdstat_get")
wat = strings.ReplaceAll(wat, `"wasi_snapshot_preview1" "path_filestat_get"`, `"env" "proxy_get_header_map_value"`)
err = os.WriteFile(filepath.Join("build", "main.wat"), []byte(wat), 0644)
if err != nil {
return err
}
return sh.RunV("docker", "run", "--rm", "-v", fmt.Sprintf("%s:/build", filepath.Join(wd, "build")), "ghcr.io/corazawaf/coraza-proxy-wasm/buildtools-tinygo:main", "bash", "-c",
"wat2wasm --enable-all /build/main.wat -o /build/main.wasm")
return stubUnusedWasmImports(filepath.Join("build", "mainraw.wasm"), filepath.Join("build", "main.wasm"))
}

// UpdateLibs updates the C++ filter dependencies.
Expand Down Expand Up @@ -183,3 +160,31 @@ func TeardownExample() error {
}

var Default = Build

func stubUnusedWasmImports(inPath, outPath string) error {
raw, err := os.ReadFile(inPath)
if err != nil {
return err
}
mod, err := binary.DecodeModule(raw, wasm.CoreFeaturesV2)
if err != nil {
return err
}

for _, imp := range mod.ImportSection {
switch {
case imp.Name == "fd_filestat_get":
imp.Name = "fd_fdstat_get"
case imp.Name == "path_filestat_get":
imp.Module = "env"
imp.Name = "proxy_get_header_map_value"
}
}

out := binary.EncodeModule(mod)
if err = os.WriteFile(outPath, out, 0644); err != nil {
return err
}

return nil
}

0 comments on commit bc11806

Please sign in to comment.