From b815ec25ed3426fef6e3a0dea354d3c25cc15bcb Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Tue, 16 Aug 2022 22:42:43 +0000 Subject: [PATCH] Move `toPointerSize` and `splitPointerSize` to helpers.go --- lib/runtime/common.go | 14 --------- lib/runtime/wasmer/helpers.go | 16 ++++++++-- lib/runtime/wasmer/helpers_test.go | 49 +++++++++++++++++++++++++++++ lib/runtime/wasmer/imports.go | 9 ++---- lib/runtime/wasmer/imports_test.go | 2 +- lib/runtime/wasmer/instance.go | 2 +- lib/runtime/wasmer/instance_test.go | 9 ------ 7 files changed, 68 insertions(+), 33 deletions(-) delete mode 100644 lib/runtime/common.go create mode 100644 lib/runtime/wasmer/helpers_test.go diff --git a/lib/runtime/common.go b/lib/runtime/common.go deleted file mode 100644 index 41133b65f91..00000000000 --- a/lib/runtime/common.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2021 ChainSafe Systems (ON) -// SPDX-License-Identifier: LGPL-3.0-only - -package runtime - -// Int64ToPointerAndSize converts an int64 into a int32 pointer and a int32 length -func Int64ToPointerAndSize(in int64) (ptr, length int32) { - return int32(in), int32(in >> 32) -} - -// PointerAndSizeToInt64 converts int32 pointer and size to a int64 -func PointerAndSizeToInt64(ptr, size int32) int64 { - return int64(ptr) | (int64(size) << 32) -} diff --git a/lib/runtime/wasmer/helpers.go b/lib/runtime/wasmer/helpers.go index 89008b476d1..9b2d9e38fa3 100644 --- a/lib/runtime/wasmer/helpers.go +++ b/lib/runtime/wasmer/helpers.go @@ -16,10 +16,22 @@ import ( "github.com/wasmerio/go-ext-wasm/wasmer" ) +// toPointerSize converts an uint32 pointer and uint32 size +// to an int64 pointer size. +func toPointerSize(ptr, size uint32) (pointerSize int64) { + return int64(ptr) | (int64(size) << 32) +} + +// splitPointerSize converts an int64 pointer size to an +// uint32 pointer and an uint32 size. +func splitPointerSize(pointerSize int64) (ptr, size uint32) { + return uint32(pointerSize), uint32(pointerSize >> 32) +} + // asMemorySlice converts a 64 bit pointer size to a Go byte slice. func asMemorySlice(context wasmer.InstanceContext, pointerSize C.int64_t) (data []byte) { memory := context.Memory().Data() - ptr, size := runtime.Int64ToPointerAndSize(int64(pointerSize)) + ptr, size := splitPointerSize(int64(pointerSize)) return memory[ptr : ptr+size] } @@ -42,7 +54,7 @@ func toWasmMemory(context wasmer.InstanceContext, data []byte) ( } copy(memory[ptr:ptr+size], data) - pointerSize = runtime.PointerAndSizeToInt64(int32(ptr), int32(size)) + pointerSize = toPointerSize(ptr, size) return pointerSize, nil } diff --git a/lib/runtime/wasmer/helpers_test.go b/lib/runtime/wasmer/helpers_test.go new file mode 100644 index 00000000000..c6bda5f8113 --- /dev/null +++ b/lib/runtime/wasmer/helpers_test.go @@ -0,0 +1,49 @@ +// Copyright 2022 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + +package wasmer + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_pointerSize(t *testing.T) { + t.Parallel() + + testCases := map[string]struct { + ptr uint32 + size uint32 + pointerSize int64 + }{ + "0": {}, + "ptr 8 size 32": { + ptr: 8, + size: 32, + pointerSize: int64(8) | (int64(32) << 32), + }, + "ptr max uint32 and size max uint32": { + ptr: ^uint32(0), + size: ^uint32(0), + pointerSize: ^int64(0), + }, + } + + for name, testCase := range testCases { + testCase := testCase + t.Run(name, func(t *testing.T) { + t.Parallel() + + pointerSize := toPointerSize(testCase.ptr, testCase.size) + + require.Equal(t, testCase.pointerSize, pointerSize) + + ptr, size := splitPointerSize(pointerSize) + + assert.Equal(t, testCase.ptr, ptr) + assert.Equal(t, testCase.size, size) + }) + } +} diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 7bce8952049..8330964d78f 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -1007,7 +1007,7 @@ func ext_default_child_storage_read_version_1(context unsafe.Pointer, return 0 } - valueBuf, valueLen := runtime.Int64ToPointerAndSize(int64(valueOut)) + valueBuf, valueLen := splitPointerSize(int64(valueOut)) copy(memory[valueBuf:valueBuf+valueLen], value[offset:]) size := uint32(len(value[offset:])) @@ -1994,12 +1994,9 @@ func ext_storage_read_version_1(context unsafe.Pointer, keySpan, valueOut C.int6 } var size uint32 - - if int(offset) > len(value) { - size = uint32(0) - } else { + if uint32(offset) <= uint32(len(value)) { size = uint32(len(value[offset:])) - valueBuf, valueLen := runtime.Int64ToPointerAndSize(int64(valueOut)) + valueBuf, valueLen := splitPointerSize(int64(valueOut)) copy(memory[valueBuf:valueBuf+valueLen], value[offset:]) } diff --git a/lib/runtime/wasmer/imports_test.go b/lib/runtime/wasmer/imports_test.go index 705f2052e3d..b73b72ae2bf 100644 --- a/lib/runtime/wasmer/imports_test.go +++ b/lib/runtime/wasmer/imports_test.go @@ -41,7 +41,7 @@ func Test_ext_offchain_timestamp_version_1(t *testing.T) { res, err := runtimeFunc(0, 0) require.NoError(t, err) - outputPtr, outputLength := runtime.Int64ToPointerAndSize(res.ToI64()) + outputPtr, outputLength := splitPointerSize(res.ToI64()) memory := inst.vm.Memory.Data() data := memory[outputPtr : outputPtr+outputLength] var timestamp int64 diff --git a/lib/runtime/wasmer/instance.go b/lib/runtime/wasmer/instance.go index 60b779be2e7..bcb0aab66b3 100644 --- a/lib/runtime/wasmer/instance.go +++ b/lib/runtime/wasmer/instance.go @@ -297,7 +297,7 @@ func (in *Instance) Exec(function string, data []byte) (result []byte, err error return nil, fmt.Errorf("running runtime function: %w", err) } - outputPtr, outputLength := runtime.Int64ToPointerAndSize(wasmValue.ToI64()) + outputPtr, outputLength := splitPointerSize(wasmValue.ToI64()) memory = in.vm.Memory.Data() // call Data() again to get larger slice return memory[outputPtr : outputPtr+outputLength], nil } diff --git a/lib/runtime/wasmer/instance_test.go b/lib/runtime/wasmer/instance_test.go index b67baa0dacf..dd1e22686ba 100644 --- a/lib/runtime/wasmer/instance_test.go +++ b/lib/runtime/wasmer/instance_test.go @@ -27,15 +27,6 @@ func TestConcurrentRuntimeCalls(t *testing.T) { }() } -func TestPointerSize(t *testing.T) { - in := int64(8) + int64(32)<<32 - ptr, length := runtime.Int64ToPointerAndSize(in) - require.Equal(t, int32(8), ptr) - require.Equal(t, int32(32), length) - res := runtime.PointerAndSizeToInt64(ptr, length) - require.Equal(t, in, res) -} - func Test_GetRuntimeVersion(t *testing.T) { polkadotRuntimeFilepath, err := runtime.GetRuntime( context.Background(), runtime.POLKADOT_RUNTIME)