Skip to content

Commit bc11806

Browse files
authored
Update to TinyGo 0.26 (#31)
1 parent 7a94d69 commit bc11806

File tree

8 files changed

+40
-131
lines changed

8 files changed

+40
-131
lines changed

.github/workflows/buildtools-tinygo.yaml

Lines changed: 0 additions & 61 deletions
This file was deleted.

.github/workflows/ci.yaml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ on:
1010
workflow_dispatch:
1111

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

42-
- name: Install TinyGo and wabt
42+
- name: Install TinyGo
4343
run: |
4444
gh release download v${TINYGO_VERSION} -p '*.linux-amd64.tar.gz' -D ~ -R github.com/tinygo-org/tinygo
4545
tar -xf ~/tinygo${TINYGO_VERSION}.linux-amd64.tar.gz -C $HOME
4646
echo "$HOME/tinygo/bin" >> $GITHUB_PATH
47-
48-
gh release download ${WABT_VERSION} -p '*-ubuntu.tar.gz' -D ~ -R github.com/WebAssembly/wabt
49-
tar -xf ~/wabt-${WABT_VERSION}-ubuntu.tar.gz -C $HOME
50-
echo "$HOME/wabt-${WABT_VERSION}/bin" >> $GITHUB_PATH
5147
env:
5248
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5349

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ Targets:
2424

2525
### Building the filter
2626

27-
>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
2827
```
29-
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
28+
go run mage.go build
3029
```
3130
You will find the WASM plugin under `./build/main.wasm`.
3231

buildtools/tinygo/Dockerfile

Lines changed: 0 additions & 20 deletions
This file was deleted.

buildtools/tinygo/wasi-libc.Dockerfile

Lines changed: 0 additions & 13 deletions
This file was deleted.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/magefile/mage v1.14.0
88
github.com/stretchr/testify v1.8.0
99
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220922045757-132ee0a06ac2
10+
github.com/tetratelabs/wabin v0.0.0-20220927005300-3b0fbf39a46a
1011
github.com/tidwall/gjson v1.14.3
1112
)
1213

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK
2525
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
2626
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220922045757-132ee0a06ac2 h1:Dd0c7ZCOStoWSnBtfcoF7mXC0MPA9YfbrCHkE4qexZs=
2727
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220922045757-132ee0a06ac2/go.mod h1:YVEdbjpYmWRaTXSHuIK6O1kibi313mcQpTbcuBTd4Mg=
28+
github.com/tetratelabs/wabin v0.0.0-20220927005300-3b0fbf39a46a h1:P0R3+CTAT7daT8ig5gh9GEd/eDQ5md1xl4pkYMcwOqg=
29+
github.com/tetratelabs/wabin v0.0.0-20220927005300-3b0fbf39a46a/go.mod h1:m9ymHTgNSEjuxvw8E7WWe4Pl4hZQHXONY8wE6dMLaRk=
2830
github.com/tetratelabs/wazero v1.0.0-beta.2 h1:Qa1R1oizAYHcmy8PljgINdXUZ/nRQkxUBbYfGSb4IBE=
2931
github.com/tetratelabs/wazero v1.0.0-beta.2/go.mod h1:CD5smBN5rGZo7UNe8aUiWyYE3bDWED/CQSonog9NSEg=
3032
github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw=

magefile.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import (
1212
"io"
1313
"os"
1414
"path/filepath"
15-
"strings"
1615

1716
"github.com/magefile/mage/mg"
1817
"github.com/magefile/mage/sh"
18+
"github.com/tetratelabs/wabin/binary"
19+
"github.com/tetratelabs/wabin/wasm"
1920
)
2021

2122
var addLicenseVersion = "04bfe4ee9ca5764577b029acc6a1957fd1997153" // https://github.com/google/addlicense
@@ -95,41 +96,17 @@ func Build() error {
9596
if err := os.MkdirAll("build", 0755); err != nil {
9697
return err
9798
}
98-
wd, err := os.Getwd()
99-
if err != nil {
100-
return err
101-
}
10299

103100
timingBuildTag := ""
104101
if os.Getenv("TIMING") == "true" {
105-
timingBuildTag = "-tags 'timing proxywasm_timing'"
102+
timingBuildTag = "-tags='timing proxywasm_timing'"
106103
}
107104

108-
script := fmt.Sprintf(`
109-
cd /src && \
110-
tinygo build -opt 2 -o build/mainraw.wasm -scheduler=none -target=wasi %s . && \
111-
wasm-opt -Os -c build/mainraw.wasm -o build/mainopt.wasm && \
112-
wasm2wat --enable-all build/mainopt.wasm -o build/mainopt.wat
113-
`, timingBuildTag)
114-
115-
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",
116-
strings.TrimSpace(script)); err != nil {
105+
if err := sh.RunV("tinygo", "build", "-opt=2", "-o", filepath.Join("build", "mainraw.wasm"), "-scheduler=none", "-target=wasi", timingBuildTag); err != nil {
117106
return err
118107
}
119108

120-
watBytes, err := os.ReadFile(filepath.Join("build", "mainopt.wat"))
121-
if err != nil {
122-
return err
123-
}
124-
wat := string(watBytes)
125-
wat = strings.ReplaceAll(wat, "fd_filestat_get", "fd_fdstat_get")
126-
wat = strings.ReplaceAll(wat, `"wasi_snapshot_preview1" "path_filestat_get"`, `"env" "proxy_get_header_map_value"`)
127-
err = os.WriteFile(filepath.Join("build", "main.wat"), []byte(wat), 0644)
128-
if err != nil {
129-
return err
130-
}
131-
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",
132-
"wat2wasm --enable-all /build/main.wat -o /build/main.wasm")
109+
return stubUnusedWasmImports(filepath.Join("build", "mainraw.wasm"), filepath.Join("build", "main.wasm"))
133110
}
134111

135112
// UpdateLibs updates the C++ filter dependencies.
@@ -183,3 +160,31 @@ func TeardownExample() error {
183160
}
184161

185162
var Default = Build
163+
164+
func stubUnusedWasmImports(inPath, outPath string) error {
165+
raw, err := os.ReadFile(inPath)
166+
if err != nil {
167+
return err
168+
}
169+
mod, err := binary.DecodeModule(raw, wasm.CoreFeaturesV2)
170+
if err != nil {
171+
return err
172+
}
173+
174+
for _, imp := range mod.ImportSection {
175+
switch {
176+
case imp.Name == "fd_filestat_get":
177+
imp.Name = "fd_fdstat_get"
178+
case imp.Name == "path_filestat_get":
179+
imp.Module = "env"
180+
imp.Name = "proxy_get_header_map_value"
181+
}
182+
}
183+
184+
out := binary.EncodeModule(mod)
185+
if err = os.WriteFile(outPath, out, 0644); err != nil {
186+
return err
187+
}
188+
189+
return nil
190+
}

0 commit comments

Comments
 (0)