chore(go): updates wazero to 1.0.0-pre.3 (#3090)

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-11-13 08:53:01 +01:00
committed by GitHub
parent 02f77bc120
commit 8fcca9c8cf
3 changed files with 44 additions and 17 deletions

2
go.mod
View File

@@ -63,7 +63,7 @@ require (
github.com/spf13/viper v1.13.0 github.com/spf13/viper v1.13.0
github.com/stretchr/testify v1.8.0 github.com/stretchr/testify v1.8.0
github.com/testcontainers/testcontainers-go v0.13.0 github.com/testcontainers/testcontainers-go v0.13.0
github.com/tetratelabs/wazero v1.0.0-pre.2 github.com/tetratelabs/wazero v1.0.0-pre.3
github.com/twitchtv/twirp v8.1.2+incompatible github.com/twitchtv/twirp v8.1.2+incompatible
github.com/xlab/treeprint v1.1.0 github.com/xlab/treeprint v1.1.0
go.etcd.io/bbolt v1.3.6 go.etcd.io/bbolt v1.3.6

4
go.sum
View File

@@ -1521,8 +1521,8 @@ github.com/tchap/go-patricia/v2 v2.3.1 h1:6rQp39lgIYZ+MHmdEq4xzuk1t7OdC35z/xm0BG
github.com/tchap/go-patricia/v2 v2.3.1/go.mod h1:VZRHKAb53DLaG+nA9EaYYiaEx6YztwDlLElMsnSHD4k= github.com/tchap/go-patricia/v2 v2.3.1/go.mod h1:VZRHKAb53DLaG+nA9EaYYiaEx6YztwDlLElMsnSHD4k=
github.com/testcontainers/testcontainers-go v0.13.0 h1:OUujSlEGsXVo/ykPVZk3KanBNGN0TYb/7oKIPVn15JA= github.com/testcontainers/testcontainers-go v0.13.0 h1:OUujSlEGsXVo/ykPVZk3KanBNGN0TYb/7oKIPVn15JA=
github.com/testcontainers/testcontainers-go v0.13.0/go.mod h1:z1abufU633Eb/FmSBTzV6ntZAC1eZBYPtaFsn4nPuDk= github.com/testcontainers/testcontainers-go v0.13.0/go.mod h1:z1abufU633Eb/FmSBTzV6ntZAC1eZBYPtaFsn4nPuDk=
github.com/tetratelabs/wazero v1.0.0-pre.2 h1:sHYi8DKUL7s7c4sKz6lw0pNqky5EogYK0Iq4pSIsDog= github.com/tetratelabs/wazero v1.0.0-pre.3 h1:Z5fbogMUGcERzaQb9mQU8+yJSy0bVvv2ce3dfR4wcZg=
github.com/tetratelabs/wazero v1.0.0-pre.2/go.mod h1:M8UDNECGm/HVjOfq0EOe4QfCY9Les1eq54IChMLETbc= github.com/tetratelabs/wazero v1.0.0-pre.3/go.mod h1:M8UDNECGm/HVjOfq0EOe4QfCY9Les1eq54IChMLETbc=
github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M= github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=

View File

@@ -30,7 +30,7 @@ import (
) )
var ( var (
exportFunctions = map[string]interface{}{ logFunctions = map[string]api.GoModuleFunc{
"debug": logDebug, "debug": logDebug,
"info": logInfo, "info": logInfo,
"warn": logWarn, "warn": logWarn,
@@ -40,32 +40,52 @@ var (
RelativeDir = filepath.Join(".trivy", "modules") RelativeDir = filepath.Join(".trivy", "modules")
) )
func logDebug(ctx context.Context, m api.Module, offset, size uint32) { // logDebug is defined as an api.GoModuleFunc for lower overhead vs reflection.
buf := readMemory(ctx, m, offset, size) func logDebug(ctx context.Context, mod api.Module, params []uint64) (_ []uint64) {
offset, size := uint32(params[0]), uint32(params[1])
buf := readMemory(ctx, mod, offset, size)
if buf != nil { if buf != nil {
log.Logger.Debug(string(buf)) log.Logger.Debug(string(buf))
} }
return
} }
func logInfo(ctx context.Context, m api.Module, offset, size uint32) { // logInfo is defined as an api.GoModuleFunc for lower overhead vs reflection.
buf := readMemory(ctx, m, offset, size) func logInfo(ctx context.Context, mod api.Module, params []uint64) (_ []uint64) {
offset, size := uint32(params[0]), uint32(params[1])
buf := readMemory(ctx, mod, offset, size)
if buf != nil { if buf != nil {
log.Logger.Info(string(buf)) log.Logger.Info(string(buf))
} }
return
} }
func logWarn(ctx context.Context, m api.Module, offset, size uint32) { // logWarn is defined as an api.GoModuleFunc for lower overhead vs reflection.
buf := readMemory(ctx, m, offset, size) func logWarn(ctx context.Context, mod api.Module, params []uint64) (_ []uint64) {
offset, size := uint32(params[0]), uint32(params[1])
buf := readMemory(ctx, mod, offset, size)
if buf != nil { if buf != nil {
log.Logger.Warn(string(buf)) log.Logger.Warn(string(buf))
} }
return
} }
func logError(ctx context.Context, m api.Module, offset, size uint32) { // logError is defined as an api.GoModuleFunc for lower overhead vs reflection.
buf := readMemory(ctx, m, offset, size) func logError(ctx context.Context, mod api.Module, params []uint64) (_ []uint64) {
offset, size := uint32(params[0]), uint32(params[1])
buf := readMemory(ctx, mod, offset, size)
if buf != nil { if buf != nil {
log.Logger.Error(string(buf)) log.Logger.Error(string(buf))
} }
return
} }
func readMemory(ctx context.Context, m api.Module, offset, size uint32) []byte { func readMemory(ctx context.Context, m api.Module, offset, size uint32) []byte {
@@ -242,14 +262,21 @@ func newWASMPlugin(ctx context.Context, r wazero.Runtime, code []byte) (*wasmMod
ns := r.NewNamespace(ctx) ns := r.NewNamespace(ctx)
// Instantiate a Go-defined module named "env" that exports functions. // Instantiate a Go-defined module named "env" that exports functions.
_, err := r.NewHostModuleBuilder("env"). envBuilder := r.NewHostModuleBuilder("env")
ExportFunctions(exportFunctions).
Instantiate(ctx, ns) // Avoid reflection for logging as it implies an overhead of >1us per call.
if err != nil { for n, f := range logFunctions {
envBuilder.NewFunctionBuilder().
WithGoModuleFunction(f, []api.ValueType{api.ValueTypeI32, api.ValueTypeI32}, []api.ValueType{}).
WithParameterNames("offset", "size").
Export(n)
}
if _, err := envBuilder.Instantiate(ctx, ns); err != nil {
return nil, xerrors.Errorf("wasm module build error: %w", err) return nil, xerrors.Errorf("wasm module build error: %w", err)
} }
if _, err = wasi.NewBuilder(r).Instantiate(ctx, ns); err != nil { if _, err := wasi.NewBuilder(r).Instantiate(ctx, ns); err != nil {
return nil, xerrors.Errorf("WASI init error: %w", err) return nil, xerrors.Errorf("WASI init error: %w", err)
} }