From ca43f43691f448c7dff3e9cab4c1876483ffd803 Mon Sep 17 00:00:00 2001 From: gram Date: Fri, 8 Mar 2024 11:30:31 +0100 Subject: [PATCH 1/3] Use wypes for managing refs --- engine/interp.go | 2 -- engine/references.go | 58 ------------------------------------ engine/references_test.go | 53 -------------------------------- go.mod | 2 +- go.sum | 4 +-- interp/tester/interp.go | 55 ---------------------------------- interp/wasman/interp.go | 26 +++------------- interp/wasman/interp_test.go | 4 --- interp/wazero/interp.go | 13 ++------ 9 files changed, 10 insertions(+), 207 deletions(-) delete mode 100644 engine/references.go delete mode 100644 engine/references_test.go diff --git a/engine/interp.go b/engine/interp.go index 991f9fc..9ebb0f8 100644 --- a/engine/interp.go +++ b/engine/interp.go @@ -29,6 +29,4 @@ type Interpreter interface { SetModules(wypes.Modules) 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 } diff --git a/engine/references.go b/engine/references.go deleted file mode 100644 index 1549a3d..0000000 --- a/engine/references.go +++ /dev/null @@ -1,58 +0,0 @@ -package engine - -import ( - "math/rand" -) - -// ExternalReferences manages external references to be used by the Interpreter -// to store references that can be passed to guest modules. -type ExternalReferences struct { - refs map[int32]any -} - -// NewReferences creates a new ExternalReferences store. -func NewReferences() ExternalReferences { - return ExternalReferences{ - refs: make(map[int32]any), - } -} - -// Add adds a reference to the ExternalReferences store and returns the new reference id. -func (r *ExternalReferences) Add(thing any) int32 { - id := r.newReferenceId() - r.refs[id] = thing - return id -} - -// Get returns the reference for the given id. -func (r *ExternalReferences) Get(id int32) any { - if val, ok := r.refs[id]; ok { - return val - } - return nil -} - -// Remove removes the reference for the given id. -func (r *ExternalReferences) Remove(id int32) { - delete(r.refs, id) -} - -// Remove removes the reference for the given id. -func (r *ExternalReferences) Clear() { - clear(r.refs) -} - -// generates a random reference id that is not already in use. -func (r *ExternalReferences) newReferenceId() int32 { - for { - id := int32(randomInt(1, 0x7FFFFFFF)) - if _, ok := r.refs[id]; !ok { - return id - } - } -} - -// Returns an int >= min, < max -func randomInt(min, max int) int { - return min + rand.Intn(max-min) -} diff --git a/engine/references_test.go b/engine/references_test.go deleted file mode 100644 index de5815c..0000000 --- a/engine/references_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package engine - -import ( - "testing" -) - -type testingType struct { - val1 string - val2 string -} - -func TestExternalReferences(t *testing.T) { - refs := NewReferences() - 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 = refs.Add(&thing1) - id2 = refs.Add(&thing2) - - if id1 == id2 { - t.Errorf("id1 and id2 should not be the same") - } - }) - - t.Run("get references", func(t *testing.T) { - if refs.Get(id1).(*testingType).val1 != thing1.val1 { - t.Errorf("refs.Get(id1) %d failed %v %v", id1, refs.Get(id1).(*testingType).val1, thing1.val1) - } - if refs.Get(id2).(*testingType).val2 != thing2.val2 { - t.Errorf("refs.Get(id2) %d failed %v %v", id2, refs.Get(id2).(*testingType).val2, thing1.val2) - } - }) - - t.Run("remove references", func(t *testing.T) { - refs.Remove(id1) - refs.Remove(id2) - - if refs.Get(id1) != nil { - t.Errorf("refs.Get(id1) failed") - } - if refs.Get(id2) != nil { - t.Errorf("refs.Get(id2) failed") - } - }) -} diff --git a/go.mod b/go.mod index 214186f..103c884 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ replace github.com/tetratelabs/wazero => github.com/orsinium-forks/wazero v0.0.0 require ( github.com/hybridgroup/wasman v0.0.0-20240304140329-ce1ea6b61834 - github.com/orsinium-labs/wypes v0.1.2 + github.com/orsinium-labs/wypes v0.1.3 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 diff --git a/go.sum b/go.sum index 067c26f..e385955 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ github.com/orsinium-forks/wazero v0.0.0-20240217173836-b12c024bcbe4 h1:MUh9e2izc github.com/orsinium-forks/wazero v0.0.0-20240217173836-b12c024bcbe4/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A= github.com/orsinium-labs/tinytest v1.0.0 h1:YiGm/whlGm3mn/ynx9CCFuvEa3Q6yEGrzrKXEqJOkdc= github.com/orsinium-labs/tinytest v1.0.0/go.mod h1:GwcYBp0aKi6zujzBXFpCnqw6RSLSp9JSedDyu/V1DF4= -github.com/orsinium-labs/wypes v0.1.2 h1:xa+d83u6jkGENM2I63KY1dxTC9S/D2At/rB7EsvU5QY= -github.com/orsinium-labs/wypes v0.1.2/go.mod h1:FSNWGo8I6/D5RYXMkCxyu71TXJAlwJGQUxgs4i6MAwo= +github.com/orsinium-labs/wypes v0.1.3 h1:k5nKMRqVF89KO/ffVOylf1+9w3bGlRsxShNdAszqd4Y= +github.com/orsinium-labs/wypes v0.1.3/go.mod h1:FSNWGo8I6/D5RYXMkCxyu71TXJAlwJGQUxgs4i6MAwo= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho= diff --git a/interp/tester/interp.go b/interp/tester/interp.go index dd7fcd5..b883cd1 100644 --- a/interp/tester/interp.go +++ b/interp/tester/interp.go @@ -12,14 +12,9 @@ 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) { @@ -60,53 +55,3 @@ 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(&thing1) - id2 = i.References().Add(&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).(*testingType).val1 != thing1.val1 { - t.Errorf("refs.Get(id1) %d failed %v %v", id1, i.References().Get(id1).(*testingType).val1, thing1.val1) - } - if i.References().Get(id2).(*testingType).val2 != thing2.val2 { - t.Errorf("refs.Get(id2) %d failed %v %v", id2, i.References().Get(id2).(*testingType).val2, thing1.val2) - } - }) - - t.Run("remove references", func(t *testing.T) { - i.References().Remove(id1) - i.References().Remove(id2) - - if i.References().Get(id1) != nil { - t.Errorf("refs.Get(id1) failed") - } - if i.References().Get(id2) != nil { - t.Errorf("refs.Get(id2) failed") - } - }) -} diff --git a/interp/wasman/interp.go b/interp/wasman/interp.go index 6eb1871..47c1c85 100644 --- a/interp/wasman/interp.go +++ b/interp/wasman/interp.go @@ -15,11 +15,10 @@ import ( ) type Interpreter struct { - linker *wasmaneng.Linker - module *wasmaneng.Module - instance *wasmaneng.Instance - Memory []byte - references engine.ExternalReferences + linker *wasmaneng.Linker + module *wasmaneng.Module + instance *wasmaneng.Instance + Memory []byte } func (i *Interpreter) Name() string { @@ -28,20 +27,12 @@ func (i *Interpreter) Name() string { func (i *Interpreter) Init() error { i.linker = wasmaneng.NewLinker(config.LinkerConfig{}) - // use host pre-allocated memory for instances if i.Memory != nil { - if len(i.Memory)%65536 != 0 { - return engine.ErrInvalidMemorySize - } - if err := i.linker.DefineMemory("env", "memory", i.Memory); err != nil { return err } } - - i.references = engine.NewReferences() - return nil } @@ -99,12 +90,8 @@ func (i *Interpreter) Halt() error { runtime.ReadMemStats(&ms) mechanoid.Debug("Interpreter Halt - Heap Used: ", ms.HeapInuse, " Free: ", ms.HeapIdle, " Meta: ", ms.GCSys) } - - // clean up extern refs - i.references.Clear() i.instance = nil i.module = nil - // force a garbage collection to free memory runtime.GC() @@ -168,11 +155,6 @@ 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 -} - func wrapValueTypes(ins []wypes.ValueType) []types.ValueType { outs := make([]types.ValueType, 0, len(ins)) for _, in := range ins { diff --git a/interp/wasman/interp_test.go b/interp/wasman/interp_test.go index f6b6266..2eb0476 100644 --- a/interp/wasman/interp_test.go +++ b/interp/wasman/interp_test.go @@ -29,10 +29,6 @@ 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") } diff --git a/interp/wazero/interp.go b/interp/wazero/interp.go index f6665d2..ab47809 100644 --- a/interp/wazero/interp.go +++ b/interp/wazero/interp.go @@ -13,10 +13,9 @@ import ( ) type Interpreter struct { - runtime wazero.Runtime - module api.Module - references engine.ExternalReferences - modules wypes.Modules + runtime wazero.Runtime + module api.Module + modules wypes.Modules } func (i *Interpreter) Name() string { @@ -29,7 +28,6 @@ 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 } @@ -148,11 +146,6 @@ 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. From 8f7290694ae08e25d991392167b1b2bc7672aa8d Mon Sep 17 00:00:00 2001 From: gram Date: Fri, 8 Mar 2024 11:34:49 +0100 Subject: [PATCH 2/3] drop one dead test --- go.mod | 2 +- go.sum | 4 ++-- interp/tester/instance.go | 18 ------------------ 3 files changed, 3 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index 103c884..a255dc7 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ replace github.com/tetratelabs/wazero => github.com/orsinium-forks/wazero v0.0.0 require ( github.com/hybridgroup/wasman v0.0.0-20240304140329-ce1ea6b61834 - github.com/orsinium-labs/wypes v0.1.3 + github.com/orsinium-labs/wypes v0.1.4 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 diff --git a/go.sum b/go.sum index e385955..2de166a 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ github.com/orsinium-forks/wazero v0.0.0-20240217173836-b12c024bcbe4 h1:MUh9e2izc github.com/orsinium-forks/wazero v0.0.0-20240217173836-b12c024bcbe4/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A= github.com/orsinium-labs/tinytest v1.0.0 h1:YiGm/whlGm3mn/ynx9CCFuvEa3Q6yEGrzrKXEqJOkdc= github.com/orsinium-labs/tinytest v1.0.0/go.mod h1:GwcYBp0aKi6zujzBXFpCnqw6RSLSp9JSedDyu/V1DF4= -github.com/orsinium-labs/wypes v0.1.3 h1:k5nKMRqVF89KO/ffVOylf1+9w3bGlRsxShNdAszqd4Y= -github.com/orsinium-labs/wypes v0.1.3/go.mod h1:FSNWGo8I6/D5RYXMkCxyu71TXJAlwJGQUxgs4i6MAwo= +github.com/orsinium-labs/wypes v0.1.4 h1:+7oih2IDlEpz7laiL3sQFlIU8vjd5j/SwcYlPdIPc0Q= +github.com/orsinium-labs/wypes v0.1.4/go.mod h1:FSNWGo8I6/D5RYXMkCxyu71TXJAlwJGQUxgs4i6MAwo= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho= diff --git a/interp/tester/instance.go b/interp/tester/instance.go index 242b5b3..1e86e22 100644 --- a/interp/tester/instance.go +++ b/interp/tester/instance.go @@ -3,7 +3,6 @@ package tester import ( "bytes" "testing" - "unsafe" "github.com/hybridgroup/mechanoid/engine" ) @@ -86,21 +85,4 @@ func InstanceTest(t *testing.T, i engine.Interpreter) { t.Errorf("Instance.Call() failed: %v", results) } }) - - t.Run("Call externref", func(t *testing.T) { - thing := testingType{val1: "hello", val2: "world"} - - // This is a hack to get the pointer value as an int32 - // Externelref is an opaque type, so we can't do anything with it - // We just want to make sure that the pointer value is passed through correctly - ptr := uintptr(unsafe.Pointer(&thing)) & 0xFFFFFFFF - - results, err := inst.Call("test_externref", ptr) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if uintptr(results.(int32)) != ptr { - t.Errorf("Instance.Call() incorrect: %v %v", ptr, results) - } - }) } From c927b58d6e7970918f2b0328d8c5da9bddbedb28 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 8 Mar 2024 11:48:50 +0100 Subject: [PATCH 3/3] tests: remove tests with ExternalReferences Signed-off-by: deadprogram --- engine/engine_test.go | 4 ---- interp/wazero/interp_test.go | 4 ---- 2 files changed, 8 deletions(-) diff --git a/engine/engine_test.go b/engine/engine_test.go index 582d753..0c84ab2 100644 --- a/engine/engine_test.go +++ b/engine/engine_test.go @@ -55,7 +55,3 @@ func (i *mockInterpreter) SetModules(wypes.Modules) error { func (i *mockInterpreter) MemoryData(ptr, sz uint32) ([]byte, error) { return nil, nil } - -func (i *mockInterpreter) References() *ExternalReferences { - return nil -} diff --git a/interp/wazero/interp_test.go b/interp/wazero/interp_test.go index a908456..5a723ca 100644 --- a/interp/wazero/interp_test.go +++ b/interp/wazero/interp_test.go @@ -29,10 +29,6 @@ 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") }