Merge pull request #5 from mathetake/unittests

add unit tests: hostfunc, vm.go
This commit is contained in:
Takeshi Yoneda
2020-05-09 18:08:12 +09:00
committed by GitHub
7 changed files with 198 additions and 21 deletions

4
examples/README.md Normal file
View File

@@ -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.

View File

@@ -19,6 +19,10 @@ func NewModuleBuilderWith(in map[string]*wasm.Module) *ModuleBuilder {
return &ModuleBuilder{modules: in}
}
func (m *ModuleBuilder) Done() map[string]*wasm.Module {
return m.modules
}
func (m *ModuleBuilder) MustSetFunction(modName, funcName string, fn func(machine *wasm.VirtualMachine) reflect.Value) {
if err := m.SetFunction(modName, funcName, fn); err != nil {
panic(err)
@@ -34,6 +38,7 @@ func (m *ModuleBuilder) SetFunction(modName, funcName string, fn func(machine *w
}
mod.SecExports[funcName] = &wasm.ExportSegment{
Name: funcName,
Desc: &wasm.ExportDesc{
Kind: wasm.ExportKindFunction,
Index: uint32(len(mod.IndexSpace.Function)),
@@ -81,12 +86,8 @@ func getTypeOf(kind reflect.Kind) (wasm.ValueType, error) {
case reflect.Int32, reflect.Uint32:
return wasm.ValueTypeI32, nil
case reflect.Int64, reflect.Uint64:
return wasm.ValueTypeI32, nil
return wasm.ValueTypeI64, nil
default:
return 0x00, fmt.Errorf("invalid type: %s", kind.String())
}
}
func (m *ModuleBuilder) Done() map[string]*wasm.Module {
return m.modules
}

View File

@@ -1,3 +1,87 @@
package hostfunc
// fixme:
import (
"reflect"
"testing"
"github.com/mathetake/gasm/wasm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestModuleBuilder_SetFunction(t *testing.T) {
t.Run("register", func(t *testing.T) {
builder := NewModuleBuilder()
cases := []struct {
modName, funcName string
expIndex uint32
}{
{modName: "env", funcName: "foo", expIndex: 0},
{modName: "env", funcName: "bar", expIndex: 1},
{modName: "golang", funcName: "hello", expIndex: 0},
{modName: "env", funcName: "great", expIndex: 2},
}
for _, c := range cases {
builder.MustSetFunction(c.modName, c.funcName, func(machine *wasm.VirtualMachine) reflect.Value {
return reflect.ValueOf(func() {})
})
}
ms := builder.Done()
for _, c := range cases {
mod, ok := ms[c.modName]
require.True(t, ok)
e, ok := mod.SecExports[c.funcName]
require.True(t, ok)
require.Equal(t, c.funcName, e.Name)
require.Equal(t, wasm.ExportKindFunction, e.Desc.Kind)
require.Equal(t, c.expIndex, e.Desc.Index)
}
})
t.Run("type", func(t *testing.T) {
builder := NewModuleBuilder()
builder.MustSetFunction("a", "b", func(machine *wasm.VirtualMachine) reflect.Value {
return reflect.ValueOf(func(int32, int64) (float32, float64, int32) {
return 0, 0, 0
})
})
ms := builder.Done()
mod, ok := ms["a"]
require.True(t, ok)
actualType := mod.IndexSpace.Function[0].FunctionType()
require.Equal(t, &wasm.FunctionType{
InputTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64},
ReturnTypes: []wasm.ValueType{wasm.ValueTypeF32, wasm.ValueTypeF64, wasm.ValueTypeI32},
}, actualType)
})
}
func Test_getSignature(t *testing.T) {
v := reflect.ValueOf(func(int32, int64, float32, float64) (int32, float64) { return 0, 0 })
actual, err := getSignature(v.Type())
require.NoError(t, err)
require.Equal(t, &wasm.FunctionType{
InputTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64, wasm.ValueTypeF32, wasm.ValueTypeF64},
ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeF64},
}, actual)
}
func Test_getTypeOf(t *testing.T) {
for _, c := range []struct {
kind reflect.Kind
exp wasm.ValueType
}{
{kind: reflect.Int32, exp: wasm.ValueTypeI32},
{kind: reflect.Uint32, exp: wasm.ValueTypeI32},
{kind: reflect.Int64, exp: wasm.ValueTypeI64},
{kind: reflect.Uint64, exp: wasm.ValueTypeI64},
{kind: reflect.Float32, exp: wasm.ValueTypeF32},
{kind: reflect.Float64, exp: wasm.ValueTypeF64},
} {
actual, err := getTypeOf(c.kind)
require.NoError(t, err)
assert.Equal(t, c.exp, actual)
}
}

View File

@@ -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 {

View File

@@ -118,10 +118,10 @@ type ExportDesc struct {
}
const (
ExportKindFunction = 0x00
ExportKindTable = 0x01
ExportKindMem = 0x02
ExportKindGlobal = 0x03
ExportKindFunction byte = 0x00
ExportKindTable byte = 0x01
ExportKindMem byte = 0x02
ExportKindGlobal byte = 0x03
)
func readExportDesc(r io.Reader) (*ExportDesc, error) {

View File

@@ -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))

View File

@@ -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)
}