github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/integration_test/fuzzcases/fuzzcases_test.go (about)

     1  package fuzzcases
     2  
     3  import (
     4  	"context"
     5  	"embed"
     6  	"encoding/hex"
     7  	"fmt"
     8  	"math"
     9  	"runtime"
    10  	"testing"
    11  
    12  	"github.com/tetratelabs/wazero"
    13  	"github.com/tetratelabs/wazero/api"
    14  	"github.com/tetratelabs/wazero/internal/platform"
    15  	"github.com/tetratelabs/wazero/internal/testing/binaryencoding"
    16  	"github.com/tetratelabs/wazero/internal/testing/nodiff"
    17  	"github.com/tetratelabs/wazero/internal/testing/require"
    18  	"github.com/tetratelabs/wazero/internal/wasm"
    19  )
    20  
    21  var ctx = context.Background()
    22  
    23  // Note: the name of the test is the PR number. It may be followed by a letter
    24  // if the PR includes more than one test (e.g. "1234a", "1234b").
    25  //
    26  //go:embed testdata/*.wasm
    27  var testcases embed.FS
    28  
    29  func getWasmBinary(t *testing.T, testId string) []byte {
    30  	ret, err := testcases.ReadFile(fmt.Sprintf("testdata/%s.wasm", testId))
    31  	require.NoError(t, err)
    32  	return ret
    33  }
    34  
    35  func runWithCompiler(t *testing.T, runner func(t *testing.T, r wazero.Runtime)) {
    36  	if !platform.CompilerSupported() {
    37  		return
    38  	}
    39  	t.Run("compiler", func(t *testing.T) {
    40  		r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigCompiler())
    41  		defer r.Close(ctx)
    42  		runner(t, r)
    43  	})
    44  }
    45  
    46  func runWithInterpreter(t *testing.T, runner func(t *testing.T, r wazero.Runtime)) {
    47  	t.Run("interpreter", func(t *testing.T) {
    48  		r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter())
    49  		defer r.Close(ctx)
    50  		runner(t, r)
    51  	})
    52  }
    53  
    54  func run(t *testing.T, runner func(t *testing.T, r wazero.Runtime)) {
    55  	runWithInterpreter(t, runner)
    56  	runWithCompiler(t, runner)
    57  }
    58  
    59  // Test695 requires two functions to exit with "out of bounds memory access" consistently across the implementations.
    60  func Test695(t *testing.T) {
    61  	run(t, func(t *testing.T, r wazero.Runtime) {
    62  		module, err := r.Instantiate(ctx, getWasmBinary(t, "695"))
    63  		require.NoError(t, err)
    64  
    65  		_, err = module.ExportedFunction("i8x16s").Call(ctx)
    66  		require.NotNil(t, err)
    67  		require.Contains(t, err.Error(), "out of bounds memory access")
    68  
    69  		_, err = module.ExportedFunction("i16x8s").Call(ctx)
    70  		require.NotNil(t, err)
    71  		require.Contains(t, err.Error(), "out of bounds memory access")
    72  	})
    73  }
    74  
    75  func Test696(t *testing.T) {
    76  	run(t, func(t *testing.T, r wazero.Runtime) {
    77  		module, err := r.Instantiate(ctx, getWasmBinary(t, "696"))
    78  		require.NoError(t, err)
    79  		for _, tc := range []struct {
    80  			fnName string
    81  			in     uint64
    82  			exp    [2]uint64
    83  		}{
    84  			{fnName: "select", in: 1 << 5, exp: [2]uint64{0xffffffffffffffff, 0xeeeeeeeeeeeeeeee}},
    85  			{fnName: "select", in: 1, exp: [2]uint64{0xffffffffffffffff, 0xeeeeeeeeeeeeeeee}},
    86  			{fnName: "select", in: 0, exp: [2]uint64{0x1111111111111111, 0x2222222222222222}},
    87  			{fnName: "select", in: 0xffffff, exp: [2]uint64{0xffffffffffffffff, 0xeeeeeeeeeeeeeeee}},
    88  			{fnName: "select", in: 0xffff00, exp: [2]uint64{0xffffffffffffffff, 0xeeeeeeeeeeeeeeee}},
    89  			{fnName: "select", in: 0x000000, exp: [2]uint64{0x1111111111111111, 0x2222222222222222}},
    90  			{fnName: "typed select", in: 1, exp: [2]uint64{0xffffffffffffffff, 0xeeeeeeeeeeeeeeee}},
    91  			{fnName: "typed select", in: 0, exp: [2]uint64{0x1111111111111111, 0x2222222222222222}},
    92  		} {
    93  			res, err := module.ExportedFunction(tc.fnName).Call(ctx, tc.in)
    94  			require.NoError(t, err)
    95  			require.Equal(t, tc.exp[:], res)
    96  		}
    97  	})
    98  }
    99  
   100  // Test699 ensures that accessing element instances and data instances works
   101  // without crash even when the access happens in the nested function call.
   102  func Test699(t *testing.T) {
   103  	run(t, func(t *testing.T, r wazero.Runtime) {
   104  		defer r.Close(ctx)
   105  		_, err := r.Instantiate(ctx, getWasmBinary(t, "699"))
   106  		require.NoError(t, err)
   107  	})
   108  }
   109  
   110  // Test701 requires two functions to exit with "out of bounds memory access" consistently across the implementations.
   111  func Test701(t *testing.T) {
   112  	run(t, func(t *testing.T, r wazero.Runtime) {
   113  		module, err := r.Instantiate(ctx, getWasmBinary(t, "701"))
   114  		require.NoError(t, err)
   115  
   116  		_, err = module.ExportedFunction("i32.extend16_s").Call(ctx)
   117  		require.NotNil(t, err)
   118  		require.Contains(t, err.Error(), "out of bounds memory access")
   119  
   120  		_, err = module.ExportedFunction("i32.extend8_s").Call(ctx)
   121  		require.NotNil(t, err)
   122  		require.Contains(t, err.Error(), "out of bounds memory access")
   123  	})
   124  }
   125  
   126  func Test704(t *testing.T) {
   127  	run(t, func(t *testing.T, r wazero.Runtime) {
   128  		_, err := r.Instantiate(ctx, getWasmBinary(t, "704"))
   129  		require.NoError(t, err)
   130  	})
   131  }
   132  
   133  func Test708(t *testing.T) {
   134  	run(t, func(t *testing.T, r wazero.Runtime) {
   135  		_, err := r.Instantiate(ctx, getWasmBinary(t, "708"))
   136  		require.NotNil(t, err)
   137  		require.Contains(t, err.Error(), "out of bounds memory access")
   138  	})
   139  }
   140  
   141  func Test709(t *testing.T) {
   142  	run(t, func(t *testing.T, r wazero.Runtime) {
   143  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "709"))
   144  		require.NoError(t, err)
   145  
   146  		f := mod.ExportedFunction("f64x2.promote_low_f32x4")
   147  		require.NotNil(t, f)
   148  		res, err := f.Call(ctx)
   149  		require.NoError(t, err)
   150  
   151  		require.NotEqual(t, uint64(0), res[0])
   152  		require.NotEqual(t, uint64(0), res[1])
   153  	})
   154  }
   155  
   156  func Test715(t *testing.T) {
   157  	run(t, func(t *testing.T, r wazero.Runtime) {
   158  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "715"))
   159  		require.NoError(t, err)
   160  
   161  		f := mod.ExportedFunction("select on conditional value after table.size")
   162  		require.NotNil(t, f)
   163  		res, err := f.Call(ctx)
   164  		require.NoError(t, err)
   165  
   166  		require.Equal(t, uint64(1), res[0])
   167  	})
   168  }
   169  
   170  func Test716(t *testing.T) {
   171  	run(t, func(t *testing.T, r wazero.Runtime) {
   172  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "716"))
   173  		require.NoError(t, err)
   174  
   175  		f := mod.ExportedFunction("select on ref.func")
   176  		require.NotNil(t, f)
   177  		res, err := f.Call(ctx)
   178  		require.NoError(t, err)
   179  
   180  		require.Equal(t, uint64(1), res[0])
   181  	})
   182  }
   183  
   184  func Test717(t *testing.T) {
   185  	run(t, func(t *testing.T, r wazero.Runtime) {
   186  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "717"))
   187  		require.NoError(t, err)
   188  
   189  		f := mod.ExportedFunction("vectors")
   190  		require.NotNil(t, f)
   191  		res, err := f.Call(ctx)
   192  		require.NoError(t, err)
   193  
   194  		const expectedLen = 35
   195  		require.Equal(t, expectedLen, len(res))
   196  		for i := 0; i < expectedLen; i++ {
   197  			require.Equal(t, uint64(i), res[i])
   198  		}
   199  	})
   200  }
   201  
   202  func Test718(t *testing.T) {
   203  	run(t, func(t *testing.T, r wazero.Runtime) {
   204  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "718"))
   205  		require.NoError(t, err)
   206  
   207  		f := mod.ExportedFunction("v128.load_zero on the ceil")
   208  		require.NotNil(t, f)
   209  		_, err = f.Call(ctx)
   210  		require.NoError(t, err)
   211  	})
   212  }
   213  
   214  func Test719(t *testing.T) {
   215  	run(t, func(t *testing.T, r wazero.Runtime) {
   216  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "719"))
   217  		require.NoError(t, err)
   218  
   219  		f := mod.ExportedFunction("require unreachable")
   220  		require.NotNil(t, f)
   221  		_, err = f.Call(ctx)
   222  		require.Error(t, err)
   223  		require.Contains(t, err.Error(), "wasm error: unreachable\nwasm stack trace:")
   224  	})
   225  }
   226  
   227  func Test720(t *testing.T) {
   228  	run(t, func(t *testing.T, r wazero.Runtime) {
   229  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "720"))
   230  		require.NoError(t, err)
   231  
   232  		f := mod.ExportedFunction("access memory after table.grow")
   233  		require.NotNil(t, f)
   234  		res, err := f.Call(ctx)
   235  		require.NoError(t, err)
   236  		require.Equal(t, uint32(0xffffffff), uint32(res[0]))
   237  	})
   238  }
   239  
   240  func Test721(t *testing.T) {
   241  	run(t, func(t *testing.T, r wazero.Runtime) {
   242  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "721"))
   243  		require.NoError(t, err)
   244  
   245  		f := mod.ExportedFunction("conditional before elem.drop")
   246  		require.NotNil(t, f)
   247  		ret, err := f.Call(ctx)
   248  		require.NoError(t, err)
   249  
   250  		require.Equal(t, uint64(1), ret[0])
   251  	})
   252  }
   253  
   254  func Test722(t *testing.T) {
   255  	run(t, func(t *testing.T, r wazero.Runtime) {
   256  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "722"))
   257  		require.NoError(t, err)
   258  
   259  		f := mod.ExportedFunction("conditional before data.drop")
   260  		require.NotNil(t, f)
   261  		ret, err := f.Call(ctx)
   262  		require.NoError(t, err)
   263  
   264  		require.Equal(t, uint64(1), ret[0])
   265  	})
   266  }
   267  
   268  func Test725(t *testing.T) {
   269  	functions := []string{"i32.load8_s", "i32.load16_s"}
   270  	run(t, func(t *testing.T, r wazero.Runtime) {
   271  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "725"))
   272  		require.NoError(t, err)
   273  
   274  		for _, fn := range functions {
   275  			f := mod.ExportedFunction(fn)
   276  			require.NotNil(t, f)
   277  			_, err := f.Call(ctx)
   278  			require.Error(t, err)
   279  			require.Contains(t, err.Error(), "out of bounds memory")
   280  		}
   281  	})
   282  }
   283  
   284  // Test730 ensures that the vector min/max operations comply with the spec wrt sign bits of zeros:
   285  //
   286  //   - min(0, 0) = 0, min(-0, 0) = -0, min(0, -0) = -0, min(-0, -0) = -0
   287  //   - max(0, 0) = 0, max(-0, 0) =  0, max(0, -0) =  0, max(-0, -0) = -0
   288  func Test730(t *testing.T) {
   289  	tests := []struct {
   290  		name string
   291  		exp  [2]uint64
   292  	}{
   293  		{name: "f32x4.max", exp: [2]uint64{0x80000000 << 32, 0x00000000}},
   294  		{name: "f32x4.min", exp: [2]uint64{0x80000000, 0x80000000<<32 | 0x80000000}},
   295  		{name: "f64x2.max", exp: [2]uint64{0, 0}},
   296  		{name: "f64x2.min", exp: [2]uint64{1 << 63, 1 << 63}},
   297  		{name: "f64x2.max/mix", exp: [2]uint64{0, 1 << 63}},
   298  		{name: "f64x2.min/mix", exp: [2]uint64{1 << 63, 0}},
   299  	}
   300  
   301  	run(t, func(t *testing.T, r wazero.Runtime) {
   302  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "730"))
   303  		require.NoError(t, err)
   304  
   305  		for _, tc := range tests {
   306  			t.Run(tc.name, func(t *testing.T) {
   307  				f := mod.ExportedFunction(tc.name)
   308  				require.NotNil(t, f)
   309  				actual, err := f.Call(ctx)
   310  				require.NoError(t, err)
   311  				require.Equal(t, tc.exp[:], actual)
   312  			})
   313  		}
   314  	})
   315  }
   316  
   317  func Test733(t *testing.T) {
   318  	run(t, func(t *testing.T, r wazero.Runtime) {
   319  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "733"))
   320  		require.NoError(t, err)
   321  
   322  		t.Run("out of bounds", func(t *testing.T) {
   323  			f := mod.ExportedFunction("out of bounds")
   324  			require.NotNil(t, f)
   325  			_, err = f.Call(ctx)
   326  			require.Error(t, err)
   327  			require.Contains(t, err.Error(), "out of bounds memory")
   328  		})
   329  
   330  		t.Run("store higher offset", func(t *testing.T) {
   331  			if testing.Short() {
   332  				// Note: this case uses large memory space, so can be slow like 1 to 2 seconds even without -race.
   333  				// The reason is that this test requires roughly 2GB of in-Wasm memory.
   334  				t.SkipNow()
   335  			}
   336  			f := mod.ExportedFunction("store higher offset")
   337  			require.NotNil(t, f)
   338  			_, err = f.Call(ctx)
   339  			require.NoError(t, err)
   340  
   341  			mem := mod.Memory()
   342  			require.NotNil(t, mem)
   343  
   344  			v, ok := mem.ReadUint64Le(0x80000100)
   345  			require.True(t, ok)
   346  			require.Equal(t, uint64(0xffffffffffffffff), v)
   347  		})
   348  	})
   349  }
   350  
   351  func Test873(t *testing.T) {
   352  	run(t, func(t *testing.T, r wazero.Runtime) {
   353  		_, err := r.Instantiate(ctx, getWasmBinary(t, "873"))
   354  		require.NoError(t, err)
   355  	})
   356  }
   357  
   358  func Test874(t *testing.T) {
   359  	run(t, func(t *testing.T, r wazero.Runtime) {
   360  		_, err := r.Instantiate(ctx, getWasmBinary(t, "874"))
   361  		require.NoError(t, err)
   362  	})
   363  }
   364  
   365  func Test888(t *testing.T) {
   366  	// This tests that importing FuncRef type globals and using it as an initialization of the locally-defined
   367  	// FuncRef global works fine.
   368  	run(t, func(t *testing.T, r wazero.Runtime) {
   369  		imported := binaryencoding.EncodeModule(&wasm.Module{
   370  			MemorySection: &wasm.Memory{Min: 0, Max: 5, IsMaxEncoded: true},
   371  			GlobalSection: []wasm.Global{
   372  				{
   373  					Type: wasm.GlobalType{
   374  						ValType: wasm.ValueTypeFuncref,
   375  						Mutable: false,
   376  					},
   377  					Init: wasm.ConstantExpression{
   378  						Opcode: wasm.OpcodeRefNull,
   379  						Data:   []byte{wasm.ValueTypeFuncref},
   380  					},
   381  				},
   382  			},
   383  			ExportSection: []wasm.Export{
   384  				{Name: "", Type: wasm.ExternTypeGlobal, Index: 0},
   385  				{Name: "s", Type: wasm.ExternTypeMemory, Index: 0},
   386  			},
   387  		})
   388  
   389  		_, err := r.InstantiateWithConfig(ctx, imported, wazero.NewModuleConfig().WithName("host"))
   390  		require.NoError(t, err)
   391  
   392  		_, err = r.InstantiateWithConfig(ctx, getWasmBinary(t, "888"),
   393  			wazero.NewModuleConfig().WithName("test"))
   394  		require.NoError(t, err)
   395  	})
   396  }
   397  
   398  func Test1054(t *testing.T) {
   399  	if !platform.CompilerSupported() {
   400  		return
   401  	}
   402  
   403  	modules := make([]api.Module, 0, 3)
   404  	run(t, func(t *testing.T, r wazero.Runtime) {
   405  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1054"))
   406  		require.NoError(t, err)
   407  		modules = append(modules, mod)
   408  	})
   409  
   410  	// Checks if the memory state is the same between engines.
   411  	exp := modules[0].Memory().(*wasm.MemoryInstance).Buffer
   412  	for i := 1; i < len(modules); i++ {
   413  		actual := modules[i].Memory().(*wasm.MemoryInstance).Buffer
   414  		require.Equal(t, exp, actual)
   415  	}
   416  }
   417  
   418  // Test1777 tests that br_table with multiple args works fine even if
   419  // there might be phi eliminations.
   420  func Test1777(t *testing.T) {
   421  	if !platform.CompilerSupported() {
   422  		return
   423  	}
   424  
   425  	run(t, func(t *testing.T, r wazero.Runtime) {
   426  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1777"))
   427  		require.NoError(t, err)
   428  		f := mod.ExportedFunction("")
   429  		require.NotNil(t, f)
   430  		res, err := f.Call(ctx)
   431  		require.NoError(t, err)
   432  		require.Equal(t, []uint64{18446626425965379583, 4607736361554183979}, res)
   433  	})
   434  }
   435  
   436  // Test1792a tests that v128.const i32x4 is not skipped when state is unreachable.
   437  // This test fails at build-time.
   438  func Test1792a(t *testing.T) {
   439  	if !platform.CompilerSupported() {
   440  		return
   441  	}
   442  	run(t, func(t *testing.T, r wazero.Runtime) {
   443  		_, err := r.Instantiate(ctx, getWasmBinary(t, "1792a"))
   444  		require.NoError(t, err)
   445  	})
   446  }
   447  
   448  // Test1792b tests that OpcodeVhighBits (v128.Bitmask) is typed as V128.
   449  // This test fails at build-time.
   450  func Test1792b(t *testing.T) {
   451  	if !platform.CompilerSupported() {
   452  		return
   453  	}
   454  	run(t, func(t *testing.T, r wazero.Runtime) {
   455  		_, err := r.Instantiate(ctx, getWasmBinary(t, "1792b"))
   456  		require.NoError(t, err)
   457  	})
   458  }
   459  
   460  // Test1792c tests that OpcodeVFcmp (f32x4.eq) is typed as V128.
   461  func Test1792c(t *testing.T) {
   462  	if !platform.CompilerSupported() {
   463  		return
   464  	}
   465  	run(t, func(t *testing.T, r wazero.Runtime) {
   466  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1792c"))
   467  		require.NoError(t, err)
   468  		f := mod.ExportedFunction("")
   469  		require.NotNil(t, f)
   470  		_, err = f.Call(ctx, 0, 0, 0)
   471  		require.NoError(t, err)
   472  		m := mod.(*wasm.ModuleInstance)
   473  
   474  		lo, hi := m.Globals[0].Value()
   475  		require.Equal(t, uint64(5044022786561933312), lo)
   476  		require.Equal(t, uint64(9205357640488583168), hi)
   477  	})
   478  }
   479  
   480  // Test1793a tests that OpcodeVAllTrue is lowered to the right registers.
   481  func Test1793a(t *testing.T) {
   482  	if !platform.CompilerSupported() {
   483  		return
   484  	}
   485  	run(t, func(t *testing.T, r wazero.Runtime) {
   486  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1793a"))
   487  		require.NoError(t, err)
   488  		m := mod.(*wasm.ModuleInstance)
   489  		_, err = m.ExportedFunction("").Call(ctx)
   490  		require.NoError(t, err)
   491  		lo, hi := m.Globals[2].Value()
   492  		require.Equal(t, uint64(2531906066518671488), lo)
   493  		require.Equal(t, uint64(18446744073709551615), hi)
   494  	})
   495  }
   496  
   497  // Test1793b tests that OpcodeVIcmp, OpcodeVFcmp are lowered to the right registers.
   498  func Test1793b(t *testing.T) {
   499  	if !platform.CompilerSupported() {
   500  		return
   501  	}
   502  	run(t, func(t *testing.T, r wazero.Runtime) {
   503  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1793b"))
   504  		require.NoError(t, err)
   505  		m := mod.(*wasm.ModuleInstance)
   506  		_, err = m.ExportedFunction("").Call(ctx, 0, 0, 0, 0)
   507  		require.NoError(t, err)
   508  		lo, hi := m.Globals[1].Value()
   509  		require.Equal(t, uint64(18374967954648334335), lo)
   510  		require.Equal(t, uint64(18446744073709551615), hi)
   511  	})
   512  }
   513  
   514  // Test1793c tests that OpcodeVIcmp is lowered to the right registers.
   515  func Test1793c(t *testing.T) {
   516  	if !platform.CompilerSupported() {
   517  		return
   518  	}
   519  	run(t, func(t *testing.T, r wazero.Runtime) {
   520  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1793c"))
   521  		require.NoError(t, err)
   522  		m := mod.(*wasm.ModuleInstance)
   523  		_, err = m.ExportedFunction("").Call(ctx, 0, 0)
   524  		require.NoError(t, err)
   525  		lo, hi := m.Globals[0].Value()
   526  		require.Equal(t, uint64(18446744073709551615), lo)
   527  		require.Equal(t, uint64(18446744073709551615), hi)
   528  	})
   529  }
   530  
   531  // Test1793c tests that OpcodeVShift is lowered to the right registers.
   532  func Test1793d(t *testing.T) {
   533  	if !platform.CompilerSupported() {
   534  		return
   535  	}
   536  	run(t, func(t *testing.T, r wazero.Runtime) {
   537  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1793d"))
   538  		require.NoError(t, err)
   539  		m := mod.(*wasm.ModuleInstance)
   540  		_, err = m.ExportedFunction("").Call(ctx)
   541  		require.NoError(t, err)
   542  		require.Equal(t, uint64(0), m.Globals[1].Val)
   543  	})
   544  }
   545  
   546  // Test1797a tests that i8x16.shl uses the right register types when lowered.
   547  func Test1797a(t *testing.T) {
   548  	if !platform.CompilerSupported() {
   549  		return
   550  	}
   551  	run(t, func(t *testing.T, r wazero.Runtime) {
   552  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1797a"))
   553  		require.NoError(t, err)
   554  		m := mod.(*wasm.ModuleInstance)
   555  		res, err := m.ExportedFunction("").Call(ctx)
   556  		require.NoError(t, err)
   557  		require.Equal(t, uint64(0), res[0])
   558  	})
   559  }
   560  
   561  // Test1797a tests that i16x8.shr_u uses the right register types when lowered.
   562  func Test1797b(t *testing.T) {
   563  	if !platform.CompilerSupported() {
   564  		return
   565  	}
   566  	run(t, func(t *testing.T, r wazero.Runtime) {
   567  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1797b"))
   568  		require.NoError(t, err)
   569  		m := mod.(*wasm.ModuleInstance)
   570  		_, err = m.ExportedFunction("\x00\x00\x00\x00\x00").Call(ctx, 0, 0, 0, 0, 0, 0)
   571  		require.NoError(t, err)
   572  		lo, hi := m.Globals[0].Value()
   573  		require.Equal(t, uint64(2666130977255796624), lo)
   574  		require.Equal(t, uint64(9223142857682330634), hi)
   575  	})
   576  }
   577  
   578  // Test1797c tests that the program counter for V128*Shuffle is advanced correctly
   579  // even when an unreachable instruction is present.
   580  func Test1797c(t *testing.T) {
   581  	if !platform.CompilerSupported() {
   582  		return
   583  	}
   584  	run(t, func(t *testing.T, r wazero.Runtime) {
   585  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1797c"))
   586  		require.NoError(t, err)
   587  		m := mod.(*wasm.ModuleInstance)
   588  		params := make([]uint64, 20)
   589  		_, err = m.ExportedFunction("~zz\x00E1E\x00EE\x00$").Call(ctx, params...)
   590  		require.Error(t, err, "wasm error: unreachable")
   591  	})
   592  }
   593  
   594  // Test1797d tests that the registers are allocated correctly in Vbitselect.
   595  func Test1797d(t *testing.T) {
   596  	if !platform.CompilerSupported() {
   597  		return
   598  	}
   599  	run(t, func(t *testing.T, r wazero.Runtime) {
   600  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1797d"))
   601  		require.NoError(t, err)
   602  		m := mod.(*wasm.ModuleInstance)
   603  		params := make([]uint64, 20)
   604  		_, err = m.ExportedFunction("p").Call(ctx, params...)
   605  		require.NoError(t, err)
   606  		lo, hi := m.Globals[2].Value()
   607  		require.Equal(t, uint64(15092115255309870764), lo)
   608  		require.Equal(t, uint64(9241386435284803069), hi)
   609  	})
   610  }
   611  
   612  // Test1802 tests that load32_splat computes the load from the right offset
   613  // when a nonzero value is on the stack.
   614  func Test1802(t *testing.T) {
   615  	if !platform.CompilerSupported() {
   616  		return
   617  	}
   618  	run(t, func(t *testing.T, r wazero.Runtime) {
   619  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1802"))
   620  		require.NoError(t, err, "wasm binary should build successfully")
   621  		m := mod.(*wasm.ModuleInstance)
   622  		_, err = m.ExportedFunction("").Call(ctx)
   623  		require.Contains(t, err.Error(), "wasm error: unreachable")
   624  	})
   625  }
   626  
   627  // Test1812 tests that many constant block params work fine.
   628  func Test1812(t *testing.T) {
   629  	if !platform.CompilerSupported() {
   630  		return
   631  	}
   632  	run(t, func(t *testing.T, r wazero.Runtime) {
   633  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1812"))
   634  		require.NoError(t, err)
   635  		m := mod.(*wasm.ModuleInstance)
   636  		res, err := m.ExportedFunction("").Call(ctx)
   637  		require.NoError(t, err)
   638  		require.Equal(t,
   639  			[]uint64{
   640  				0x8301fd00, 0xfd838783, 0x87878383, 0x9b000087, 0x170001fd,
   641  				0xfd8383fd, 0x87838301, 0x878787, 0x83fd9b00, 0x201fd83, 0x878783,
   642  				0x83fd9b00, 0x9b00fd83, 0xfd8383fd, 0x87838301, 0x87878787,
   643  				0xfd9b0000, 0x87878383, 0x1fd8383,
   644  			}, res)
   645  	})
   646  }
   647  
   648  // Test1817 tests that v128.store uses the right memory layout.
   649  func Test1817(t *testing.T) {
   650  	if !platform.CompilerSupported() {
   651  		return
   652  	}
   653  	run(t, func(t *testing.T, r wazero.Runtime) {
   654  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1817"))
   655  		require.NoError(t, err)
   656  		m := mod.(*wasm.ModuleInstance)
   657  		_, err = m.ExportedFunction("").Call(ctx)
   658  		require.NoError(t, err)
   659  		buf, ok := m.Memory().Read(15616, 16)
   660  		require.True(t, ok)
   661  		require.Equal(t, []uint8{0, 0, 0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, buf)
   662  		lo, hi := m.Globals[0].Value()
   663  		require.Equal(t, uint64(0x8000000080000000), lo)
   664  		require.Equal(t, uint64(0x8000000080000000), hi)
   665  	})
   666  }
   667  
   668  // Test1820 tests that i16x8.narrow_i32x4_u assigns the dest register correctly.
   669  func Test1820(t *testing.T) {
   670  	if !platform.CompilerSupported() {
   671  		return
   672  	}
   673  	run(t, func(t *testing.T, r wazero.Runtime) {
   674  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1820"))
   675  		require.NoError(t, err)
   676  		m := mod.(*wasm.ModuleInstance)
   677  		_, err = m.ExportedFunction("").Call(ctx)
   678  		require.NoError(t, err)
   679  		lo, hi := m.Globals[1].Value()
   680  		require.Equal(t, uint64(0xFFFFFFFFFFFF0000), lo)
   681  		require.Equal(t, uint64(0xFFFF), hi)
   682  	})
   683  }
   684  
   685  // Test1823 tests that f64x2.pmin lowers to BSL with the right register usage
   686  // (condition register gets overwritten).
   687  func Test1823(t *testing.T) {
   688  	if !platform.CompilerSupported() {
   689  		return
   690  	}
   691  	run(t, func(t *testing.T, r wazero.Runtime) {
   692  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1823"))
   693  		require.NoError(t, err)
   694  		m := mod.(*wasm.ModuleInstance)
   695  		_, err = m.ExportedFunction("").Call(ctx)
   696  		require.NoError(t, err)
   697  		lo, hi := m.Globals[0].Value()
   698  		require.Equal(t, uint64(17282609607625994159), lo)
   699  		require.Equal(t, uint64(4671060543367625455), hi)
   700  	})
   701  }
   702  
   703  // Test1825 tests that OpcodeInsertlane allocates correctly the temporary registers.
   704  func Test1825(t *testing.T) {
   705  	if !platform.CompilerSupported() {
   706  		return
   707  	}
   708  	run(t, func(t *testing.T, r wazero.Runtime) {
   709  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1825"))
   710  		require.NoError(t, err)
   711  		m := mod.(*wasm.ModuleInstance)
   712  		_, err = m.ExportedFunction("").Call(ctx)
   713  		require.NoError(t, err)
   714  		lo, hi := m.Globals[6].Value()
   715  		require.Equal(t, uint64(1099511627775), lo)
   716  		require.Equal(t, uint64(18446744073709551615), hi)
   717  	})
   718  }
   719  
   720  // Test1826 tests that lowerFcopysignImpl allocates correctly the temporary registers.
   721  func Test1826(t *testing.T) {
   722  	if !platform.CompilerSupported() {
   723  		return
   724  	}
   725  	run(t, func(t *testing.T, r wazero.Runtime) {
   726  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1826"))
   727  		require.NoError(t, err)
   728  		m := mod.(*wasm.ModuleInstance)
   729  		_, err = m.ExportedFunction("3").Call(ctx, 0, 0)
   730  		require.NoError(t, err)
   731  		lo, hi := m.Globals[0].Value()
   732  		require.Equal(t, uint64(1608723901141126568), lo)
   733  		require.Equal(t, uint64(0), hi)
   734  	})
   735  }
   736  
   737  func Test1846(t *testing.T) {
   738  	if !platform.CompilerSupported() {
   739  		return
   740  	}
   741  	run(t, func(t *testing.T, r wazero.Runtime) {
   742  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1846"))
   743  		require.NoError(t, err)
   744  		m := mod.(*wasm.ModuleInstance)
   745  		_, err = m.ExportedFunction("").Call(ctx)
   746  		require.NoError(t, err)
   747  		lo, hi := m.Globals[0].Value()
   748  		require.Equal(t, math.Float64bits(2), lo)
   749  		require.Equal(t, uint64(0), hi)
   750  	})
   751  }
   752  
   753  // Test1847 verifies that an attempt to write a v128 value to an OOB memory location
   754  // does not result in a partial write (e.g. lower 64 bits) to memory.
   755  func Test1949(t *testing.T) {
   756  	if !platform.CompilerSupported() {
   757  		return
   758  	}
   759  	const offset = 65526
   760  	run(t, func(t *testing.T, r wazero.Runtime) {
   761  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1949"))
   762  		require.NoError(t, err)
   763  		_, err = mod.ExportedFunction("").Call(ctx)
   764  		require.Error(t, err)
   765  
   766  		read, ok := mod.Memory().Read(offset, 8)
   767  		require.True(t, ok)
   768  		require.Equal(t, []byte{0xfe, 0xca, 0xfe, 0xca, 0, 0, 0, 0}, read)
   769  	})
   770  }
   771  
   772  func Test1999(t *testing.T) {
   773  	if !platform.CompilerSupported() {
   774  		return
   775  	}
   776  	run(t, func(t *testing.T, r wazero.Runtime) {
   777  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "1999"))
   778  		require.NoError(t, err)
   779  		_, err = mod.ExportedFunction("").Call(ctx, 0)
   780  		require.Error(t, err)
   781  		require.Contains(t, err.Error(), "unreachable")
   782  	})
   783  }
   784  
   785  func Test2000a(t *testing.T) {
   786  	if !platform.CompilerSupported() {
   787  		return
   788  	}
   789  	run(t, func(t *testing.T, r wazero.Runtime) {
   790  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2000a"))
   791  		require.NoError(t, err)
   792  		_, err = mod.ExportedFunction("").Call(ctx, 0)
   793  		require.NoError(t, err)
   794  	})
   795  }
   796  
   797  func Test2000b(t *testing.T) {
   798  	if !platform.CompilerSupported() {
   799  		return
   800  	}
   801  	run(t, func(t *testing.T, r wazero.Runtime) {
   802  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2000b"))
   803  		require.NoError(t, err)
   804  		_, err = mod.ExportedFunction("").Call(ctx)
   805  		require.Error(t, err)
   806  		require.Contains(t, err.Error(), "integer overflow")
   807  	})
   808  }
   809  
   810  func Test2001(t *testing.T) {
   811  	if !platform.CompilerSupported() {
   812  		return
   813  	}
   814  	run(t, func(t *testing.T, r wazero.Runtime) {
   815  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2001"))
   816  		require.NoError(t, err)
   817  		_, err = mod.ExportedFunction("").Call(ctx)
   818  		require.NoError(t, err)
   819  	})
   820  }
   821  
   822  func Test2006(t *testing.T) {
   823  	if !platform.CompilerSupported() {
   824  		return
   825  	}
   826  	run(t, func(t *testing.T, r wazero.Runtime) {
   827  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2006"))
   828  		require.NoError(t, err)
   829  		_, err = mod.ExportedFunction("").Call(ctx, 0)
   830  		require.NoError(t, err)
   831  	})
   832  }
   833  
   834  func Test2007(t *testing.T) {
   835  	if !platform.CompilerSupported() {
   836  		return
   837  	}
   838  	run(t, func(t *testing.T, r wazero.Runtime) {
   839  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2007"))
   840  		require.NoError(t, err)
   841  		_, err = mod.ExportedFunction("").Call(ctx)
   842  		require.Error(t, err)
   843  		require.Contains(t, err.Error(), "integer overflow")
   844  	})
   845  }
   846  
   847  func Test2008(t *testing.T) {
   848  	if !platform.CompilerSupported() {
   849  		return
   850  	}
   851  	run(t, func(t *testing.T, r wazero.Runtime) {
   852  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2008"))
   853  		require.NoError(t, err)
   854  		_, err = mod.ExportedFunction("").Call(ctx)
   855  		require.Error(t, err)
   856  		require.Contains(t, err.Error(), "unreachable")
   857  	})
   858  }
   859  
   860  func Test2009(t *testing.T) {
   861  	if !platform.CompilerSupported() {
   862  		return
   863  	}
   864  	run(t, func(t *testing.T, r wazero.Runtime) {
   865  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2009"))
   866  		require.NoError(t, err)
   867  		res, err := mod.ExportedFunction("").Call(ctx)
   868  		require.NoError(t, err)
   869  		require.Equal(t, res, []uint64{0})
   870  	})
   871  }
   872  
   873  func Test2017(t *testing.T) {
   874  	if !platform.CompilerSupported() {
   875  		return
   876  	}
   877  	run(t, func(t *testing.T, r wazero.Runtime) {
   878  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2017"))
   879  		require.NoError(t, err)
   880  		_, err = mod.ExportedFunction("").Call(ctx)
   881  		require.Error(t, err)
   882  		require.Contains(t, err.Error(), "integer divide by zero")
   883  	})
   884  }
   885  
   886  func Test2031(t *testing.T) {
   887  	if !platform.CompilerSupported() {
   888  		return
   889  	}
   890  	if runtime.GOARCH == "amd64" && runtime.GOOS == "darwin" {
   891  		t.Skip()
   892  	}
   893  	run(t, func(t *testing.T, r wazero.Runtime) {
   894  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2031"))
   895  		require.NoError(t, err)
   896  		res, err := mod.ExportedFunction("").Call(ctx, 0)
   897  		require.NoError(t, err)
   898  		require.Equal(t, []uint64{2139095040, 9218868437227405312}, res)
   899  	})
   900  }
   901  
   902  func Test2034(t *testing.T) {
   903  	if !platform.CompilerSupported() {
   904  		return
   905  	}
   906  	run(t, func(t *testing.T, r wazero.Runtime) {
   907  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2034"))
   908  		require.NoError(t, err)
   909  		res, err := mod.ExportedFunction("").Call(ctx, make([]uint64, 20)...)
   910  		require.NoError(t, err)
   911  		require.Equal(t, []uint64{0xf0f0f280f0f280f, 0x0, 0x0, 0x0, 0x0}, res)
   912  	})
   913  }
   914  
   915  func Test2037(t *testing.T) {
   916  	if !platform.CompilerSupported() {
   917  		return
   918  	}
   919  	run(t, func(t *testing.T, r wazero.Runtime) {
   920  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2037"))
   921  		require.NoError(t, err)
   922  		res, err := mod.ExportedFunction("").Call(ctx)
   923  		require.NoError(t, err)
   924  		require.Equal(t, []uint64{0x0, 0x0, 0xbbbbbbbbbbbbbbbb, 0xbbbbbbbbbbbbbbbb, 0xcb6151c8d497b060, 0xbbbbbbbbbbbbbbbb, 0xe71c3971a22b233b, 0xa0a0a0a0a0a0a0a, 0x0, 0xfffffffb00000030, 0x0, 0x6c6cbbbbbbbbbbbb, 0xfeb44590ef194fa2, 0x1313131313131313, 0x1898a98e9daf4f22, 0xf8f8f8f80a0a0aa0, 0x6c6c6c6c6c6cf1f8, 0x6c6c6c6c6c6c6c6c, 0x9abbbbbbbbbbbb6c, 0x9a9ad39a9a9a9a9a}, res)
   925  	})
   926  }
   927  
   928  func Test2040(t *testing.T) {
   929  	if !platform.CompilerSupported() {
   930  		return
   931  	}
   932  	run(t, func(t *testing.T, r wazero.Runtime) {
   933  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2040"))
   934  		require.NoError(t, err)
   935  		_, err = mod.ExportedFunction("").Call(ctx, 0)
   936  		require.NoError(t, err)
   937  	})
   938  }
   939  
   940  func Test2057(t *testing.T) {
   941  	if !platform.CompilerSupported() {
   942  		return
   943  	}
   944  	run(t, func(t *testing.T, r wazero.Runtime) {
   945  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2057"))
   946  		require.NoError(t, err)
   947  		res, err := mod.ExportedFunction("").Call(ctx, 0)
   948  		require.NoError(t, err)
   949  		require.Equal(t, []uint64{0xe2012900e20129, 0xe2012900e20129}, res)
   950  	})
   951  }
   952  
   953  func Test2058(t *testing.T) {
   954  	if !platform.CompilerSupported() {
   955  		return
   956  	}
   957  	run(t, func(t *testing.T, r wazero.Runtime) {
   958  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2058"))
   959  		require.NoError(t, err)
   960  		_, err = mod.ExportedFunction("").Call(ctx, 0, 0, 0)
   961  		require.NoError(t, err)
   962  		m := mod.(*wasm.ModuleInstance)
   963  		lo, hi := m.Globals[0].Value()
   964  		require.Equal(t, uint64(18446744073709551615), lo)
   965  		require.Equal(t, uint64(0), hi)
   966  	})
   967  }
   968  
   969  func Test2060(t *testing.T) {
   970  	if !platform.CompilerSupported() {
   971  		return
   972  	}
   973  	run(t, func(t *testing.T, r wazero.Runtime) {
   974  		mod, err := r.Instantiate(ctx, getWasmBinary(t, "2060"))
   975  		require.NoError(t, err)
   976  		_, err = mod.ExportedFunction("").Call(ctx, 0)
   977  		require.NoError(t, err)
   978  		m := mod.(*wasm.ModuleInstance)
   979  		lo, hi := m.Globals[0].Value()
   980  		require.Equal(t, uint64(1), lo)
   981  		require.Equal(t, uint64(1), hi)
   982  		lo, hi = m.Globals[1].Value()
   983  		require.Equal(t, uint64(18446744073709551615), lo)
   984  		require.Equal(t, uint64(18446744073709551615), hi)
   985  	})
   986  }
   987  
   988  func Test2070(t *testing.T) {
   989  	run(t, func(t *testing.T, r wazero.Runtime) {
   990  		_, err := r.Instantiate(ctx, getWasmBinary(t, "2070"))
   991  		require.NoError(t, err)
   992  	})
   993  }
   994  
   995  func Test2078(t *testing.T) {
   996  	if !platform.CompilerSupported() {
   997  		return
   998  	}
   999  	nodiff.RequireNoDiffT(t, getWasmBinary(t, "2078"), true, true)
  1000  }
  1001  
  1002  func Test2082(t *testing.T) {
  1003  	if !platform.CompilerSupported() {
  1004  		return
  1005  	}
  1006  	nodiff.RequireNoDiffT(t, getWasmBinary(t, "2082"), true, true)
  1007  }
  1008  
  1009  func Test2084(t *testing.T) {
  1010  	if !platform.CompilerSupported() {
  1011  		return
  1012  	}
  1013  	nodiff.RequireNoDiffT(t, getWasmBinary(t, "2084"), true, true)
  1014  }
  1015  
  1016  func Test2096(t *testing.T) {
  1017  	if !platform.CompilerSupported() {
  1018  		return
  1019  	}
  1020  	nodiff.RequireNoDiffT(t, getWasmBinary(t, "2096"), true, true)
  1021  }
  1022  
  1023  func Test2097(t *testing.T) {
  1024  	bin, err := hex.DecodeString("0061736d010000000107016000037f7f7f02010003030200000a49022f0010010004001001027d00024002400240024002400240440000000000000000000b0b0b0b0b0b000b0005000b000b1703017c017c017e00000000000000000000000b43420b0b")
  1025  	require.NoError(t, err)
  1026  
  1027  	r := wazero.NewRuntime(context.Background())
  1028  	_, err = r.Instantiate(ctx, bin)
  1029  	require.Error(t, err)
  1030  	require.Contains(t, err.Error(), "unexpected end of function")
  1031  }
  1032  
  1033  func Test2112(t *testing.T) {
  1034  	bin, err := hex.DecodeString("0061736d0100000001050160017e00020100030201000404017000000a06010400fc300b")
  1035  	require.NoError(t, err)
  1036  	if !platform.CompilerSupported() {
  1037  		return
  1038  	}
  1039  
  1040  	r := wazero.NewRuntimeWithConfig(context.Background(), wazero.NewRuntimeConfigCompiler())
  1041  	_, err = r.Instantiate(ctx, bin)
  1042  	require.Error(t, err)
  1043  	require.Contains(t, err.Error(), "invalid function[0]: unknown misc opcode 0x30")
  1044  }
  1045  
  1046  func Test2118(t *testing.T) {
  1047  	if !platform.CompilerSupported() {
  1048  		return
  1049  	}
  1050  	nodiff.RequireNoDiffT(t, getWasmBinary(t, "2118"), true, true)
  1051  }
  1052  
  1053  func Test2131(t *testing.T) {
  1054  	if !platform.CompilerSupported() {
  1055  		return
  1056  	}
  1057  	nodiff.RequireNoDiffT(t, getWasmBinary(t, "2131"), true, true)
  1058  }
  1059  
  1060  func Test2136(t *testing.T) {
  1061  	if !platform.CompilerSupported() {
  1062  		return
  1063  	}
  1064  	nodiff.RequireNoDiffT(t, getWasmBinary(t, "2136"), true, true)
  1065  }
  1066  
  1067  func Test2137(t *testing.T) {
  1068  	if !platform.CompilerSupported() {
  1069  		return
  1070  	}
  1071  	nodiff.RequireNoDiffT(t, getWasmBinary(t, "2137"), true, true)
  1072  }
  1073  
  1074  func Test2140(t *testing.T) {
  1075  	if !platform.CompilerSupported() {
  1076  		return
  1077  	}
  1078  	nodiff.RequireNoDiffT(t, getWasmBinary(t, "2140"), true, true)
  1079  }
  1080  
  1081  func Test2201(t *testing.T) {
  1082  	if !platform.CompilerSupported() {
  1083  		return
  1084  	}
  1085  	nodiff.RequireNoDiffT(t, getWasmBinary(t, "2201"), false, false)
  1086  }