Skip to content

Commit

Permalink
Move toPointerSize and splitPointerSize to helpers.go
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Aug 17, 2022
1 parent b8ea75c commit b815ec2
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 33 deletions.
14 changes: 0 additions & 14 deletions lib/runtime/common.go

This file was deleted.

16 changes: 14 additions & 2 deletions lib/runtime/wasmer/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand All @@ -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
}

Expand Down
49 changes: 49 additions & 0 deletions lib/runtime/wasmer/helpers_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
9 changes: 3 additions & 6 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]))
Expand Down Expand Up @@ -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:])
}

Expand Down
2 changes: 1 addition & 1 deletion lib/runtime/wasmer/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime/wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 0 additions & 9 deletions lib/runtime/wasmer/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b815ec2

Please sign in to comment.