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

engine, interp: connect ExternalReferences to Interpreters #10

Merged
merged 4 commits into from
Mar 1, 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
15 changes: 15 additions & 0 deletions convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@ func IntToString(i int) string {

return string(result)
}

// WasmPtrToBytes returns a slice of bytes from WebAssembly compatible numeric types
// representing its pointer and length.
func WasmPtrToBytes(ptr uint32, size uint32) []byte {
return unsafe.Slice((*byte)(unsafe.Pointer(uintptr(ptr))), size)
}

// BytesToWasmPtr returns a pointer and size pair for the given string in a way
// compatible with WebAssembly numeric types.
// The returned pointer aliases the string hence the string must be kept alive
// until ptr is no longer needed.
func BytesToWasmPtr(b []byte) (uint32, uint32) {
ptr := uintptr(unsafe.Pointer(unsafe.SliceData(b)))
return uint32(ptr), uint32(len(b))
}
8 changes: 5 additions & 3 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package engine

import (
"testing"
)
import "testing"

func TestEngine(t *testing.T) {
t.Run("cannot init without interpreter", func(t *testing.T) {
Expand Down Expand Up @@ -53,3 +51,7 @@ func (i *mockInterpreter) DefineFunc(modulename, funcname string, f interface{})
func (i *mockInterpreter) MemoryData(ptr, sz uint32) ([]byte, error) {
return nil, nil
}

func (i *mockInterpreter) References() *ExternalReferences {
return nil
}
2 changes: 2 additions & 0 deletions engine/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ type Interpreter interface {
DefineFunc(module, name string, f interface{}) error
// MemoryData returns a slice of memory data from the memory managed by the host.
MemoryData(ptr, sz uint32) ([]byte, error)
// References are the external references managed by the host module.
References() *ExternalReferences
}
4 changes: 2 additions & 2 deletions engine/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type ExternalReferences struct {
}

// NewReferences creates a new ExternalReferences store.
func NewReferences() *ExternalReferences {
return &ExternalReferences{
func NewReferences() ExternalReferences {
return ExternalReferences{
refs: make(map[int32]uintptr),
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.0
replace github.com/tetratelabs/wazero => github.com/orsinium-forks/wazero v0.0.0-20240217173836-b12c024bcbe4

require (
github.com/hybridgroup/wasman v0.0.0-20240228124029-ecd9fd3f900a
github.com/hybridgroup/wasman v0.0.0-20240229144219-f3288962ab34
github.com/tetratelabs/wazero v1.6.0
github.com/urfave/cli/v2 v2.27.1
tinygo.org/x/tinyfs v0.3.1-0.20231212053859-32ae3f6bbad9
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHH
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/hybridgroup/wasman v0.0.0-20240228124029-ecd9fd3f900a h1:6q1Gn1MR643sBn4PnvgX40jCCXmAyjuNXFfk8wirpds=
github.com/hybridgroup/wasman v0.0.0-20240228124029-ecd9fd3f900a/go.mod h1:rLavUo4P0xVcDeDnViYEpPQPoACmp1py9UTLPY/R7Lg=
github.com/hybridgroup/wasman v0.0.0-20240229144219-f3288962ab34 h1:XGX7Qt+ylSTF7VrwfXePwXAy2cK2iB6At+Ni/PcaNok=
github.com/hybridgroup/wasman v0.0.0-20240229144219-f3288962ab34/go.mod h1:rLavUo4P0xVcDeDnViYEpPQPoACmp1py9UTLPY/R7Lg=
github.com/orsinium-forks/wazero v0.0.0-20240217173836-b12c024bcbe4 h1:MUh9e2izck9aROiwDsDm24UU7kHieYM2911U1t+NASs=
github.com/orsinium-forks/wazero v0.0.0-20240217173836-b12c024bcbe4/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
Expand Down
59 changes: 59 additions & 0 deletions interp/tester/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tester

import (
"testing"
"unsafe"

"github.com/hybridgroup/mechanoid/engine"
)
Expand All @@ -11,6 +12,14 @@ func InitTest(t *testing.T, i engine.Interpreter) {
if err != nil {
t.Errorf("Interpreter.Init() failed: %v", err)
}

if i.Name() == "" {
t.Errorf("Interpreter.Name() failed: %v", i.Name())
}

if i.References() == nil {
t.Errorf("Interpreter.References() failed: %v", i.References())
}
}

func LoadTest(t *testing.T, i engine.Interpreter) {
Expand Down Expand Up @@ -51,3 +60,53 @@ func HaltTest(t *testing.T, i engine.Interpreter) {
t.Errorf("Interpreter.Halt() failed: %v", err)
}
}

func ReferencesTest(t *testing.T, i engine.Interpreter) {
err := i.Init()
if err != nil {
t.Errorf("Interpreter.Init() failed: %v", err)
}
if i.References() == nil {
t.Errorf("Interpreter.References() failed: %v", i.References())
}

var id1, id2 int32
thing1 := &testingType{
val1: "hello",
val2: "world",
}
thing2 := &testingType{
val1: "hola",
val2: "mundo",
}

t.Run("add references", func(t *testing.T) {
id1 = i.References().Add(unsafe.Pointer(&thing1))
id2 = i.References().Add(unsafe.Pointer(&thing2))

if id1 == id2 {
t.Errorf("id1 and id2 should not be the same")
}
})

t.Run("get references", func(t *testing.T) {
if i.References().Get(id1) != uintptr(unsafe.Pointer(&thing1)) {
t.Errorf("refs.Get(id1) failed")
}
if i.References().Get(id2) != uintptr(unsafe.Pointer(&thing2)) {
t.Errorf("refs.Get(id2) failed")
}
})

t.Run("remove references", func(t *testing.T) {
i.References().Remove(id1)
i.References().Remove(id2)

if i.References().Get(id1) != uintptr(0) {
t.Errorf("refs.Get(id1) failed")
}
if i.References().Get(id2) != uintptr(0) {
t.Errorf("refs.Get(id2) failed")
}
})
}
11 changes: 11 additions & 0 deletions interp/wasman/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Interpreter struct {
module *wasmaneng.Module
instance *wasmaneng.Instance
Memory []byte

references engine.ExternalReferences
}

func (i *Interpreter) Name() string {
Expand All @@ -34,6 +36,8 @@ func (i *Interpreter) Init() error {
}
}

i.references = engine.NewReferences()

return nil
}

Expand Down Expand Up @@ -100,6 +104,8 @@ func (i *Interpreter) DefineFunc(moduleName, funcName string, f any) error {
return wasmaneng.DefineFunc20(i.linker, moduleName, funcName, tf)
case func(uint32, uint32) uint32:
return wasmaneng.DefineFunc21(i.linker, moduleName, funcName, tf)
case func(uint32, uint32, uint32) uint32:
return wasmaneng.DefineFunc31(i.linker, moduleName, funcName, tf)
default:
return engine.ErrInvalidFuncType
}
Expand All @@ -115,3 +121,8 @@ func (i *Interpreter) MemoryData(ptr, sz uint32) ([]byte, error) {

return i.instance.Memory.Value[ptr : ptr+sz], nil
}

// References are the external references managed by the host module.
func (i *Interpreter) References() *engine.ExternalReferences {
return &i.references
}
4 changes: 4 additions & 0 deletions interp/wasman/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func TestHalt(t *testing.T) {
tester.HaltTest(t, &Interpreter{})
}

func TestExternalReferences(t *testing.T) {
tester.ReferencesTest(t, &Interpreter{})
}

func TestDefineFunc(t *testing.T) {
t.Skip("TODO: implement TestDefineFunc")
}
8 changes: 8 additions & 0 deletions interp/wazero/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Interpreter struct {
runtime wazero.Runtime
defs map[string]map[string]any
module api.Module

references engine.ExternalReferences
}

func (i *Interpreter) Name() string {
Expand All @@ -26,6 +28,7 @@ func (i *Interpreter) Init() error {
conf = conf.WithDebugInfoEnabled(false)
conf = conf.WithMemoryLimitPages(1)
i.runtime = wazero.NewRuntimeWithConfig(ctx, conf)
i.references = engine.NewReferences()
return nil
}

Expand Down Expand Up @@ -95,6 +98,11 @@ func (i *Interpreter) MemoryData(ptr, sz uint32) ([]byte, error) {
return data, nil
}

// References are the external references managed by the host module.
func (i *Interpreter) References() *engine.ExternalReferences {
return &i.references
}

// A fake RandSource for having fewer memory allocations.
//
// Should not be used with modules that do need an access to random functions.
Expand Down
4 changes: 4 additions & 0 deletions interp/wazero/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func TestHalt(t *testing.T) {
tester.HaltTest(t, &Interpreter{})
}

func TestExternalReferences(t *testing.T) {
tester.ReferencesTest(t, &Interpreter{})
}

func TestDefineFunc(t *testing.T) {
t.Skip("TODO: implement TestDefineFunc")
}