Skip to content

Commit

Permalink
chore(go): updates wazero to v1.0.0-pre.7 (#3355)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Cole <adrian@tetrate.io>
  • Loading branch information
codefromthecrypt authored Jan 3, 2023
1 parent 9c645b9 commit 5b944d2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ require (
github.com/spf13/viper v1.14.0
github.com/stretchr/testify v1.8.1
github.com/testcontainers/testcontainers-go v0.15.0
github.com/tetratelabs/wazero v1.0.0-pre.4
github.com/tetratelabs/wazero v1.0.0-pre.7
github.com/twitchtv/twirp v8.1.2+incompatible
github.com/xlab/treeprint v1.1.0
go.etcd.io/bbolt v1.3.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1502,8 +1502,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/testcontainers/testcontainers-go v0.15.0 h1:3Ex7PUGFv0b2bBsdOv6R42+SK2qoZnWBd21LvZYhUtQ=
github.com/testcontainers/testcontainers-go v0.15.0/go.mod h1:PkohMRH2X8Hib0IWtifVexDfLPVT+tb5E9hsf7cW12w=
github.com/tetratelabs/wazero v1.0.0-pre.4 h1:RBJQT5OzmORkSp6MmZDWoFEr0zXjk4pmvMKAdeUnsaI=
github.com/tetratelabs/wazero v1.0.0-pre.4/go.mod h1:u8wrFmpdrykiFK0DFPiFm5a4+0RzsdmXYVtijBKqUVo=
github.com/tetratelabs/wazero v1.0.0-pre.7 h1:WI5N14XxoXw+ZWhcjSazJ6rEowhJbH/x8hglxC5gN7k=
github.com/tetratelabs/wazero v1.0.0-pre.7/go.mod h1:u8wrFmpdrykiFK0DFPiFm5a4+0RzsdmXYVtijBKqUVo=
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.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
Expand Down
3 changes: 3 additions & 0 deletions pkg/module/memfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type memFS struct {

// Open implements fs.FS.
func (m *memFS) Open(name string) (fs.File, error) {
if m.current == nil {
return nil, fs.ErrNotExist
}
return m.current.Open(name)
}

Expand Down
46 changes: 23 additions & 23 deletions pkg/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ var (
)

// logDebug is defined as an api.GoModuleFunc for lower overhead vs reflection.
func logDebug(ctx context.Context, mod api.Module, params []uint64) {
func logDebug(_ context.Context, mod api.Module, params []uint64) {
offset, size := uint32(params[0]), uint32(params[1])

buf := readMemory(ctx, mod, offset, size)
buf := readMemory(mod.Memory(), offset, size)
if buf != nil {
log.Logger.Debug(string(buf))
}
Expand All @@ -50,10 +50,10 @@ func logDebug(ctx context.Context, mod api.Module, params []uint64) {
}

// logInfo is defined as an api.GoModuleFunc for lower overhead vs reflection.
func logInfo(ctx context.Context, mod api.Module, params []uint64) {
func logInfo(_ context.Context, mod api.Module, params []uint64) {
offset, size := uint32(params[0]), uint32(params[1])

buf := readMemory(ctx, mod, offset, size)
buf := readMemory(mod.Memory(), offset, size)
if buf != nil {
log.Logger.Info(string(buf))
}
Expand All @@ -62,10 +62,10 @@ func logInfo(ctx context.Context, mod api.Module, params []uint64) {
}

// logWarn is defined as an api.GoModuleFunc for lower overhead vs reflection.
func logWarn(ctx context.Context, mod api.Module, params []uint64) {
func logWarn(_ context.Context, mod api.Module, params []uint64) {
offset, size := uint32(params[0]), uint32(params[1])

buf := readMemory(ctx, mod, offset, size)
buf := readMemory(mod.Memory(), offset, size)
if buf != nil {
log.Logger.Warn(string(buf))
}
Expand All @@ -74,19 +74,19 @@ func logWarn(ctx context.Context, mod api.Module, params []uint64) {
}

// logError is defined as an api.GoModuleFunc for lower overhead vs reflection.
func logError(ctx context.Context, mod api.Module, params []uint64) {
func logError(_ context.Context, mod api.Module, params []uint64) {
offset, size := uint32(params[0]), uint32(params[1])

buf := readMemory(ctx, mod, offset, size)
buf := readMemory(mod.Memory(), offset, size)
if buf != nil {
log.Logger.Error(string(buf))
}

return
}

func readMemory(ctx context.Context, m api.Module, offset, size uint32) []byte {
buf, ok := m.Memory().Read(ctx, offset, size)
func readMemory(mem api.Memory, offset, size uint32) []byte {
buf, ok := mem.Read(offset, size)
if !ok {
log.Logger.Errorf("Memory.Read(%d, %d) out of range", offset, size)
return nil
Expand Down Expand Up @@ -171,9 +171,9 @@ func splitPtrSize(u uint64) (uint32, uint32) {
return ptr, size
}

func ptrSizeToString(ctx context.Context, m api.Module, ptrSize uint64) (string, error) {
func ptrSizeToString(mem api.Memory, ptrSize uint64) (string, error) {
ptr, size := splitPtrSize(ptrSize)
buf := readMemory(ctx, m, ptr, size)
buf := readMemory(mem, ptr, size)
if buf == nil {
return "", xerrors.New("unable to read memory")
}
Expand All @@ -190,17 +190,17 @@ func stringToPtrSize(ctx context.Context, s string, mod api.Module, malloc api.F

// The pointer is a linear memory offset, which is where we write the string.
ptr := results[0]
if !mod.Memory().Write(ctx, uint32(ptr), []byte(s)) {
if !mod.Memory().Write(uint32(ptr), []byte(s)) {
return 0, 0, xerrors.Errorf("Memory.Write(%d, %d) out of range of memory size %d",
ptr, size, mod.Memory().Size(ctx))
ptr, size, mod.Memory().Size())
}

return ptr, size, nil
}

func unmarshal(ctx context.Context, m api.Module, ptrSize uint64, v any) error {
func unmarshal(mem api.Memory, ptrSize uint64, v any) error {
ptr, size := splitPtrSize(ptrSize)
buf := readMemory(ctx, m, ptr, size)
buf := readMemory(mem, ptr, size)
if buf == nil {
return xerrors.New("unable to read memory")
}
Expand All @@ -225,9 +225,9 @@ func marshal(ctx context.Context, m api.Module, malloc api.Function, v easyjson.

// The pointer is a linear memory offset, which is where we write the marshaled value.
ptr := results[0]
if !m.Memory().Write(ctx, uint32(ptr), b) {
if !m.Memory().Write(uint32(ptr), b) {
return 0, 0, xerrors.Errorf("Memory.Write(%d, %d) out of range of memory size %d",
ptr, size, m.Memory().Size(ctx))
ptr, size, m.Memory().Size())
}

return ptr, size, nil
Expand Down Expand Up @@ -440,7 +440,7 @@ func (m *wasmModule) Analyze(ctx context.Context, input analyzer.AnalysisInput)
}

var result analyzer.AnalysisResult
if err = unmarshal(ctx, m.mod, analyzeRes[0], &result); err != nil {
if err = unmarshal(m.mod.Memory(), analyzeRes[0], &result); err != nil {
return nil, xerrors.Errorf("invalid return value: %w", err)
}

Expand Down Expand Up @@ -481,7 +481,7 @@ func (m *wasmModule) PostScan(ctx context.Context, results types.Results) (types
}

var got types.Results
if err = unmarshal(ctx, m.mod, analyzeRes[0], &got); err != nil {
if err = unmarshal(m.mod.Memory(), analyzeRes[0], &got); err != nil {
return nil, xerrors.Errorf("post scan unmarshal error: %w", err)
}

Expand Down Expand Up @@ -604,7 +604,7 @@ func moduleName(ctx context.Context, mod api.Module) (string, error) {
return "", xerrors.New("invalid signature: name()")
}

name, err := ptrSizeToString(ctx, mod, nameRes[0])
name, err := ptrSizeToString(mod.Memory(), nameRes[0])
if err != nil {
return "", xerrors.Errorf("invalid return value: %w", err)
}
Expand Down Expand Up @@ -657,7 +657,7 @@ func moduleRequiredFiles(ctx context.Context, mod api.Module) ([]*regexp.Regexp,
}

var fileRegexps serialize.StringSlice
if err = unmarshal(ctx, mod, requiredFilesRes[0], &fileRegexps); err != nil {
if err = unmarshal(mod.Memory(), requiredFilesRes[0], &fileRegexps); err != nil {
return nil, xerrors.Errorf("invalid return value: %w", err)
}

Expand Down Expand Up @@ -715,7 +715,7 @@ func modulePostScanSpec(ctx context.Context, mod api.Module) (serialize.PostScan
}

var spec serialize.PostScanSpec
if err = unmarshal(ctx, mod, postScanSpecRes[0], &spec); err != nil {
if err = unmarshal(mod.Memory(), postScanSpecRes[0], &spec); err != nil {
return serialize.PostScanSpec{}, xerrors.Errorf("invalid return value: %w", err)
}

Expand Down

0 comments on commit 5b944d2

Please sign in to comment.