@@ -30,7 +30,7 @@ import (
30
30
)
31
31
32
32
var (
33
- exportFunctions = map [string ]interface {} {
33
+ logFunctions = map [string ]api. GoModuleFunc {
34
34
"debug" : logDebug ,
35
35
"info" : logInfo ,
36
36
"warn" : logWarn ,
@@ -40,32 +40,52 @@ var (
40
40
RelativeDir = filepath .Join (".trivy" , "modules" )
41
41
)
42
42
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 )
45
48
if buf != nil {
46
49
log .Logger .Debug (string (buf ))
47
50
}
51
+
52
+ return
48
53
}
49
54
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 )
52
60
if buf != nil {
53
61
log .Logger .Info (string (buf ))
54
62
}
63
+
64
+ return
55
65
}
56
66
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 )
59
72
if buf != nil {
60
73
log .Logger .Warn (string (buf ))
61
74
}
75
+
76
+ return
62
77
}
63
78
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 )
66
84
if buf != nil {
67
85
log .Logger .Error (string (buf ))
68
86
}
87
+
88
+ return
69
89
}
70
90
71
91
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
242
262
ns := r .NewNamespace (ctx )
243
263
244
264
// 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 {
249
276
return nil , xerrors .Errorf ("wasm module build error: %w" , err )
250
277
}
251
278
252
- if _ , err = wasi .NewBuilder (r ).Instantiate (ctx , ns ); err != nil {
279
+ if _ , err : = wasi .NewBuilder (r ).Instantiate (ctx , ns ); err != nil {
253
280
return nil , xerrors .Errorf ("WASI init error: %w" , err )
254
281
}
255
282
0 commit comments