From d05063f56daffb4ab386b3a4cce6a15294628b9d Mon Sep 17 00:00:00 2001 From: Crypt Keeper <64215+codefromthecrypt@users.noreply.github.com> Date: Sun, 1 Jan 2023 11:03:50 +0800 Subject: [PATCH] Removes context parameter to MemorySize and updates runtimes. (#47) 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 Signed-off-by: Adrian Cole --- engine.go | 2 +- engine_test.go | 4 ++-- engines/wasmer/wasmer.go | 2 +- engines/wasmtime/wasmtime.go | 2 +- engines/wazero/wazero.go | 36 ++++++++++++++++++------------------ go.mod | 2 +- go.sum | 4 ++-- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/engine.go b/engine.go index e26f8f7..94e7161 100644 --- a/engine.go +++ b/engine.go @@ -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) diff --git a/engine_test.go b/engine_test.go index 9d33eb4..7af5988 100644 --- a/engine_test.go +++ b/engine_test.go @@ -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) } }) diff --git a/engines/wasmer/wasmer.go b/engines/wasmer/wasmer.go index a67dc6e..885657d 100644 --- a/engines/wasmer/wasmer.go +++ b/engines/wasmer/wasmer.go @@ -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()) } diff --git a/engines/wasmtime/wasmtime.go b/engines/wasmtime/wasmtime.go index 0c9a334..62879cc 100644 --- a/engines/wasmtime/wasmtime.go +++ b/engines/wasmtime/wasmtime.go @@ -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)) } diff --git a/engines/wazero/wazero.go b/engines/wazero/wazero.go index 9e088d0..5fee22d 100644 --- a/engines/wazero/wazero.go +++ b/engines/wazero/wazero.go @@ -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?) @@ -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) } } @@ -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) } } @@ -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) } } @@ -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) } } @@ -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) } } @@ -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())) } } @@ -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{} @@ -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)) } diff --git a/go.mod b/go.mod index 5fe849b..aaaff32 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 955e72e..4ca7888 100644 --- a/go.sum +++ b/go.sum @@ -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=