Repository: tetratelabs/wazero
Pull request #884 (2022-12-04 07:06:32)

Delete obsolete integration test from wat parser
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>

mathetake: `EncodeModule` is not maintained anymore AFAIK plus this example `newExample()` is too unrealistic to measure the perf of decoder. I would replace it with TinyGo one and delete `newExample()` instead. on it.

Patch:
diff --git a/internal/wasm/func_validation.go b/internal/wasm/func_validation.go
index 4f1c3307..49188673 100644
--- a/internal/wasm/func_validation.go
+++ b/internal/wasm/func_validation.go
@@ -1451,6 +1451,9 @@ func (m *Module) validateFunctionWithMaxStackValues(
 			valueTypeStack.pushStackLimit(len(bt.Params))
 			pc += num
 		} else if op == OpcodeElse {
+			if len(controlBlockStack) == 0 {
+				return fmt.Errorf("redundant Else instruction at %#x", pc)
+			}
 			bl := controlBlockStack[len(controlBlockStack)-1]
 			bl.elseAt = pc
 			// Check the type soundness of the instructions *before* entering this else Op.


Test patch:
diff --git a/internal/integration_test/bench/codec_test.go b/internal/integration_test/bench/codec_test.go
deleted file mode 100644
index 2774c190..00000000
--- a/internal/integration_test/bench/codec_test.go
+++ /dev/null
@@ -1,130 +0,0 @@
-package bench
-
-import (
-	"testing"
-
-	"github.com/tetratelabs/wazero"
-	"github.com/tetratelabs/wazero/api"
-	"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
-	"github.com/tetratelabs/wazero/internal/testing/require"
-	"github.com/tetratelabs/wazero/internal/wasm"
-	"github.com/tetratelabs/wazero/internal/wasm/binary"
-)
-
-// example holds the latest supported features as described in the comments of exampleText
-var example = newExample()
-
-// exampleWasm is the exampleText encoded in the WebAssembly 1.0 binary format.
-var exampleWasm = binary.EncodeModule(example)
-
-func newExample() *wasm.Module {
-	three := wasm.Index(3)
-	f32, i32, i64 := wasm.ValueTypeF32, wasm.ValueTypeI32, wasm.ValueTypeI64
-	return &wasm.Module{
-		TypeSection: []*wasm.FunctionType{
-			{Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}},
-			{},
-			{Params: []wasm.ValueType{i32, i32, i32, i32}, Results: []wasm.ValueType{i32}},
-			{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i64}},
-			{Params: []wasm.ValueType{f32}, Results: []wasm.ValueType{i32}},
-			{Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32, i32}},
-		},
-		ImportSection: []*wasm.Import{
-			{
-				Module: "wasi_snapshot_preview1", Name: "args_sizes_get",
-				Type:     wasm.ExternTypeFunc,
-				DescFunc: 0,
-			}, {
-				Module: "wasi_snapshot_preview1", Name: "fd_write",
-				Type:     wasm.ExternTypeFunc,
-				DescFunc: 2,
-			},
-		},
-		FunctionSection: []wasm.Index{wasm.Index(1), wasm.Index(1), wasm.Index(0), wasm.Index(3), wasm.Index(4), wasm.Index(5)},
-		CodeSection: []*wasm.Code{
-			{Body: []byte{wasm.OpcodeCall, 3, wasm.OpcodeEnd}},
-			{Body: []byte{wasm.OpcodeEnd}},
-			{Body: []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 1, wasm.OpcodeI32Add, wasm.OpcodeEnd}},
-			{Body: []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeI64Extend16S, wasm.OpcodeEnd}},
-			{Body: []byte{
-				wasm.OpcodeLocalGet, 0x00,
-				wasm.OpcodeMiscPrefix, wasm.OpcodeMiscI32TruncSatF32S,
-				wasm.OpcodeEnd,
-			}},
-			{Body: []byte{wasm.OpcodeLocalGet, 1, wasm.OpcodeLocalGet, 0, wasm.OpcodeEnd}},
-		},
-		MemorySection: &wasm.Memory{Min: 1, Cap: 1, Max: three, IsMaxEncoded: true},
-		ExportSection: []*wasm.Export{
-			{Name: "AddInt", Type: wasm.ExternTypeFunc, Index: wasm.Index(4)},
-			{Name: "", Type: wasm.ExternTypeFunc, Index: wasm.Index(3)},
-			{Name: "mem", Type: wasm.ExternTypeMemory, Index: wasm.Index(0)},
-			{Name: "swap", Type: wasm.ExternTypeFunc, Index: wasm.Index(7)},
-		},
-		StartSection: &three,
-		NameSection: &wasm.NameSection{
-			ModuleName: "example",
-			FunctionNames: wasm.NameMap{
-				{Index: wasm.Index(0), Name: "wasi.args_sizes_get"},
-				{Index: wasm.Index(1), Name: "wasi.fd_write"},
-				{Index: wasm.Index(2), Name: "call_hello"},
-				{Index: wasm.Index(3), Name: "hello"},
-				{Index: wasm.Index(4), Name: "addInt"},
-				{Index: wasm.Index(7), Name: "swap"},
-			},
-			LocalNames: wasm.IndirectNameMap{
-				{Index: wasm.Index(1), NameMap: wasm.NameMap{
-					{Index: wasm.Index(0), Name: "fd"},
-					{Index: wasm.Index(1), Name: "iovs_ptr"},
-					{Index: wasm.Index(2), Name: "iovs_len"},
-					{Index: wasm.Index(3), Name: "nwritten_ptr"},
-				}},
-				{Index: wasm.Index(4), NameMap: wasm.NameMap{
-					{Index: wasm.Index(0), Name: "value_1"},
-					{Index: wasm.Index(1), Name: "value_2"},
-				}},
-			},
-		},
-	}
-}
-
-func TestExampleUpToDate(t *testing.T) {
-	t.Run("binary.DecodeModule", func(t *testing.T) {
-		m, err := binary.DecodeModule(exampleWasm, api.CoreFeaturesV2, wasm.MemoryLimitPages, false, false)
-		require.NoError(t, err)
-		require.Equal(t, example, m)
-	})
-
-	t.Run("Executable", func(t *testing.T) {
-		r := wazero.NewRuntimeWithConfig(testCtx, wazero.NewRuntimeConfig())
-		defer r.Close(testCtx)
-
-		// Add WASI to satisfy import tests
-		wasi_snapshot_preview1.MustInstantiate(testCtx, r)
-
-		// Decode and instantiate the module
-		module, err := r.InstantiateModuleFromBinary(testCtx, exampleWasm)
-		require.NoError(t, err)
-
-		// Call the swap function as a smoke test
-		results, err := module.ExportedFunction("swap").Call(testCtx, 1, 2)
-		require.NoError(t, err)
-		require.Equal(t, []uint64{2, 1}, results)
-	})
-}
-
-func BenchmarkCodec(b *testing.B) {
-	b.Run("binary.DecodeModule", func(b *testing.B) {
-		b.ReportAllocs()
-		for i := 0; i < b.N; i++ {
-			if _, err := binary.DecodeModule(exampleWasm, api.CoreFeaturesV2, wasm.MemoryLimitPages, false, false); err != nil {
-				b.Fatal(err)
-			}
-		}
-	})
-	b.Run("binary.EncodeModule", func(b *testing.B) {
-		b.ReportAllocs()
-		for i := 0; i < b.N; i++ {
-			_ = binary.EncodeModule(example)
-		}
-	})
-}
diff --git a/internal/integration_test/bench/decoder_bench_test.go b/internal/integration_test/bench/decoder_bench_test.go
new file mode 100644
index 00000000..ecec8441
--- /dev/null
+++ b/internal/integration_test/bench/decoder_bench_test.go
@@ -0,0 +1,20 @@
+package bench
+
+import (
+	"testing"
+
+	"github.com/tetratelabs/wazero/api"
+	"github.com/tetratelabs/wazero/internal/wasm"
+	"github.com/tetratelabs/wazero/internal/wasm/binary"
+)
+
+func BenchmarkCodec(b *testing.B) {
+	b.Run("binary.DecodeModule", func(b *testing.B) {
+		b.ReportAllocs()
+		for i := 0; i < b.N; i++ {
+			if _, err := binary.DecodeModule(caseWasm, api.CoreFeaturesV2, wasm.MemoryLimitPages, false, false); err != nil {
+				b.Fatal(err)
+			}
+		}
+	})
+}
diff --git a/internal/wasm/func_validation_test.go b/internal/wasm/func_validation_test.go
index 59564e7d..07f7e1f6 100644
--- a/internal/wasm/func_validation_test.go
+++ b/internal/wasm/func_validation_test.go
@@ -3553,3 +3553,14 @@ func TestFunctionValidation_redundantEnd(t *testing.T) {
 	err := m.validateFunction(api.CoreFeaturesV2, 0, nil, nil, nil, nil, nil)
 	require.EqualError(t, err, "redundant End instruction at 0x1")
 }
+
+// TestFunctionValidation_redundantEnd is found in th validation fuzzing.
+func TestFunctionValidation_redundantElse(t *testing.T) {
+	m := &Module{
+		TypeSection:     []*FunctionType{{}},
+		FunctionSection: []Index{0},
+		CodeSection:     []*Code{{Body: []byte{OpcodeEnd, OpcodeElse}}},
+	}
+	err := m.validateFunction(api.CoreFeaturesV2, 0, nil, nil, nil, nil, nil)
+	require.EqualError(t, err, "redundant Else instruction at 0x1")
+}