From d307d0e755f11d83ab7922286d07f4b879da46e0 Mon Sep 17 00:00:00 2001 From: rinfx <893383980@qq.com> Date: Mon, 7 Aug 2023 13:35:45 +0800 Subject: [PATCH] update wasm go sdk to tinygo 0.28.1 (#462) --- plugins/wasm-go/Dockerfile | 4 +- plugins/wasm-go/Makefile | 2 +- plugins/wasm-go/README.md | 4 +- .../wasm-go/extensions/chatgpt-proxy/go.mod | 2 + .../wasm-go/extensions/gw-error-format/go.mod | 2 + plugins/wasm-go/extensions/waf/go.mod | 12 +- plugins/wasm-go/extensions/waf/go.sum | 25 +--- plugins/wasm-go/extensions/waf/init_tinygo.go | 30 ++++- .../wasm-go/extensions/waf/magefiles/go.mod | 8 -- .../wasm-go/extensions/waf/magefiles/go.sum | 13 -- .../extensions/waf/magefiles/loadtest.go | 81 ------------ .../extensions/waf/magefiles/magefile.go | 120 +----------------- plugins/wasm-go/go.mod | 2 + plugins/wasm-go/go.sum | 4 + plugins/wasm-go/pkg/wrapper/plugin_wrapper.go | 6 + 15 files changed, 50 insertions(+), 265 deletions(-) delete mode 100644 plugins/wasm-go/extensions/waf/magefiles/loadtest.go diff --git a/plugins/wasm-go/Dockerfile b/plugins/wasm-go/Dockerfile index 046fd1154..9900871db 100644 --- a/plugins/wasm-go/Dockerfile +++ b/plugins/wasm-go/Dockerfile @@ -1,4 +1,4 @@ -ARG BUILDER=higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/wasm-go-builder:go1.19-tinygo0.25.0-oras1.0.0 +ARG BUILDER=higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/wasm-go-builder:go1.19-tinygo0.28.1-oras1.0.0 FROM $BUILDER as builder @@ -14,7 +14,7 @@ COPY . . WORKDIR /workspace/extensions/$PLUGIN_NAME RUN go mod tidy -RUN tinygo build -o /main.wasm -scheduler=none -target=wasi ./main.go +RUN tinygo build -o /main.wasm -scheduler=none -gc=custom -tags='custommalloc nottinygc_finalizer' -target=wasi ./main.go FROM scratch as output diff --git a/plugins/wasm-go/Makefile b/plugins/wasm-go/Makefile index b6d83c5d2..db6c31d00 100644 --- a/plugins/wasm-go/Makefile +++ b/plugins/wasm-go/Makefile @@ -2,7 +2,7 @@ PLUGIN_NAME ?= hello-world BUILDER_REGISTRY ?= higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/ REGISTRY ?= GO_VERSION ?= 1.19 -TINYGO_VERSION ?= 0.25.0 +TINYGO_VERSION ?= 0.28.1 ORAS_VERSION ?= 1.0.0 HIGRESS_VERSION ?= 1.0.0-rc USE_HIGRESS_TINYGO ?= true diff --git a/plugins/wasm-go/README.md b/plugins/wasm-go/README.md index 804ef7910..ab3f1d85e 100644 --- a/plugins/wasm-go/README.md +++ b/plugins/wasm-go/README.md @@ -45,14 +45,14 @@ output wasm file: extensions/request-block/plugin.wasm - Go 版本: >= 1.18 (需要支持范型特性) -- TinyGo 版本: >= 0.25.0 +- TinyGo 版本: >= 0.28.1 下面是本地多步骤构建 [request-block](extensions/request-block) 的例子。 ### step1. 编译 wasm ```bash -tinygo build -o main.wasm -scheduler=none -target=wasi ./extensions/request-block/main.go +tinygo build -o main.wasm -scheduler=none -target=wasi -gc=custom -tags='custommalloc nottinygc_finalizer' ./extensions/request-block/main.go ``` ### step2. 构建并推送插件的 docker 镜像 diff --git a/plugins/wasm-go/extensions/chatgpt-proxy/go.mod b/plugins/wasm-go/extensions/chatgpt-proxy/go.mod index 61e9f298b..39cfbab96 100644 --- a/plugins/wasm-go/extensions/chatgpt-proxy/go.mod +++ b/plugins/wasm-go/extensions/chatgpt-proxy/go.mod @@ -2,6 +2,8 @@ module chatgpt-proxy go 1.19 +replace github.com/alibaba/higress/plugins/wasm-go => ../.. + require ( github.com/alibaba/higress/plugins/wasm-go v0.0.0-20230629030002-81e467b6242d github.com/tetratelabs/proxy-wasm-go-sdk v0.22.0 diff --git a/plugins/wasm-go/extensions/gw-error-format/go.mod b/plugins/wasm-go/extensions/gw-error-format/go.mod index b5c437da5..c4f886308 100644 --- a/plugins/wasm-go/extensions/gw-error-format/go.mod +++ b/plugins/wasm-go/extensions/gw-error-format/go.mod @@ -2,6 +2,8 @@ module wasm-demo go 1.18 +replace github.com/alibaba/higress/plugins/wasm-go => ../.. + require ( github.com/mse-group/wasm-extensions-go v1.0.1 github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220822060051-f9d179a57f8c diff --git a/plugins/wasm-go/extensions/waf/go.mod b/plugins/wasm-go/extensions/waf/go.mod index 48cc0533b..8bc1b824f 100644 --- a/plugins/wasm-go/extensions/waf/go.mod +++ b/plugins/wasm-go/extensions/waf/go.mod @@ -6,32 +6,24 @@ require ( github.com/alibaba/higress/plugins/wasm-go v0.0.0 github.com/corazawaf/coraza-wasilibs v0.0.0-20230408002644-e2e3af21f503 github.com/corazawaf/coraza/v3 v3.0.0-rc.1.0.20230407165813-a18681b1ec28 - github.com/stretchr/testify v1.8.0 github.com/tetratelabs/proxy-wasm-go-sdk v0.22.0 github.com/tidwall/gjson v1.14.4 - github.com/wasilibs/nottinygc v0.2.0 ) require ( github.com/corazawaf/libinjection-go v0.1.2 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect github.com/google/uuid v1.3.0 // indirect - github.com/kr/pretty v0.1.0 // indirect github.com/magefile/mage v1.14.0 // indirect github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/tetratelabs/wazero v1.0.1 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/wasilibs/go-aho-corasick v0.3.0 // indirect github.com/wasilibs/go-libinjection v0.2.1 // indirect github.com/wasilibs/go-re2 v1.0.0 // indirect + github.com/wasilibs/nottinygc v0.4.0 // indirect golang.org/x/net v0.9.0 // indirect - gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect rsc.io/binaryregexp v0.2.0 // indirect ) -replace ( - replace github.com/alibaba/higress/plugins/wasm-go => ../.. -) \ No newline at end of file +replace github.com/alibaba/higress/plugins/wasm-go => ../.. diff --git a/plugins/wasm-go/extensions/waf/go.sum b/plugins/wasm-go/extensions/waf/go.sum index b5d21d76c..6eb6632c3 100644 --- a/plugins/wasm-go/extensions/waf/go.sum +++ b/plugins/wasm-go/extensions/waf/go.sum @@ -1,36 +1,20 @@ -github.com/alibaba/higress/plugins/wasm-go v0.0.0-20230504075705-7e358eb1db7c h1:RKZJGYAkczZVqvJ/CuNJSY6YI5oyJjzm12MVzf3Z7/U= -github.com/alibaba/higress/plugins/wasm-go v0.0.0-20230504075705-7e358eb1db7c/go.mod h1:AzSnkuon5c26nIePTiJQIAFsKdhkNdncLcTuahpGtQs= github.com/corazawaf/coraza-wasilibs v0.0.0-20230408002644-e2e3af21f503 h1:hGXspDwUBHQUne1NT2D6PmkR9wFCXsibjaJpz7xhf+g= github.com/corazawaf/coraza-wasilibs v0.0.0-20230408002644-e2e3af21f503/go.mod h1:bTc+NV7T2wQevFQHDDWhD/+IAA5bvKbbK4CxzfvJx/o= github.com/corazawaf/coraza/v3 v3.0.0-rc.1.0.20230407165813-a18681b1ec28 h1:Jrlvhe4YCR/PMCazDEBeun/XTYhlzczBN0WN4/ejORo= github.com/corazawaf/coraza/v3 v3.0.0-rc.1.0.20230407165813-a18681b1ec28/go.mod h1:TKREBLh55w3SiBbLsQpH9EFzjBAmEUH4KRaZ/kFYz20= github.com/corazawaf/libinjection-go v0.1.2 h1:oeiV9pc5rvJ+2oqOqXEAMJousPpGiup6f7Y3nZj5GoM= github.com/corazawaf/libinjection-go v0.1.2/go.mod h1:OP4TM7xdJ2skyXqNX1AN1wN5nNZEmJNuWbNPOItn7aw= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/foxcpp/go-mockdns v1.0.0 h1:7jBqxd3WDWwi/6WhDvacvH1XsN3rOLXyHM1uhvIx6FI= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo= github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA= github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 h1:lL+y4Xv20pVlCGyLzNHRC0I0rIHhIL1lTvHizoS/dU8= github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9/go.mod h1:EHPiTAKtiFmrMldLUNswFwfZ2eJIYBHktdaUTZxYWRw= 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/rinfx/higress/plugins/wasm-go v0.0.0-20230508112120-f2d89b0606ee h1:5qHbEcei04hDcUTzvoFNtYtZiewnTgVv6wR9co8Ih6c= -github.com/rinfx/higress/plugins/wasm-go v0.0.0-20230508112120-f2d89b0606ee/go.mod h1:AzSnkuon5c26nIePTiJQIAFsKdhkNdncLcTuahpGtQs= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 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= github.com/tetratelabs/proxy-wasm-go-sdk v0.22.0 h1:kS7BvMKN+FiptV4pfwiNX8e3q14evxAWkhYbxt8EI1M= github.com/tetratelabs/proxy-wasm-go-sdk v0.22.0/go.mod h1:qkW5MBz2jch2u8bS59wws65WC+Gtx3x0aPUX5JL7CXI= github.com/tetratelabs/wazero v1.0.1 h1:xyWBoGyMjYekG3mEQ/W7xm9E05S89kJ/at696d/9yuc= @@ -48,18 +32,13 @@ github.com/wasilibs/go-libinjection v0.2.1 h1:1aSwyE4oNpPGpFw3i3hoM15sF3qn1s4P0j github.com/wasilibs/go-libinjection v0.2.1/go.mod h1:ZUoVe+HLQYq+QPBNTSgg3fxGvZsvXiDbi0UomBlsGzo= github.com/wasilibs/go-re2 v1.0.0 h1:pvrqtMzZgTMHVPfXJrk4YZwiqIXOKdfo5aed6CzUAW4= github.com/wasilibs/go-re2 v1.0.0/go.mod h1:8g69JapfgjSCx49dKOQij1dqA3sOvoH5NteaUy1X0SA= -github.com/wasilibs/nottinygc v0.2.0 h1:cXz2Ac9bVMLkpuOlUlPQMWowjw0K2cOErXZOFdAj7yE= -github.com/wasilibs/nottinygc v0.2.0/go.mod h1:oDcIotskuYNMpqMF23l7Z8uzD4TC0WXHK8jetlB3HIo= +github.com/wasilibs/nottinygc v0.4.0 h1:h1TJMihMC4neN6Zq+WKpLxgd9xCFMw7O9ETLwY2exJQ= +github.com/wasilibs/nottinygc v0.4.0/go.mod h1:oDcIotskuYNMpqMF23l7Z8uzD4TC0WXHK8jetlB3HIo= golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= rsc.io/binaryregexp v0.2.0 h1:HfqmD5MEmC0zvwBuF187nq9mdnXjXsSivRiXN7SmRkE= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= diff --git a/plugins/wasm-go/extensions/waf/init_tinygo.go b/plugins/wasm-go/extensions/waf/init_tinygo.go index f7a7813e6..182d0853b 100644 --- a/plugins/wasm-go/extensions/waf/init_tinygo.go +++ b/plugins/wasm-go/extensions/waf/init_tinygo.go @@ -1,14 +1,30 @@ +// Copyright The OWASP Coraza contributors +// SPDX-License-Identifier: Apache-2.0 + //go:build tinygo package main -import _ "github.com/wasilibs/nottinygc" +import ( + "unsafe" +) -// Compiled by nottinygc for delayed free but Envoy doesn't stub it yet, -// luckily nottinygc doesn't actually call the function, so it's fine to -// stub it out. +// Some host functions that are not implemented by Envoy end up getting imported anyways +// by code that gets compiled but not executed at runtime. Because we know they are not +// executed, we can stub them out to allow functioning on Envoy. Note, these match the +// names and signatures of wasi-libc, used by TinyGo, not WASI ABI. Review these exports when either +// the minimum supported version of Envoy changes or the maximum version of TinyGo. -//export sched_yield -func sched_yield() int32 { - return 0 +// fdopendir is re-exported to avoid TinyGo 0.28's import of wasi_snapshot_preview1.fd_readdir. +// +//export fdopendir +func fdopendir(fd int32) unsafe.Pointer { + return nil +} + +// readdir is re-exported to avoid TinyGo 0.28's import of wasi_snapshot_preview1.fd_readdir. +// +//export readdir +func readdir(unsafe.Pointer) unsafe.Pointer { + return nil } diff --git a/plugins/wasm-go/extensions/waf/magefiles/go.mod b/plugins/wasm-go/extensions/waf/magefiles/go.mod index 5cd743b24..8fe61db7b 100644 --- a/plugins/wasm-go/extensions/waf/magefiles/go.mod +++ b/plugins/wasm-go/extensions/waf/magefiles/go.mod @@ -3,14 +3,6 @@ module github.com/corazawaf/coraza-proxy-wasm/magefiles go 1.19 require ( - fortio.org/fortio v1.38.4 github.com/magefile/mage v1.14.0 github.com/tetratelabs/wabin v0.0.0-20220927005300-3b0fbf39a46a ) - -require ( - github.com/google/uuid v1.3.0 // indirect - golang.org/x/exp v0.0.0-20221111204811-129d8d6c17ab // indirect - golang.org/x/net v0.2.0 // indirect - golang.org/x/text v0.4.0 // indirect -) diff --git a/plugins/wasm-go/extensions/waf/magefiles/go.sum b/plugins/wasm-go/extensions/waf/magefiles/go.sum index b4a7ba8be..6f53f64cd 100644 --- a/plugins/wasm-go/extensions/waf/magefiles/go.sum +++ b/plugins/wasm-go/extensions/waf/magefiles/go.sum @@ -1,21 +1,8 @@ -fortio.org/assert v1.1.2 h1:t6WGDqPD5VFrUvx30U0+3mgXXcoPonrdKqt0vfJHn8E= -fortio.org/fortio v1.38.4 h1:HkVsu9E4emMU+i2D2HjPll1Dbu5IqCRTeKeoiYWxFWA= -fortio.org/fortio v1.38.4/go.mod h1:xZTReCI6wlPJQN+JssVO6jsqXJV2vC32dcZJrUaqmcU= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo= github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= 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= -golang.org/x/exp v0.0.0-20221111204811-129d8d6c17ab h1:1S7USr8/C0Sgk4egxq4zZ07zYt2Xh1IiFp8hUMXH/us= -golang.org/x/exp v0.0.0-20221111204811-129d8d6c17ab/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= -golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU= -golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= -golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= -golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= -golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/plugins/wasm-go/extensions/waf/magefiles/loadtest.go b/plugins/wasm-go/extensions/waf/magefiles/loadtest.go deleted file mode 100644 index 68fa0db23..000000000 --- a/plugins/wasm-go/extensions/waf/magefiles/loadtest.go +++ /dev/null @@ -1,81 +0,0 @@ -package main - -import ( - "fmt" - "net/http" - "time" - - "fortio.org/fortio/fhttp" - "fortio.org/fortio/fnet" - "fortio.org/fortio/periodic" - "github.com/magefile/mage/sh" -) - -// LoadTest runs load tests against the ftw deployment. -func LoadTest() error { - for _, threads := range []int{1, 2, 4} { - for _, payloadSize := range []int{0, 100, 1000, 10000} { - for _, conf := range []string{"envoy-config.yaml", "envoy-config-nowasm.yaml"} { - if err := doLoadTest(conf, payloadSize, threads); err != nil { - return err - } - } - } - } - return nil -} - -func doLoadTest(conf string, payloadSize int, threads int) error { - if err := sh.RunV("docker-compose", "--file", "ftw/docker-compose.yml", "build", "--pull"); err != nil { - return err - } - defer func() { - _ = sh.RunV("docker-compose", "--file", "ftw/docker-compose.yml", "kill") - _ = sh.RunV("docker-compose", "--file", "ftw/docker-compose.yml", "down", "-v") - }() - if err := sh.RunWithV(map[string]string{"ENVOY_CONFIG": fmt.Sprintf("/conf/%s", conf)}, "docker-compose", - "--file", "ftw/docker-compose.yml", "run", "--service-ports", "--rm", "-d", "envoy"); err != nil { - return err - } - - // Wait for Envoy to start. - for i := 0; i < 1000; i++ { - if resp, err := http.Get("http://localhost:8080/anything"); err != nil { - continue - } else { - if resp.Body != nil { - resp.Body.Close() - } - if resp.StatusCode == http.StatusOK { - break - } - } - time.Sleep(50 * time.Millisecond) - } - - opts := &fhttp.HTTPRunnerOptions{ - RunnerOptions: periodic.RunnerOptions{ - QPS: 100, - NumThreads: threads, - Duration: 10 * time.Second, - }, - HTTPOptions: fhttp.HTTPOptions{ - URL: "http://localhost:8080/anything", - Payload: fnet.GenerateRandomPayload(payloadSize), - }, - } - - fmt.Printf("Running load test with config=%s, payloadSize=%d, threads=%d\n", conf, payloadSize, threads) - res, err := fhttp.RunHTTPTest(opts) - if err != nil { - return err - } - rr := res.Result() - fmt.Printf("All done %d calls (plus %d warmup) %.3f ms avg, %.1f qps\n", - rr.DurationHistogram.Count, - 0, - 1000.*rr.DurationHistogram.Avg, - rr.ActualQPS) - - return nil -} diff --git a/plugins/wasm-go/extensions/waf/magefiles/magefile.go b/plugins/wasm-go/extensions/waf/magefiles/magefile.go index b815d9518..f26d708fc 100644 --- a/plugins/wasm-go/extensions/waf/magefiles/magefile.go +++ b/plugins/wasm-go/extensions/waf/magefiles/magefile.go @@ -1,29 +1,21 @@ package main import ( - "errors" "fmt" - "io" "os" "path/filepath" "regexp" "strconv" "strings" - "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" "github.com/tetratelabs/wabin/binary" "github.com/tetratelabs/wabin/wasm" ) var minGoVersion = "1.19" -var tinygoMinorVersion = "0.27" -var addLicenseVersion = "04bfe4ee9ca5764577b029acc6a1957fd1997153" // https://github.com/google/addlicense -var golangCILintVer = "v1.48.0" // https://github.com/golangci/golangci-lint/releases -var gosImportsVer = "v0.3.1" // https://github.com/rinchsan/gosimports/releases/tag/v0.3.1 - -var errCommitFormatting = errors.New("files not formatted, please commit formatting changes") -var errNoGitDir = errors.New("no .git directory found") +var tinygoMinorVersion = "0.28" +var Default = Build func init() { for _, check := range []func() error{ @@ -91,71 +83,6 @@ func checkTinygoVersion() error { return nil } -// Format formats code in this repository. -func Format() error { - if err := sh.RunV("go", "mod", "tidy"); err != nil { - return err - } - // addlicense strangely logs skipped files to stderr despite not being erroneous, so use the long sh.Exec form to - // discard stderr too. - if _, err := sh.Exec(map[string]string{}, io.Discard, io.Discard, "go", "run", fmt.Sprintf("github.com/google/addlicense@%s", addLicenseVersion), - "-c", "The OWASP Coraza contributors", - "-s=only", - "-y=", - "-ignore", "**/*.yml", - "-ignore", "**/*.yaml", - "-ignore", "examples/**", "."); err != nil { - return err - } - return sh.RunV("go", "run", fmt.Sprintf("github.com/rinchsan/gosimports/cmd/gosimports@%s", gosImportsVer), - "-w", - "-local", - "github.com/corazawaf/coraza-proxy-wasm", - ".") -} - -// Lint verifies code quality. -func Lint() error { - if err := sh.RunV("go", "run", fmt.Sprintf("github.com/golangci/golangci-lint/cmd/golangci-lint@%s", golangCILintVer), "run"); err != nil { - return err - } - - mg.SerialDeps(Format) - - if sh.Run("git", "diff", "--exit-code") != nil { - return errCommitFormatting - } - - return nil -} - -// Test runs all unit tests. -func Test() error { - return sh.RunV("go", "test", "./...") -} - -// Coverage runs tests with coverage and race detector enabled. -func Coverage() error { - if err := os.MkdirAll("build", 0755); err != nil { - return err - } - if err := sh.RunV("go", "test", "-race", "-coverprofile=build/coverage.txt", "-covermode=atomic", "-coverpkg=./...", "./..."); err != nil { - return err - } - - return sh.RunV("go", "tool", "cover", "-html=build/coverage.txt", "-o", "build/coverage.html") -} - -// Doc runs godoc, access at http://localhost:6060 -func Doc() error { - return sh.RunV("go", "run", "golang.org/x/tools/cmd/godoc@latest", "-http=:6060") -} - -// Check runs lint and tests. -func Check() { - mg.SerialDeps(Lint, Test) -} - // Build builds the Coraza wasm plugin. func Build() error { if err := os.MkdirAll("local", 0755); err != nil { @@ -197,49 +124,6 @@ func Build() error { return nil } -// E2e runs e2e tests with a built plugin against the example deployment. Requires docker-compose. -func E2e() error { - if err := sh.RunV("docker-compose", "--file", "e2e/docker-compose.yml", "build", "--pull"); err != nil { - return err - } - return sh.RunV("docker-compose", "-f", "e2e/docker-compose.yml", "up", "--abort-on-container-exit", "tests") -} - -// Ftw runs ftw tests with a built plugin and Envoy. Requires docker-compose. -func Ftw() error { - if err := sh.RunV("docker-compose", "--file", "ftw/docker-compose.yml", "build", "--pull"); err != nil { - return err - } - defer func() { - _ = sh.RunV("docker-compose", "--file", "ftw/docker-compose.yml", "down", "-v") - }() - env := map[string]string{ - "FTW_CLOUDMODE": os.Getenv("FTW_CLOUDMODE"), - "FTW_INCLUDE": os.Getenv("FTW_INCLUDE"), - "ENVOY_IMAGE": os.Getenv("ENVOY_IMAGE"), - } - if os.Getenv("ENVOY_NOWASM") == "true" { - env["ENVOY_CONFIG"] = "/conf/envoy-config-nowasm.yaml" - } - task := "ftw" - if os.Getenv("MEMSTATS") == "true" { - task = "ftw-memstats" - } - return sh.RunWithV(env, "docker-compose", "--file", "ftw/docker-compose.yml", "run", "--rm", task) -} - -// RunExample spins up the test environment, access at http://localhost:8080. Requires docker-compose. -func RunExample() error { - return sh.RunWithV(map[string]string{"ENVOY_IMAGE": os.Getenv("ENVOY_IMAGE")}, "docker-compose", "--file", "example/docker-compose.yml", "up", "-d", "envoy-logs") -} - -// TeardownExample tears down the test environment. Requires docker-compose. -func TeardownExample() error { - return sh.RunV("docker-compose", "--file", "example/docker-compose.yml", "down") -} - -var Default = Build - func patchWasm(inPath, outPath string, initialPages int) error { raw, err := os.ReadFile(inPath) if err != nil { diff --git a/plugins/wasm-go/go.mod b/plugins/wasm-go/go.mod index 9f2595053..d1b15a87f 100644 --- a/plugins/wasm-go/go.mod +++ b/plugins/wasm-go/go.mod @@ -11,8 +11,10 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/magefile/mage v1.14.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect + github.com/wasilibs/nottinygc v0.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/plugins/wasm-go/go.sum b/plugins/wasm-go/go.sum index dd9c5a664..80864a7c3 100644 --- a/plugins/wasm-go/go.sum +++ b/plugins/wasm-go/go.sum @@ -3,6 +3,8 @@ 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/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo= +github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= 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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -18,6 +20,8 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/wasilibs/nottinygc v0.4.0 h1:h1TJMihMC4neN6Zq+WKpLxgd9xCFMw7O9ETLwY2exJQ= +github.com/wasilibs/nottinygc v0.4.0/go.mod h1:oDcIotskuYNMpqMF23l7Z8uzD4TC0WXHK8jetlB3HIo= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/plugins/wasm-go/pkg/wrapper/plugin_wrapper.go b/plugins/wasm-go/pkg/wrapper/plugin_wrapper.go index 8e07262c0..b33b4169a 100644 --- a/plugins/wasm-go/pkg/wrapper/plugin_wrapper.go +++ b/plugins/wasm-go/pkg/wrapper/plugin_wrapper.go @@ -22,8 +22,14 @@ import ( "github.com/tidwall/gjson" "github.com/alibaba/higress/plugins/wasm-go/pkg/matcher" + _ "github.com/wasilibs/nottinygc" ) +//export sched_yield +func sched_yield() int32 { + return 0 +} + type HttpContext interface { Scheme() string Host() string