Skip to content

Commit

Permalink
Removes context parameter to MemorySize and updates runtimes. (#47)
Browse files Browse the repository at this point in the history
This removes the go context parameter from MemorySize. This was never used in practice due to the scope being very narrow, as well most memory affects happening inside native code.

This also updates runtime dependencies of wasmtime and wazero. Below are notes about wazero:

wazero [1.0.0-pre.6](https://github.com/tetratelabs/wazero/releases/tag/v1.0.0-pre.6):
* improves module compilation and initialization performance
* adds `writefs.NewDirFS` which lets you add new files.
  * `path_open` `O_CREAT` flags, `path_remove_directory` `path_rename` `path_unlink_file`
* adds `NewFilesystemLoggingListenerFactory` with dramatically improved logging.

Since the previous version was 1.0.0-pre.4, this also includes changes from [1.0.0-pre.5](https://github.com/tetratelabs/wazero/releases/tag/v1.0.0-pre.5).

Signed-off-by: Adrian Cole <[email protected]>

Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
codefromthecrypt authored Jan 1, 2023
1 parent 02ea3db commit d05063f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type (
// Instance is an instantiated Module
Instance interface {
// MemorySize is the size in bytes of the memory available to this Instance.
MemorySize(context.Context) uint32
MemorySize() uint32

// Invoke calls `operation` with `payload` on the module and returns a byte slice payload.
Invoke(ctx context.Context, operation string, payload []byte) ([]byte, error)
Expand Down
4 changes: 2 additions & 2 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func TestModule(t *testing.T) {
t.Run("Check MemorySize", func(t *testing.T) {
// Verify implementations didn't mistake size in bytes for page count.
expectedMemorySize := uint32(65536) // 1 page
if i.MemorySize(ctx) != expectedMemorySize {
t.Errorf("Unexpected memory size, got %d, expected %d", i.MemorySize(ctx), expectedMemorySize)
if i.MemorySize() != expectedMemorySize {
t.Errorf("Unexpected memory size, got %d, expected %d", i.MemorySize(), expectedMemorySize)
}
})

Expand Down
2 changes: 1 addition & 1 deletion engines/wasmer/wasmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ func (i *Instance) wasiRuntime() map[string]wasmer.IntoExtern {
}

// MemorySize returns the memory length of the underlying instance.
func (i *Instance) MemorySize(context.Context) uint32 {
func (i *Instance) MemorySize() uint32 {
return uint32(i.mem.DataSize())
}

Expand Down
2 changes: 1 addition & 1 deletion engines/wasmtime/wasmtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (i *Instance) wapcRuntime() map[string]*wasmtime.Func {
}

// MemorySize returns the memory length of the underlying instance.
func (i *Instance) MemorySize(context.Context) uint32 {
func (i *Instance) MemorySize() uint32 {
return uint32(i.mem.DataSize(i.m.store))
}

Expand Down
36 changes: 18 additions & 18 deletions engines/wazero/wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ func (w *wapcHost) hostCall(ctx context.Context, m api.Module, stack []uint64) {
}

mem := m.Memory()
binding := requireReadString(ctx, mem, "binding", bindPtr, bindLen)
namespace := requireReadString(ctx, mem, "namespace", nsPtr, nsLen)
operation := requireReadString(ctx, mem, "operation", cmdPtr, cmdLen)
payload := requireRead(ctx, mem, "payload", payloadPtr, payloadLen)
binding := requireReadString(mem, "binding", bindPtr, bindLen)
namespace := requireReadString(mem, "namespace", nsPtr, nsLen)
operation := requireReadString(mem, "operation", cmdPtr, cmdLen)
payload := requireRead(mem, "payload", payloadPtr, payloadLen)

if ic.hostResp, ic.hostErr = w.callHandler(ctx, binding, namespace, operation, payload); ic.hostErr != nil {
stack[0] = 0 // false: error (assumed to be logged already?)
Expand All @@ -241,12 +241,12 @@ func (w *wapcHost) hostCall(ctx context.Context, m api.Module, stack []uint64) {

// consoleLog is the WebAssembly function export "__console_log", which logs the message stored by the guest at the
// given offset (ptr) and length (len) in linear memory (wasm.Memory).
func (w *wapcHost) consoleLog(ctx context.Context, m api.Module, params []uint64) {
func (w *wapcHost) consoleLog(_ context.Context, m api.Module, params []uint64) {
ptr := uint32(params[0])
len := uint32(params[1])

if log := w.logger; log != nil {
msg := requireReadString(ctx, m.Memory(), "msg", ptr, len)
msg := requireReadString(m.Memory(), "msg", ptr, len)
w.logger(msg)
}
}
Expand All @@ -264,10 +264,10 @@ func (w *wapcHost) guestRequest(ctx context.Context, m api.Module, params []uint

mem := m.Memory()
if operation := ic.operation; operation != "" {
mem.Write(ctx, opPtr, []byte(operation))
mem.Write(opPtr, []byte(operation))
}
if guestReq := ic.guestReq; guestReq != nil {
mem.Write(ctx, ptr, guestReq)
mem.Write(ptr, guestReq)
}
}

Expand All @@ -279,7 +279,7 @@ func (w *wapcHost) hostResponse(ctx context.Context, m api.Module, params []uint
if ic := fromInvokeContext(ctx); ic == nil {
return // no invoke context
} else if hostResp := ic.hostResp; hostResp != nil {
m.Memory().Write(ctx, ptr, hostResp)
m.Memory().Write(ptr, hostResp)
}
}

Expand All @@ -306,7 +306,7 @@ func (w *wapcHost) guestResponse(ctx context.Context, m api.Module, params []uin
if ic := fromInvokeContext(ctx); ic == nil {
return // no invoke context
} else {
ic.guestResp = requireRead(ctx, m.Memory(), "guestResp", ptr, len)
ic.guestResp = requireRead(m.Memory(), "guestResp", ptr, len)
}
}

Expand All @@ -319,7 +319,7 @@ func (w *wapcHost) guestError(ctx context.Context, m api.Module, params []uint64
if ic := fromInvokeContext(ctx); ic == nil {
return // no invoke context
} else {
ic.guestErr = requireReadString(ctx, m.Memory(), "guestErr", ptr, len)
ic.guestErr = requireReadString(m.Memory(), "guestErr", ptr, len)
}
}

Expand All @@ -331,7 +331,7 @@ func (w *wapcHost) hostError(ctx context.Context, m api.Module, params []uint64)
if ic := fromInvokeContext(ctx); ic == nil {
return // no invoke context
} else if hostErr := ic.hostErr; hostErr != nil {
m.Memory().Write(ctx, ptr, []byte(hostErr.Error()))
m.Memory().Write(ptr, []byte(hostErr.Error()))
}
}

Expand Down Expand Up @@ -374,8 +374,8 @@ func (m *Module) Instantiate(ctx context.Context) (wapc.Instance, error) {
}

// MemorySize implements the same method as documented on wapc.Instance.
func (i *Instance) MemorySize(ctx context.Context) uint32 {
return i.m.Memory().Size(ctx)
func (i *Instance) MemorySize() uint32 {
return i.m.Memory().Size()
}

type invokeContextKey struct{}
Expand Down Expand Up @@ -445,13 +445,13 @@ func (m *Module) Close(ctx context.Context) (err error) {
}

// requireReadString is a convenience function that casts requireRead
func requireReadString(ctx context.Context, mem api.Memory, fieldName string, offset, byteCount uint32) string {
return string(requireRead(ctx, mem, fieldName, offset, byteCount))
func requireReadString(mem api.Memory, fieldName string, offset, byteCount uint32) string {
return string(requireRead(mem, fieldName, offset, byteCount))
}

// requireRead is like api.Memory except that it panics if the offset and byteCount are out of range.
func requireRead(ctx context.Context, mem api.Memory, fieldName string, offset, byteCount uint32) []byte {
buf, ok := mem.Read(ctx, offset, byteCount)
func requireRead(mem api.Memory, fieldName string, offset, byteCount uint32) []byte {
buf, ok := mem.Read(offset, byteCount)
if !ok {
panic(fmt.Errorf("out of memory reading %s", fieldName))
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ go 1.18
require (
github.com/Workiva/go-datastructures v1.0.53
github.com/bytecodealliance/wasmtime-go v1.0.0
github.com/tetratelabs/wazero v1.0.0-pre.4
github.com/tetratelabs/wazero v1.0.0-pre.6
github.com/wasmerio/wasmer-go v1.0.4
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
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.6 h1:3DRqjuHazHyZmgWCgqu7nKgYIYNEi2+2RQpCwTqbVHs=
github.com/tetratelabs/wazero v1.0.0-pre.6/go.mod h1:u8wrFmpdrykiFK0DFPiFm5a4+0RzsdmXYVtijBKqUVo=
github.com/tinylib/msgp v1.1.5/go.mod h1:eQsjooMTnV42mHu917E26IogZ2930nFyBQdofk10Udg=
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31/go.mod h1:onvgF043R+lC5RZ8IT9rBXDaEDnpnw/Cl+HFiw+v/7Q=
github.com/wasmerio/wasmer-go v1.0.4 h1:MnqHoOGfiQ8MMq2RF6wyCeebKOe84G88h5yv+vmxJgs=
Expand Down

0 comments on commit d05063f

Please sign in to comment.