interpreter: fix integer Eq instruction (#638)

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
Takeshi Yoneda
2022-06-20 09:02:40 +09:00
committed by GitHub
parent 7fbb8e1703
commit 691a1eddab
4 changed files with 43 additions and 1 deletions

View File

@@ -1058,7 +1058,10 @@ func (ce *callEngine) callNativeFunc(ctx context.Context, callCtx *wasm.CallCont
case wazeroir.OperationKindEq:
var b bool
switch wazeroir.UnsignedType(op.b1) {
case wazeroir.UnsignedTypeI32, wazeroir.UnsignedTypeI64:
case wazeroir.UnsignedTypeI32:
v2, v1 := ce.popValue(), ce.popValue()
b = uint32(v1) == uint32(v2)
case wazeroir.UnsignedTypeI64:
v2, v1 := ce.popValue(), ce.popValue()
b = v1 == v2
case wazeroir.UnsignedTypeF32:

View File

@@ -45,6 +45,7 @@ var tests = map[string]func(t *testing.T, r wazero.Runtime){
"multiple instantiation from same source": testMultipleInstantiation,
"exported function that grows memory": testMemOps,
"import functions with reference type in signature": testReftypeImports,
"overflow integer addition": testOverflow,
}
func TestEngineCompiler(t *testing.T) {
@@ -79,6 +80,8 @@ var (
hugestackWasm []byte
//go:embed testdata/reftype_imports.wasm
reftypeImportsWasm []byte
//go:embed testdata/overflow.wasm
overflowWasm []byte
)
func testReftypeImports(t *testing.T, r wazero.Runtime) {
@@ -120,6 +123,24 @@ func testHugeStack(t *testing.T, r wazero.Runtime) {
require.NoError(t, err)
}
// testOverflow ensures that adding one into the maximum integer results in the
// minimum one. See #636.
func testOverflow(t *testing.T, r wazero.Runtime) {
module, err := r.InstantiateModuleFromBinary(testCtx, overflowWasm)
require.NoError(t, err)
defer module.Close(testCtx)
for _, name := range []string{"i32", "i64"} {
i32 := module.ExportedFunction(name)
require.NotNil(t, i32)
res, err := i32.Call(testCtx)
require.NoError(t, err)
require.Equal(t, uint64(1), res[0])
}
}
func testUnreachable(t *testing.T, r wazero.Runtime) {
callUnreachable := func(nil api.Module) {
panic("panic in host function")

Binary file not shown.

View File

@@ -0,0 +1,18 @@
(module
(global $i32g i32 (i32.const -2147483648)) ;; min of 32-bit signed integer
(global $i64g i64 (i64.const -9223372036854775808)) ;; min of 64-bit signed integer
(func $i32 (export "i32") (result i32)
i32.const 2147483647 ;; max of 32-bit signed integer
i32.const 1
i32.add
global.get $i32g
i32.eq
)
(func $i64 (export "i64") (result i32)
i64.const 9223372036854775807 ;; max of 64-bit signed integer
i64.const 1
i64.add
global.get $i64g
i64.eq
)
)