Skip to content

Commit d11bfdf

Browse files
authored
Fix fast jit int rem_s and const shl issues (#1213)
int rem_s -1 should return 0 int32 lhs << int32 rhs may cause sanitizer check failure fix codegen I8TOI64, I16TOI64, I64TOI8, I64TOI16 implement codegen neg operations
1 parent ab2e959 commit d11bfdf

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

core/iwasm/fast-jit/cg/x86-64/jit_codegen_x86_64.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,7 +2081,9 @@ convert_r_f64_to_r_u32(x86::Assembler &a, int32 reg_no_dst, int32 reg_no_src)
20812081
static bool
20822082
neg_imm_to_r_i32(x86::Assembler &a, int32 reg_no, int32 data)
20832083
{
2084-
return false;
2084+
Imm imm(-data);
2085+
a.mov(regs_i32[reg_no], imm);
2086+
return true;
20852087
}
20862088

20872089
/**
@@ -2096,7 +2098,9 @@ neg_imm_to_r_i32(x86::Assembler &a, int32 reg_no, int32 data)
20962098
static bool
20972099
neg_r_to_r_i32(x86::Assembler &a, int32 reg_no_dst, int32 reg_no_src)
20982100
{
2099-
return false;
2101+
mov_r_to_r_i32(a, reg_no_dst, reg_no_src);
2102+
a.neg(regs_i32[reg_no_dst]);
2103+
return true;
21002104
}
21012105

21022106
/**
@@ -2111,7 +2115,9 @@ neg_r_to_r_i32(x86::Assembler &a, int32 reg_no_dst, int32 reg_no_src)
21112115
static bool
21122116
neg_imm_to_r_i64(x86::Assembler &a, int32 reg_no, int64 data)
21132117
{
2114-
return false;
2118+
Imm imm(-data);
2119+
a.mov(regs_i64[reg_no], imm);
2120+
return true;
21152121
}
21162122

21172123
/**
@@ -2126,7 +2132,9 @@ neg_imm_to_r_i64(x86::Assembler &a, int32 reg_no, int64 data)
21262132
static bool
21272133
neg_r_to_r_i64(x86::Assembler &a, int32 reg_no_dst, int32 reg_no_src)
21282134
{
2129-
return false;
2135+
mov_r_to_r_i64(a, reg_no_dst, reg_no_src);
2136+
a.neg(regs_i64[reg_no_dst]);
2137+
return true;
21302138
}
21312139

21322140
/**
@@ -5923,7 +5931,7 @@ jit_codegen_gen_native(JitCompContext *cc)
59235931

59245932
case JIT_OP_I8TOI64:
59255933
LOAD_2ARGS();
5926-
CONVERT_R_R(I64, I64, i64, i8, int8);
5934+
CONVERT_R_R(I64, I32, i64, i8, int8);
59275935
break;
59285936

59295937
case JIT_OP_I16TOI32:
@@ -5933,7 +5941,7 @@ jit_codegen_gen_native(JitCompContext *cc)
59335941

59345942
case JIT_OP_I16TOI64:
59355943
LOAD_2ARGS();
5936-
CONVERT_R_R(I64, I64, i64, i16, int16);
5944+
CONVERT_R_R(I64, I32, i64, i16, int16);
59375945
break;
59385946

59395947
case JIT_OP_I32TOI8:
@@ -5988,12 +5996,12 @@ jit_codegen_gen_native(JitCompContext *cc)
59885996

59895997
case JIT_OP_I64TOI8:
59905998
LOAD_2ARGS();
5991-
CONVERT_R_R(I64, I64, i8, i64, int64);
5999+
CONVERT_R_R(I32, I64, i8, i64, int64);
59926000
break;
59936001

59946002
case JIT_OP_I64TOI16:
59956003
LOAD_2ARGS();
5996-
CONVERT_R_R(I64, I64, i16, i64, int64);
6004+
CONVERT_R_R(I32, I64, i16, i64, int64);
59976005
break;
59986006

59996007
case JIT_OP_I64TOI32:

core/iwasm/fast-jit/fe/jit_emit_conversion.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ jit_compile_op_i64_extend_i64(JitCompContext *cc, int8 bitwidth)
185185

186186
POP_I64(value);
187187

188-
tmp = jit_cc_new_reg_I64(cc);
188+
tmp = jit_cc_new_reg_I32(cc);
189189
res = jit_cc_new_reg_I64(cc);
190190

191191
switch (bitwidth) {

core/iwasm/fast-jit/fe/jit_emit_numberic.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ compile_int_div(JitCompContext *cc, IntArithmetic arith_op, bool is_i32,
701701

702702
switch (arith_op) {
703703
case INT_DIV_S:
704-
case INT_REM_S:
704+
{
705705
/* Check integer overflow */
706706
GEN_INSN(CMP, cc->cmp_reg, left,
707707
is_i32 ? NEW_CONST(I32, INT32_MIN)
@@ -723,11 +723,27 @@ compile_int_div(JitCompContext *cc, IntArithmetic arith_op, bool is_i32,
723723
/* Build default div and rem */
724724
return compile_int_div_no_check(cc, arith_op, is_i32, left,
725725
right, res);
726-
return true;
726+
}
727+
case INT_REM_S:
728+
{
729+
GEN_INSN(CMP, cc->cmp_reg, right,
730+
is_i32 ? NEW_CONST(I32, -1) : NEW_CONST(I64, -1LL));
731+
if (is_i32)
732+
GEN_INSN(SELECTEQ, left, cc->cmp_reg, NEW_CONST(I32, 0),
733+
left);
734+
else
735+
GEN_INSN(SELECTEQ, left, cc->cmp_reg, NEW_CONST(I64, 0),
736+
left);
737+
/* Build default div and rem */
738+
return compile_int_div_no_check(cc, arith_op, is_i32, left,
739+
right, res);
740+
}
727741
default:
742+
{
728743
/* Build default div and rem */
729744
return compile_int_div_no_check(cc, arith_op, is_i32, left,
730745
right, res);
746+
}
731747
}
732748
}
733749

@@ -990,7 +1006,17 @@ DEF_UNI_INT_CONST_OPS(shru)
9901006
return 0;
9911007
}
9921008

993-
DEF_BI_INT_CONST_OPS(shl, <<)
1009+
static int32
1010+
do_i32_const_shl(int32 lhs, int32 rhs)
1011+
{
1012+
return (int32)((uint32)lhs << (uint32)rhs);
1013+
}
1014+
1015+
static int64
1016+
do_i64_const_shl(int64 lhs, int64 rhs)
1017+
{
1018+
return (int32)((uint64)lhs << (uint64)rhs);
1019+
}
9941020

9951021
DEF_BI_INT_CONST_OPS(shrs, >>)
9961022

@@ -1505,7 +1531,6 @@ compile_op_float_arithmetic(JitCompContext *cc, FloatArithmetic arith_op,
15051531
}
15061532
case FLOAT_DIV:
15071533
{
1508-
/*TODO: add divided by zero interception */
15091534
GEN_INSN(DIV_S, res, lhs, rhs);
15101535
break;
15111536
}

0 commit comments

Comments
 (0)