Files
wazero/internal/wasm/binary/decoder_test.go
Crypt Keeper cfb11f352a Adds ability to disable mutable globals and improves decode perf (#315)
This adds `RuntimeConfig.WithFeatureMutableGlobal(enabled bool)`, which
allows disabling of mutable globals. When disabled, any attempt to add a
mutable global, either explicitly or implicitly via decoding wasm will
fail.

To support this, there's a new `Features` bitflag that can allow up to
63 feature toggles without passing structs.

While here, I fixed a significant performance problem in decoding
binary:

Before
```
BenchmarkCodecExample/binary.DecodeModule-16         	  184243	      5623 ns/op	    3848 B/op	     184 allocs/op
```

Now
```
BenchmarkCodecExample/binary.DecodeModule-16         	  294084	      3520 ns/op	    2176 B/op	      91 allocs/op

```

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-03 10:31:10 +08:00

180 lines
5.2 KiB
Go

package binary
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
wasm "github.com/tetratelabs/wazero/internal/wasm"
)
// TestDecodeModule relies on unit tests for Module.Encode, specifically that the encoding is both known and correct.
// This avoids having to copy/paste or share variables to assert against byte arrays.
func TestDecodeModule(t *testing.T) {
i32, f32 := wasm.ValueTypeI32, wasm.ValueTypeF32
zero := uint32(0)
tests := []struct {
name string
input *wasm.Module // round trip test!
}{
{
name: "empty",
input: &wasm.Module{},
},
{
name: "only name section",
input: &wasm.Module{NameSection: &wasm.NameSection{ModuleName: "simple"}},
},
{
name: "type section",
input: &wasm.Module{
TypeSection: []*wasm.FunctionType{
{},
{Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}},
{Params: []wasm.ValueType{i32, i32, i32, i32}, Results: []wasm.ValueType{i32}},
},
},
},
{
name: "type and import section",
input: &wasm.Module{
TypeSection: []*wasm.FunctionType{
{Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}},
{Params: []wasm.ValueType{f32, f32}, Results: []wasm.ValueType{f32}},
},
ImportSection: []*wasm.Import{
{
Module: "Math", Name: "Mul",
Type: wasm.ExternTypeFunc,
DescFunc: 1,
}, {
Module: "Math", Name: "Add",
Type: wasm.ExternTypeFunc,
DescFunc: 0,
},
},
},
},
{
name: "memory and export section",
input: &wasm.Module{
MemorySection: []*wasm.MemoryType{{Min: 0, Max: &zero}},
ExportSection: map[string]*wasm.Export{
"mem": {
Name: "mem",
Type: wasm.ExternTypeMemory,
Index: 0,
},
},
},
},
{
name: "type function and start section",
input: &wasm.Module{
TypeSection: []*wasm.FunctionType{{}},
ImportSection: []*wasm.Import{{
Module: "", Name: "hello",
Type: wasm.ExternTypeFunc,
DescFunc: 0,
}},
StartSection: &zero,
},
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
m, e := DecodeModule(EncodeModule(tc.input), wasm.Features20191205)
require.NoError(t, e)
require.Equal(t, tc.input, m)
})
}
t.Run("skips custom section", func(t *testing.T) {
input := append(append(Magic, version...),
wasm.SectionIDCustom, 0xf, // 15 bytes in this section
0x04, 'm', 'e', 'm', 'e',
1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
m, e := DecodeModule(input, wasm.Features20191205)
require.NoError(t, e)
require.Equal(t, &wasm.Module{}, m)
})
t.Run("skips custom section, but not name", func(t *testing.T) {
input := append(append(Magic, version...),
wasm.SectionIDCustom, 0xf, // 15 bytes in this section
0x04, 'm', 'e', 'm', 'e',
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
wasm.SectionIDCustom, 0x0e, // 14 bytes in this section
0x04, 'n', 'a', 'm', 'e',
subsectionIDModuleName, 0x07, // 7 bytes in this subsection
0x06, // the Module name simple is 6 bytes long
's', 'i', 'm', 'p', 'l', 'e')
m, e := DecodeModule(input, wasm.Features20191205)
require.NoError(t, e)
require.Equal(t, &wasm.Module{NameSection: &wasm.NameSection{ModuleName: "simple"}}, m)
})
}
func TestDecodeModule_Errors(t *testing.T) {
tests := []struct {
name string
input []byte
features wasm.Features
expectedErr string
}{
{
name: "wrong magic",
input: []byte("wasm\x01\x00\x00\x00"),
expectedErr: "invalid magic number",
},
{
name: "wrong version",
input: []byte("\x00asm\x01\x00\x00\x01"),
expectedErr: "invalid version header",
},
{
name: "redundant name section",
input: append(append(Magic, version...),
wasm.SectionIDCustom, 0x09, // 9 bytes in this section
0x04, 'n', 'a', 'm', 'e',
subsectionIDModuleName, 0x02, 0x01, 'x',
wasm.SectionIDCustom, 0x09, // 9 bytes in this section
0x04, 'n', 'a', 'm', 'e',
subsectionIDModuleName, 0x02, 0x01, 'x'),
expectedErr: "section custom: redundant custom section name",
},
{
name: fmt.Sprintf("define mutable global when %s disabled", wasm.FeatureMutableGlobal),
features: wasm.Features20191205.Set(wasm.FeatureMutableGlobal, false),
input: append(append(Magic, version...),
wasm.SectionIDGlobal, 0x06, // 6 bytes in this section
0x01, wasm.ValueTypeI32, 0x01, // 1 global i32 mutable
wasm.OpcodeI32Const, 0x00, wasm.OpcodeEnd, // arbitrary init to zero
),
expectedErr: "global[0]: feature mutable-global is disabled",
},
{
name: fmt.Sprintf("import mutable global when %s disabled", wasm.FeatureMutableGlobal),
features: wasm.Features20191205.Set(wasm.FeatureMutableGlobal, false),
input: append(append(Magic, version...),
wasm.SectionIDImport, 0x08, // 8 bytes in this section
0x01, 0x01, 'a', 0x01, 'b', wasm.ExternTypeGlobal, // 1 import a.b of type global
wasm.ValueTypeI32, 0x01, // 1 global i32 mutable
),
expectedErr: "import[0] global[a.b]: feature mutable-global is disabled",
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
_, e := DecodeModule(tc.input, tc.features)
require.EqualError(t, e, tc.expectedErr)
})
}
}