Skip to content

Commit

Permalink
wip: testing wazero fork upgrade with uint64 size()
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Mar 6, 2024
1 parent f5e48a9 commit 390bd86
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ tmp
# node_modules used by polkadot.js/api tests
tests/polkadotjs_test/node_modules
!tests/polkadotjs_test/test/*.wasm

*.json
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,6 @@ require (

go 1.21

replace github.com/tetratelabs/wazero => github.com/ChainSafe/wazero v0.0.0-20231114190045-1d874d099362
replace github.com/tetratelabs/wazero => github.com/ChainSafe/wazero v0.0.0-20240306154750-130c2d1dc80b

replace github.com/centrifuge/go-substrate-rpc-client/v4 => github.com/timwu20/go-substrate-rpc-client/v4 v4.0.0-20231110032757-3d8e441b7303
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGy
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/ChainSafe/go-schnorrkel v1.1.0 h1:rZ6EU+CZFCjB4sHUE1jIu8VDoB/wRKZxoe1tkcO71Wk=
github.com/ChainSafe/go-schnorrkel v1.1.0/go.mod h1:ABkENxiP+cvjFiByMIZ9LYbRoNNLeBLiakC1XeTFxfE=
github.com/ChainSafe/wazero v0.0.0-20231114190045-1d874d099362 h1:hbvvSSB436JJalwq/2fRZwJpptvq9HMOLYVZX9oVHKM=
github.com/ChainSafe/wazero v0.0.0-20231114190045-1d874d099362/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A=
github.com/ChainSafe/wazero v0.0.0-20240306154750-130c2d1dc80b h1:EqM7+3eUCn0aKpB4Zr9527/D8xt6LAxHPZoaJMIr25I=
github.com/ChainSafe/wazero v0.0.0-20240306154750-130c2d1dc80b/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8=
github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
Expand Down
15 changes: 9 additions & 6 deletions lib/runtime/allocator/freeing_bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
MaxPossibleAllocations uint32 = 33554432

PageSize = 65536
MaxWasmPages = (4 * 1024 * 1024 * 1024 / PageSize) - 1
MaxWasmPages = 4 * 1024 * 1024 * 1024 / PageSize
)

var (
Expand Down Expand Up @@ -332,7 +332,7 @@ type FreeingBumpHeapAllocator struct {
bumper uint32
freeLists *FreeLists
poisoned bool
lastObservedMemorySize uint32
lastObservedMemorySize uint64
stats AllocationStats
}

Expand Down Expand Up @@ -508,11 +508,14 @@ func bump(bumper *uint32, size uint32, mem runtime.Memory) (uint32, error) {
if requiredSize > uint64(mem.Size()) {
requiredPages, ok := pagesFromSize(requiredSize)
if !ok {
return 0, fmt.Errorf("%w: required size %d dont fit uint32",
ErrAllocatorOutOfSpace, requiredSize)
panic(fmt.Sprintf("cannot calculate number of pages from size %d", requiredSize))
}

currentPages, ok := pagesFromSize(mem.Size())
if !ok {
panic(fmt.Sprintf("cannot calculate current number of pages, current size: %d", mem.Size()))
}

currentPages := mem.Size() / PageSize
if currentPages >= requiredPages {
panic(fmt.Sprintf("current pages %d >= required pages %d", currentPages, requiredPages))
}
Expand All @@ -539,7 +542,7 @@ func bump(bumper *uint32, size uint32, mem runtime.Memory) (uint32, error) {
ErrCannotGrowLinearMemory, currentPages, nextPages)
}

pagesIncrease := (mem.Size() / PageSize) == nextPages
pagesIncrease := (mem.Size() / PageSize) == uint64(nextPages)
if !pagesIncrease {
panic(fmt.Sprintf("number of pages should have increased! previous: %d, desired: %d", currentPages, nextPages))
}
Expand Down
6 changes: 3 additions & 3 deletions lib/runtime/allocator/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (m *MemoryInstance) pages() uint32 {
return pages
}

func (m *MemoryInstance) Size() uint32 {
return m.pages() * PageSize
func (m *MemoryInstance) Size() uint64 {
return uint64(m.pages() * PageSize)
}

func (m *MemoryInstance) Grow(pages uint32) (uint32, bool) {
Expand All @@ -54,7 +54,7 @@ func (m *MemoryInstance) WriteUint64Le(offset uint32, v uint64) bool {
copy(m.data[offset:offset+8], encoded)
return true
}
func (*MemoryInstance) Read(_, _ uint32) ([]byte, bool) {
func (*MemoryInstance) Read(_ uint32, _ uint64) ([]byte, bool) {
return nil, false
}

Expand Down
4 changes: 2 additions & 2 deletions lib/runtime/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Memory interface {
// has 1 page: 65536
//
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#-hrefsyntax-instr-memorymathsfmemorysize%E2%91%A0
Size() uint32
Size() uint64

// Grow increases memory by the delta in pages (65536 bytes per page).
// The return val is the previous memory size in pages, or false if the
Expand Down Expand Up @@ -66,7 +66,7 @@ type Memory interface {
// shared. Those who need a stable view must set Wasm memory min=max, or
// use wazero.RuntimeConfig WithMemoryCapacityPages to ensure max is always
// allocated.
Read(offset, byteCount uint32) ([]byte, bool)
Read(offset uint32, byteCount uint64) ([]byte, bool)

// WriteByte writes a single byte to the underlying buffer at the offset in or returns false if out of range.
WriteByte(offset uint32, v byte) bool //nolint:govet
Expand Down
6 changes: 3 additions & 3 deletions lib/runtime/wazero/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func newPointerSize(ptr, size uint32) (pointerSize uint64) {

// splitPointerSize converts a 64bit pointer size to an
// uint32 pointer and a uint32 size.
func splitPointerSize(pointerSize uint64) (ptr, size uint32) {
return uint32(pointerSize), uint32(pointerSize >> 32)
func splitPointerSize(pointerSize uint64) (ptr uint32, size uint64) {
return uint32(pointerSize), uint64(pointerSize >> 32)
}

// read will read from 64 bit pointer size and return a byte slice
Expand Down Expand Up @@ -2266,7 +2266,7 @@ func ext_storage_read_version_1(ctx context.Context, m api.Module, keySpan, valu

var written uint
valueOutPtr, valueOutSize := splitPointerSize(valueOut)
if uint32(len(data)) <= valueOutSize {
if uint64(len(data)) <= valueOutSize {
written = uint(len(data))
} else {
written = uint(valueOutSize)
Expand Down

0 comments on commit 390bd86

Please sign in to comment.