aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pall <mike>2022-12-22 00:03:06 +0100
committerMike Pall <mike>2022-12-22 00:03:06 +0100
commit8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b (patch)
treedae089564f58db2963bae8e3530c1faae104cc61
parentb2791179ef96d652d00d78d2a8780af690537f6a (diff)
downloadluajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.tar.gz
luajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.tar.bz2
luajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.zip
Avoid negation of signed integers in C that may hold INT*_MIN.
Reported by minoki. Recent C compilers 'take advantage' of the undefined behavior. This completely changes the meaning of expressions like (k == -k).
-rw-r--r--src/lib_bit.c4
-rw-r--r--src/lj_asm_mips.h2
-rw-r--r--src/lj_carith.c2
-rw-r--r--src/lj_cparse.c2
-rw-r--r--src/lj_ctype.c2
-rw-r--r--src/lj_emit_arm.h2
-rw-r--r--src/lj_obj.h2
-rw-r--r--src/lj_opt_fold.c6
-rw-r--r--src/lj_parse.c12
-rw-r--r--src/lj_str.c2
-rw-r--r--src/lj_strscan.c20
-rw-r--r--src/lj_vmmath.c8
12 files changed, 32 insertions, 32 deletions
diff --git a/src/lib_bit.c b/src/lib_bit.c
index 9e75eef3..b7988d70 100644
--- a/src/lib_bit.c
+++ b/src/lib_bit.c
@@ -55,8 +55,8 @@ LJLIB_CF(bit_tohex)
55 int32_t i, n = L->base+1 >= L->top ? 8 : lj_lib_checkbit(L, 2); 55 int32_t i, n = L->base+1 >= L->top ? 8 : lj_lib_checkbit(L, 2);
56 const char *hexdigits = "0123456789abcdef"; 56 const char *hexdigits = "0123456789abcdef";
57 char buf[8]; 57 char buf[8];
58 if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; } 58 if (n < 0) { n = (int32_t)(~(uint32_t)n+1u); hexdigits = "0123456789ABCDEF"; }
59 if (n > 8) n = 8; 59 if ((uint32_t)n > 8) n = 8;
60 for (i = n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; } 60 for (i = n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
61 lua_pushlstring(L, buf, (size_t)n); 61 lua_pushlstring(L, buf, (size_t)n);
62 return 1; 62 return 1;
diff --git a/src/lj_asm_mips.h b/src/lj_asm_mips.h
index c0e491a6..1d4c8a25 100644
--- a/src/lj_asm_mips.h
+++ b/src/lj_asm_mips.h
@@ -1227,7 +1227,7 @@ static void asm_arithov(ASMState *as, IRIns *ir)
1227 Reg right, left, tmp, dest = ra_dest(as, ir, RSET_GPR); 1227 Reg right, left, tmp, dest = ra_dest(as, ir, RSET_GPR);
1228 if (irref_isk(ir->op2)) { 1228 if (irref_isk(ir->op2)) {
1229 int k = IR(ir->op2)->i; 1229 int k = IR(ir->op2)->i;
1230 if (ir->o == IR_SUBOV) k = -k; 1230 if (ir->o == IR_SUBOV) k = (int)(~(unsigned int)k+1u);
1231 if (checki16(k)) { /* (dest < left) == (k >= 0 ? 1 : 0) */ 1231 if (checki16(k)) { /* (dest < left) == (k >= 0 ? 1 : 0) */
1232 left = ra_alloc1(as, ir->op1, RSET_GPR); 1232 left = ra_alloc1(as, ir->op1, RSET_GPR);
1233 asm_guard(as, k >= 0 ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); 1233 asm_guard(as, k >= 0 ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);
diff --git a/src/lj_carith.c b/src/lj_carith.c
index 462dbae4..231d7a8a 100644
--- a/src/lj_carith.c
+++ b/src/lj_carith.c
@@ -205,7 +205,7 @@ static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
205 else 205 else
206 *up = lj_carith_powu64(u0, u1); 206 *up = lj_carith_powu64(u0, u1);
207 break; 207 break;
208 case MM_unm: *up = (uint64_t)-(int64_t)u0; break; 208 case MM_unm: *up = ~u0+1u; break;
209 default: lua_assert(0); break; 209 default: lua_assert(0); break;
210 } 210 }
211 lj_gc_check(L); 211 lj_gc_check(L);
diff --git a/src/lj_cparse.c b/src/lj_cparse.c
index df85d23b..8c681c56 100644
--- a/src/lj_cparse.c
+++ b/src/lj_cparse.c
@@ -477,7 +477,7 @@ static void cp_expr_prefix(CPState *cp, CPValue *k)
477 } else if (cp_opt(cp, '+')) { 477 } else if (cp_opt(cp, '+')) {
478 cp_expr_unary(cp, k); /* Nothing to do (well, integer promotion). */ 478 cp_expr_unary(cp, k); /* Nothing to do (well, integer promotion). */
479 } else if (cp_opt(cp, '-')) { 479 } else if (cp_opt(cp, '-')) {
480 cp_expr_unary(cp, k); k->i32 = -k->i32; 480 cp_expr_unary(cp, k); k->i32 = (int32_t)(~(uint32_t)k->i32+1);
481 } else if (cp_opt(cp, '~')) { 481 } else if (cp_opt(cp, '~')) {
482 cp_expr_unary(cp, k); k->i32 = ~k->i32; 482 cp_expr_unary(cp, k); k->i32 = ~k->i32;
483 } else if (cp_opt(cp, '!')) { 483 } else if (cp_opt(cp, '!')) {
diff --git a/src/lj_ctype.c b/src/lj_ctype.c
index adbacaec..04e8c35c 100644
--- a/src/lj_ctype.c
+++ b/src/lj_ctype.c
@@ -577,7 +577,7 @@ GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned)
577 if (isunsigned) { 577 if (isunsigned) {
578 *--p = 'U'; 578 *--p = 'U';
579 } else if ((int64_t)n < 0) { 579 } else if ((int64_t)n < 0) {
580 n = (uint64_t)-(int64_t)n; 580 n = ~n+1u;
581 sign = 1; 581 sign = 1;
582 } 582 }
583 do { *--p = (char)('0' + n % 10); } while (n /= 10); 583 do { *--p = (char)('0' + n % 10); } while (n /= 10);
diff --git a/src/lj_emit_arm.h b/src/lj_emit_arm.h
index 2db07ef6..633afb49 100644
--- a/src/lj_emit_arm.h
+++ b/src/lj_emit_arm.h
@@ -154,7 +154,7 @@ static int emit_kdelta2(ASMState *as, Reg d, int32_t i)
154 if (other) { 154 if (other) {
155 int32_t delta = i - other; 155 int32_t delta = i - other;
156 uint32_t sh, inv = 0, k2, k; 156 uint32_t sh, inv = 0, k2, k;
157 if (delta < 0) { delta = -delta; inv = ARMI_ADD^ARMI_SUB; } 157 if (delta < 0) { delta = (int32_t)(~(uint32_t)delta+1u); inv = ARMI_ADD^ARMI_SUB; }
158 sh = lj_ffs(delta) & ~1; 158 sh = lj_ffs(delta) & ~1;
159 k2 = emit_isk12(0, delta & (255 << sh)); 159 k2 = emit_isk12(0, delta & (255 << sh));
160 k = emit_isk12(0, delta & ~(255 << sh)); 160 k = emit_isk12(0, delta & ~(255 << sh));
diff --git a/src/lj_obj.h b/src/lj_obj.h
index ea8fe870..ef45ae12 100644
--- a/src/lj_obj.h
+++ b/src/lj_obj.h
@@ -327,7 +327,7 @@ typedef struct GCproto {
327#define PROTO_UV_IMMUTABLE 0x4000 /* Immutable upvalue. */ 327#define PROTO_UV_IMMUTABLE 0x4000 /* Immutable upvalue. */
328 328
329#define proto_kgc(pt, idx) \ 329#define proto_kgc(pt, idx) \
330 check_exp((uintptr_t)(intptr_t)(idx) >= (uintptr_t)-(intptr_t)(pt)->sizekgc, \ 330 check_exp((uintptr_t)(intptr_t)(idx) >= ~(uintptr_t)(pt)->sizekgc+1u, \
331 gcref(mref((pt)->k, GCRef)[(idx)])) 331 gcref(mref((pt)->k, GCRef)[(idx)]))
332#define proto_knumtv(pt, idx) \ 332#define proto_knumtv(pt, idx) \
333 check_exp((uintptr_t)(idx) < (pt)->sizekn, &mref((pt)->k, TValue)[(idx)]) 333 check_exp((uintptr_t)(idx) < (pt)->sizekn, &mref((pt)->k, TValue)[(idx)])
diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c
index e9a6532a..482abdef 100644
--- a/src/lj_opt_fold.c
+++ b/src/lj_opt_fold.c
@@ -236,7 +236,7 @@ static int32_t kfold_intop(int32_t k1, int32_t k2, IROp op)
236 case IR_SUB: k1 -= k2; break; 236 case IR_SUB: k1 -= k2; break;
237 case IR_MUL: k1 *= k2; break; 237 case IR_MUL: k1 *= k2; break;
238 case IR_MOD: k1 = lj_vm_modi(k1, k2); break; 238 case IR_MOD: k1 = lj_vm_modi(k1, k2); break;
239 case IR_NEG: k1 = -k1; break; 239 case IR_NEG: k1 = (int32_t)(~(uint32_t)k1+1u); break;
240 case IR_BAND: k1 &= k2; break; 240 case IR_BAND: k1 &= k2; break;
241 case IR_BOR: k1 |= k2; break; 241 case IR_BOR: k1 |= k2; break;
242 case IR_BXOR: k1 ^= k2; break; 242 case IR_BXOR: k1 ^= k2; break;
@@ -1160,7 +1160,7 @@ LJFOLDF(simplify_intsub_k)
1160 if (fright->i == 0) /* i - 0 ==> i */ 1160 if (fright->i == 0) /* i - 0 ==> i */
1161 return LEFTFOLD; 1161 return LEFTFOLD;
1162 fins->o = IR_ADD; /* i - k ==> i + (-k) */ 1162 fins->o = IR_ADD; /* i - k ==> i + (-k) */
1163 fins->op2 = (IRRef1)lj_ir_kint(J, -fright->i); /* Overflow for -2^31 ok. */ 1163 fins->op2 = (IRRef1)lj_ir_kint(J, (int32_t)(~(uint32_t)fright->i+1u)); /* Overflow for -2^31 ok. */
1164 return RETRYFOLD; 1164 return RETRYFOLD;
1165} 1165}
1166 1166
@@ -1191,7 +1191,7 @@ LJFOLDF(simplify_intsub_k64)
1191 if (k == 0) /* i - 0 ==> i */ 1191 if (k == 0) /* i - 0 ==> i */
1192 return LEFTFOLD; 1192 return LEFTFOLD;
1193 fins->o = IR_ADD; /* i - k ==> i + (-k) */ 1193 fins->o = IR_ADD; /* i - k ==> i + (-k) */
1194 fins->op2 = (IRRef1)lj_ir_kint64(J, (uint64_t)-(int64_t)k); 1194 fins->op2 = (IRRef1)lj_ir_kint64(J, ~k+1u);
1195 return RETRYFOLD; 1195 return RETRYFOLD;
1196} 1196}
1197 1197
diff --git a/src/lj_parse.c b/src/lj_parse.c
index 5a8bcff9..57eb11cc 100644
--- a/src/lj_parse.c
+++ b/src/lj_parse.c
@@ -951,22 +951,22 @@ static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e)
951#if LJ_HASFFI 951#if LJ_HASFFI
952 if (e->k == VKCDATA) { /* Fold in-place since cdata is not interned. */ 952 if (e->k == VKCDATA) { /* Fold in-place since cdata is not interned. */
953 GCcdata *cd = cdataV(&e->u.nval); 953 GCcdata *cd = cdataV(&e->u.nval);
954 int64_t *p = (int64_t *)cdataptr(cd); 954 uint64_t *p = (uint64_t *)cdataptr(cd);
955 if (cd->ctypeid == CTID_COMPLEX_DOUBLE) 955 if (cd->ctypeid == CTID_COMPLEX_DOUBLE)
956 p[1] ^= (int64_t)U64x(80000000,00000000); 956 p[1] ^= U64x(80000000,00000000);
957 else 957 else
958 *p = -*p; 958 *p = ~*p+1u;
959 return; 959 return;
960 } else 960 } else
961#endif 961#endif
962 if (expr_isnumk(e) && !expr_numiszero(e)) { /* Avoid folding to -0. */ 962 if (expr_isnumk(e) && !expr_numiszero(e)) { /* Avoid folding to -0. */
963 TValue *o = expr_numtv(e); 963 TValue *o = expr_numtv(e);
964 if (tvisint(o)) { 964 if (tvisint(o)) {
965 int32_t k = intV(o); 965 int32_t k = intV(o), negk = (int32_t)(~(uint32_t)k+1u);
966 if (k == -k) 966 if (k == negk)
967 setnumV(o, -(lua_Number)k); 967 setnumV(o, -(lua_Number)k);
968 else 968 else
969 setintV(o, -k); 969 setintV(o, negk);
970 return; 970 return;
971 } else { 971 } else {
972 o->u64 ^= U64x(80000000,00000000); 972 o->u64 ^= U64x(80000000,00000000);
diff --git a/src/lj_str.c b/src/lj_str.c
index 60912aed..f1fc8ee1 100644
--- a/src/lj_str.c
+++ b/src/lj_str.c
@@ -190,7 +190,7 @@ size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o)
190/* Print integer to buffer. Returns pointer to start. */ 190/* Print integer to buffer. Returns pointer to start. */
191char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k) 191char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k)
192{ 192{
193 uint32_t u = (uint32_t)(k < 0 ? -k : k); 193 uint32_t u = k < 0 ? ~(uint32_t)k+1u : (uint32_t)k;
194 p += 1+10; 194 p += 1+10;
195 do { *--p = (char)('0' + u % 10); } while (u /= 10); 195 do { *--p = (char)('0' + u % 10); } while (u /= 10);
196 if (k < 0) *--p = '-'; 196 if (k < 0) *--p = '-';
diff --git a/src/lj_strscan.c b/src/lj_strscan.c
index 914cfb7a..9e8023b5 100644
--- a/src/lj_strscan.c
+++ b/src/lj_strscan.c
@@ -124,19 +124,19 @@ static StrScanFmt strscan_hex(const uint8_t *p, TValue *o,
124 case STRSCAN_INT: 124 case STRSCAN_INT:
125 if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg && 125 if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg &&
126 !(x == 0 && neg)) { 126 !(x == 0 && neg)) {
127 o->i = neg ? -(int32_t)x : (int32_t)x; 127 o->i = neg ? (int32_t)(~x+1u) : (int32_t)x;
128 return STRSCAN_INT; /* Fast path for 32 bit integers. */ 128 return STRSCAN_INT; /* Fast path for 32 bit integers. */
129 } 129 }
130 if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; break; } 130 if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; break; }
131 /* fallthrough */ 131 /* fallthrough */
132 case STRSCAN_U32: 132 case STRSCAN_U32:
133 if (dig > 8) return STRSCAN_ERROR; 133 if (dig > 8) return STRSCAN_ERROR;
134 o->i = neg ? -(int32_t)x : (int32_t)x; 134 o->i = neg ? (int32_t)(~x+1u) : (int32_t)x;
135 return STRSCAN_U32; 135 return STRSCAN_U32;
136 case STRSCAN_I64: 136 case STRSCAN_I64:
137 case STRSCAN_U64: 137 case STRSCAN_U64:
138 if (dig > 16) return STRSCAN_ERROR; 138 if (dig > 16) return STRSCAN_ERROR;
139 o->u64 = neg ? (uint64_t)-(int64_t)x : x; 139 o->u64 = neg ? ~x+1u : x;
140 return fmt; 140 return fmt;
141 default: 141 default:
142 break; 142 break;
@@ -168,12 +168,12 @@ static StrScanFmt strscan_oct(const uint8_t *p, TValue *o,
168 /* fallthrough */ 168 /* fallthrough */
169 case STRSCAN_U32: 169 case STRSCAN_U32:
170 if ((x >> 32)) return STRSCAN_ERROR; 170 if ((x >> 32)) return STRSCAN_ERROR;
171 o->i = neg ? -(int32_t)x : (int32_t)x; 171 o->i = neg ? (int32_t)(~(uint32_t)x+1u) : (int32_t)x;
172 break; 172 break;
173 default: 173 default:
174 case STRSCAN_I64: 174 case STRSCAN_I64:
175 case STRSCAN_U64: 175 case STRSCAN_U64:
176 o->u64 = neg ? (uint64_t)-(int64_t)x : x; 176 o->u64 = neg ? ~x+1u : x;
177 break; 177 break;
178 } 178 }
179 return fmt; 179 return fmt;
@@ -229,18 +229,18 @@ static StrScanFmt strscan_dec(const uint8_t *p, TValue *o,
229 switch (fmt) { 229 switch (fmt) {
230 case STRSCAN_INT: 230 case STRSCAN_INT:
231 if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg) { 231 if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg) {
232 o->i = neg ? -(int32_t)x : (int32_t)x; 232 o->i = neg ? (int32_t)(~x+1u) : (int32_t)x;
233 return STRSCAN_INT; /* Fast path for 32 bit integers. */ 233 return STRSCAN_INT; /* Fast path for 32 bit integers. */
234 } 234 }
235 if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; goto plainnumber; } 235 if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; goto plainnumber; }
236 /* fallthrough */ 236 /* fallthrough */
237 case STRSCAN_U32: 237 case STRSCAN_U32:
238 if ((x >> 32) != 0) return STRSCAN_ERROR; 238 if ((x >> 32) != 0) return STRSCAN_ERROR;
239 o->i = neg ? -(int32_t)x : (int32_t)x; 239 o->i = neg ? (int32_t)(~x+1u) : (int32_t)x;
240 return STRSCAN_U32; 240 return STRSCAN_U32;
241 case STRSCAN_I64: 241 case STRSCAN_I64:
242 case STRSCAN_U64: 242 case STRSCAN_U64:
243 o->u64 = neg ? (uint64_t)-(int64_t)x : x; 243 o->u64 = neg ? ~x+1u : x;
244 return fmt; 244 return fmt;
245 default: 245 default:
246 plainnumber: /* Fast path for plain numbers < 2^63. */ 246 plainnumber: /* Fast path for plain numbers < 2^63. */
@@ -418,7 +418,7 @@ StrScanFmt lj_strscan_scan(const uint8_t *p, TValue *o, uint32_t opt)
418 if (xx >= STRSCAN_MAXEXP) return STRSCAN_ERROR; 418 if (xx >= STRSCAN_MAXEXP) return STRSCAN_ERROR;
419 p++; 419 p++;
420 } 420 }
421 ex += negx ? -(int32_t)xx : (int32_t)xx; 421 ex += negx ? (int32_t)(~xx+1u) : (int32_t)xx;
422 } 422 }
423 423
424 /* Parse suffix. */ 424 /* Parse suffix. */
@@ -456,7 +456,7 @@ StrScanFmt lj_strscan_scan(const uint8_t *p, TValue *o, uint32_t opt)
456 o->n = -0.0; 456 o->n = -0.0;
457 return STRSCAN_NUM; 457 return STRSCAN_NUM;
458 } else { 458 } else {
459 o->i = neg ? -(int32_t)x : (int32_t)x; 459 o->i = neg ? (int32_t)(~x+1u) : (int32_t)x;
460 return STRSCAN_INT; 460 return STRSCAN_INT;
461 } 461 }
462 } 462 }
diff --git a/src/lj_vmmath.c b/src/lj_vmmath.c
index ff41ba28..6369bc6b 100644
--- a/src/lj_vmmath.c
+++ b/src/lj_vmmath.c
@@ -66,11 +66,11 @@ int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b)
66{ 66{
67 uint32_t y, ua, ub; 67 uint32_t y, ua, ub;
68 lua_assert(b != 0); /* This must be checked before using this function. */ 68 lua_assert(b != 0); /* This must be checked before using this function. */
69 ua = a < 0 ? (uint32_t)-a : (uint32_t)a; 69 ua = a < 0 ? ~(uint32_t)a+1u : (uint32_t)a;
70 ub = b < 0 ? (uint32_t)-b : (uint32_t)b; 70 ub = b < 0 ? ~(uint32_t)b+1u : (uint32_t)b;
71 y = ua % ub; 71 y = ua % ub;
72 if (y != 0 && (a^b) < 0) y = y - ub; 72 if (y != 0 && (a^b) < 0) y = y - ub;
73 if (((int32_t)y^b) < 0) y = (uint32_t)-(int32_t)y; 73 if (((int32_t)y^b) < 0) y = ~y+1u;
74 return (int32_t)y; 74 return (int32_t)y;
75} 75}
76#endif 76#endif
@@ -105,7 +105,7 @@ double lj_vm_powi(double x, int32_t k)
105 else if (k == 0) 105 else if (k == 0)
106 return 1.0; 106 return 1.0;
107 else 107 else
108 return 1.0 / lj_vm_powui(x, (uint32_t)-k); 108 return 1.0 / lj_vm_powui(x, ~(uint32_t)k+1u);
109} 109}
110 110
111/* Computes fpm(x) for extended math functions. */ 111/* Computes fpm(x) for extended math functions. */