From eebf21369c01801435e6705835765c3c9a8f9bbe Mon Sep 17 00:00:00 2001 From: GenericHeroGuy <86008856+GenericHeroGuy@users.noreply.github.com> Date: Fri, 15 Nov 2024 00:00:51 +0100 Subject: [PATCH] Check division/modulo by zero (a74a4eb) --- src/bluajit/lj_errmsg.h | 4 +++ src/bluajit/lj_meta.c | 2 ++ src/bluajit/lj_opt_narrow.c | 1 + src/bluajit/lj_parse.c | 12 ++++++--- src/bluajit/vm_x64.dasc | 53 +++++++++++++------------------------ src/bluajit/vm_x86.dasc | 41 +++++++++------------------- 6 files changed, 46 insertions(+), 67 deletions(-) diff --git a/src/bluajit/lj_errmsg.h b/src/bluajit/lj_errmsg.h index 943e3e0d5..bd4a0a024 100644 --- a/src/bluajit/lj_errmsg.h +++ b/src/bluajit/lj_errmsg.h @@ -37,6 +37,10 @@ ERRDEF(OPBIT, "perform bitwise operation on") ERRDEF(NOINT, "number has no integer representation") ERRDEF(OPCAT, "concatenate") ERRDEF(OPLEN, "get length of") +#if LJ_INTONLY +ERRDEF(DIVZERO, "division by zero") +ERRDEF(MODZERO, "modulo by zero") +#endif /* Type checks. */ ERRDEF(BADSELF, "calling " LUA_QS " on bad self (%s)") diff --git a/src/bluajit/lj_meta.c b/src/bluajit/lj_meta.c index b633e6fef..50a063011 100644 --- a/src/bluajit/lj_meta.c +++ b/src/bluajit/lj_meta.c @@ -228,6 +228,8 @@ TValue *lj_meta_arith(lua_State *L, TValue *ra, cTValue *rb, cTValue *rc, if ((b = str2num(rb, &tempb)) != NULL && (c = str2num(rc, &tempc)) != NULL) { /* Try coercion first. */ #if LJ_INTONLY + if ((mm == MM_div || mm == MM_mod) && !intV(c)) + lj_err_msg(L, mm == MM_div ? LJ_ERR_DIVZERO : LJ_ERR_MODZERO); setintV(ra, lj_vm_foldarith_int(intV(b), intV(c), (int)mm-MM_add)); #else setnumV(ra, lj_vm_foldarith(numV(b), numV(c), (int)mm-MM_add)); diff --git a/src/bluajit/lj_opt_narrow.c b/src/bluajit/lj_opt_narrow.c index ae9957f01..c0c4d0540 100644 --- a/src/bluajit/lj_opt_narrow.c +++ b/src/bluajit/lj_opt_narrow.c @@ -555,6 +555,7 @@ TRef lj_opt_narrow_arith(jit_State *J, TRef rb, TRef rc, if (!tref_isnum(rb)) rb = emitir(IRTN(IR_CONV), rb, IRCONV_NUM_INT); if (!tref_isnum(rc)) rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT); #if LJ_INTONLY + if (op == IR_DIV) emitir(IRTG(IR_NE, IRT_NUM), rc, lj_ir_knum_zero(J)); return emitir(IRTI(IR_CONV), emitir(IRTN(op), rb, rc), IRCONV_INT_NUM); #else return emitir(IRTN(op), rb, rc); diff --git a/src/bluajit/lj_parse.c b/src/bluajit/lj_parse.c index 79460e32f..0e0b382cf 100644 --- a/src/bluajit/lj_parse.c +++ b/src/bluajit/lj_parse.c @@ -78,6 +78,7 @@ typedef struct ExpDesc { #define expr_numtv(e) check_exp(expr_isnumk((e)), &(e)->u.nval) #define expr_numberV(e) numberVnum(expr_numtv((e))) +#define expr_intV(e) intV(expr_numtv((e))) /* Initialize expression. */ static LJ_AINLINE void expr_init(ExpDesc *e, ExpKind k, uint32_t info) @@ -799,14 +800,16 @@ static void bcemit_branch_f(FuncState *fs, ExpDesc *e) /* Try constant-folding of arithmetic operators. */ static int foldarith(BinOpr opr, ExpDesc *e1, ExpDesc *e2) { - TValue o; - lua_Number n; if (!expr_isnumk_nojump(e1) || !expr_isnumk_nojump(e2)) return 0; #if LJ_INTONLY - n = lj_vm_foldarith_int(expr_numberV(e1), expr_numberV(e2), (int)opr-OPR_ADD); + /* Don't fold division by zero. */ + if ((opr == OPR_DIV || opr == OPR_MOD) && !expr_intV(e2)) return 0; + setintV(&e1->u.nval, + lj_vm_foldarith_int(expr_intV(e1), expr_intV(e2), (int)opr-OPR_ADD)); #else + lua_Number n; + TValue o; n = lj_vm_foldarith(expr_numberV(e1), expr_numberV(e2), (int)opr-OPR_ADD); -#endif setnumV(&o, n); if (tvisnan(&o) || tvismzero(&o)) return 0; /* Avoid NaN and -0 as consts. */ if (LJ_DUALNUM) { @@ -817,6 +820,7 @@ static int foldarith(BinOpr opr, ExpDesc *e1, ExpDesc *e2) } } setnumV(&e1->u.nval, n); +#endif return 1; } diff --git a/src/bluajit/vm_x64.dasc b/src/bluajit/vm_x64.dasc index 6ac9ced56..4d3f659b5 100644 --- a/src/bluajit/vm_x64.dasc +++ b/src/bluajit/vm_x64.dasc @@ -3428,52 +3428,37 @@ static void build_ins(BuildCtx *ctx, BCOp op, int defop) ||case 0: | mov TMPR, [KBASE+RC*8] | mov RC, [BASE+RB*8] - | mov RB, TMPR + | checkint TMPR, ->vmeta_arith_vno | checkint RC, ->vmeta_arith_vno - | checkint RB, ->vmeta_arith_vno - | // Caveat: BASE is rdx. - | mov TMPR, BASE - | cdq; idiv RBd - |.if mod - | mov RBd, edx - |.else - | mov RBd, RCd - |.endif + | test TMPRd, TMPRd + | jz ->vmeta_arith_vno || break; ||case 1: - | mov RB, [BASE+RB*8] + | mov TMPR, [BASE+RB*8] | mov RC, [KBASE+RC*8] - | checkint RB, ->vmeta_arith_nvo + | checkint TMPR, ->vmeta_arith_nvo | checkint RC, ->vmeta_arith_nvo - | mov TMPR, BASE - | cdq; idiv RBd - |.if mod - | mov RCd, edx - |.endif + | test TMPRd, TMPRd + | jz ->vmeta_arith_nvo || break; ||default: | mov TMPR, [BASE+RC*8] | mov RC, [BASE+RB*8] - | mov RB, TMPR - | checkint RB, ->vmeta_arith_vvo + | checkint TMPR, ->vmeta_arith_vvo | checkint RC, ->vmeta_arith_vvo - | mov TMPR, BASE - | cdq; idiv RBd - |.if mod - | mov RBd, edx - |.else - | mov RBd, RCd - |.endif + | test TMPRd, TMPRd + | jz ->vmeta_arith_vvo || break; ||} - | mov BASE, TMPR - ||if (vk == 1) { - | setint RC - | mov [BASE+RA*8], RC - ||} else { - | setint RB - | mov [BASE+RA*8], RB - ||} + | // Caveat: BASE is rdx. + | mov RB, BASE + | cdq; idiv TMPRd + |.if mod + | mov RCd, edx + |.endif + | mov BASE, RB + | setint RC + | mov [BASE+RA*8], RC | ins_next |.endmacro | diff --git a/src/bluajit/vm_x86.dasc b/src/bluajit/vm_x86.dasc index 9aaec2b1c..7794cf5c1 100644 --- a/src/bluajit/vm_x86.dasc +++ b/src/bluajit/vm_x86.dasc @@ -4026,52 +4026,35 @@ static void build_ins(BuildCtx *ctx, BCOp op, int defop) ||case 0: | checkint RB, ->vmeta_arith_vn | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_vn + | cmp dword [KBASE+RC*8], 0; jz ->vmeta_arith_vn | mov RA, [KBASE+RC*8] | mov RC, [BASE+RB*8] - | mov RB, RA - | // Caveat: BASE is edx. - | mov RA, BASE - | cdq; idiv RB - |.if mod - | mov RB, BASE - |.else - | mov RB, RC - |.endif || break; ||case 1: | checkint RB, ->vmeta_arith_nv + | cmp dword [BASE+RB*8], 0; jz ->vmeta_arith_nv | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_nv | mov RC, [KBASE+RC*8] - | mov RB, [BASE+RB*8] - | mov RA, BASE - | cdq; idiv RB - |.if mod - | mov RC, BASE - |.endif + | mov RA, [BASE+RB*8] || break; ||default: | checkint RB, ->vmeta_arith_vv | checkint RC, ->vmeta_arith_vv + | cmp dword [BASE+RC*8], 0; jz ->vmeta_arith_vv | mov RA, [BASE+RC*8] | mov RC, [BASE+RB*8] - | mov RB, RA - | mov RA, BASE - | cdq; idiv RB - |.if mod - | mov RB, BASE - |.else - | mov RB, RC - |.endif || break; ||} - | mov BASE, RA + | // Caveat: BASE is edx. + | mov RB, BASE + | cdq; idiv RA + |.if mod + | mov RC, BASE + |.endif + | mov BASE, RB | movzx RA, PC_RA | mov dword [BASE+RA*8+4], LJ_TISNUM - ||if (vk == 1) { - | mov dword [BASE+RA*8], RC - ||} else { - | mov dword [BASE+RA*8], RB - ||} + | mov dword [BASE+RA*8], RC | ins_next |.endmacro |