Files
wazero/internal/wasm/binary/section_test.go
Crypt Keeper 8f8c9ee205 Extracts CompileConfig and consolidates code. (#533)
This performs several changes to allow compilation config to be
centralized and scoped properly. The immediate effects are that we can
now process external types during `Runtime.CompileModule` instead of
doing so later during `Runtime.InstantiateModule`. Another nice side
effect is memory size problems can err at a source line instead of
having to be handled in several places.

There are some API effects to this, and to pay for them, some less used
APIs were removed. The "easy APIs" are left alone. For example, the APIs
to compile and instantiate a module from Go or Wasm in one step are left
alone.

Here are the changes, some of which are only for consistency. Rationale
is summarized in each point.
* ModuleBuilder.Build -> ModuleBuilder.Compile
  * The result of this is similar to `CompileModule`, and pairs better
    with `ModuleBuilder.Instantiate` which is like `InstantiateModule`.
* CompiledCode -> CompiledModule
  * We punted on this name, the result is more than just code. This is
    better I think and more consistent as it introduces less terms.
* Adds CompileConfig param to Runtime.CompileModule.
  * This holds existing features and will have future ones, such as
    mapping externtypes to uint64 for wasm that doesn't yet support it.
* Merges Runtime.InstantiateModuleWithConfig with Runtime.InstantiateModule
  * This allows us to explain APIs in terms of implicit or explicit
    compilation and config, vs implicit, kindof implicit, and explicit.
* Removes Runtime.InstantiateModuleFromCodeWithConfig
  * Similar to above, this API only saves the compilation step and also
    difficult to reason with from a name POV.
* RuntimeConfig.WithMemory(CapacityPages|LimitPages) -> CompileConfig.WithMemorySizer
  * This allows all error handling to be attached to the source line
  * This also allows someone to reduce unbounded memory while knowing
    what its minimum is.
* ModuleConfig.With(Import|ImportModule) -> CompileConfig.WithImportRenamer
  * This allows more types of import manipulation, also without
    conflating functions with globals.
* Adds api.ExternType
  * Needed for ImportRenamer and will be needed later for ExportRenamer.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-05-09 11:02:32 +08:00

231 lines
5.6 KiB
Go

package binary
import (
"bytes"
"testing"
"github.com/tetratelabs/wazero/internal/testing/require"
"github.com/tetratelabs/wazero/internal/wasm"
)
func TestTableSection(t *testing.T) {
three := uint32(3)
tests := []struct {
name string
input []byte
expected []*wasm.Table
}{
{
name: "min and min with max",
input: []byte{
0x01, // 1 table
wasm.RefTypeFuncref, 0x01, 2, 3, // (table 2 3)
},
expected: []*wasm.Table{{Min: 2, Max: &three, Type: wasm.RefTypeFuncref}},
},
{
name: "min and min with max - three tables",
input: []byte{
0x03, // 3 table
wasm.RefTypeFuncref, 0x01, 2, 3, // (table 2 3)
wasm.RefTypeExternref, 0x01, 2, 3, // (table 2 3)
wasm.RefTypeFuncref, 0x01, 2, 3, // (table 2 3)
},
expected: []*wasm.Table{
{Min: 2, Max: &three, Type: wasm.RefTypeFuncref},
{Min: 2, Max: &three, Type: wasm.RefTypeExternref},
{Min: 2, Max: &three, Type: wasm.RefTypeFuncref},
},
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
tables, err := decodeTableSection(bytes.NewReader(tc.input), wasm.FeatureReferenceTypes)
require.NoError(t, err)
require.Equal(t, tc.expected, tables)
})
}
}
func TestTableSection_Errors(t *testing.T) {
tests := []struct {
name string
input []byte
expectedErr string
features wasm.Features
}{
{
name: "min and min with max",
input: []byte{
0x02, // 2 tables
wasm.RefTypeFuncref, 0x00, 0x01, // (table 1)
wasm.RefTypeFuncref, 0x01, 0x02, 0x03, // (table 2 3)
},
expectedErr: "at most one table allowed in module as feature \"reference-types\" is disabled",
features: wasm.Features20191205,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
_, err := decodeTableSection(bytes.NewReader(tc.input), tc.features)
require.EqualError(t, err, tc.expectedErr)
})
}
}
func TestMemorySection(t *testing.T) {
three := uint32(3)
tests := []struct {
name string
input []byte
expected *wasm.Memory
}{
{
name: "min and min with max",
input: []byte{
0x01, // 1 memory
0x01, 0x02, 0x03, // (memory 2 3)
},
expected: &wasm.Memory{Min: 2, Cap: 2, Max: three, IsMaxEncoded: true},
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
memories, err := decodeMemorySection(bytes.NewReader(tc.input), wasm.MemorySizer)
require.NoError(t, err)
require.Equal(t, tc.expected, memories)
})
}
}
func TestMemorySection_Errors(t *testing.T) {
tests := []struct {
name string
input []byte
expectedErr string
}{
{
name: "min and min with max",
input: []byte{
0x02, // 2 memories
0x01, // (memory 1)
0x02, 0x03, // (memory 2 3)
},
expectedErr: "at most one memory allowed in module, but read 2",
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
_, err := decodeMemorySection(bytes.NewReader(tc.input), wasm.MemorySizer)
require.EqualError(t, err, tc.expectedErr)
})
}
}
func TestDecodeExportSection(t *testing.T) {
tests := []struct {
name string
input []byte
expected []*wasm.Export
}{
{
name: "empty and non-empty name",
input: []byte{
0x02, // 2 exports
0x00, // Size of empty name
wasm.ExternTypeFunc, 0x02, // func[2]
0x01, 'a', // Size of name, name
wasm.ExternTypeFunc, 0x01, // func[1]
},
expected: []*wasm.Export{
{Name: "", Type: wasm.ExternTypeFunc, Index: wasm.Index(2)},
{Name: "a", Type: wasm.ExternTypeFunc, Index: wasm.Index(1)},
},
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
exports, err := decodeExportSection(bytes.NewReader(tc.input))
require.NoError(t, err)
require.Equal(t, tc.expected, exports)
})
}
}
func TestDecodeExportSection_Errors(t *testing.T) {
tests := []struct {
name string
input []byte
expectedErr string
}{
{
name: "duplicates empty name",
input: []byte{
0x02, // 2 exports
0x00, // Size of empty name
wasm.ExternTypeFunc, 0x00, // func[0]
0x00, // Size of empty name
wasm.ExternTypeFunc, 0x00, // func[0]
},
expectedErr: "export[1] duplicates name \"\"",
},
{
name: "duplicates name",
input: []byte{
0x02, // 2 exports
0x01, 'a', // Size of name, name
wasm.ExternTypeFunc, 0x00, // func[0]
0x01, 'a', // Size of name, name
wasm.ExternTypeFunc, 0x00, // func[0]
},
expectedErr: "export[1] duplicates name \"a\"",
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
_, err := decodeExportSection(bytes.NewReader(tc.input))
require.EqualError(t, err, tc.expectedErr)
})
}
}
func TestEncodeFunctionSection(t *testing.T) {
require.Equal(t, []byte{wasm.SectionIDFunction, 0x2, 0x01, 0x05}, encodeFunctionSection([]wasm.Index{5}))
}
// TestEncodeStartSection uses the same index as TestEncodeFunctionSection to highlight the encoding is different.
func TestEncodeStartSection(t *testing.T) {
require.Equal(t, []byte{wasm.SectionIDStart, 0x01, 0x05}, encodeStartSection(5))
}
func TestDecodeDataCountSection(t *testing.T) {
t.Run("ok", func(t *testing.T) {
v, err := decodeDataCountSection(bytes.NewReader([]byte{0x1}))
require.NoError(t, err)
require.Equal(t, uint32(1), *v)
})
t.Run("eof", func(t *testing.T) {
// EOF is fine as the datacount is optional.
_, err := decodeDataCountSection(bytes.NewReader([]byte{}))
require.NoError(t, err)
})
}