github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/testing/nodiff/nodiff_test.go (about)

     1  package nodiff
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/tetratelabs/wazero/internal/testing/require"
     7  	"github.com/tetratelabs/wazero/internal/wasm"
     8  )
     9  
    10  func Test_ensureMutableGlobalsMatch(t *testing.T) {
    11  	for _, tc := range []struct {
    12  		name   string
    13  		cm, im *wasm.ModuleInstance
    14  		expErr string
    15  	}{
    16  		{
    17  			name: "no globals",
    18  			cm:   &wasm.ModuleInstance{},
    19  			im:   &wasm.ModuleInstance{},
    20  		},
    21  		{
    22  			name: "i32 match",
    23  			cm: &wasm.ModuleInstance{
    24  				Globals: []*wasm.GlobalInstance{
    25  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
    26  					{Val: 10, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeI32}},
    27  				},
    28  			},
    29  			im: &wasm.ModuleInstance{
    30  				Globals: []*wasm.GlobalInstance{
    31  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
    32  					{Val: 10, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeI32}},
    33  				},
    34  			},
    35  		},
    36  		{
    37  			name: "i32 match not match",
    38  			cm: &wasm.ModuleInstance{
    39  				Globals: []*wasm.GlobalInstance{
    40  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
    41  					{Val: 10, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeI32}},
    42  				},
    43  			},
    44  			im: &wasm.ModuleInstance{
    45  				Globals: []*wasm.GlobalInstance{
    46  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
    47  					{Val: 11, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeI32}},
    48  				},
    49  			},
    50  			expErr: `mutable globals mismatch:
    51  	[1] i32: 10 != 11`,
    52  		},
    53  		{
    54  			name: "i64 match",
    55  			cm: &wasm.ModuleInstance{
    56  				Globals: []*wasm.GlobalInstance{
    57  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
    58  					{Val: 1 << 62, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeI64}},
    59  				},
    60  			},
    61  			im: &wasm.ModuleInstance{
    62  				Globals: []*wasm.GlobalInstance{
    63  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
    64  					{Val: 1 << 62, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeI64}},
    65  				},
    66  			},
    67  		},
    68  		{
    69  			name: "i64 match not match",
    70  			cm: &wasm.ModuleInstance{
    71  				Globals: []*wasm.GlobalInstance{
    72  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
    73  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
    74  					{Val: 1 << 62, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeI64}},
    75  				},
    76  			},
    77  			im: &wasm.ModuleInstance{
    78  				Globals: []*wasm.GlobalInstance{
    79  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
    80  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
    81  					{Val: 1 << 63, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeI64}},
    82  				},
    83  			},
    84  			expErr: `mutable globals mismatch:
    85  	[2] i64: 4611686018427387904 != 9223372036854775808`,
    86  		},
    87  		{
    88  			name: "f32 match",
    89  			cm: &wasm.ModuleInstance{
    90  				Globals: []*wasm.GlobalInstance{
    91  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
    92  					{Val: 10, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeF32}},
    93  				},
    94  			},
    95  			im: &wasm.ModuleInstance{
    96  				Globals: []*wasm.GlobalInstance{
    97  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
    98  					{Val: 10, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeF32}},
    99  				},
   100  			},
   101  		},
   102  		{
   103  			name: "f32 match not match",
   104  			cm: &wasm.ModuleInstance{
   105  				Globals: []*wasm.GlobalInstance{
   106  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   107  					{Val: 10, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeF32}},
   108  				},
   109  			},
   110  			im: &wasm.ModuleInstance{
   111  				Globals: []*wasm.GlobalInstance{
   112  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   113  					{Val: 11, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeF32}},
   114  				},
   115  			},
   116  			expErr: `mutable globals mismatch:
   117  	[1] f32: 10 != 11`,
   118  		},
   119  		{
   120  			name: "f64 match",
   121  			cm: &wasm.ModuleInstance{
   122  				Globals: []*wasm.GlobalInstance{
   123  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   124  					{Val: 1 << 62, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeF64}},
   125  				},
   126  			},
   127  			im: &wasm.ModuleInstance{
   128  				Globals: []*wasm.GlobalInstance{
   129  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   130  					{Val: 1 << 62, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeF64}},
   131  				},
   132  			},
   133  		},
   134  		{
   135  			name: "f64 match not match",
   136  			cm: &wasm.ModuleInstance{
   137  				Globals: []*wasm.GlobalInstance{
   138  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   139  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   140  					{Val: 1 << 62, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeF64}},
   141  				},
   142  			},
   143  			im: &wasm.ModuleInstance{
   144  				Globals: []*wasm.GlobalInstance{
   145  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   146  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   147  					{Val: 1 << 63, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeF64}},
   148  				},
   149  			},
   150  			expErr: `mutable globals mismatch:
   151  	[2] f64: 4611686018427387904 != 9223372036854775808`,
   152  		},
   153  
   154  		{
   155  			name: "v128 match",
   156  			cm: &wasm.ModuleInstance{
   157  				Globals: []*wasm.GlobalInstance{
   158  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   159  					{ValHi: 1 << 62, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeV128}},
   160  				},
   161  			},
   162  			im: &wasm.ModuleInstance{
   163  				Globals: []*wasm.GlobalInstance{
   164  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   165  					{ValHi: 1 << 62, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeV128}},
   166  				},
   167  			},
   168  		},
   169  		{
   170  			name: "v128 match not match",
   171  			cm: &wasm.ModuleInstance{
   172  				Globals: []*wasm.GlobalInstance{
   173  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   174  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   175  					{Val: 1 << 62, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeV128}},
   176  				},
   177  			},
   178  			im: &wasm.ModuleInstance{
   179  				Globals: []*wasm.GlobalInstance{
   180  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   181  					{Type: wasm.GlobalType{ValType: wasm.ValueTypeV128}},
   182  					{Val: 1 << 62, ValHi: 1234, Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeV128}},
   183  				},
   184  			},
   185  			expErr: `mutable globals mismatch:
   186  	[2] v128: (4611686018427387904,0) != (4611686018427387904,1234)`,
   187  		},
   188  	} {
   189  		t.Run(tc.name, func(t *testing.T) {
   190  			var actualErr error
   191  
   192  			// Append the "fuel" inserted by the fuzzer, which will be ignored by ensureMutableGlobalsMatch.
   193  			tc.im.Globals = append(tc.im.Globals, &wasm.GlobalInstance{Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeI32}, Val: 10000})
   194  			tc.cm.Globals = append(tc.cm.Globals, &wasm.GlobalInstance{Type: wasm.GlobalType{Mutable: true, ValType: wasm.ValueTypeI32}, Val: 1})
   195  			ensureMutableGlobalsMatch(tc.cm, tc.im, func(err error) {
   196  				actualErr = err
   197  			})
   198  			if tc.expErr == "" {
   199  				require.NoError(t, actualErr)
   200  			} else {
   201  				require.Equal(t, tc.expErr, actualErr.Error())
   202  			}
   203  		})
   204  	}
   205  }