From 779f8b5365a41b863578ce0fc33e423adb28ee15 Mon Sep 17 00:00:00 2001 From: mathetake Date: Sat, 9 May 2020 18:06:17 +0900 Subject: [PATCH] unittest: vm.go --- examples/README.md | 4 ++ wasm/section.go | 12 ++---- wasm/vm.go | 6 ++- wasm/vm_test.go | 92 +++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 examples/README.md diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..527122a7 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,4 @@ + +This is where we put e2e tests of virtual machine. Please place examples so that `examples/foo_test.go` is testing +`examples/wasm/foo.wasm` binary which is generated by compiling +`examples/wasm/foo.go` with latest version of TinyGo. diff --git a/wasm/section.go b/wasm/section.go index 89e8f76c..efd537d8 100644 --- a/wasm/section.go +++ b/wasm/section.go @@ -8,10 +8,6 @@ import ( "github.com/mathetake/gasm/wasm/leb128" ) -var ( - ErrInvalidSectionID = errors.New("invalid section id") -) - type SectionID byte const ( @@ -31,7 +27,7 @@ const ( func (m *Module) readSections(r io.Reader) error { for { - if err := m.readSection(r); err == io.EOF { + if err := m.readSection(r); errors.Is(err, io.EOF) { return nil } else if err != nil { return err @@ -41,9 +37,7 @@ func (m *Module) readSections(r io.Reader) error { func (m *Module) readSection(r io.Reader) error { b := make([]byte, 1) - if _, err := io.ReadFull(r, b); err == io.EOF { - return err - } else if err != nil { + if _, err := io.ReadFull(r, b); err != nil { return fmt.Errorf("read section id: %w", err) } @@ -80,7 +74,7 @@ func (m *Module) readSection(r io.Reader) error { case SectionIDData: err = m.readSectionData(r) default: - err = ErrInvalidSectionID + err = errors.New("invalid section id") } if err != nil { diff --git a/wasm/vm.go b/wasm/vm.go index f516187d..8a9367e9 100644 --- a/wasm/vm.go +++ b/wasm/vm.go @@ -97,11 +97,15 @@ func (vm *VirtualMachine) ExecExportedFunction(name string, args ...uint64) (ret return nil, nil, fmt.Errorf("function index out of range") } + f := vm.Functions[exp.Desc.Index] + if len(f.FunctionType().InputTypes) != len(args) { + return nil, nil, fmt.Errorf("invalid number of arguments") + } + for _, arg := range args { vm.OperandStack.Push(arg) } - f := vm.Functions[exp.Desc.Index] f.Call(vm) ret := make([]uint64, len(f.FunctionType().ReturnTypes)) diff --git a/wasm/vm_test.go b/wasm/vm_test.go index 10f4f665..d3f1fa3e 100644 --- a/wasm/vm_test.go +++ b/wasm/vm_test.go @@ -1,3 +1,93 @@ package wasm -// fixme: +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestVirtualMachine_ExecExportedFunction(t *testing.T) { + vm := &VirtualMachine{ + InnerModule: &Module{ + SecExports: map[string]*ExportSegment{ + "a": {Desc: &ExportDesc{Index: 0, Kind: ExportKindFunction}}, + "b": {Desc: &ExportDesc{Index: 0, Kind: ExportKindGlobal}}, + "c": {Desc: &ExportDesc{Index: 100, Kind: ExportKindFunction}}, + }, + }, + Functions: []VirtualMachineFunction{&HostFunction{ + function: reflect.ValueOf(func(in int64) int64 { + return in * 2 + }), + Signature: &FunctionType{ + InputTypes: []ValueType{ValueTypeI64}, + ReturnTypes: []ValueType{ValueTypeI64}, + }, + }}, + OperandStack: NewVirtualMachineOperandStack(), + } + + ret, retTypes, err := vm.ExecExportedFunction("a", 1) + require.NoError(t, err) + require.Len(t, retTypes, 1) + require.Len(t, ret, 1) + require.Equal(t, retTypes[0], ValueTypeI64) + require.Equal(t, int64(2), int64(ret[0])) + + _, _, err = vm.ExecExportedFunction("a") + require.Error(t, err) + _, _, err = vm.ExecExportedFunction("b") + require.Error(t, err) + _, _, err = vm.ExecExportedFunction("c") + require.Error(t, err) +} + +func TestVirtualMachine_FetchInt32(t *testing.T) { + vm := &VirtualMachine{ + ActiveContext: &NativeFunctionContext{ + PC: 1, + Function: &NativeFunction{Body: []byte{0x00, 0xFF, 0x00}}, + }, + } + actual := vm.FetchInt32() + require.Equal(t, int32(127), actual) + require.Equal(t, uint64(2), vm.ActiveContext.PC) +} + +func TestVirtualMachine_FetchInt64(t *testing.T) { + vm := &VirtualMachine{ + ActiveContext: &NativeFunctionContext{ + PC: 1, + Function: &NativeFunction{Body: []byte{0x00, 0xFF, 0x00}}, + }, + } + actual := vm.FetchInt64() + require.Equal(t, int64(127), actual) + require.Equal(t, uint64(2), vm.ActiveContext.PC) +} + +func TestVirtualMachine_FetchFloat32(t *testing.T) { + vm := &VirtualMachine{ + ActiveContext: &NativeFunctionContext{ + PC: 2, + Function: &NativeFunction{Body: []byte{0x00, 0x00, 0x40, 0xe1, 0x47, 0x40}}, + }, + } + actual := vm.FetchFloat32() + require.Equal(t, float32(3.1231232), actual) + require.Equal(t, uint64(5), vm.ActiveContext.PC) +} + +func TestVirtualMachine_FetchFloat64(t *testing.T) { + vm := &VirtualMachine{ + ActiveContext: &NativeFunctionContext{ + Function: &NativeFunction{Body: []byte{ + 0x5e, 0xc4, 0xd8, 0xf9, 0x27, 0xfc, 0x08, 0x40, + }}, + }, + } + actual := vm.FetchFloat64() + require.Equal(t, 3.1231231231, actual) + require.Equal(t, uint64(7), vm.ActiveContext.PC) +}