From 350e81e632f9e70f4f21eb3fa2da9bf8c5e5c26e Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Tue, 14 Mar 2023 21:45:52 -0700 Subject: [PATCH] Holds function types as values, not ptrs in wasm.Module (#1227) Signed-off-by: Takeshi Yoneda --- builder_test.go | 21 +- cache_test.go | 2 +- experimental/listener_test.go | 2 +- experimental/logging/log_listener_test.go | 50 ++-- .../compiler/compiler_controlflow_test.go | 24 +- internal/engine/compiler/engine_test.go | 6 +- internal/engine/compiler/impl_amd64.go | 4 +- internal/engine/compiler/impl_amd64_test.go | 2 +- internal/engine/compiler/impl_arm64.go | 4 +- internal/engine/compiler/impl_arm64_test.go | 2 +- .../engine/interpreter/interpreter_test.go | 4 +- .../bench/hostfunc_bench_test.go | 6 +- .../integration_test/engine/adhoc_test.go | 10 +- .../testing/binaryencoding/encoder_test.go | 10 +- internal/testing/binaryencoding/section.go | 5 +- internal/testing/enginetest/enginetest.go | 24 +- internal/testing/proxy/proxy.go | 2 +- internal/wasm/binary/decoder_test.go | 11 +- internal/wasm/binary/function.go | 24 +- internal/wasm/binary/function_test.go | 34 +-- internal/wasm/binary/section.go | 6 +- internal/wasm/counts_test.go | 6 +- internal/wasm/func_validation.go | 19 +- internal/wasm/func_validation_test.go | 233 +++++++++--------- internal/wasm/function_definition.go | 4 +- internal/wasm/function_definition_test.go | 32 +-- internal/wasm/host.go | 6 +- internal/wasm/host_test.go | 4 +- internal/wasm/module.go | 9 +- internal/wasm/module_test.go | 34 +-- internal/wasm/store.go | 7 +- internal/wasm/store_test.go | 24 +- internal/wasm/table_test.go | 76 +++--- internal/wazeroir/compiler.go | 8 +- internal/wazeroir/compiler_test.go | 156 ++++++------ internal/wazeroir/signature.go | 4 +- internal/wazeroir/signature_test.go | 2 +- runtime_test.go | 12 +- 38 files changed, 447 insertions(+), 442 deletions(-) diff --git a/builder_test.go b/builder_test.go index e635317d..6b4be532 100644 --- a/builder_test.go +++ b/builder_test.go @@ -53,7 +53,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) { NewFunctionBuilder().WithFunc(uint32_uint32).Export("1") }, expected: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, }, FunctionSection: []wasm.Index{0}, @@ -75,7 +75,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) { Export("1") }, expected: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, }, FunctionSection: []wasm.Index{0}, @@ -98,7 +98,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) { Export("1") }, expected: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, }, FunctionSection: []wasm.Index{0}, @@ -120,7 +120,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) { NewFunctionBuilder().WithFunc(uint64_uint32).Export("1") }, expected: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []api.ValueType{i64}, Results: []api.ValueType{i32}}, }, FunctionSection: []wasm.Index{0}, @@ -142,7 +142,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) { NewFunctionBuilder().WithFunc(uint32_uint32).Export("1") }, expected: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, {Params: []api.ValueType{i64}, Results: []api.ValueType{i32}}, }, @@ -166,7 +166,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) { Export("1") }, expected: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, }, FunctionSection: []wasm.Index{0}, @@ -190,7 +190,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) { Export("1") }, expected: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, }, FunctionSection: []wasm.Index{0}, @@ -218,7 +218,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) { Export("1") }, expected: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []api.ValueType{i64}, Results: []api.ValueType{i32}}, }, FunctionSection: []wasm.Index{0}, @@ -246,7 +246,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) { Export("1") }, expected: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, {Params: []api.ValueType{i64}, Results: []api.ValueType{i32}}, }, @@ -356,7 +356,8 @@ func TestNewHostModuleBuilder_Instantiate_Errors(t *testing.T) { // requireHostModuleEquals is redefined from internal/wasm/host_test.go to avoid an import cycle extracting it. func requireHostModuleEquals(t *testing.T, expected, actual *wasm.Module) { // `require.Equal(t, expected, actual)` fails reflect pointers don't match, so brute compare: - for _, tp := range expected.TypeSection { + for i := range expected.TypeSection { + tp := &expected.TypeSection[i] tp.CacheNumInUint64() // When creating the compiled module, we get the type IDs for types, which results in caching type keys. _ = tp.String() diff --git a/cache_test.go b/cache_test.go index fcc6ad28..628adff9 100644 --- a/cache_test.go +++ b/cache_test.go @@ -30,7 +30,7 @@ func TestCompilationCache(t *testing.T) { // Create a different type id on the bar's store so that we can emulate that bar instantiated the module before facWasm. _, err := bar.store.GetFunctionTypeIDs( // Arbitrary one is fine as long as it is not used in facWasm. - []*wasm.FunctionType{{Params: []wasm.ValueType{ + []wasm.FunctionType{{Params: []wasm.ValueType{ wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeV128, wasm.ValueTypeI32, wasm.ValueTypeV128, wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeV128, wasm.ValueTypeI32, wasm.ValueTypeV128, wasm.ValueTypeI32, wasm.ValueTypeI32, diff --git a/experimental/listener_test.go b/experimental/listener_test.go index d50c351c..f7e19a88 100644 --- a/experimental/listener_test.go +++ b/experimental/listener_test.go @@ -42,7 +42,7 @@ func TestFunctionListenerFactory(t *testing.T) { // Define a module with two functions bin := binaryencoding.EncodeModule(&wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, ImportSection: []wasm.Import{{}}, FunctionSection: []wasm.Index{0, 0}, CodeSection: []*wasm.Code{ diff --git a/experimental/logging/log_listener_test.go b/experimental/logging/log_listener_test.go index d0271323..55f2c111 100644 --- a/experimental/logging/log_listener_test.go +++ b/experimental/logging/log_listener_test.go @@ -19,7 +19,7 @@ var testCtx = context.WithValue(context.Background(), struct{}{}, "arbitrary") func Test_loggingListener(t *testing.T) { wasiFuncName := wasi.RandomGetName - wasiFuncType := &wasm.FunctionType{ + wasiFuncType := wasm.FunctionType{ Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI32}, Results: []api.ValueType{api.ValueTypeI32}, } @@ -30,7 +30,7 @@ func Test_loggingListener(t *testing.T) { tests := []struct { name string moduleName, funcName string - functype *wasm.FunctionType + functype wasm.FunctionType isHostFunc bool paramNames, resultNames []string params, results []uint64 @@ -39,14 +39,14 @@ func Test_loggingListener(t *testing.T) { }{ { name: "v_v", - functype: &wasm.FunctionType{}, + functype: wasm.FunctionType{}, expected: `--> test.fn() <-- `, }, { name: "error", - functype: &wasm.FunctionType{}, + functype: wasm.FunctionType{}, err: io.EOF, expected: `--> test.fn() <-- error: EOF @@ -54,7 +54,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "host", - functype: &wasm.FunctionType{}, + functype: wasm.FunctionType{}, isHostFunc: true, expected: `==> test.fn() <== @@ -104,7 +104,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "i32", - functype: &wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI32}}, + functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI32}}, params: []uint64{math.MaxUint32}, expected: `--> test.fn(-1) <-- @@ -112,7 +112,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "i32 named", - functype: &wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI32}}, + functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI32}}, params: []uint64{math.MaxUint32}, paramNames: []string{"x"}, expected: `--> test.fn(x=-1) @@ -121,7 +121,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "i64", - functype: &wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI64}}, + functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI64}}, params: []uint64{math.MaxUint64}, expected: `--> test.fn(-1) <-- @@ -129,7 +129,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "i64 named", - functype: &wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI64}}, + functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI64}}, params: []uint64{math.MaxUint64}, paramNames: []string{"x"}, expected: `--> test.fn(x=-1) @@ -138,7 +138,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "f32", - functype: &wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF32}}, + functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF32}}, params: []uint64{api.EncodeF32(math.MaxFloat32)}, expected: `--> test.fn(3.4028235e+38) <-- @@ -146,7 +146,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "f32 named", - functype: &wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF32}}, + functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF32}}, params: []uint64{api.EncodeF32(math.MaxFloat32)}, paramNames: []string{"x"}, expected: `--> test.fn(x=3.4028235e+38) @@ -155,7 +155,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "f64", - functype: &wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF64}}, + functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF64}}, params: []uint64{api.EncodeF64(math.MaxFloat64)}, expected: `--> test.fn(1.7976931348623157e+308) <-- @@ -163,7 +163,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "f64 named", - functype: &wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF64}}, + functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF64}}, params: []uint64{api.EncodeF64(math.MaxFloat64)}, paramNames: []string{"x"}, expected: `--> test.fn(x=1.7976931348623157e+308) @@ -172,7 +172,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "externref", - functype: &wasm.FunctionType{Params: []api.ValueType{api.ValueTypeExternref}}, + functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeExternref}}, params: []uint64{0}, expected: `--> test.fn(0000000000000000) <-- @@ -180,7 +180,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "externref named", - functype: &wasm.FunctionType{Params: []api.ValueType{api.ValueTypeExternref}}, + functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeExternref}}, params: []uint64{0}, paramNames: []string{"x"}, expected: `--> test.fn(x=0000000000000000) @@ -189,7 +189,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "v128", - functype: &wasm.FunctionType{Params: []api.ValueType{0x7b}}, + functype: wasm.FunctionType{Params: []api.ValueType{0x7b}}, params: []uint64{0, 1}, expected: `--> test.fn(00000000000000000000000000000001) <-- @@ -197,7 +197,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "v128 named", - functype: &wasm.FunctionType{Params: []api.ValueType{0x7b}}, + functype: wasm.FunctionType{Params: []api.ValueType{0x7b}}, params: []uint64{0, 1}, paramNames: []string{"x"}, expected: `--> test.fn(x=00000000000000000000000000000001) @@ -206,7 +206,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "funcref", - functype: &wasm.FunctionType{Params: []api.ValueType{0x70}}, + functype: wasm.FunctionType{Params: []api.ValueType{0x70}}, params: []uint64{0}, expected: `--> test.fn(0000000000000000) <-- @@ -214,7 +214,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "funcref named", - functype: &wasm.FunctionType{Params: []api.ValueType{0x70}}, + functype: wasm.FunctionType{Params: []api.ValueType{0x70}}, params: []uint64{0}, paramNames: []string{"x"}, expected: `--> test.fn(x=0000000000000000) @@ -223,7 +223,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "no params, one result", - functype: &wasm.FunctionType{Results: []api.ValueType{api.ValueTypeI32}}, + functype: wasm.FunctionType{Results: []api.ValueType{api.ValueTypeI32}}, results: []uint64{math.MaxUint32}, expected: `--> test.fn() <-- -1 @@ -231,7 +231,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "one param, one result", - functype: &wasm.FunctionType{ + functype: wasm.FunctionType{ Params: []api.ValueType{api.ValueTypeI32}, Results: []api.ValueType{api.ValueTypeF32}, }, @@ -243,7 +243,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "two params, two results", - functype: &wasm.FunctionType{ + functype: wasm.FunctionType{ Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI64}, Results: []api.ValueType{api.ValueTypeF32, api.ValueTypeF64}, }, @@ -255,7 +255,7 @@ func Test_loggingListener(t *testing.T) { }, { name: "two params, two results named", - functype: &wasm.FunctionType{ + functype: wasm.FunctionType{ Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI64}, Results: []api.ValueType{api.ValueTypeF32, api.ValueTypeF64}, }, @@ -281,7 +281,7 @@ func Test_loggingListener(t *testing.T) { tc.funcName = "fn" } m := &wasm.Module{ - TypeSection: []*wasm.FunctionType{tc.functype}, + TypeSection: []wasm.FunctionType{tc.functype}, FunctionSection: []wasm.Index{0}, NameSection: &wasm.NameSection{ ModuleName: tc.moduleName, @@ -323,7 +323,7 @@ func Test_loggingListener_indentation(t *testing.T) { out := bytes.NewBuffer(nil) lf := logging.NewLoggingListenerFactory(out) m := &wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, FunctionSection: []wasm.Index{0, 0}, CodeSection: []*wasm.Code{{Body: []byte{wasm.OpcodeEnd}}, {Body: []byte{wasm.OpcodeEnd}}}, NameSection: &wasm.NameSection{ diff --git a/internal/engine/compiler/compiler_controlflow_test.go b/internal/engine/compiler/compiler_controlflow_test.go index ef806f88..f33308b1 100644 --- a/internal/engine/compiler/compiler_controlflow_test.go +++ b/internal/engine/compiler/compiler_controlflow_test.go @@ -541,7 +541,7 @@ func TestCompiler_compileCallIndirect(t *testing.T) { env.addTable(&wasm.TableInstance{References: make([]wasm.Reference, 10)}) compiler := env.requireNewCompiler(t, newCompiler, &wazeroir.CompilationResult{ Signature: &wasm.FunctionType{}, - Types: []*wasm.FunctionType{{}}, + Types: []wasm.FunctionType{{}}, HasTable: true, }) err := compiler.compilePreamble() @@ -571,7 +571,7 @@ func TestCompiler_compileCallIndirect(t *testing.T) { env := newCompilerEnvironment() compiler := env.requireNewCompiler(t, newCompiler, &wazeroir.CompilationResult{ Signature: &wasm.FunctionType{}, - Types: []*wasm.FunctionType{{}}, + Types: []wasm.FunctionType{{}}, HasTable: true, }) err := compiler.compilePreamble() @@ -607,7 +607,7 @@ func TestCompiler_compileCallIndirect(t *testing.T) { env := newCompilerEnvironment() compiler := env.requireNewCompiler(t, newCompiler, &wazeroir.CompilationResult{ Signature: &wasm.FunctionType{}, - Types: []*wasm.FunctionType{{}}, + Types: []wasm.FunctionType{{}}, HasTable: true, }) err := compiler.compilePreamble() @@ -644,7 +644,7 @@ func TestCompiler_compileCallIndirect(t *testing.T) { }) t.Run("ok", func(t *testing.T) { - targetType := &wasm.FunctionType{ + targetType := wasm.FunctionType{ Results: []wasm.ValueType{wasm.ValueTypeI32}, ResultNumInUint64: 1, } @@ -669,7 +669,7 @@ func TestCompiler_compileCallIndirect(t *testing.T) { expectedReturnValue := uint32(i * 1000) compiler := env.requireNewCompiler(t, newCompiler, &wazeroir.CompilationResult{ - Signature: targetType, + Signature: &targetType, }) err := compiler.compilePreamble() require.NoError(t, err) @@ -704,7 +704,7 @@ func TestCompiler_compileCallIndirect(t *testing.T) { compiler := env.requireNewCompiler(t, newCompiler, &wazeroir.CompilationResult{ Signature: &wasm.FunctionType{}, - Types: []*wasm.FunctionType{targetType}, + Types: []wasm.FunctionType{targetType}, HasTable: true, }, ) @@ -754,8 +754,8 @@ func TestCompiler_callIndirect_largeTypeIndex(t *testing.T) { env.module().TypeIDs[typeIndex] = typeID env.module().Engine = &moduleEngine{functions: []function{}} - types := make([]*wasm.FunctionType, typeIndex+1) - types[typeIndex] = &wasm.FunctionType{} + types := make([]wasm.FunctionType, typeIndex+1) + types[typeIndex] = wasm.FunctionType{} me := env.moduleEngine() { // Compiling call target. @@ -807,7 +807,7 @@ func TestCompiler_compileCall(t *testing.T) { // Emit the call target function. const numCalls = 3 - targetFunctionType := &wasm.FunctionType{ + targetFunctionType := wasm.FunctionType{ Params: []wasm.ValueType{wasm.ValueTypeI32}, Results: []wasm.ValueType{wasm.ValueTypeI32}, ParamNumInUint64: 1, ResultNumInUint64: 1, @@ -817,7 +817,7 @@ func TestCompiler_compileCall(t *testing.T) { addTargetValue := uint32(100 + i) expectedValue += addTargetValue compiler := env.requireNewCompiler(t, newCompiler, &wazeroir.CompilationResult{ - Signature: targetFunctionType, + Signature: &targetFunctionType, }) err := compiler.compilePreamble() @@ -847,14 +847,14 @@ func TestCompiler_compileCall(t *testing.T) { moduleInstanceAddress: uintptr(unsafe.Pointer(env.moduleInstance)), }) env.module().Functions = append(env.module().Functions, - wasm.FunctionInstance{Type: targetFunctionType, Idx: index}) + wasm.FunctionInstance{Type: &targetFunctionType, Idx: index}) } // Now we start building the caller's code. compiler := env.requireNewCompiler(t, newCompiler, &wazeroir.CompilationResult{ Signature: &wasm.FunctionType{}, Functions: make([]uint32, numCalls), - Types: []*wasm.FunctionType{targetFunctionType}, + Types: []wasm.FunctionType{targetFunctionType}, }) err := compiler.compilePreamble() diff --git a/internal/engine/compiler/engine_test.go b/internal/engine/compiler/engine_test.go index bddfe3d5..87d72a02 100644 --- a/internal/engine/compiler/engine_test.go +++ b/internal/engine/compiler/engine_test.go @@ -169,7 +169,7 @@ func TestCompiler_CompileModule(t *testing.T) { e.setFinalizer = ff.setFinalizer okModule := &wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, FunctionSection: []wasm.Index{0, 0, 0, 0}, CodeSection: []*wasm.Code{ {Body: []byte{wasm.OpcodeEnd}}, @@ -199,7 +199,7 @@ func TestCompiler_CompileModule(t *testing.T) { t.Run("fail", func(t *testing.T) { errModule := &wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, FunctionSection: []wasm.Index{0, 0, 0}, CodeSection: []*wasm.Code{ {Body: []byte{wasm.OpcodeEnd}}, @@ -274,7 +274,7 @@ func TestCompiler_SliceAllocatedOnHeap(t *testing.T) { const callStackCorruption = "call_stack_corruption" const expectedReturnValue = 0x1 m := &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []wasm.ValueType{}, Results: []wasm.ValueType{wasm.ValueTypeI32}, ResultNumInUint64: 1}, {Params: []wasm.ValueType{}, Results: []wasm.ValueType{}}, }, diff --git a/internal/engine/compiler/impl_amd64.go b/internal/engine/compiler/impl_amd64.go index 54b3bcef..978db4f5 100644 --- a/internal/engine/compiler/impl_amd64.go +++ b/internal/engine/compiler/impl_amd64.go @@ -728,7 +728,7 @@ func (c *amd64Compiler) compileCall(o wazeroir.OperationCall) error { } target := c.ir.Functions[o.FunctionIndex] - targetType := c.ir.Types[target] + targetType := &c.ir.Types[target] targetAddressRegister, err := c.allocateRegister(registerTypeGeneralPurpose) if err != nil { @@ -826,7 +826,7 @@ func (c *amd64Compiler) compileCallIndirect(o wazeroir.OperationCallIndirect) er c.compileExitFromNativeCode(nativeCallStatusCodeTypeMismatchOnIndirectCall) c.assembler.SetJumpTargetOnNext(jumpIfTypeMatch) - targetFunctionType := c.ir.Types[o.TypeIndex] + targetFunctionType := &c.ir.Types[o.TypeIndex] if err = c.compileCallFunctionImpl(offset.register, targetFunctionType); err != nil { return nil } diff --git a/internal/engine/compiler/impl_amd64_test.go b/internal/engine/compiler/impl_amd64_test.go index d57b850c..818ddb5b 100644 --- a/internal/engine/compiler/impl_amd64_test.go +++ b/internal/engine/compiler/impl_amd64_test.go @@ -49,7 +49,7 @@ func TestAmd64Compiler_indirectCallWithTargetOnCallingConvReg(t *testing.T) { compiler := env.requireNewCompiler(t, newCompiler, &wazeroir.CompilationResult{ Signature: &wasm.FunctionType{}, - Types: []*wasm.FunctionType{{}}, + Types: []wasm.FunctionType{{}}, HasTable: true, }).(*amd64Compiler) err := compiler.compilePreamble() diff --git a/internal/engine/compiler/impl_arm64.go b/internal/engine/compiler/impl_arm64.go index 97381540..11b1012c 100644 --- a/internal/engine/compiler/impl_arm64.go +++ b/internal/engine/compiler/impl_arm64.go @@ -929,7 +929,7 @@ func (c *arm64Compiler) compileCall(o wazeroir.OperationCall) error { return err } - tp := c.ir.Types[c.ir.Functions[o.FunctionIndex]] + tp := &c.ir.Types[c.ir.Functions[o.FunctionIndex]] targetFunctionAddressReg, err := c.allocateRegister(registerTypeGeneralPurpose) if err != nil { @@ -1172,7 +1172,7 @@ func (c *arm64Compiler) compileCallIndirect(o wazeroir.OperationCallIndirect) (e c.assembler.SetJumpTargetOnNext(brIfTypeMatched) - targetFunctionType := c.ir.Types[o.TypeIndex] + targetFunctionType := &c.ir.Types[o.TypeIndex] if err := c.compileCallImpl(offsetReg, targetFunctionType); err != nil { return err } diff --git a/internal/engine/compiler/impl_arm64_test.go b/internal/engine/compiler/impl_arm64_test.go index e6b046c8..b1705333 100644 --- a/internal/engine/compiler/impl_arm64_test.go +++ b/internal/engine/compiler/impl_arm64_test.go @@ -46,7 +46,7 @@ func TestArm64Compiler_indirectCallWithTargetOnCallingConvReg(t *testing.T) { compiler := env.requireNewCompiler(t, newCompiler, &wazeroir.CompilationResult{ Signature: &wasm.FunctionType{}, - Types: []*wasm.FunctionType{{}}, + Types: []wasm.FunctionType{{}}, HasTable: true, }).(*arm64Compiler) err := compiler.compilePreamble() diff --git a/internal/engine/interpreter/interpreter_test.go b/internal/engine/interpreter/interpreter_test.go index 742ca9b9..d0bb640c 100644 --- a/internal/engine/interpreter/interpreter_test.go +++ b/internal/engine/interpreter/interpreter_test.go @@ -508,7 +508,7 @@ func TestInterpreter_Compile(t *testing.T) { e := et.NewEngine(api.CoreFeaturesV1).(*engine) errModule := &wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, FunctionSection: []wasm.Index{0, 0, 0}, CodeSection: []*wasm.Code{ {Body: []byte{wasm.OpcodeEnd}}, @@ -530,7 +530,7 @@ func TestInterpreter_Compile(t *testing.T) { e := et.NewEngine(api.CoreFeaturesV1).(*engine) okModule := &wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, FunctionSection: []wasm.Index{0, 0, 0, 0}, CodeSection: []*wasm.Code{ {Body: []byte{wasm.OpcodeEnd}}, diff --git a/internal/integration_test/bench/hostfunc_bench_test.go b/internal/integration_test/bench/hostfunc_bench_test.go index c01779a7..38a8e4ce 100644 --- a/internal/integration_test/bench/hostfunc_bench_test.go +++ b/internal/integration_test/bench/hostfunc_bench_test.go @@ -128,7 +128,7 @@ func getCallEngine(m *wasm.ModuleInstance, name string) (ce wasm.CallEngine, err func setupHostCallBench(requireNoError func(error)) *wasm.ModuleInstance { eng := compiler.NewEngine(context.Background(), api.CoreFeaturesV2, nil) - ft := &wasm.FunctionType{ + ft := wasm.FunctionType{ Params: []wasm.ValueType{wasm.ValueTypeI32}, Results: []wasm.ValueType{wasm.ValueTypeF32}, ParamNumInUint64: 1, ResultNumInUint64: 1, @@ -136,7 +136,7 @@ func setupHostCallBench(requireNoError func(error)) *wasm.ModuleInstance { // Build the host module. hostModule := &wasm.Module{ - TypeSection: []*wasm.FunctionType{ft}, + TypeSection: []wasm.FunctionType{ft}, FunctionSection: []wasm.Index{0, 0}, CodeSection: []*wasm.Code{ { @@ -182,7 +182,7 @@ func setupHostCallBench(requireNoError func(error)) *wasm.ModuleInstance { // Build the importing module. importingModule := &wasm.Module{ - TypeSection: []*wasm.FunctionType{ft}, + TypeSection: []wasm.FunctionType{ft}, ImportSection: []wasm.Import{ // Placeholders for imports from hostModule. {Type: wasm.ExternTypeFunc}, diff --git a/internal/integration_test/engine/adhoc_test.go b/internal/integration_test/engine/adhoc_test.go index a0849309..63fb9739 100644 --- a/internal/integration_test/engine/adhoc_test.go +++ b/internal/integration_test/engine/adhoc_test.go @@ -488,7 +488,7 @@ func callHostFunctionIndirect(t *testing.T, r wazero.Runtime) { const hostModule, importingWasmModule, originWasmModule = "host", "importing", "origin" const hostFn, importingWasmModuleFn, originModuleFn = "host_fn", "call_host_func", "origin" importingModule := &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []wasm.ValueType{}, Results: []wasm.ValueType{}}}, + TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{}, Results: []wasm.ValueType{}}}, ImportSection: []wasm.Import{{Module: hostModule, Name: hostFn, Type: wasm.ExternTypeFunc, DescFunc: 0}}, FunctionSection: []wasm.Index{0}, ExportSection: []wasm.Export{{Name: importingWasmModuleFn, Type: wasm.ExternTypeFunc, Index: 1}}, @@ -497,7 +497,7 @@ func callHostFunctionIndirect(t *testing.T, r wazero.Runtime) { } originModule := &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []wasm.ValueType{}, Results: []wasm.ValueType{}}}, + TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{}, Results: []wasm.ValueType{}}}, ImportSection: []wasm.Import{{Module: importingWasmModule, Name: importingWasmModuleFn, Type: wasm.ExternTypeFunc, DescFunc: 0}}, FunctionSection: []wasm.Index{0}, ExportSection: []wasm.Export{{Name: "origin", Type: wasm.ExternTypeFunc, Index: 1}}, @@ -539,7 +539,7 @@ func callHostFunctionIndirect(t *testing.T, r wazero.Runtime) { func callReturnImportWasm(t *testing.T, importedModule, importingModule string, vt wasm.ValueType) []byte { // test an imported function by re-exporting it module := &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []wasm.ValueType{vt}, Results: []wasm.ValueType{vt}}}, + TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{vt}, Results: []wasm.ValueType{vt}}}, // (import "%[2]s" "return_input" (func $return_input (param i32) (result i32))) ImportSection: []wasm.Import{ {Module: importedModule, Name: "return_input", Type: wasm.ExternTypeFunc, DescFunc: 0}, @@ -569,7 +569,7 @@ func callReturnImportWasm(t *testing.T, importedModule, importingModule string, func callOuterInnerWasm(t *testing.T, importedModule, importingModule string) []byte { module := &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}}}, + TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}}}, // (import "%[2]s" "outer" (func $outer (param i32) (result i32))) // (import "%[2]s" "inner" (func $inner (param i32) (result i32))) ImportSection: []wasm.Import{ @@ -758,7 +758,7 @@ func testMemOps(t *testing.T, r wazero.Runtime) { func testMultipleInstantiation(t *testing.T, r wazero.Runtime) { bin := binaryencoding.EncodeModule(&wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, FunctionSection: []wasm.Index{0}, MemorySection: &wasm.Memory{Min: 1, Cap: 1, Max: 1, IsMaxEncoded: true}, CodeSection: []*wasm.Code{{ diff --git a/internal/testing/binaryencoding/encoder_test.go b/internal/testing/binaryencoding/encoder_test.go index 3651c3f1..883742fe 100644 --- a/internal/testing/binaryencoding/encoder_test.go +++ b/internal/testing/binaryencoding/encoder_test.go @@ -35,7 +35,7 @@ func TestModule_Encode(t *testing.T) { { name: "type section", input: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {}, {Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}}, {Params: []wasm.ValueType{i32, i32, i32, i32}, Results: []wasm.ValueType{i32}}, @@ -52,7 +52,7 @@ func TestModule_Encode(t *testing.T) { { name: "type and import section", input: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}}, {Params: []wasm.ValueType{f32, f32}, Results: []wasm.ValueType{f32}}, }, @@ -84,7 +84,7 @@ func TestModule_Encode(t *testing.T) { { name: "type function and start section", input: &wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, ImportSection: []wasm.Import{{ Module: "", Name: "hello", Type: wasm.ExternTypeFunc, @@ -122,7 +122,7 @@ func TestModule_Encode(t *testing.T) { { name: "exported func with instructions", input: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}}, }, FunctionSection: []wasm.Index{0}, @@ -214,7 +214,7 @@ func TestModule_Encode_HostFunctionSection_Unsupported(t *testing.T) { captured := require.CapturePanic(func() { EncodeModule(&wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, CodeSection: []*wasm.Code{wasm.MustParseGoReflectFuncCode(fn)}, }) }) diff --git a/internal/testing/binaryencoding/section.go b/internal/testing/binaryencoding/section.go index 73bfcf20..2509c9db 100644 --- a/internal/testing/binaryencoding/section.go +++ b/internal/testing/binaryencoding/section.go @@ -16,9 +16,10 @@ func encodeSection(sectionID wasm.SectionID, contents []byte) []byte { // // See EncodeFunctionType // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#type-section%E2%91%A0 -func encodeTypeSection(types []*wasm.FunctionType) []byte { +func encodeTypeSection(types []wasm.FunctionType) []byte { contents := leb128.EncodeUint32(uint32(len(types))) - for _, t := range types { + for i := range types { + t := &types[i] contents = append(contents, EncodeFunctionType(t)...) } return encodeSection(wasm.SectionIDType, contents) diff --git a/internal/testing/enginetest/enginetest.go b/internal/testing/enginetest/enginetest.go index d170027f..c478f90d 100644 --- a/internal/testing/enginetest/enginetest.go +++ b/internal/testing/enginetest/enginetest.go @@ -40,7 +40,7 @@ var ( // testCtx is an arbitrary, non-default context. Non-nil also prevents linter errors. testCtx = context.WithValue(context.Background(), struct{}{}, "arbitrary") // v_v is a nullary function type (void -> void) - v_v = &wasm.FunctionType{} + v_v = wasm.FunctionType{} ) type EngineTester interface { @@ -81,7 +81,7 @@ func RunTestEngine_MemoryGrowInRecursiveCall(t *testing.T, et EngineTester) { require.NoError(t, err) m := &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []wasm.ValueType{}, Results: []wasm.ValueType{}}}, + TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{}, Results: []wasm.ValueType{}}}, FunctionSection: []wasm.Index{0, 0}, CodeSection: []*wasm.Code{ { @@ -144,7 +144,7 @@ func RunTestModuleEngine_Call(t *testing.T, et EngineTester) { // Define a basic function which defines two parameters and two results. // This is used to test results when incorrect arity is used. m := &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ { Params: []wasm.ValueType{i64, i64}, Results: []wasm.ValueType{i64, i64}, @@ -203,7 +203,7 @@ func RunTestModuleEngine_LookupFunction(t *testing.T, et EngineTester) { e := et.NewEngine(api.CoreFeaturesV1) mod := &wasm.Module{ - TypeSection: []*wasm.FunctionType{{}, {Params: []wasm.ValueType{wasm.ValueTypeV128}}}, + TypeSection: []wasm.FunctionType{{}, {Params: []wasm.ValueType{wasm.ValueTypeV128}}}, FunctionSection: []wasm.Index{0, 0, 0}, CodeSection: []*wasm.Code{ { @@ -487,7 +487,7 @@ func RunTestModuleEngine_Memory(t *testing.T, et EngineTester) { // Define a basic function which defines one parameter. This is used to test results when incorrect arity is used. one := uint32(1) m := &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}, ParamNumInUint64: 1}, v_v}, + TypeSection: []wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}, ParamNumInUint64: 1}, v_v}, FunctionSection: []wasm.Index{0, 1}, MemorySection: &wasm.Memory{Min: 1, Cap: 1, Max: 2}, DataSection: []wasm.DataSegment{ @@ -635,14 +635,14 @@ func readMemGo(_ context.Context, m api.Module) uint64 { var hostReadMemGo = wasm.MustParseGoReflectFuncCode(readMemGo) func setupCallTests(t *testing.T, e wasm.Engine, divBy *wasm.Code, fnlf experimental.FunctionListenerFactory) (*wasm.ModuleInstance, *wasm.ModuleInstance, *wasm.ModuleInstance, func()) { - ft := &wasm.FunctionType{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}, ParamNumInUint64: 1, ResultNumInUint64: 1} + ft := wasm.FunctionType{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}, ParamNumInUint64: 1, ResultNumInUint64: 1} divByName := divByWasmName if divBy.GoFunc != nil { divByName = divByGoName } hostModule := &wasm.Module{ - TypeSection: []*wasm.FunctionType{ft}, + TypeSection: []wasm.FunctionType{ft}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{divBy}, ExportSection: []wasm.Export{{Name: divByGoName, Type: wasm.ExternTypeFunc, Index: 0}}, @@ -667,7 +667,7 @@ func setupCallTests(t *testing.T, e wasm.Engine, divBy *wasm.Code, fnlf experime importedModule := &wasm.Module{ ImportSection: []wasm.Import{{}}, - TypeSection: []*wasm.FunctionType{ft}, + TypeSection: []wasm.FunctionType{ft}, FunctionSection: []wasm.Index{0, 0}, CodeSection: []*wasm.Code{ {Body: divByWasm}, @@ -705,7 +705,7 @@ func setupCallTests(t *testing.T, e wasm.Engine, divBy *wasm.Code, fnlf experime // To test stack traces, call the same function from another module importingModule := &wasm.Module{ - TypeSection: []*wasm.FunctionType{ft}, + TypeSection: []wasm.FunctionType{ft}, ImportSection: []wasm.Import{{}}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{ @@ -744,10 +744,10 @@ func setupCallTests(t *testing.T, e wasm.Engine, divBy *wasm.Code, fnlf experime } func setupCallMemTests(t *testing.T, e wasm.Engine, readMem *wasm.Code, fnlf experimental.FunctionListenerFactory) (*wasm.ModuleInstance, *wasm.ModuleInstance, func()) { - ft := &wasm.FunctionType{Results: []wasm.ValueType{i64}, ResultNumInUint64: 1} + ft := wasm.FunctionType{Results: []wasm.ValueType{i64}, ResultNumInUint64: 1} hostModule := &wasm.Module{ - TypeSection: []*wasm.FunctionType{ft}, + TypeSection: []wasm.FunctionType{ft}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{readMem}, ExportSection: []wasm.Export{ @@ -772,7 +772,7 @@ func setupCallMemTests(t *testing.T, e wasm.Engine, readMem *wasm.Code, fnlf exp linkModuleToEngine(host, hostME) importingModule := &wasm.Module{ - TypeSection: []*wasm.FunctionType{ft}, + TypeSection: []wasm.FunctionType{ft}, ImportSection: []wasm.Import{ // Placeholder for two import functions from `importedModule`. {Type: wasm.ExternTypeFunc, DescFunc: 0}, diff --git a/internal/testing/proxy/proxy.go b/internal/testing/proxy/proxy.go index 1b5db6d1..cfdb7d10 100644 --- a/internal/testing/proxy/proxy.go +++ b/internal/testing/proxy/proxy.go @@ -48,7 +48,7 @@ func NewModuleBinary(moduleName string, proxyTarget wazero.CompiledModule) []byt } var cnt wasm.Index for _, def := range funcDefs { - proxyModule.TypeSection = append(proxyModule.TypeSection, &wasm.FunctionType{ + proxyModule.TypeSection = append(proxyModule.TypeSection, wasm.FunctionType{ Params: def.ParamTypes(), Results: def.ResultTypes(), }) diff --git a/internal/wasm/binary/decoder_test.go b/internal/wasm/binary/decoder_test.go index 8e1439f9..4896b6ae 100644 --- a/internal/wasm/binary/decoder_test.go +++ b/internal/wasm/binary/decoder_test.go @@ -31,7 +31,7 @@ func TestDecodeModule(t *testing.T) { { name: "type section", input: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {}, {Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}}, {Params: []wasm.ValueType{i32, i32, i32, i32}, Results: []wasm.ValueType{i32}}, @@ -41,7 +41,7 @@ func TestDecodeModule(t *testing.T) { { name: "type and import section", input: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ {Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}}, {Params: []wasm.ValueType{f32, f32}, Results: []wasm.ValueType{f32}}, }, @@ -68,7 +68,7 @@ func TestDecodeModule(t *testing.T) { { name: "type function and start section", input: &wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, ImportSection: []wasm.Import{{ Module: "", Name: "hello", Type: wasm.ExternTypeFunc, @@ -86,8 +86,9 @@ func TestDecodeModule(t *testing.T) { m, e := DecodeModule(binaryencoding.EncodeModule(tc.input), api.CoreFeaturesV1, wasm.MemoryLimitPages, false, false, false) require.NoError(t, e) // Set the FunctionType keys on the input. - for _, f := range tc.input.TypeSection { - _ = f.String() + for i := range tc.input.TypeSection { + tp := &(tc.input.TypeSection)[i] + _ = tp.String() } require.Equal(t, tc.input, m) }) diff --git a/internal/wasm/binary/function.go b/internal/wasm/binary/function.go index e1665478..bb9e2b64 100644 --- a/internal/wasm/binary/function.go +++ b/internal/wasm/binary/function.go @@ -9,50 +9,48 @@ import ( "github.com/tetratelabs/wazero/internal/wasm" ) -func decodeFunctionType(enabledFeatures api.CoreFeatures, r *bytes.Reader) (*wasm.FunctionType, error) { +func decodeFunctionType(enabledFeatures api.CoreFeatures, r *bytes.Reader, ret *wasm.FunctionType) (err error) { b, err := r.ReadByte() if err != nil { - return nil, fmt.Errorf("read leading byte: %w", err) + return fmt.Errorf("read leading byte: %w", err) } if b != 0x60 { - return nil, fmt.Errorf("%w: %#x != 0x60", ErrInvalidByte, b) + return fmt.Errorf("%w: %#x != 0x60", ErrInvalidByte, b) } paramCount, _, err := leb128.DecodeUint32(r) if err != nil { - return nil, fmt.Errorf("could not read parameter count: %w", err) + return fmt.Errorf("could not read parameter count: %w", err) } paramTypes, err := decodeValueTypes(r, paramCount) if err != nil { - return nil, fmt.Errorf("could not read parameter types: %w", err) + return fmt.Errorf("could not read parameter types: %w", err) } resultCount, _, err := leb128.DecodeUint32(r) if err != nil { - return nil, fmt.Errorf("could not read result count: %w", err) + return fmt.Errorf("could not read result count: %w", err) } // Guard >1.0 feature multi-value if resultCount > 1 { if err = enabledFeatures.RequireEnabled(api.CoreFeatureMultiValue); err != nil { - return nil, fmt.Errorf("multiple result types invalid as %v", err) + return fmt.Errorf("multiple result types invalid as %v", err) } } resultTypes, err := decodeValueTypes(r, resultCount) if err != nil { - return nil, fmt.Errorf("could not read result types: %w", err) + return fmt.Errorf("could not read result types: %w", err) } - ret := &wasm.FunctionType{ - Params: paramTypes, - Results: resultTypes, - } + ret.Params = paramTypes + ret.Results = resultTypes // cache the key for the function type _ = ret.String() - return ret, nil + return nil } diff --git a/internal/wasm/binary/function_test.go b/internal/wasm/binary/function_test.go index 354ab75a..3c8ca3ea 100644 --- a/internal/wasm/binary/function_test.go +++ b/internal/wasm/binary/function_test.go @@ -15,62 +15,62 @@ func TestFunctionType(t *testing.T) { i32, i64, funcRef, externRef := wasm.ValueTypeI32, wasm.ValueTypeI64, wasm.ValueTypeFuncref, wasm.ValueTypeExternref tests := []struct { name string - input *wasm.FunctionType + input wasm.FunctionType expected []byte }{ { name: "empty", - input: &wasm.FunctionType{}, + input: wasm.FunctionType{}, expected: []byte{0x60, 0, 0}, }, { name: "one param no result", - input: &wasm.FunctionType{Params: []wasm.ValueType{i32}}, + input: wasm.FunctionType{Params: []wasm.ValueType{i32}}, expected: []byte{0x60, 1, i32, 0}, }, { name: "no param one result", - input: &wasm.FunctionType{Results: []wasm.ValueType{i32}}, + input: wasm.FunctionType{Results: []wasm.ValueType{i32}}, expected: []byte{0x60, 0, 1, i32}, }, { name: "one param one result", - input: &wasm.FunctionType{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}}, + input: wasm.FunctionType{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}}, expected: []byte{0x60, 1, i64, 1, i32}, }, { name: "two params no result", - input: &wasm.FunctionType{Params: []wasm.ValueType{i32, i64}}, + input: wasm.FunctionType{Params: []wasm.ValueType{i32, i64}}, expected: []byte{0x60, 2, i32, i64, 0}, }, { name: "two param one result", - input: &wasm.FunctionType{Params: []wasm.ValueType{i32, i64}, Results: []wasm.ValueType{i32}}, + input: wasm.FunctionType{Params: []wasm.ValueType{i32, i64}, Results: []wasm.ValueType{i32}}, expected: []byte{0x60, 2, i32, i64, 1, i32}, }, { name: "no param two results", - input: &wasm.FunctionType{Results: []wasm.ValueType{i32, i64}}, + input: wasm.FunctionType{Results: []wasm.ValueType{i32, i64}}, expected: []byte{0x60, 0, 2, i32, i64}, }, { name: "one param two results", - input: &wasm.FunctionType{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32, i64}}, + input: wasm.FunctionType{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32, i64}}, expected: []byte{0x60, 1, i64, 2, i32, i64}, }, { name: "two param two results", - input: &wasm.FunctionType{Params: []wasm.ValueType{i32, i64}, Results: []wasm.ValueType{i32, i64}}, + input: wasm.FunctionType{Params: []wasm.ValueType{i32, i64}, Results: []wasm.ValueType{i32, i64}}, expected: []byte{0x60, 2, i32, i64, 2, i32, i64}, }, { name: "two param two results with funcrefs", - input: &wasm.FunctionType{Params: []wasm.ValueType{i32, funcRef}, Results: []wasm.ValueType{funcRef, i64}}, + input: wasm.FunctionType{Params: []wasm.ValueType{i32, funcRef}, Results: []wasm.ValueType{funcRef, i64}}, expected: []byte{0x60, 2, i32, funcRef, 2, funcRef, i64}, }, { name: "two param two results with externrefs", - input: &wasm.FunctionType{Params: []wasm.ValueType{i32, externRef}, Results: []wasm.ValueType{externRef, i64}}, + input: wasm.FunctionType{Params: []wasm.ValueType{i32, externRef}, Results: []wasm.ValueType{externRef, i64}}, expected: []byte{0x60, 2, i32, externRef, 2, externRef, i64}, }, } @@ -78,17 +78,18 @@ func TestFunctionType(t *testing.T) { for _, tt := range tests { tc := tt - b := binaryencoding.EncodeFunctionType(tc.input) + b := binaryencoding.EncodeFunctionType(&tc.input) t.Run(fmt.Sprintf("encode - %s", tc.name), func(t *testing.T) { require.Equal(t, tc.expected, b) }) t.Run(fmt.Sprintf("decode - %s", tc.name), func(t *testing.T) { - binary, err := decodeFunctionType(api.CoreFeaturesV2, bytes.NewReader(b)) + var actual wasm.FunctionType + err := decodeFunctionType(api.CoreFeaturesV2, bytes.NewReader(b), &actual) require.NoError(t, err) // Set the FunctionType key on the input. _ = tc.input.String() - require.Equal(t, binary, tc.input) + require.Equal(t, actual, tc.input) }) } } @@ -137,7 +138,8 @@ func TestDecodeFunctionType_Errors(t *testing.T) { tc := tt t.Run(tc.name, func(t *testing.T) { - _, err := decodeFunctionType(api.CoreFeaturesV1, bytes.NewReader(tc.input)) + var actual wasm.FunctionType + err := decodeFunctionType(api.CoreFeaturesV1, bytes.NewReader(tc.input), &actual) require.EqualError(t, err, tc.expectedErr) }) } diff --git a/internal/wasm/binary/section.go b/internal/wasm/binary/section.go index 95ff8bfd..944117f2 100644 --- a/internal/wasm/binary/section.go +++ b/internal/wasm/binary/section.go @@ -10,15 +10,15 @@ import ( "github.com/tetratelabs/wazero/internal/wasm" ) -func decodeTypeSection(enabledFeatures api.CoreFeatures, r *bytes.Reader) ([]*wasm.FunctionType, error) { +func decodeTypeSection(enabledFeatures api.CoreFeatures, r *bytes.Reader) ([]wasm.FunctionType, error) { vs, _, err := leb128.DecodeUint32(r) if err != nil { return nil, fmt.Errorf("get size of vector: %w", err) } - result := make([]*wasm.FunctionType, vs) + result := make([]wasm.FunctionType, vs) for i := uint32(0); i < vs; i++ { - if result[i], err = decodeFunctionType(enabledFeatures, r); err != nil { + if err = decodeFunctionType(enabledFeatures, r, &result[i]); err != nil { return nil, fmt.Errorf("read %d-th type: %v", i, err) } } diff --git a/internal/wasm/counts_test.go b/internal/wasm/counts_test.go index be981777..7d4267b3 100644 --- a/internal/wasm/counts_test.go +++ b/internal/wasm/counts_test.go @@ -219,7 +219,7 @@ func TestModule_SectionElementCount(t *testing.T) { { name: "TypeSection", input: &Module{ - TypeSection: []*FunctionType{ + TypeSection: []FunctionType{ {}, {Params: []ValueType{i32, i32}, Results: []ValueType{i32}}, {Params: []ValueType{i32, i32, i32, i32}, Results: []ValueType{i32}}, @@ -230,7 +230,7 @@ func TestModule_SectionElementCount(t *testing.T) { { name: "TypeSection and ImportSection", input: &Module{ - TypeSection: []*FunctionType{ + TypeSection: []FunctionType{ {Params: []ValueType{i32, i32}, Results: []ValueType{i32}}, {Params: []ValueType{f32, f32}, Results: []ValueType{f32}}, }, @@ -251,7 +251,7 @@ func TestModule_SectionElementCount(t *testing.T) { { name: "TypeSection, FunctionSection, CodeSection, ExportSection and StartSection", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, FunctionSection: []Index{0}, CodeSection: []*Code{ {Body: []byte{OpcodeLocalGet, 0, OpcodeLocalGet, 1, OpcodeI32Add, OpcodeEnd}}, diff --git a/internal/wasm/func_validation.go b/internal/wasm/func_validation.go index 89cd683b..86d64cc8 100644 --- a/internal/wasm/func_validation.go +++ b/internal/wasm/func_validation.go @@ -63,11 +63,10 @@ func (m *Module) validateFunctionWithMaxStackValues( maxStackValues int, declaredFunctionIndexes map[Index]struct{}, ) error { - functionType := m.TypeSection[m.FunctionSection[idx]] + functionType := &m.TypeSection[m.FunctionSection[idx]] code := m.CodeSection[idx] body := code.Body localTypes := code.LocalTypes - types := m.TypeSection // We start with the outermost control block which is for function return if the code branches into it. controlBlockStack := []*controlBlock{{blockType: functionType}} @@ -539,7 +538,7 @@ func (m *Module) validateFunctionWithMaxStackValues( if int(index) >= len(functions) { return fmt.Errorf("invalid function index") } - funcType := types[functions[index]] + funcType := &m.TypeSection[functions[index]] for i := 0; i < len(funcType.Params); i++ { if err := valueTypeStack.popAndVerifyType(funcType.Params[len(funcType.Params)-1-i]); err != nil { return fmt.Errorf("type mismatch on %s operation param type: %v", OpcodeCallName, err) @@ -556,7 +555,7 @@ func (m *Module) validateFunctionWithMaxStackValues( } pc += num - if int(typeIndex) >= len(types) { + if int(typeIndex) >= len(m.TypeSection) { return fmt.Errorf("invalid type index at %s: %d", OpcodeCallIndirectName, typeIndex) } @@ -583,7 +582,7 @@ func (m *Module) validateFunctionWithMaxStackValues( if err = valueTypeStack.popAndVerifyType(ValueTypeI32); err != nil { return fmt.Errorf("cannot pop the offset in table for %s", OpcodeCallIndirectName) } - funcType := types[typeIndex] + funcType := &m.TypeSection[typeIndex] for i := 0; i < len(funcType.Params); i++ { if err = valueTypeStack.popAndVerifyType(funcType.Params[len(funcType.Params)-1-i]); err != nil { return fmt.Errorf("type mismatch on %s operation input type", OpcodeCallIndirectName) @@ -1392,7 +1391,7 @@ func (m *Module) validateFunctionWithMaxStackValues( } } else if op == OpcodeBlock { br.Reset(body[pc+1:]) - bt, num, err := DecodeBlockType(types, br, enabledFeatures) + bt, num, err := DecodeBlockType(m.TypeSection, br, enabledFeatures) if err != nil { return fmt.Errorf("read block: %w", err) } @@ -1412,7 +1411,7 @@ func (m *Module) validateFunctionWithMaxStackValues( pc += num } else if op == OpcodeLoop { br.Reset(body[pc+1:]) - bt, num, err := DecodeBlockType(types, br, enabledFeatures) + bt, num, err := DecodeBlockType(m.TypeSection, br, enabledFeatures) if err != nil { return fmt.Errorf("read block: %w", err) } @@ -1433,7 +1432,7 @@ func (m *Module) validateFunctionWithMaxStackValues( pc += num } else if op == OpcodeIf { br.Reset(body[pc+1:]) - bt, num, err := DecodeBlockType(types, br, enabledFeatures) + bt, num, err := DecodeBlockType(m.TypeSection, br, enabledFeatures) if err != nil { return fmt.Errorf("read block: %w", err) } @@ -1869,7 +1868,7 @@ type controlBlock struct { // // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-blocktype // See https://github.com/WebAssembly/spec/blob/wg-2.0.draft1/proposals/multi-value/Overview.md -func DecodeBlockType(types []*FunctionType, r *bytes.Reader, enabledFeatures api.CoreFeatures) (*FunctionType, uint64, error) { +func DecodeBlockType(types []FunctionType, r *bytes.Reader, enabledFeatures api.CoreFeatures) (*FunctionType, uint64, error) { raw, num, err := leb128.DecodeInt33AsInt64(r) if err != nil { return nil, 0, fmt.Errorf("decode int33: %w", err) @@ -1900,7 +1899,7 @@ func DecodeBlockType(types []*FunctionType, r *bytes.Reader, enabledFeatures api if raw < 0 || (raw >= int64(len(types))) { return nil, 0, fmt.Errorf("type index out of range: %d", raw) } - ret = types[raw] + ret = &types[raw] } return ret, num, err } diff --git a/internal/wasm/func_validation_test.go b/internal/wasm/func_validation_test.go index 6c9ca5f8..9234c3bc 100644 --- a/internal/wasm/func_validation_test.go +++ b/internal/wasm/func_validation_test.go @@ -29,7 +29,7 @@ func TestModule_ValidateFunction_validateFunctionWithMaxStackValues(t *testing.T body = append(body, OpcodeEnd) m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: body}}, } @@ -78,7 +78,7 @@ func TestModule_ValidateFunction_SignExtensionOps(t *testing.T) { t.Run(InstructionName(tc.input), func(t *testing.T) { t.Run("disabled", func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{tc.input}}}, } @@ -95,7 +95,7 @@ func TestModule_ValidateFunction_SignExtensionOps(t *testing.T) { } body = append(body, tc.input, 123, OpcodeDrop, OpcodeEnd) m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: body}}, } @@ -150,7 +150,7 @@ func TestModule_ValidateFunction_NonTrappingFloatToIntConversion(t *testing.T) { t.Run(InstructionName(tc.input), func(t *testing.T) { t.Run("disabled", func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{OpcodeMiscPrefix, tc.input}}}, } @@ -168,7 +168,7 @@ func TestModule_ValidateFunction_NonTrappingFloatToIntConversion(t *testing.T) { body = append(body, OpcodeMiscPrefix, tc.input, OpcodeDrop, OpcodeEnd) m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: body}}, } @@ -192,7 +192,7 @@ func TestModule_ValidateFunction_MultiValue(t *testing.T) { { name: "block with function type", module: &Module{ - TypeSection: []*FunctionType{v_f64f64}, + TypeSection: []FunctionType{v_f64f64}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBlock, 0, // (block (result f64 f64) @@ -210,7 +210,7 @@ func TestModule_ValidateFunction_MultiValue(t *testing.T) { { name: "if with function type", // a.k.a. "param" module: &Module{ - TypeSection: []*FunctionType{i32_i32}, // (func (param i32) (result i32) + TypeSection: []FunctionType{i32_i32}, // (func (param i32) (result i32) FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, // (i32.const 1) @@ -226,7 +226,7 @@ func TestModule_ValidateFunction_MultiValue(t *testing.T) { { name: "if with function type - br", // a.k.a. "params-break" module: &Module{ - TypeSection: []*FunctionType{ + TypeSection: []FunctionType{ i32_i32, // (func (param i32) (result i32) i32i32_i32, // (if (param i32 i32) (result i32) }, @@ -283,7 +283,7 @@ func TestModule_ValidateFunction_BulkMemoryOperations(t *testing.T) { c := uint32(0) m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: body}}, DataSection: []DataSegment{{}}, @@ -644,7 +644,7 @@ func TestModule_ValidateFunction_BulkMemoryOperations(t *testing.T) { tc := tt t.Run(tc.expectedErr, func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: tc.body}}, ElementSection: tc.elementSection, @@ -663,20 +663,20 @@ func TestModule_ValidateFunction_BulkMemoryOperations(t *testing.T) { var ( f32, f64, i32, i64, externref = ValueTypeF32, ValueTypeF64, ValueTypeI32, ValueTypeI64, ValueTypeExternref - f32i32_v = &FunctionType{Params: []ValueType{f32, i32}} - i32_i32 = &FunctionType{Params: []ValueType{i32}, Results: []ValueType{i32}} - i32f64_v = &FunctionType{Params: []ValueType{i32, f64}} - i32i32_i32 = &FunctionType{Params: []ValueType{i32, i32}, Results: []ValueType{i32}} - i32_v = &FunctionType{Params: []ValueType{i32}} - v_v = &FunctionType{} - v_f32 = &FunctionType{Results: []ValueType{f32}} - v_f32f32 = &FunctionType{Results: []ValueType{f32, f32}} - v_f64i32 = &FunctionType{Results: []ValueType{f64, i32}} - v_f64f64 = &FunctionType{Results: []ValueType{f64, f64}} - v_i32 = &FunctionType{Results: []ValueType{i32}} - v_i32i32 = &FunctionType{Results: []ValueType{i32, i32}} - v_i32i64 = &FunctionType{Results: []ValueType{i32, i64}} - v_i64i64 = &FunctionType{Results: []ValueType{i64, i64}} + f32i32_v = FunctionType{Params: []ValueType{f32, i32}} + i32_i32 = FunctionType{Params: []ValueType{i32}, Results: []ValueType{i32}} + i32f64_v = FunctionType{Params: []ValueType{i32, f64}} + i32i32_i32 = FunctionType{Params: []ValueType{i32, i32}, Results: []ValueType{i32}} + i32_v = FunctionType{Params: []ValueType{i32}} + v_v = FunctionType{} + v_f32 = FunctionType{Results: []ValueType{f32}} + v_f32f32 = FunctionType{Results: []ValueType{f32, f32}} + v_f64i32 = FunctionType{Results: []ValueType{f64, i32}} + v_f64f64 = FunctionType{Results: []ValueType{f64, f64}} + v_i32 = FunctionType{Results: []ValueType{i32}} + v_i32i32 = FunctionType{Results: []ValueType{i32, i32}} + v_i32i64 = FunctionType{Results: []ValueType{i32, i64}} + v_i64i64 = FunctionType{Results: []ValueType{i64, i64}} ) // TestModule_ValidateFunction_TypeMismatchSpecTests are "type mismatch" tests when "multi-value" was merged. @@ -694,7 +694,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { { name: `func.wast - type-empty-f64-i32`, module: &Module{ - TypeSection: []*FunctionType{v_f64i32}, + TypeSection: []FunctionType{v_f64i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{OpcodeEnd}}}, }, @@ -705,7 +705,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { { name: `func.wast - type-value-void-vs-nums`, module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{OpcodeNop, OpcodeEnd}}}, }, @@ -716,7 +716,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { { name: `func.wast - type-value-nums-vs-void`, module: &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{OpcodeI32Const, 0, OpcodeI64Const, 0, OpcodeEnd}}}, }, @@ -727,7 +727,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { { name: `func.wast - type-value-num-vs-nums - v_f32f32 -> f32`, module: &Module{ - TypeSection: []*FunctionType{v_f32f32}, + TypeSection: []FunctionType{v_f32f32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeF32Const, 0, 0, 0, 0, // (f32.const 0) @@ -741,7 +741,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { { name: `func.wast - type-value-num-vs-nums - v_f32 -> f32f32`, module: &Module{ - TypeSection: []*FunctionType{v_f32}, + TypeSection: []FunctionType{v_f32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeF32Const, 0, 0, 0, 0, OpcodeF32Const, 0, 0, 0, 0, // (f32.const 0) (f32.const 0) @@ -755,7 +755,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { { name: `func.wast - type-return-last-empty-vs-nums`, module: &Module{ - TypeSection: []*FunctionType{v_f32f32}, + TypeSection: []FunctionType{v_f32f32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{OpcodeReturn, OpcodeEnd}}}, }, @@ -766,7 +766,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { { name: `func.wast - type-return-last-void-vs-nums`, module: &Module{ - TypeSection: []*FunctionType{v_i32i64}, + TypeSection: []FunctionType{v_i32i64}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{OpcodeNop, OpcodeReturn, OpcodeEnd}}}, // (return (nop)) }, @@ -777,7 +777,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { { name: `func.wast - type-return-last-num-vs-nums`, module: &Module{ - TypeSection: []*FunctionType{v_i64i64}, + TypeSection: []FunctionType{v_i64i64}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI64Const, 0, OpcodeReturn, // (return (i64.const 0)) @@ -795,7 +795,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (return) (i32.const 1) (i32.const 2) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeReturn, OpcodeI32Const, 1, OpcodeI32Const, 2, @@ -813,7 +813,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (i32.const 1) (return) (i32.const 2) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeReturn, OpcodeI32Const, 2, @@ -831,7 +831,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (return (nop)) (i32.const 1) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeNop, OpcodeReturn, // (return (nop)) @@ -847,7 +847,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { { name: `func.wast - type-return-num-vs-nums`, module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI64Const, 1, OpcodeReturn, // (return (i64.const 1)) @@ -866,7 +866,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (return (i32.const 1)) (return (i32.const 1) (i32.const 2)) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI64Const, 1, OpcodeReturn, // (return (i64.const 1)) @@ -881,7 +881,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { { name: `func.wast - type-break-last-num-vs-nums`, module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 0, OpcodeBr, 0, // (br 0 (i32.const 0)) @@ -899,7 +899,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (br 0) (i32.const 1) (i32.const 2) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBr, 0, // (br 0) @@ -918,7 +918,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (br 0 (i32.const 1)) (i32.const 1) (i32.const 2) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeBr, 0, // (br 0 (i32.const 1)) @@ -937,7 +937,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (block (br 1)) (br 0 (i32.const 1) (i32.const 2)) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBlock, 0x40, OpcodeBr, 0x01, OpcodeEnd, // (block (br 1)) @@ -956,7 +956,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (block (br 1 (nop))) (br 0 (i32.const 1) (i32.const 2)) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBlock, 0x40, OpcodeNop, OpcodeBr, 0x01, OpcodeEnd, // (block (br 1 (nop))) @@ -975,7 +975,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (block (result i32) (br 1 (i32.const 1))) (br 0 (i32.const 1) (i32.const 2)) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBlock, 0x7f, OpcodeI32Const, 1, OpcodeBr, 1, OpcodeEnd, // (block (result i32) (br 1 (i32.const 1))) @@ -997,7 +997,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (func (i32.const 1) (if (type $sig) (i32.const 0) (then))) // ) module: &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, // (i32.const 1) @@ -1017,7 +1017,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (i32.const 1) (then (i32.const 1) (i32.const 2))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x40, // (if (i32.const 1) @@ -1037,7 +1037,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (i32.const 1) (then (i32.const 1) (i32.const 2)) (else)) // )) module: &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x40, // (if (i32.const 1) @@ -1058,7 +1058,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (i32.const 1) (then) (else (i32.const 1) (i32.const 2))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x40, // (if (i32.const 1) (then) @@ -1078,7 +1078,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (i32.const 1) (then (i32.const 1) (i32.const 2)) (else (i32.const 2) (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x40, // (if (i32.const 1) @@ -1099,7 +1099,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then) (else (i32.const 0) (i32.const 2))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, v_i32i32}, + TypeSection: []FunctionType{v_v, v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x01, // (if (result i32 i32) (i32.const 1) (then) @@ -1119,7 +1119,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (i32.const 0) (i32.const 1)) (else)) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, v_i32i32}, + TypeSection: []FunctionType{v_v, v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x01, // (if (result i32 i32) (i32.const 1) @@ -1140,7 +1140,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then) (else)) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, v_i32i32}, + TypeSection: []FunctionType{v_v, v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x01, // (if (result i32 i32) (i32.const 1) (then) @@ -1160,7 +1160,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (i32.const 1) (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, v_i32i32}, + TypeSection: []FunctionType{v_v, v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x01, // (if (result i32 i32) (i32.const 1) @@ -1180,7 +1180,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (nop)) (else (i32.const 0) (i32.const 0))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, v_i32i32}, + TypeSection: []FunctionType{v_v, v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x01, // (if (result i32 i32) (i32.const 1) @@ -1201,7 +1201,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (i32.const 0) (i32.const 0)) (else (nop))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1222,7 +1222,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (nop)) (else (nop))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1243,7 +1243,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (i32.const 1)) (else (i32.const 1) (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1264,7 +1264,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (i32.const 1) (i32.const 1)) (else (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1285,7 +1285,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (i32.const 1)) (else (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1307,7 +1307,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (i32.const 1)) (else (i32.const 1) (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 0, // (i32.const 0) - NOTE: this is outside the (if) @@ -1330,7 +1330,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (i32.const 1) (i32.const 1)) (else (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 0, // (i32.const 0) - NOTE: this is outside the (if) @@ -1353,7 +1353,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (i32.const 1)) (else (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 0, // (i32.const 0) - NOTE: this is outside the (if) @@ -1375,7 +1375,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32) (i32.const 1) (then (i32.const 1) (i32.const 1)) (else (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32}, + TypeSection: []FunctionType{v_i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32) (i32.const 1) @@ -1396,7 +1396,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32) (i32.const 1) (then (i32.const 1)) (else (i32.const 1) (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32}, + TypeSection: []FunctionType{v_i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32) (i32.const 1) @@ -1417,7 +1417,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32) (i32.const 1) (then (i32.const 1) (i32.const 1)) (else (i32.const 1) (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32}, + TypeSection: []FunctionType{v_i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32) (i32.const 1) @@ -1438,7 +1438,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (i32.const 1) (i32.const 1) (i32.const 1)) (else (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1459,7 +1459,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (br 0)) (else (i32.const 1) (i32.const 1))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1480,7 +1480,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (result i32 i32) (i32.const 1) (then (i32.const 1) (i32.const 1)) (else (br 0))) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1504,7 +1504,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // ) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1529,7 +1529,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // ) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1554,7 +1554,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // ) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1579,7 +1579,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // ) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1604,7 +1604,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // ) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1629,7 +1629,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // ) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x00, // (if (result i32 i32) (i32.const 1) @@ -1655,7 +1655,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // ) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, // (i32.const 1) @@ -1682,7 +1682,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // ) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, // (i32.const 1) @@ -1705,7 +1705,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (param i32) (i32.const 1) (then (drop))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, i32_v}, + TypeSection: []FunctionType{v_v, i32_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x01, // (if (param i32) (i32.const 1) @@ -1725,7 +1725,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (if (param i32 f64) (i32.const 1) (then (drop) (drop))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, i32f64_v}, + TypeSection: []FunctionType{v_v, i32f64_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, OpcodeIf, 0x01, // (if (param i32 f64) (i32.const 1) @@ -1745,7 +1745,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (f32.const 0) (if (param i32) (i32.const 1) (then (drop))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, i32_v}, + TypeSection: []FunctionType{v_v, i32_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeF32Const, 0, 0, 0, 0, // (f32.const 0) @@ -1764,7 +1764,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (f32.const 0) (if (param f32 i32) (i32.const 1) (then (drop) (drop))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, f32i32_v}, + TypeSection: []FunctionType{v_v, f32i32_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeF32Const, 0, 0, 0, 0, // (f32.const 0) @@ -1785,7 +1785,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (block (if (param i32) (i32.const 1) (then (drop)))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, i32_v}, + TypeSection: []FunctionType{v_v, i32_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBlock, 0x40, // (block @@ -1807,7 +1807,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (block (if (param i32 f64) (i32.const 1) (then (drop) (drop)))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, i32f64_v}, + TypeSection: []FunctionType{v_v, i32f64_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBlock, 0x40, // (block @@ -1829,7 +1829,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (block (f32.const 0) (if (param i32) (i32.const 1) (then (drop)))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, i32_v}, + TypeSection: []FunctionType{v_v, i32_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBlock, 0x40, // (block @@ -1850,7 +1850,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (block (f32.const 0) (if (param f32 i32) (i32.const 1) (then (drop) (drop)))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, f32i32_v}, + TypeSection: []FunctionType{v_v, f32i32_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBlock, 0x40, // (block @@ -1876,7 +1876,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (func (loop (type $sig) (i32.const 0))) // ) module: &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeLoop, 0, OpcodeI32Const, 0, // (loop (type $sig) (i32.const 0)) @@ -1895,7 +1895,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (loop (i32.const 1) (i32.const 2)) // )) module: &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeLoop, 0x40, OpcodeI32Const, 1, OpcodeI32Const, 2, // (loop (i32.const 1) (i32.const 2)) @@ -1914,7 +1914,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (loop (result i32 i32)) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeLoop, 0x0, // (loop (result i32 i32)) - matches existing func type @@ -1933,7 +1933,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (loop (result i32 i32) (nop)) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeLoop, 0x0, // (loop (result i32 i32) - matches existing func type @@ -1953,7 +1953,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (loop (result i32 i32) (i32.const 0)) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeLoop, 0x0, // (loop (result i32 i32) - matches existing func type @@ -1973,7 +1973,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (i32.const 1) (loop (result i32 i32) (i32.const 2)) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32i32}, + TypeSection: []FunctionType{v_i32i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, // (i32.const 1) - NOTE: outside the loop! @@ -1994,7 +1994,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (loop (result i32) (i32.const 1) (i32.const 2)) // )) module: &Module{ - TypeSection: []*FunctionType{v_i32}, + TypeSection: []FunctionType{v_i32}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeLoop, 0x0, // (loop (result i32) - matches existing func type @@ -2014,7 +2014,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (loop (param i32) (drop)) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, i32_v}, + TypeSection: []FunctionType{v_v, i32_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeLoop, 0x1, // (loop (param i32) @@ -2034,7 +2034,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (loop (param i32 f64) (drop) (drop)) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, i32f64_v}, + TypeSection: []FunctionType{v_v, i32f64_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeLoop, 0x1, // (loop (param i32 f64) @@ -2055,7 +2055,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (f32.const 0) (loop (param i32) (drop)) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, i32_v}, + TypeSection: []FunctionType{v_v, i32_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeF32Const, 0, 0, 0, 0, // (f32.const 0) @@ -2074,7 +2074,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (f32.const 0) (loop (param f32 i32) (drop) (drop)) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, f32i32_v}, + TypeSection: []FunctionType{v_v, f32i32_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeF32Const, 0, 0, 0, 0, // (f32.const 0) @@ -2095,7 +2095,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (block (loop (param i32) (drop))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, i32_v}, + TypeSection: []FunctionType{v_v, i32_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBlock, 0x40, // (block @@ -2117,7 +2117,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (block (loop (param i32 f64) (drop) (drop))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, i32f64_v}, + TypeSection: []FunctionType{v_v, i32f64_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBlock, 0x40, // (block @@ -2139,7 +2139,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (block (f32.const 0) (loop (param i32) (drop))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, i32_v}, + TypeSection: []FunctionType{v_v, i32_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBlock, 0x40, // (block @@ -2160,7 +2160,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { // (block (f32.const 0) (loop (param f32 i32) (drop) (drop))) // )) module: &Module{ - TypeSection: []*FunctionType{v_v, f32i32_v}, + TypeSection: []FunctionType{v_v, f32i32_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeBlock, 0x40, // (block @@ -2191,7 +2191,7 @@ func TestModule_ValidateFunction_MultiValue_TypeMismatch(t *testing.T) { func TestModule_funcValidation_CallIndirect(t *testing.T) { t.Run("ok", func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, @@ -2204,7 +2204,7 @@ func TestModule_funcValidation_CallIndirect(t *testing.T) { }) t.Run("non zero table index", func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, @@ -2223,7 +2223,7 @@ func TestModule_funcValidation_CallIndirect(t *testing.T) { }) t.Run("non funcref table", func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{ OpcodeI32Const, 1, @@ -2322,7 +2322,7 @@ func TestModule_funcValidation_RefTypes(t *testing.T) { tc := tt t.Run(tc.name, func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: tc.body}}, } @@ -2490,7 +2490,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) { tc := tt t.Run(tc.name, func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: tc.body}}, } @@ -2602,7 +2602,7 @@ func TestModule_funcValidation_TableGetSet(t *testing.T) { tc := tt t.Run(tc.name, func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: tc.body}}, } @@ -2659,7 +2659,7 @@ func TestModule_funcValidation_Select_error(t *testing.T) { tc := tt t.Run(tc.name, func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: tc.body}}, } @@ -3144,7 +3144,7 @@ func TestModule_funcValidation_SIMD(t *testing.T) { tc := tt t.Run(tc.name, func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: tc.body}}, } @@ -3289,7 +3289,7 @@ func TestModule_funcValidation_SIMD_error(t *testing.T) { tc := tt t.Run(tc.name, func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: tc.body}}, } @@ -3334,14 +3334,15 @@ func TestDecodeBlockType(t *testing.T) { } }) t.Run("function type", func(t *testing.T) { - types := []*FunctionType{ + types := []FunctionType{ {}, {Params: []ValueType{ValueTypeI32}}, {Results: []ValueType{ValueTypeI32}}, {Params: []ValueType{ValueTypeF32, ValueTypeV128}, Results: []ValueType{ValueTypeI32}}, {Params: []ValueType{ValueTypeF32, ValueTypeV128}, Results: []ValueType{ValueTypeI32, ValueTypeF32, ValueTypeV128}}, } - for index, expected := range types { + for index := range types { + expected := &types[index] actual, read, err := DecodeBlockType(types, bytes.NewReader([]byte{byte(index)}), api.CoreFeatureMultiValue) require.NoError(t, err) require.Equal(t, uint64(1), read) @@ -3354,8 +3355,8 @@ func TestDecodeBlockType(t *testing.T) { // original function type during the function validation with the presence of unreachable br_table // targeting the function return label. func TestFuncValidation_UnreachableBrTable_NotModifyTypes(t *testing.T) { - funcType := &FunctionType{Results: []ValueType{i32, i64}, Params: []ValueType{i32}} - copiedFuncType := &FunctionType{ + funcType := FunctionType{Results: []ValueType{i32, i64}, Params: []ValueType{i32}} + copiedFuncType := FunctionType{ Params: make([]ValueType, len(funcType.Params)), Results: make([]ValueType, len(funcType.Results)), } @@ -3370,7 +3371,7 @@ func TestFuncValidation_UnreachableBrTable_NotModifyTypes(t *testing.T) { { name: "on function return", m: &Module{ - TypeSection: []*FunctionType{funcType}, + TypeSection: []FunctionType{funcType}, FunctionSection: []Index{0}, CodeSection: []*Code{ {Body: []byte{ @@ -3388,7 +3389,7 @@ func TestFuncValidation_UnreachableBrTable_NotModifyTypes(t *testing.T) { { name: "on loop return", m: &Module{ - TypeSection: []*FunctionType{funcType}, + TypeSection: []FunctionType{funcType}, FunctionSection: []Index{0}, CodeSection: []*Code{ {Body: []byte{ @@ -3526,7 +3527,7 @@ func TestModule_funcValidation_loopWithParams(t *testing.T) { tc := tt t.Run(tc.name, func(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{ + TypeSection: []FunctionType{ v_i32, i32_v, }, @@ -3546,7 +3547,7 @@ func TestModule_funcValidation_loopWithParams(t *testing.T) { // TestFunctionValidation_redundantEnd is found in th validation fuzzing #879. func TestFunctionValidation_redundantEnd(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{OpcodeEnd, OpcodeEnd}}}, } @@ -3557,7 +3558,7 @@ func TestFunctionValidation_redundantEnd(t *testing.T) { // TestFunctionValidation_redundantEnd is found in th validation fuzzing. func TestFunctionValidation_redundantElse(t *testing.T) { m := &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{OpcodeEnd, OpcodeElse}}}, } diff --git a/internal/wasm/function_definition.go b/internal/wasm/function_definition.go index 3cf4646e..52be3827 100644 --- a/internal/wasm/function_definition.go +++ b/internal/wasm/function_definition.go @@ -63,7 +63,7 @@ func (m *Module) BuildFunctionDefinitions() { def := &m.FunctionDefinitionSection[importFuncIdx] def.importDesc = imp def.index = importFuncIdx - def.funcType = m.TypeSection[imp.DescFunc] + def.funcType = &m.TypeSection[imp.DescFunc] importFuncIdx++ } @@ -72,7 +72,7 @@ func (m *Module) BuildFunctionDefinitions() { idx := importFuncIdx + Index(codeIndex) def := &m.FunctionDefinitionSection[idx] def.index = idx - def.funcType = m.TypeSection[typeIndex] + def.funcType = &m.TypeSection[typeIndex] def.goFunc = code.GoFunc } diff --git a/internal/wasm/function_definition_test.go b/internal/wasm/function_definition_test.go index 04456083..336b66c6 100644 --- a/internal/wasm/function_definition_test.go +++ b/internal/wasm/function_definition_test.go @@ -38,7 +38,7 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) { { name: "host func go", m: &Module{ - TypeSection: []*FunctionType{i32_i32}, + TypeSection: []FunctionType{i32_i32}, FunctionSection: []Index{0}, CodeSection: []*Code{MustParseGoReflectFuncCode(fn)}, NameSection: &NameSection{ @@ -55,7 +55,7 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) { moduleName: "m", debugName: "m.fn", goFunc: MustParseGoReflectFuncCode(fn).GoFunc, - funcType: i32_i32, + funcType: &i32_i32, paramNames: []string{"x"}, resultNames: []string{"y"}, }, @@ -78,7 +78,7 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) { {Body: []byte{OpcodeEnd}}, {Body: []byte{OpcodeEnd}}, }, - TypeSection: []*FunctionType{ + TypeSection: []FunctionType{ v_v, {Params: []ValueType{ValueTypeF64, ValueTypeI32}, Results: []ValueType{ValueTypeV128, ValueTypeI64}}, {Params: []ValueType{ValueTypeF64, ValueTypeF32}, Results: []ValueType{ValueTypeI64}}, @@ -101,7 +101,7 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) { index: 2, debugName: ".$2", exportNames: []string{"function_index=2"}, - funcType: v_v, + funcType: &v_v, }, }, expectedExports: map[string]api.FunctionDefinition{ @@ -121,7 +121,7 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) { index: 2, debugName: ".$2", exportNames: []string{"function_index=2"}, - funcType: v_v, + funcType: &v_v, }, }, }, @@ -136,7 +136,7 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) { }, FunctionSection: []Index{1, 0}, CodeSection: []*Code{{Body: []byte{OpcodeEnd}}, {Body: []byte{OpcodeEnd}}}, - TypeSection: []*FunctionType{ + TypeSection: []FunctionType{ v_v, {Params: []ValueType{ValueTypeF64, ValueTypeI32}, Results: []ValueType{ValueTypeV128, ValueTypeI64}}, {Params: []ValueType{ValueTypeF64, ValueTypeF32}, Results: []ValueType{ValueTypeI64}}, @@ -160,7 +160,7 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) { index: 2, debugName: ".$2", exportNames: []string{"function_index=2"}, - funcType: v_v, + funcType: &v_v, }, }, expectedImports: []api.FunctionDefinition{ @@ -190,14 +190,14 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) { index: 2, debugName: ".$2", exportNames: []string{"function_index=2"}, - funcType: v_v, + funcType: &v_v, }, }, }, { name: "with names", m: &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, ImportSection: []Import{{Module: "i", Name: "f", Type: ExternTypeFunc}}, NameSection: &NameSection{ ModuleName: "module", @@ -211,15 +211,15 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) { CodeSection: []*Code{nopCode, nopCode, nopCode, nopCode, nopCode}, }, expected: []FunctionDefinition{ - {moduleName: "module", index: 0, debugName: "module.$0", importDesc: &Import{Module: "i", Name: "f"}, funcType: v_v}, - {moduleName: "module", index: 1, debugName: "module.$1", funcType: v_v}, - {moduleName: "module", index: 2, debugName: "module.two", funcType: v_v, name: "two"}, - {moduleName: "module", index: 3, debugName: "module.$3", funcType: v_v}, - {moduleName: "module", index: 4, debugName: "module.four", funcType: v_v, name: "four"}, - {moduleName: "module", index: 5, debugName: "module.five", funcType: v_v, name: "five"}, + {moduleName: "module", index: 0, debugName: "module.$0", importDesc: &Import{Module: "i", Name: "f"}, funcType: &v_v}, + {moduleName: "module", index: 1, debugName: "module.$1", funcType: &v_v}, + {moduleName: "module", index: 2, debugName: "module.two", funcType: &v_v, name: "two"}, + {moduleName: "module", index: 3, debugName: "module.$3", funcType: &v_v}, + {moduleName: "module", index: 4, debugName: "module.four", funcType: &v_v, name: "four"}, + {moduleName: "module", index: 5, debugName: "module.five", funcType: &v_v, name: "five"}, }, expectedImports: []api.FunctionDefinition{ - &FunctionDefinition{moduleName: "module", index: 0, debugName: "module.$0", importDesc: &Import{Module: "i", Name: "f"}, funcType: v_v}, + &FunctionDefinition{moduleName: "module", index: 0, debugName: "module.$0", importDesc: &Import{Module: "i", Name: "f"}, funcType: &v_v}, }, expectedExports: map[string]api.FunctionDefinition{}, }, diff --git a/internal/wasm/host.go b/internal/wasm/host.go index 311982fd..6b657852 100644 --- a/internal/wasm/host.go +++ b/internal/wasm/host.go @@ -213,14 +213,14 @@ func (m *Module) maybeAddType(params, results []ValueType, enabledFeatures api.C return 0, fmt.Errorf("multiple result types invalid as %v", err) } } - for i, t := range m.TypeSection { + for i := range m.TypeSection { + t := &m.TypeSection[i] if t.EqualsSignature(params, results) { return Index(i), nil } } result := m.SectionElementCount(SectionIDType) - toAdd := &FunctionType{Params: params, Results: results} - m.TypeSection = append(m.TypeSection, toAdd) + m.TypeSection = append(m.TypeSection, FunctionType{Params: params, Results: results}) return result, nil } diff --git a/internal/wasm/host_test.go b/internal/wasm/host_test.go index dc86a5ac..33c0750f 100644 --- a/internal/wasm/host_test.go +++ b/internal/wasm/host_test.go @@ -59,7 +59,7 @@ func TestNewHostModule(t *testing.T) { }, }, expected: &Module{ - TypeSection: []*FunctionType{ + TypeSection: []FunctionType{ {Params: []ValueType{i32, i32}, Results: []ValueType{i32}}, {Params: []ValueType{i32, i32, i32, i32}, Results: []ValueType{i32}}, }, @@ -102,7 +102,7 @@ func TestNewHostModule(t *testing.T) { }, funcToNames: map[string]*HostFuncNames{swapName: {}}, expected: &Module{ - TypeSection: []*FunctionType{{Params: []ValueType{i32, i32}, Results: []ValueType{i32, i32}}}, + TypeSection: []FunctionType{{Params: []ValueType{i32, i32}, Results: []ValueType{i32, i32}}}, FunctionSection: []Index{0}, CodeSection: []*Code{MustParseGoReflectFuncCode(swap)}, ExportSection: []Export{{Name: "swap", Type: ExternTypeFunc, Index: 0}}, diff --git a/internal/wasm/module.go b/internal/wasm/module.go index 9d4bea7f..d09d028f 100644 --- a/internal/wasm/module.go +++ b/internal/wasm/module.go @@ -48,7 +48,7 @@ type Module struct { // Note: In the Binary Format, this is SectionIDType. // // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#types%E2%91%A0%E2%91%A0 - TypeSection []*FunctionType + TypeSection []FunctionType // ImportSection contains imported functions, tables, memories or globals required for instantiation // (Store.Instantiate). @@ -227,7 +227,7 @@ func (m *Module) TypeOfFunction(funcIdx Index) *FunctionType { if imp.DescFunc >= typeSectionLength { return nil } - return m.TypeSection[imp.DescFunc] + return &m.TypeSection[imp.DescFunc] } funcImportCount++ } @@ -240,11 +240,12 @@ func (m *Module) TypeOfFunction(funcIdx Index) *FunctionType { if typeIdx >= typeSectionLength { return nil } - return m.TypeSection[typeIdx] + return &m.TypeSection[typeIdx] } func (m *Module) Validate(enabledFeatures api.CoreFeatures) error { - for _, tp := range m.TypeSection { + for i := range m.TypeSection { + tp := &m.TypeSection[i] tp.CacheNumInUint64() } diff --git a/internal/wasm/module_test.go b/internal/wasm/module_test.go index 3b1f2282..fd68fd3c 100644 --- a/internal/wasm/module_test.go +++ b/internal/wasm/module_test.go @@ -360,14 +360,14 @@ func TestModule_validateStartSection(t *testing.T) { }) t.Run("invalid type", func(t *testing.T) { - for _, ft := range []*FunctionType{ + for _, ft := range []FunctionType{ {Params: []ValueType{ValueTypeI32}}, {Results: []ValueType{ValueTypeI32}}, {Params: []ValueType{ValueTypeI32}, Results: []ValueType{ValueTypeI32}}, } { t.Run(ft.String(), func(t *testing.T) { index := uint32(0) - m := Module{StartSection: &index, FunctionSection: []uint32{0}, TypeSection: []*FunctionType{ft}} + m := Module{StartSection: &index, FunctionSection: []uint32{0}, TypeSection: []FunctionType{ft}} err := m.validateStartSection() require.Error(t, err) }) @@ -377,7 +377,7 @@ func TestModule_validateStartSection(t *testing.T) { index := Index(1) m := Module{ StartSection: &index, - TypeSection: []*FunctionType{{}, {Results: []ValueType{ValueTypeI32}}}, + TypeSection: []FunctionType{{}, {Results: []ValueType{ValueTypeI32}}}, ImportSection: []Import{ {Type: ExternTypeFunc, DescFunc: 1}, // import with index 1 is global but this should be skipped when searching imported functions. @@ -453,7 +453,7 @@ func TestModule_validateGlobals(t *testing.T) { func TestModule_validateFunctions(t *testing.T) { t.Run("ok", func(t *testing.T) { m := Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []uint32{0}, CodeSection: []*Code{{Body: []byte{OpcodeI32Const, 0, OpcodeDrop, OpcodeEnd}}}, } @@ -468,7 +468,7 @@ func TestModule_validateFunctions(t *testing.T) { }) t.Run("function, but no code", func(t *testing.T) { m := Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: nil, } @@ -478,7 +478,7 @@ func TestModule_validateFunctions(t *testing.T) { }) t.Run("function out of range of code", func(t *testing.T) { m := Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{1}, CodeSection: []*Code{{Body: []byte{OpcodeEnd}}}, } @@ -488,7 +488,7 @@ func TestModule_validateFunctions(t *testing.T) { }) t.Run("invalid", func(t *testing.T) { m := Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{OpcodeF32Abs}}}, } @@ -498,7 +498,7 @@ func TestModule_validateFunctions(t *testing.T) { }) t.Run("in- exported", func(t *testing.T) { m := Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{OpcodeF32Abs}}}, ExportSection: []Export{{Name: "f1", Type: ExternTypeFunc, Index: 0}}, @@ -509,7 +509,7 @@ func TestModule_validateFunctions(t *testing.T) { }) t.Run("in- exported after import", func(t *testing.T) { m := Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, ImportSection: []Import{{Type: ExternTypeFunc}}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{OpcodeF32Abs}}}, @@ -521,7 +521,7 @@ func TestModule_validateFunctions(t *testing.T) { }) t.Run("in- exported twice", func(t *testing.T) { m := Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []Index{0}, CodeSection: []*Code{{Body: []byte{OpcodeF32Abs}}}, ExportSection: []Export{ @@ -811,17 +811,17 @@ func TestModule_buildGlobals(t *testing.T) { func TestModule_buildFunctions(t *testing.T) { nopCode := &Code{Body: []byte{OpcodeEnd}} m := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, ImportSection: []Import{{Type: ExternTypeFunc}}, FunctionSection: []Index{0, 0, 0, 0, 0}, CodeSection: []*Code{nopCode, nopCode, nopCode, nopCode, nopCode}, FunctionDefinitionSection: []FunctionDefinition{ - {index: 0, funcType: v_v}, - {index: 1, funcType: v_v}, - {index: 2, funcType: v_v, name: "two"}, - {index: 3, funcType: v_v}, - {index: 4, funcType: v_v, name: "four"}, - {index: 5, funcType: v_v, name: "five"}, + {index: 0, funcType: &v_v}, + {index: 1, funcType: &v_v}, + {index: 2, funcType: &v_v, name: "two"}, + {index: 3, funcType: &v_v}, + {index: 4, funcType: &v_v, name: "four"}, + {index: 5, funcType: &v_v, name: "five"}, }, } diff --git a/internal/wasm/store.go b/internal/wasm/store.go index 2174d293..f7d61c19 100644 --- a/internal/wasm/store.go +++ b/internal/wasm/store.go @@ -431,7 +431,7 @@ func resolveImports(module *Module, modules map[string]*ModuleInstance) ( err = errorInvalidImport(i, idx, fmt.Errorf("function type out of range")) return } - expectedType := module.TypeSection[i.DescFunc] + expectedType := &module.TypeSection[i.DescFunc] importedFunction := &m.Functions[imported.Index] d := importedFunction.Definition @@ -563,9 +563,10 @@ func executeConstExpression(importedGlobals []*GlobalInstance, expr *ConstantExp return } -func (s *Store) GetFunctionTypeIDs(ts []*FunctionType) ([]FunctionTypeID, error) { +func (s *Store) GetFunctionTypeIDs(ts []FunctionType) ([]FunctionTypeID, error) { ret := make([]FunctionTypeID, len(ts)) - for i, t := range ts { + for i := range ts { + t := &ts[i] inst, err := s.getFunctionTypeID(t) if err != nil { return nil, err diff --git a/internal/wasm/store_test.go b/internal/wasm/store_test.go index da90c4d0..17e03c2b 100644 --- a/internal/wasm/store_test.go +++ b/internal/wasm/store_test.go @@ -145,16 +145,16 @@ func TestStore_CloseWithExitCode(t *testing.T) { s := newStore() _, err := s.Instantiate(testCtx, &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []uint32{0}, CodeSection: []*Code{{Body: []byte{OpcodeEnd}}}, ExportSection: []Export{{Type: ExternTypeFunc, Index: 0, Name: "fn"}}, - FunctionDefinitionSection: []FunctionDefinition{{funcType: v_v}}, + FunctionDefinitionSection: []FunctionDefinition{{funcType: &v_v}}, }, importedModuleName, nil, []FunctionTypeID{0}) require.NoError(t, err) m2, err := s.Instantiate(testCtx, &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, ImportSection: []Import{{Type: ExternTypeFunc, Module: importedModuleName, Name: "fn", DescFunc: 0}}, MemorySection: &Memory{Min: 1, Cap: 1}, MemoryDefinitionSection: []MemoryDefinition{{}}, @@ -194,7 +194,7 @@ func TestStore_hammer(t *testing.T) { require.True(t, ok) importingModule := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []uint32{0}, CodeSection: []*Code{{Body: []byte{OpcodeEnd}}}, MemorySection: &Memory{Min: 1, Cap: 1}, @@ -248,7 +248,7 @@ func TestStore_hammer_close(t *testing.T) { require.True(t, ok) importingModule := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []uint32{0}, CodeSection: []*Code{{Body: []byte{OpcodeEnd}}}, MemorySection: &Memory{Min: 1, Cap: 1}, @@ -318,7 +318,7 @@ func TestStore_Instantiate_Errors(t *testing.T) { require.NotNil(t, hm) _, err = s.Instantiate(testCtx, &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, ImportSection: []Import{ // The first import resolve succeeds -> increment hm.dependentCount. {Type: ExternTypeFunc, Module: importedModuleName, Name: "fn", DescFunc: 0}, @@ -342,7 +342,7 @@ func TestStore_Instantiate_Errors(t *testing.T) { engine.shouldCompileFail = true importingModule := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []uint32{0, 0}, CodeSection: []*Code{ {Body: []byte{OpcodeEnd}}, @@ -371,7 +371,7 @@ func TestStore_Instantiate_Errors(t *testing.T) { startFuncIndex := uint32(1) importingModule := &Module{ - TypeSection: []*FunctionType{v_v}, + TypeSection: []FunctionType{v_v}, FunctionSection: []uint32{0}, CodeSection: []*Code{{Body: []byte{OpcodeEnd}}}, StartSection: &startFuncIndex, @@ -479,7 +479,7 @@ func TestStore_getFunctionTypeID(t *testing.T) { require.Error(t, err) }) t.Run("ok", func(t *testing.T) { - tests := []*FunctionType{ + tests := []FunctionType{ {Params: []ValueType{}}, {Params: []ValueType{ValueTypeF32}}, {Results: []ValueType{ValueTypeF64}}, @@ -490,7 +490,7 @@ func TestStore_getFunctionTypeID(t *testing.T) { tc := tt t.Run(tc.String(), func(t *testing.T) { s := newStore() - actual, err := s.getFunctionTypeID(tc) + actual, err := s.getFunctionTypeID(&tc) require.NoError(t, err) expectedTypeID, ok := s.typeIDs[tc.String()] @@ -695,7 +695,7 @@ func Test_resolveImports(t *testing.T) { moduleName: externMod, } m := &Module{ - TypeSection: []*FunctionType{{Results: []ValueType{ValueTypeF32}}, {Results: []ValueType{ValueTypeI32}}}, + TypeSection: []FunctionType{{Results: []ValueType{ValueTypeF32}}, {Results: []ValueType{ValueTypeI32}}}, ImportSection: []Import{ {Module: moduleName, Name: name, Type: ExternTypeFunc, DescFunc: 0}, {Module: moduleName, Name: "", Type: ExternTypeFunc, DescFunc: 1}, @@ -723,7 +723,7 @@ func Test_resolveImports(t *testing.T) { } modules := map[string]*ModuleInstance{moduleName: externMod} m := &Module{ - TypeSection: []*FunctionType{{Results: []ValueType{ValueTypeF32}}}, + TypeSection: []FunctionType{{Results: []ValueType{ValueTypeF32}}}, ImportSection: []Import{{Module: moduleName, Name: name, Type: ExternTypeFunc, DescFunc: 0}}, } _, _, _, _, err := resolveImports(m, modules) diff --git a/internal/wasm/table_test.go b/internal/wasm/table_test.go index 171e2569..0ea51af6 100644 --- a/internal/wasm/table_test.go +++ b/internal/wasm/table_test.go @@ -92,7 +92,7 @@ func TestModule_validateTable(t *testing.T) { { // See: https://github.com/WebAssembly/spec/issues/1427 name: "constant derived element offset=0 and no index", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Min: 1, Type: RefTypeFuncref}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -108,7 +108,7 @@ func TestModule_validateTable(t *testing.T) { { name: "constant derived element offset=0 and one index", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Min: 1, Type: RefTypeFuncref}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -127,7 +127,7 @@ func TestModule_validateTable(t *testing.T) { { name: "constant derived element offset - ignores min on imported table", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{{Type: ExternTypeTable, DescTable: Table{Type: RefTypeFuncref}}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -146,7 +146,7 @@ func TestModule_validateTable(t *testing.T) { { name: "constant derived element offset=0 and one index - imported table", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{{Type: ExternTypeTable, DescTable: Table{Min: 1, Type: RefTypeFuncref}}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -165,7 +165,7 @@ func TestModule_validateTable(t *testing.T) { { name: "constant derived element offset and two indices", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Min: 3, Type: RefTypeFuncref}}, FunctionSection: []Index{0, 0, 0, 0}, CodeSection: []*Code{codeEnd, codeEnd, codeEnd, codeEnd}, @@ -184,7 +184,7 @@ func TestModule_validateTable(t *testing.T) { { // See: https://github.com/WebAssembly/spec/issues/1427 name: "imported global derived element offset and no index", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, }, @@ -203,7 +203,7 @@ func TestModule_validateTable(t *testing.T) { { name: "imported global derived element offset and one index", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, }, @@ -225,7 +225,7 @@ func TestModule_validateTable(t *testing.T) { { name: "imported global derived element offset and one index - imported table", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeTable, DescTable: Table{Min: 1, Type: RefTypeFuncref}}, {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, @@ -247,7 +247,7 @@ func TestModule_validateTable(t *testing.T) { { name: "imported global derived element offset - ignores min on imported table", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeTable, DescTable: Table{Type: RefTypeFuncref}}, {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, @@ -269,7 +269,7 @@ func TestModule_validateTable(t *testing.T) { { name: "imported global derived element offset - two indices", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI64}}, {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, @@ -292,7 +292,7 @@ func TestModule_validateTable(t *testing.T) { { name: "mixed elementSegments - const before imported global", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI64}}, {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, @@ -422,7 +422,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { name: "constant derived element offset - decode error", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Type: RefTypeFuncref}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -442,7 +442,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { name: "constant derived element offset - wrong ValType", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Type: RefTypeFuncref}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -458,7 +458,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { name: "constant derived element offset - missing table", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, ElementSection: []ElementSegment{ @@ -473,7 +473,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { name: "constant derived element offset exceeds table min", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Min: 1, Type: RefTypeFuncref}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -489,7 +489,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { name: "constant derived element offset puts init beyond table min", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Min: 2, Type: RefTypeFuncref}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -509,7 +509,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { // See: https://github.com/WebAssembly/spec/issues/1427 name: "constant derived element offset beyond table min - no init elements", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Min: 1, Type: RefTypeFuncref}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -525,7 +525,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { name: "constant derived element offset - funcidx out of range", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Min: 1}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -541,7 +541,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { name: "imported global derived element offset - missing table", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, }, @@ -559,7 +559,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { name: "imported global derived element offset - funcidx out of range", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, }, @@ -578,7 +578,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { name: "imported global derived element offset - wrong ValType", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI64}}, }, @@ -597,7 +597,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { name: "imported global derived element offset - decode error", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, }, @@ -620,7 +620,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { name: "imported global derived element offset - no imports", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Type: RefTypeFuncref}}, FunctionSection: []Index{0}, GlobalSection: []Global{{Type: GlobalType{ValType: ValueTypeI32}}}, // ignored as not imported @@ -637,7 +637,7 @@ func TestModule_validateTable_Errors(t *testing.T) { { name: "imported global derived element offset - no imports are globals", input: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeFunc, DescFunc: 0}, }, @@ -708,7 +708,7 @@ func TestModule_buildTables(t *testing.T) { { // See: https://github.com/WebAssembly/spec/issues/1427 name: "constant derived element offset=0 and no index", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Min: 1}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -730,7 +730,7 @@ func TestModule_buildTables(t *testing.T) { { name: "constant derived element offset=0 and one index", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Min: 1}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -744,7 +744,7 @@ func TestModule_buildTables(t *testing.T) { { name: "constant derived element offset - imported table", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, validatedActiveElementSegments: []validatedActiveElementSegment{ @@ -758,7 +758,7 @@ func TestModule_buildTables(t *testing.T) { { name: "constant derived element offset=0 and one index - imported table", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{{Type: ExternTypeTable, DescTable: Table{Min: 1}}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -773,7 +773,7 @@ func TestModule_buildTables(t *testing.T) { { name: "constant derived element offset and two indices", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, TableSection: []Table{{Min: 3}}, FunctionSection: []Index{0, 0, 0, 0}, CodeSection: []*Code{codeEnd, codeEnd, codeEnd, codeEnd}, @@ -787,7 +787,7 @@ func TestModule_buildTables(t *testing.T) { { // See: https://github.com/WebAssembly/spec/issues/1427 name: "imported global derived element offset and no index", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, }, @@ -802,7 +802,7 @@ func TestModule_buildTables(t *testing.T) { { name: "imported global derived element offset and one index", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, }, @@ -820,7 +820,7 @@ func TestModule_buildTables(t *testing.T) { { name: "imported global derived element offset and one index - imported table", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeTable, DescTable: Table{Min: 1}}, {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, @@ -839,7 +839,7 @@ func TestModule_buildTables(t *testing.T) { { name: "imported global derived element offset - ignores min on imported table", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeTable, DescTable: Table{}}, {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, @@ -858,7 +858,7 @@ func TestModule_buildTables(t *testing.T) { { name: "imported global derived element offset - two indices", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI64}}, {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, @@ -899,7 +899,7 @@ func TestModule_buildTables(t *testing.T) { { name: "mixed elementSegments - const before imported global", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI64}}, {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, @@ -949,7 +949,7 @@ func TestModule_buildTable_Errors(t *testing.T) { { name: "constant derived element offset exceeds table min - imported table", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{{Type: ExternTypeTable, DescTable: Table{}}}, FunctionSection: []Index{0}, CodeSection: []*Code{codeEnd}, @@ -969,7 +969,7 @@ func TestModule_buildTable_Errors(t *testing.T) { { name: "imported global derived element offset exceeds table min", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, }, @@ -992,7 +992,7 @@ func TestModule_buildTable_Errors(t *testing.T) { { name: "imported global derived element offset exceeds table min imported table", module: &Module{ - TypeSection: []*FunctionType{{}}, + TypeSection: []FunctionType{{}}, ImportSection: []Import{ {Type: ExternTypeTable, DescTable: Table{}}, {Type: ExternTypeGlobal, DescGlobal: GlobalType{ValType: ValueTypeI32}}, diff --git a/internal/wazeroir/compiler.go b/internal/wazeroir/compiler.go index a8d75400..40f748bd 100644 --- a/internal/wazeroir/compiler.go +++ b/internal/wazeroir/compiler.go @@ -174,7 +174,7 @@ type compiler struct { localIndexToStackHeightInUint64 map[wasm.Index]int // types hold all the function types in the module where the targe function exists. - types []*wasm.FunctionType + types []wasm.FunctionType // funcs holds the type indexes for all declared functions in the module where the target function exists. funcs []uint32 // globals holds the global types for all declared globals in the module where the target function exists. @@ -242,7 +242,7 @@ type CompilationResult struct { // Functions holds all the declarations of function in the module from which this function is compiled, including itself. Functions []wasm.Index // Types holds all the types in the module from which this function is compiled. - Types []*wasm.FunctionType + Types []wasm.FunctionType // TableTypes holds all the reference types of all tables declared in the module. TableTypes []wasm.ValueType // HasMemory is true if the module from which this function is compiled has memory declaration. @@ -285,7 +285,7 @@ func CompileFunctions(enabledFeatures api.CoreFeatures, callFrameStackSizeInUint var ret []*CompilationResult for funcIndex := range module.FunctionSection { typeID := module.FunctionSection[funcIndex] - sig := types[typeID] + sig := &types[typeID] code := module.CodeSection[funcIndex] if code.GoFunc != nil { // Assume the function might use memory if it has a parameter for the api.Module @@ -336,7 +336,7 @@ func compile(enabledFeatures api.CoreFeatures, sig *wasm.FunctionType, body []byte, localTypes []wasm.ValueType, - types []*wasm.FunctionType, + types []wasm.FunctionType, functions []uint32, globals []wasm.GlobalType, bodyOffsetInCodeSection uint64, diff --git a/internal/wazeroir/compiler_test.go b/internal/wazeroir/compiler_test.go index 2d3f1804..61504c38 100644 --- a/internal/wazeroir/compiler_test.go +++ b/internal/wazeroir/compiler_test.go @@ -14,23 +14,23 @@ import ( var ( f32, f64, i32 = wasm.ValueTypeF32, wasm.ValueTypeF64, wasm.ValueTypeI32 - f32_i32 = &wasm.FunctionType{ + f32_i32 = wasm.FunctionType{ Params: []wasm.ValueType{f32}, Results: []wasm.ValueType{i32}, ParamNumInUint64: 1, ResultNumInUint64: 1, } - i32_i32 = &wasm.FunctionType{ + i32_i32 = wasm.FunctionType{ Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}, ParamNumInUint64: 1, ResultNumInUint64: 1, } - i32i32_i32 = &wasm.FunctionType{ + i32i32_i32 = wasm.FunctionType{ Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}, ParamNumInUint64: 2, ResultNumInUint64: 1, } - v_v = &wasm.FunctionType{} - v_f64f64 = &wasm.FunctionType{Results: []wasm.ValueType{f64, f64}, ResultNumInUint64: 2} + v_v = wasm.FunctionType{} + v_f64f64 = wasm.FunctionType{Results: []wasm.ValueType{f64, f64}, ResultNumInUint64: 2} ) func TestCompile(t *testing.T) { @@ -43,7 +43,7 @@ func TestCompile(t *testing.T) { { name: "nullary", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{wasm.OpcodeEnd}}}, }, @@ -53,15 +53,15 @@ func TestCompile(t *testing.T) { }, LabelCallers: map[LabelID]uint32{}, Functions: []uint32{0}, - Types: []*wasm.FunctionType{v_v}, - Signature: v_v, + Types: []wasm.FunctionType{v_v}, + Signature: &v_v, TableTypes: []wasm.RefType{}, }, }, { name: "host wasm nullary", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{wasm.OpcodeEnd}}}, }, @@ -71,15 +71,15 @@ func TestCompile(t *testing.T) { }, LabelCallers: map[LabelID]uint32{}, Functions: []uint32{0}, - Types: []*wasm.FunctionType{v_v}, - Signature: v_v, + Types: []wasm.FunctionType{v_v}, + Signature: &v_v, TableTypes: []wasm.RefType{}, }, }, { name: "host go nullary", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{wasm.MustParseGoReflectFuncCode(func() {})}, }, @@ -88,7 +88,7 @@ func TestCompile(t *testing.T) { { name: "host go context.Context api.Module uses memory", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{wasm.MustParseGoReflectFuncCode(func(context.Context, api.Module) {})}, }, @@ -97,7 +97,7 @@ func TestCompile(t *testing.T) { { name: "identity", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{i32_i32}, + TypeSection: []wasm.FunctionType{i32_i32}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeEnd}}}, }, @@ -108,7 +108,7 @@ func TestCompile(t *testing.T) { OperationBr{Target: Label{Kind: LabelKindReturn}}, // return! }, LabelCallers: map[LabelID]uint32{}, - Types: []*wasm.FunctionType{ + Types: []wasm.FunctionType{ { Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}, ParamNumInUint64: 1, @@ -127,7 +127,7 @@ func TestCompile(t *testing.T) { { name: "uses memory", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeI32Const, 8, // memory offset to load @@ -144,9 +144,9 @@ func TestCompile(t *testing.T) { OperationBr{Target: Label{Kind: LabelKindReturn}}, // return! }, LabelCallers: map[LabelID]uint32{}, - Types: []*wasm.FunctionType{v_v}, + Types: []wasm.FunctionType{v_v}, Functions: []uint32{0}, - Signature: v_v, + Signature: &v_v, TableTypes: []wasm.RefType{}, UsesMemory: true, }, @@ -154,7 +154,7 @@ func TestCompile(t *testing.T) { { name: "host uses memory", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeI32Const, 8, // memory offset to load @@ -171,9 +171,9 @@ func TestCompile(t *testing.T) { OperationBr{Target: Label{Kind: LabelKindReturn}}, // return! }, LabelCallers: map[LabelID]uint32{}, - Types: []*wasm.FunctionType{v_v}, + Types: []wasm.FunctionType{v_v}, Functions: []uint32{0}, - Signature: v_v, + Signature: &v_v, TableTypes: []wasm.RefType{}, UsesMemory: true, }, @@ -181,7 +181,7 @@ func TestCompile(t *testing.T) { { name: "memory.grow", // Ex to expose ops to grow memory module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{i32_i32}, + TypeSection: []wasm.FunctionType{i32_i32}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeLocalGet, 0, wasm.OpcodeMemoryGrow, 0, wasm.OpcodeEnd, @@ -195,7 +195,7 @@ func TestCompile(t *testing.T) { OperationBr{Target: Label{Kind: LabelKindReturn}}, // return! }, LabelCallers: map[LabelID]uint32{}, - Types: []*wasm.FunctionType{{ + Types: []wasm.FunctionType{{ Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}, ParamNumInUint64: 1, ResultNumInUint64: 1, @@ -248,7 +248,7 @@ func TestCompile_Block(t *testing.T) { { name: "type-i32-i32", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeBlock, 0x40, @@ -276,8 +276,8 @@ func TestCompile_Block(t *testing.T) { // two i32 parameters to add. LabelCallers: map[LabelID]uint32{Label{Kind: LabelKindContinuation, FrameID: 2}.ID(): 1}, Functions: []uint32{0}, - Types: []*wasm.FunctionType{v_v}, - Signature: v_v, + Types: []wasm.FunctionType{v_v}, + Signature: &v_v, TableTypes: []wasm.RefType{}, }, }, @@ -315,7 +315,7 @@ func TestCompile_BulkMemoryOperations(t *testing.T) { // ) two := uint32(2) module := &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, MemorySection: &wasm.Memory{Min: 1}, DataSection: []wasm.DataSegment{ @@ -355,9 +355,9 @@ func TestCompile_BulkMemoryOperations(t *testing.T) { UsesMemory: true, HasDataInstances: true, LabelCallers: map[LabelID]uint32{}, - Signature: v_v, + Signature: &v_v, Functions: []wasm.Index{0}, - Types: []*wasm.FunctionType{v_v}, + Types: []wasm.FunctionType{v_v}, TableTypes: []wasm.RefType{}, } @@ -367,13 +367,13 @@ func TestCompile_BulkMemoryOperations(t *testing.T) { } func TestCompile_MultiValue(t *testing.T) { - i32i32_i32i32 := &wasm.FunctionType{ + i32i32_i32i32 := wasm.FunctionType{ Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32}, Results: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32}, ParamNumInUint64: 2, ResultNumInUint64: 2, } - _i32i64 := &wasm.FunctionType{ + _i32i64 := wasm.FunctionType{ Results: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64}, ParamNumInUint64: 0, ResultNumInUint64: 2, @@ -388,7 +388,7 @@ func TestCompile_MultiValue(t *testing.T) { { name: "swap", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{i32i32_i32i32}, + TypeSection: []wasm.FunctionType{i32i32_i32i32}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ // (func (param $x i32) (param $y i32) (result i32 i32) local.get 1 local.get 0) @@ -403,16 +403,16 @@ func TestCompile_MultiValue(t *testing.T) { OperationBr{Target: Label{Kind: LabelKindReturn}}, // return! }, LabelCallers: map[LabelID]uint32{}, - Signature: i32i32_i32i32, + Signature: &i32i32_i32i32, Functions: []wasm.Index{0}, - Types: []*wasm.FunctionType{i32i32_i32i32}, + Types: []wasm.FunctionType{i32i32_i32i32}, TableTypes: []wasm.RefType{}, }, }, { name: "br.wast - type-f64-f64-value", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_f64f64}, + TypeSection: []wasm.FunctionType{v_f64f64}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeBlock, 0, // (block (result f64 f64) @@ -446,16 +446,16 @@ func TestCompile_MultiValue(t *testing.T) { // Note: f64.add comes after br 0 so is unreachable. This is why neither the add, nor its other operand // are in the above compilation result. LabelCallers: map[LabelID]uint32{Label{Kind: LabelKindContinuation, FrameID: 2}.ID(): 1}, // arbitrary label - Signature: v_f64f64, + Signature: &v_f64f64, Functions: []wasm.Index{0}, - Types: []*wasm.FunctionType{v_f64f64}, + Types: []wasm.FunctionType{v_f64f64}, TableTypes: []wasm.RefType{}, }, }, { name: "call.wast - $const-i32-i64", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{_i32i64}, + TypeSection: []wasm.FunctionType{_i32i64}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ // (func $const-i32-i64 (result i32 i64) i32.const 306 i64.const 356) @@ -469,16 +469,16 @@ func TestCompile_MultiValue(t *testing.T) { OperationBr{Target: Label{Kind: LabelKindReturn}}, // return! }, LabelCallers: map[LabelID]uint32{}, - Signature: _i32i64, + Signature: &_i32i64, Functions: []wasm.Index{0}, - Types: []*wasm.FunctionType{_i32i64}, + Types: []wasm.FunctionType{_i32i64}, TableTypes: []wasm.RefType{}, }, }, { name: "if.wast - param", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{i32_i32}, // (func (param i32) (result i32) + TypeSection: []wasm.FunctionType{i32_i32}, // (func (param i32) (result i32) FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeI32Const, 1, // (i32.const 1) @@ -522,16 +522,16 @@ func TestCompile_MultiValue(t *testing.T) { Label{Kind: LabelKindContinuation, FrameID: 2}.ID(): 2, Label{Kind: LabelKindElse, FrameID: 2}.ID(): 1, }, - Signature: i32_i32, + Signature: &i32_i32, Functions: []wasm.Index{0}, - Types: []*wasm.FunctionType{i32_i32}, + Types: []wasm.FunctionType{i32_i32}, TableTypes: []wasm.RefType{}, }, }, { name: "if.wast - params", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ i32_i32, // (func (param i32) (result i32) i32i32_i32, // (if (param i32 i32) (result i32) }, @@ -579,16 +579,16 @@ func TestCompile_MultiValue(t *testing.T) { Label{Kind: LabelKindContinuation, FrameID: 2}.ID(): 2, Label{Kind: LabelKindElse, FrameID: 2}.ID(): 1, }, - Signature: i32_i32, + Signature: &i32_i32, Functions: []wasm.Index{0}, - Types: []*wasm.FunctionType{i32_i32, i32i32_i32}, + Types: []wasm.FunctionType{i32_i32, i32i32_i32}, TableTypes: []wasm.RefType{}, }, }, { name: "if.wast - params-break", module: &wasm.Module{ - TypeSection: []*wasm.FunctionType{ + TypeSection: []wasm.FunctionType{ i32_i32, // (func (param i32) (result i32) i32i32_i32, // (if (param i32 i32) (result i32) }, @@ -636,9 +636,9 @@ func TestCompile_MultiValue(t *testing.T) { Label{Kind: LabelKindContinuation, FrameID: 2}.ID(): 2, Label{Kind: LabelKindElse, FrameID: 2}.ID(): 1, }, - Signature: i32_i32, + Signature: &i32_i32, Functions: []wasm.Index{0}, - Types: []*wasm.FunctionType{i32_i32, i32i32_i32}, + Types: []wasm.FunctionType{i32_i32, i32i32_i32}, TableTypes: []wasm.RefType{}, }, }, @@ -665,7 +665,7 @@ func TestCompile_MultiValue(t *testing.T) { // TestCompile_NonTrappingFloatToIntConversion picks an arbitrary operator from "nontrapping-float-to-int-conversion". func TestCompile_NonTrappingFloatToIntConversion(t *testing.T) { module := &wasm.Module{ - TypeSection: []*wasm.FunctionType{f32_i32}, + TypeSection: []wasm.FunctionType{f32_i32}, FunctionSection: []wasm.Index{0}, // (func (param f32) (result i32) local.get 0 i32.trunc_sat_f32_s) CodeSection: []*wasm.Code{{Body: []byte{ @@ -685,9 +685,9 @@ func TestCompile_NonTrappingFloatToIntConversion(t *testing.T) { OperationBr{Target: Label{Kind: LabelKindReturn}}, // return! }, LabelCallers: map[LabelID]uint32{}, - Signature: f32_i32, + Signature: &f32_i32, Functions: []wasm.Index{0}, - Types: []*wasm.FunctionType{f32_i32}, + Types: []wasm.FunctionType{f32_i32}, TableTypes: []wasm.RefType{}, } for _, tp := range module.TypeSection { @@ -701,7 +701,7 @@ func TestCompile_NonTrappingFloatToIntConversion(t *testing.T) { // TestCompile_SignExtensionOps picks an arbitrary operator from "sign-extension-ops". func TestCompile_SignExtensionOps(t *testing.T) { module := &wasm.Module{ - TypeSection: []*wasm.FunctionType{i32_i32}, + TypeSection: []wasm.FunctionType{i32_i32}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeLocalGet, 0, wasm.OpcodeI32Extend8S, wasm.OpcodeEnd, @@ -716,9 +716,9 @@ func TestCompile_SignExtensionOps(t *testing.T) { OperationBr{Target: Label{Kind: LabelKindReturn}}, // return! }, LabelCallers: map[LabelID]uint32{}, - Signature: i32_i32, + Signature: &i32_i32, Functions: []wasm.Index{0}, - Types: []*wasm.FunctionType{i32_i32}, + Types: []wasm.FunctionType{i32_i32}, TableTypes: []wasm.RefType{}, } for _, tp := range module.TypeSection { @@ -740,7 +740,7 @@ func requireCompilationResult(t *testing.T, enabledFeatures api.CoreFeatures, ex func TestCompile_CallIndirectNonZeroTableIndex(t *testing.T) { module := &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v, v_v, v_v}, + TypeSection: []wasm.FunctionType{v_v, v_v, v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeI32Const, 0, // call indirect offset @@ -767,12 +767,12 @@ func TestCompile_CallIndirectNonZeroTableIndex(t *testing.T) { }, HasTable: true, LabelCallers: map[LabelID]uint32{}, - Signature: v_v, + Signature: &v_v, Functions: []wasm.Index{0}, TableTypes: []wasm.RefType{ wasm.RefTypeExternref, wasm.RefTypeFuncref, wasm.RefTypeFuncref, wasm.RefTypeFuncref, wasm.RefTypeFuncref, wasm.RefTypeFuncref, }, - Types: []*wasm.FunctionType{v_v, v_v, v_v}, + Types: []wasm.FunctionType{v_v, v_v, v_v}, } res, err := CompileFunctions(api.CoreFeatureBulkMemoryOperations, 0, module, false) @@ -861,7 +861,7 @@ func TestCompile_Refs(t *testing.T) { tc := tt t.Run(tc.name, func(t *testing.T) { module := &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: tc.body}}, } @@ -929,7 +929,7 @@ func TestCompile_TableGetOrSet(t *testing.T) { tc := tt t.Run(tc.name, func(t *testing.T) { module := &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: tc.body}}, TableSection: []wasm.Table{{}}, @@ -998,7 +998,7 @@ func TestCompile_TableGrowFillSize(t *testing.T) { tc := tt t.Run(tc.name, func(t *testing.T) { module := &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: tc.body}}, TableSection: []wasm.Table{{}}, @@ -1020,7 +1020,7 @@ func TestCompile_Locals(t *testing.T) { { name: "local.get - func param - v128", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeV128}}}, + TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeV128}}}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeLocalGet, 0, @@ -1036,7 +1036,7 @@ func TestCompile_Locals(t *testing.T) { { name: "local.get - func param - i64", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeI64}}}, + TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeI64}}}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeLocalGet, 0, @@ -1052,7 +1052,7 @@ func TestCompile_Locals(t *testing.T) { { name: "local.get - non func param - v128", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{ Body: []byte{ @@ -1072,7 +1072,7 @@ func TestCompile_Locals(t *testing.T) { { name: "local.set - func param - v128", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeV128}}}, + TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeV128}}}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeVecPrefix, wasm.OpcodeVecV128Const, // [] -> [0x01, 0x02] @@ -1094,7 +1094,7 @@ func TestCompile_Locals(t *testing.T) { { name: "local.set - func param - i32", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeI32}}}, + TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeI32}}}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeI32Const, 0x1, // [] -> [0x01] @@ -1112,7 +1112,7 @@ func TestCompile_Locals(t *testing.T) { { name: "local.set - non func param - v128", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{ Body: []byte{ @@ -1138,7 +1138,7 @@ func TestCompile_Locals(t *testing.T) { { name: "local.tee - func param - v128", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeV128}}}, + TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeV128}}}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeVecPrefix, wasm.OpcodeVecV128Const, // [] -> [0x01, 0x02] @@ -1162,7 +1162,7 @@ func TestCompile_Locals(t *testing.T) { { name: "local.tee - func param - f32", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeF32}}}, + TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeF32}}}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeF32Const, 1, 0, 0, 0, @@ -1181,7 +1181,7 @@ func TestCompile_Locals(t *testing.T) { { name: "local.tee - non func param", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{ Body: []byte{ @@ -2782,7 +2782,7 @@ func TestCompile_Vec(t *testing.T) { tc := tt t.Run(tc.name, func(t *testing.T) { module := &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, MemorySection: &wasm.Memory{}, CodeSection: []*wasm.Code{{Body: tc.body}}, @@ -2816,7 +2816,7 @@ func TestCompile_unreachable_Br_BrIf_BrTable(t *testing.T) { { name: "br", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeBr, 0, // Return the function -> the followings are unreachable. @@ -2831,7 +2831,7 @@ func TestCompile_unreachable_Br_BrIf_BrTable(t *testing.T) { { name: "br_if", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeBr, 0, // Return the function -> the followings are unreachable. @@ -2847,7 +2847,7 @@ func TestCompile_unreachable_Br_BrIf_BrTable(t *testing.T) { { name: "br_table", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeBr, 0, // Return the function -> the followings are unreachable. @@ -2882,7 +2882,7 @@ func TestCompile_drop_vectors(t *testing.T) { { name: "basic", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeVecPrefix, @@ -2920,7 +2920,7 @@ func TestCompile_select_vectors(t *testing.T) { { name: "non typed", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeVecPrefix, @@ -2946,7 +2946,7 @@ func TestCompile_select_vectors(t *testing.T) { { name: "typed", mod: &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{ wasm.OpcodeVecPrefix, @@ -3197,7 +3197,7 @@ func Test_ensureTermination(t *testing.T) { } { t.Run(fmt.Sprintf("%v", tc.ensureTermination), func(t *testing.T) { mod := &wasm.Module{ - TypeSection: []*wasm.FunctionType{v_v}, + TypeSection: []wasm.FunctionType{v_v}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{ Body: []byte{ diff --git a/internal/wazeroir/signature.go b/internal/wazeroir/signature.go index 0437dbb6..0164f595 100644 --- a/internal/wazeroir/signature.go +++ b/internal/wazeroir/signature.go @@ -596,7 +596,7 @@ func (c *compiler) wasmOpcodeSignature(op wasm.Opcode, index uint32) (*signature type funcTypeToIRSignatures struct { directCalls []*signature indirectCalls []*signature - wasmTypes []*wasm.FunctionType + wasmTypes []wasm.FunctionType } // get returns the *signature for the direct or indirect function call against functions whose type is at `typeIndex`. @@ -611,7 +611,7 @@ func (f *funcTypeToIRSignatures) get(typeIndex wasm.Index, indirect bool) *signa return sig } - tp := f.wasmTypes[typeIndex] + tp := &f.wasmTypes[typeIndex] if indirect { sig = &signature{ in: make([]UnsignedType, 0, len(tp.Params)+1), // +1 to reserve space for call indirect index. diff --git a/internal/wazeroir/signature_test.go b/internal/wazeroir/signature_test.go index dbf4c9a3..ea860c92 100644 --- a/internal/wazeroir/signature_test.go +++ b/internal/wazeroir/signature_test.go @@ -103,7 +103,7 @@ func TestCompiler_wasmOpcodeSignature(t *testing.T) { func Test_funcTypeToIRSignatures(t *testing.T) { f := &funcTypeToIRSignatures{ - wasmTypes: []*wasm.FunctionType{v_v, i32_i32, v_f64f64}, + wasmTypes: []wasm.FunctionType{v_v, i32_i32, v_f64f64}, directCalls: make([]*signature, 3), indirectCalls: make([]*signature, 3), } diff --git a/runtime_test.go b/runtime_test.go index 5d3fda99..4237fe8b 100644 --- a/runtime_test.go +++ b/runtime_test.go @@ -66,7 +66,7 @@ func TestRuntime_CompileModule(t *testing.T) { { name: "FunctionSection, but not exported", wasm: &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}}}, + TypeSection: []wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}}}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{wasm.OpcodeEnd}}}, }, @@ -78,7 +78,7 @@ func TestRuntime_CompileModule(t *testing.T) { { name: "FunctionSection exported", wasm: &wasm.Module{ - TypeSection: []*wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}}}, + TypeSection: []wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}}}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{wasm.OpcodeEnd}}}, ExportSection: []wasm.Export{{ @@ -343,7 +343,7 @@ func TestRuntime_InstantiateModule_UsesContext(t *testing.T) { one := uint32(1) binary := binaryencoding.EncodeModule(&wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, ImportSection: []wasm.Import{{Module: "env", Name: "start", Type: wasm.ExternTypeFunc, DescFunc: 0}}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{ @@ -472,7 +472,7 @@ func TestRuntime_InstantiateModule_ExitError(t *testing.T) { one := uint32(1) binary := binaryencoding.EncodeModule(&wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, ImportSection: []wasm.Import{{Module: "env", Name: "exit", Type: wasm.ExternTypeFunc, DescFunc: 0}}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{ @@ -491,7 +491,7 @@ func TestRuntime_InstantiateModule_ExitError(t *testing.T) { func TestRuntime_CloseWithExitCode(t *testing.T) { bin := binaryencoding.EncodeModule(&wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, FunctionSection: []wasm.Index{0}, CodeSection: []*wasm.Code{{Body: []byte{wasm.OpcodeEnd}}}, ExportSection: []wasm.Export{{Type: wasm.ExternTypeFunc, Index: 0, Name: "func"}}, @@ -603,7 +603,7 @@ func TestHostFunctionWithCustomContext(t *testing.T) { startFnIndex := uint32(2) binary := binaryencoding.EncodeModule(&wasm.Module{ - TypeSection: []*wasm.FunctionType{{}}, + TypeSection: []wasm.FunctionType{{}}, ImportSection: []wasm.Import{ {Module: "env", Name: "host", Type: wasm.ExternTypeFunc, DescFunc: 0}, {Module: "env", Name: "host2", Type: wasm.ExternTypeFunc, DescFunc: 0},