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

View File

@@ -30,7 +30,7 @@ import (
)
var (
exportFunctions = map[string]interface{}{
logFunctions = map[string]api.GoModuleFunc{
"debug": logDebug,
"info": logInfo,
"warn": logWarn,
@@ -40,32 +40,52 @@ var (
RelativeDir = filepath.Join(".trivy", "modules")
)
func logDebug(ctx context.Context, m api.Module, offset, size uint32) {
buf := readMemory(ctx, m, offset, size)
// logDebug is defined as an api.GoModuleFunc for lower overhead vs reflection.
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 {
log.Logger.Debug(string(buf))
}
return
}
func logInfo(ctx context.Context, m api.Module, offset, size uint32) {
buf := readMemory(ctx, m, offset, size)
// logInfo is defined as an api.GoModuleFunc for lower overhead vs reflection.
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 {
log.Logger.Info(string(buf))
}
return
}
func logWarn(ctx context.Context, m api.Module, offset, size uint32) {
buf := readMemory(ctx, m, offset, size)
// logWarn is defined as an api.GoModuleFunc for lower overhead vs reflection.
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 {
log.Logger.Warn(string(buf))
}
return
}
func logError(ctx context.Context, m api.Module, offset, size uint32) {
buf := readMemory(ctx, m, offset, size)
// logError is defined as an api.GoModuleFunc for lower overhead vs reflection.
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 {
log.Logger.Error(string(buf))
}
return
}
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)
// Instantiate a Go-defined module named "env" that exports functions.
_, err := r.NewHostModuleBuilder("env").
ExportFunctions(exportFunctions).
Instantiate(ctx, ns)
if err != nil {
envBuilder := r.NewHostModuleBuilder("env")
// Avoid reflection for logging as it implies an overhead of >1us per call.
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)
}
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)
}