Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor reflect.SliceHeader uses to allow tinygo cross-compilation #2161

Merged
merged 6 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/engine/wazevo/backend/isa/amd64/reflect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build !tinygo

package amd64

import "reflect"

// setSliceLimits sets both Cap and Len for the given reflected slice.
func setSliceLimits(s *reflect.SliceHeader, limit uintptr) {
s.Len = int(limit)
s.Cap = int(limit)
}
11 changes: 11 additions & 0 deletions internal/engine/wazevo/backend/isa/amd64/reflect_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build tinygo

package amd64

import "reflect"

// setSliceLimits sets both Cap and Len for the given reflected slice.
func setSliceLimits(s *reflect.SliceHeader, limit uintptr) {
s.Len = limit
s.Len = limit
}
14 changes: 3 additions & 11 deletions internal/engine/wazevo/backend/isa/amd64/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import (
)

func stackView(rbp, top uintptr) []byte {
l := int(top - rbp)
var stackBuf []byte
{
// TODO: use unsafe.Slice after floor version is set to Go 1.20.
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&stackBuf))
hdr.Data = rbp
hdr.Len = l
hdr.Cap = l
setSliceLimits(hdr, top-rbp)
}
return stackBuf
}
Expand Down Expand Up @@ -74,15 +72,9 @@ func GoCallStackView(stackPointerBeforeGoCall *uint64) []uint64 {
// | SizeInBytes |
// +-----------------+ <---- stackPointerBeforeGoCall
// (low address)
data := unsafe.Pointer(uintptr(unsafe.Pointer(stackPointerBeforeGoCall)) + 8)
size := *stackPointerBeforeGoCall / 8
var view []uint64
{
sh := (*reflect.SliceHeader)(unsafe.Pointer(&view))
sh.Data = uintptr(unsafe.Pointer(stackPointerBeforeGoCall)) + 8 // skips the(sliceSize.
sh.Len = int(size)
sh.Cap = int(size)
}
return view
return unsafe.Slice((*uint64)(data), int(size))
}

func AdjustClonedStack(oldRsp, oldTop, rsp, rbp, top uintptr) {
Expand Down
9 changes: 3 additions & 6 deletions internal/engine/wazevo/call_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,7 @@ func opaqueViewFromPtr(ptr uintptr) []byte {
var opaque []byte
sh := (*reflect.SliceHeader)(unsafe.Pointer(&opaque))
sh.Data = ptr
sh.Len = 24
sh.Cap = 24
setSliceLimits(sh, 24, 24)
return opaque
}

Expand Down Expand Up @@ -574,17 +573,15 @@ func (c *callEngine) cloneStack(l uintptr) (newSP, newFP, newTop uintptr, newSta
{
sh := (*reflect.SliceHeader)(unsafe.Pointer(&prevStackAligned))
sh.Data = c.stackTop - relSp
sh.Len = int(relSp)
sh.Cap = int(relSp)
setSliceLimits(sh, relSp, relSp)
}
newTop = alignedStackTop(newStack)
{
newSP = newTop - relSp
newFP = newTop - relFp
sh := (*reflect.SliceHeader)(unsafe.Pointer(&newStackAligned))
sh.Data = newSP
sh.Len = int(relSp)
sh.Cap = int(relSp)
setSliceLimits(sh, relSp, relSp)
}
copy(newStackAligned, prevStackAligned)
return
Expand Down
3 changes: 1 addition & 2 deletions internal/engine/wazevo/hostmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func hostModuleListenersSliceFromOpaque(opaqueBegin uintptr) []experimental.Func
var ret []experimental.FunctionListener
sh = (*reflect.SliceHeader)(unsafe.Pointer(&ret))
sh.Data = uintptr(b)
sh.Len = int(l)
sh.Cap = int(c)
setSliceLimits(sh, uintptr(l), uintptr(c))
return ret
}

Expand Down
11 changes: 11 additions & 0 deletions internal/engine/wazevo/reflect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build !tinygo

package wazevo

import "reflect"

// setSliceLimits sets both Cap and Len for the given reflected slice.
func setSliceLimits(s *reflect.SliceHeader, l, c uintptr) {
s.Len = int(l)
s.Cap = int(c)
}
11 changes: 11 additions & 0 deletions internal/engine/wazevo/reflect_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build tinygo

package wazevo

import "reflect"

// setSliceLimits sets both Cap and Len for the given reflected slice.
func setSliceLimits(s *reflect.SliceHeader, l, c uintptr) {
s.Len = l
s.Cap = c
}
4 changes: 2 additions & 2 deletions internal/wasm/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,12 @@ func (m *MemoryInstance) Grow(delta uint32) (result uint32, ok bool) {
m.Cap = newPages
return currentPages, true
} else { // We already have the capacity we need.
sp := (*reflect.SliceHeader)(unsafe.Pointer(&m.Buffer))
if m.Shared {
sp := (*reflect.SliceHeader)(unsafe.Pointer(&m.Buffer))
// Use atomic write to ensure new length is visible across threads.
atomic.StoreUintptr((*uintptr)(unsafe.Pointer(&sp.Len)), uintptr(MemoryPagesToBytesNum(newPages)))
} else {
sp.Len = int(MemoryPagesToBytesNum(newPages))
m.Buffer = m.Buffer[:MemoryPagesToBytesNum(newPages)]
}
return currentPages, true
}
Expand Down
Loading