Skip to content

Commit 8fcca9c

Browse files
chore(go): updates wazero to 1.0.0-pre.3 (#3090)
Signed-off-by: Adrian Cole <[email protected]>
1 parent 02f77bc commit 8fcca9c

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ require (
6363
github.com/spf13/viper v1.13.0
6464
github.com/stretchr/testify v1.8.0
6565
github.com/testcontainers/testcontainers-go v0.13.0
66-
github.com/tetratelabs/wazero v1.0.0-pre.2
66+
github.com/tetratelabs/wazero v1.0.0-pre.3
6767
github.com/twitchtv/twirp v8.1.2+incompatible
6868
github.com/xlab/treeprint v1.1.0
6969
go.etcd.io/bbolt v1.3.6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,8 +1521,8 @@ github.com/tchap/go-patricia/v2 v2.3.1 h1:6rQp39lgIYZ+MHmdEq4xzuk1t7OdC35z/xm0BG
15211521
github.com/tchap/go-patricia/v2 v2.3.1/go.mod h1:VZRHKAb53DLaG+nA9EaYYiaEx6YztwDlLElMsnSHD4k=
15221522
github.com/testcontainers/testcontainers-go v0.13.0 h1:OUujSlEGsXVo/ykPVZk3KanBNGN0TYb/7oKIPVn15JA=
15231523
github.com/testcontainers/testcontainers-go v0.13.0/go.mod h1:z1abufU633Eb/FmSBTzV6ntZAC1eZBYPtaFsn4nPuDk=
1524-
github.com/tetratelabs/wazero v1.0.0-pre.2 h1:sHYi8DKUL7s7c4sKz6lw0pNqky5EogYK0Iq4pSIsDog=
1525-
github.com/tetratelabs/wazero v1.0.0-pre.2/go.mod h1:M8UDNECGm/HVjOfq0EOe4QfCY9Les1eq54IChMLETbc=
1524+
github.com/tetratelabs/wazero v1.0.0-pre.3 h1:Z5fbogMUGcERzaQb9mQU8+yJSy0bVvv2ce3dfR4wcZg=
1525+
github.com/tetratelabs/wazero v1.0.0-pre.3/go.mod h1:M8UDNECGm/HVjOfq0EOe4QfCY9Les1eq54IChMLETbc=
15261526
github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M=
15271527
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
15281528
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=

pkg/module/module.go

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
)
3131

3232
var (
33-
exportFunctions = map[string]interface{}{
33+
logFunctions = map[string]api.GoModuleFunc{
3434
"debug": logDebug,
3535
"info": logInfo,
3636
"warn": logWarn,
@@ -40,32 +40,52 @@ var (
4040
RelativeDir = filepath.Join(".trivy", "modules")
4141
)
4242

43-
func logDebug(ctx context.Context, m api.Module, offset, size uint32) {
44-
buf := readMemory(ctx, m, offset, size)
43+
// logDebug is defined as an api.GoModuleFunc for lower overhead vs reflection.
44+
func logDebug(ctx context.Context, mod api.Module, params []uint64) (_ []uint64) {
45+
offset, size := uint32(params[0]), uint32(params[1])
46+
47+
buf := readMemory(ctx, mod, offset, size)
4548
if buf != nil {
4649
log.Logger.Debug(string(buf))
4750
}
51+
52+
return
4853
}
4954

50-
func logInfo(ctx context.Context, m api.Module, offset, size uint32) {
51-
buf := readMemory(ctx, m, offset, size)
55+
// logInfo is defined as an api.GoModuleFunc for lower overhead vs reflection.
56+
func logInfo(ctx context.Context, mod api.Module, params []uint64) (_ []uint64) {
57+
offset, size := uint32(params[0]), uint32(params[1])
58+
59+
buf := readMemory(ctx, mod, offset, size)
5260
if buf != nil {
5361
log.Logger.Info(string(buf))
5462
}
63+
64+
return
5565
}
5666

57-
func logWarn(ctx context.Context, m api.Module, offset, size uint32) {
58-
buf := readMemory(ctx, m, offset, size)
67+
// logWarn is defined as an api.GoModuleFunc for lower overhead vs reflection.
68+
func logWarn(ctx context.Context, mod api.Module, params []uint64) (_ []uint64) {
69+
offset, size := uint32(params[0]), uint32(params[1])
70+
71+
buf := readMemory(ctx, mod, offset, size)
5972
if buf != nil {
6073
log.Logger.Warn(string(buf))
6174
}
75+
76+
return
6277
}
6378

64-
func logError(ctx context.Context, m api.Module, offset, size uint32) {
65-
buf := readMemory(ctx, m, offset, size)
79+
// logError is defined as an api.GoModuleFunc for lower overhead vs reflection.
80+
func logError(ctx context.Context, mod api.Module, params []uint64) (_ []uint64) {
81+
offset, size := uint32(params[0]), uint32(params[1])
82+
83+
buf := readMemory(ctx, mod, offset, size)
6684
if buf != nil {
6785
log.Logger.Error(string(buf))
6886
}
87+
88+
return
6989
}
7090

7191
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
242262
ns := r.NewNamespace(ctx)
243263

244264
// Instantiate a Go-defined module named "env" that exports functions.
245-
_, err := r.NewHostModuleBuilder("env").
246-
ExportFunctions(exportFunctions).
247-
Instantiate(ctx, ns)
248-
if err != nil {
265+
envBuilder := r.NewHostModuleBuilder("env")
266+
267+
// Avoid reflection for logging as it implies an overhead of >1us per call.
268+
for n, f := range logFunctions {
269+
envBuilder.NewFunctionBuilder().
270+
WithGoModuleFunction(f, []api.ValueType{api.ValueTypeI32, api.ValueTypeI32}, []api.ValueType{}).
271+
WithParameterNames("offset", "size").
272+
Export(n)
273+
}
274+
275+
if _, err := envBuilder.Instantiate(ctx, ns); err != nil {
249276
return nil, xerrors.Errorf("wasm module build error: %w", err)
250277
}
251278

252-
if _, err = wasi.NewBuilder(r).Instantiate(ctx, ns); err != nil {
279+
if _, err := wasi.NewBuilder(r).Instantiate(ctx, ns); err != nil {
253280
return nil, xerrors.Errorf("WASI init error: %w", err)
254281
}
255282

0 commit comments

Comments
 (0)