aboutsummaryrefslogtreecommitdiff
path: root/lvm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-02-18 13:22:25 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-02-18 13:22:25 -0300
commit8426d9b4d4df1da3c5b2d759e509ae1c50a86667 (patch)
treeca3b568988aed69a30a466c208c0e23fb5908a0b /lvm.c
parent1f3c6f4534c6411313361697d98d1145a1f030fa (diff)
downloadlua-8426d9b4d4df1da3c5b2d759e509ae1c50a86667.tar.gz
lua-8426d9b4d4df1da3c5b2d759e509ae1c50a86667.tar.bz2
lua-8426d9b4d4df1da3c5b2d759e509ae1c50a86667.zip
Avoid computing invalid addresses
luaV_execute should compute 'ra' only when the instruction uses it. Computing an illegal address is undefined behavior even if the address is never dereferenced.
Diffstat (limited to 'lvm.c')
-rw-r--r--lvm.c121
1 files changed, 89 insertions, 32 deletions
diff --git a/lvm.c b/lvm.c
index 2ec34400..f3a5662b 100644
--- a/lvm.c
+++ b/lvm.c
@@ -898,6 +898,7 @@ void luaV_finishOp (lua_State *L) {
898** operation, 'fop' is the float operation. 898** operation, 'fop' is the float operation.
899*/ 899*/
900#define op_arithI(L,iop,fop) { \ 900#define op_arithI(L,iop,fop) { \
901 StkId ra = RA(i); \
901 TValue *v1 = vRB(i); \ 902 TValue *v1 = vRB(i); \
902 int imm = GETARG_sC(i); \ 903 int imm = GETARG_sC(i); \
903 if (ttisinteger(v1)) { \ 904 if (ttisinteger(v1)) { \
@@ -926,6 +927,7 @@ void luaV_finishOp (lua_State *L) {
926** Arithmetic operations over floats and others with register operands. 927** Arithmetic operations over floats and others with register operands.
927*/ 928*/
928#define op_arithf(L,fop) { \ 929#define op_arithf(L,fop) { \
930 StkId ra = RA(i); \
929 TValue *v1 = vRB(i); \ 931 TValue *v1 = vRB(i); \
930 TValue *v2 = vRC(i); \ 932 TValue *v2 = vRC(i); \
931 op_arithf_aux(L, v1, v2, fop); } 933 op_arithf_aux(L, v1, v2, fop); }
@@ -935,6 +937,7 @@ void luaV_finishOp (lua_State *L) {
935** Arithmetic operations with K operands for floats. 937** Arithmetic operations with K operands for floats.
936*/ 938*/
937#define op_arithfK(L,fop) { \ 939#define op_arithfK(L,fop) { \
940 StkId ra = RA(i); \
938 TValue *v1 = vRB(i); \ 941 TValue *v1 = vRB(i); \
939 TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ 942 TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \
940 op_arithf_aux(L, v1, v2, fop); } 943 op_arithf_aux(L, v1, v2, fop); }
@@ -944,6 +947,7 @@ void luaV_finishOp (lua_State *L) {
944** Arithmetic operations over integers and floats. 947** Arithmetic operations over integers and floats.
945*/ 948*/
946#define op_arith_aux(L,v1,v2,iop,fop) { \ 949#define op_arith_aux(L,v1,v2,iop,fop) { \
950 StkId ra = RA(i); \
947 if (ttisinteger(v1) && ttisinteger(v2)) { \ 951 if (ttisinteger(v1) && ttisinteger(v2)) { \
948 lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ 952 lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \
949 pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ 953 pc++; setivalue(s2v(ra), iop(L, i1, i2)); \
@@ -973,6 +977,7 @@ void luaV_finishOp (lua_State *L) {
973** Bitwise operations with constant operand. 977** Bitwise operations with constant operand.
974*/ 978*/
975#define op_bitwiseK(L,op) { \ 979#define op_bitwiseK(L,op) { \
980 StkId ra = RA(i); \
976 TValue *v1 = vRB(i); \ 981 TValue *v1 = vRB(i); \
977 TValue *v2 = KC(i); \ 982 TValue *v2 = KC(i); \
978 lua_Integer i1; \ 983 lua_Integer i1; \
@@ -986,6 +991,7 @@ void luaV_finishOp (lua_State *L) {
986** Bitwise operations with register operands. 991** Bitwise operations with register operands.
987*/ 992*/
988#define op_bitwise(L,op) { \ 993#define op_bitwise(L,op) { \
994 StkId ra = RA(i); \
989 TValue *v1 = vRB(i); \ 995 TValue *v1 = vRB(i); \
990 TValue *v2 = vRC(i); \ 996 TValue *v2 = vRC(i); \
991 lua_Integer i1; lua_Integer i2; \ 997 lua_Integer i1; lua_Integer i2; \
@@ -1000,18 +1006,19 @@ void luaV_finishOp (lua_State *L) {
1000** integers. 1006** integers.
1001*/ 1007*/
1002#define op_order(L,opi,opn,other) { \ 1008#define op_order(L,opi,opn,other) { \
1003 int cond; \ 1009 StkId ra = RA(i); \
1004 TValue *rb = vRB(i); \ 1010 int cond; \
1005 if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ 1011 TValue *rb = vRB(i); \
1006 lua_Integer ia = ivalue(s2v(ra)); \ 1012 if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \
1007 lua_Integer ib = ivalue(rb); \ 1013 lua_Integer ia = ivalue(s2v(ra)); \
1008 cond = opi(ia, ib); \ 1014 lua_Integer ib = ivalue(rb); \
1009 } \ 1015 cond = opi(ia, ib); \
1010 else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ 1016 } \
1011 cond = opn(s2v(ra), rb); \ 1017 else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \
1012 else \ 1018 cond = opn(s2v(ra), rb); \
1013 Protect(cond = other(L, s2v(ra), rb)); \ 1019 else \
1014 docondjump(); } 1020 Protect(cond = other(L, s2v(ra), rb)); \
1021 docondjump(); }
1015 1022
1016 1023
1017/* 1024/*
@@ -1019,20 +1026,21 @@ void luaV_finishOp (lua_State *L) {
1019** always small enough to have an exact representation as a float.) 1026** always small enough to have an exact representation as a float.)
1020*/ 1027*/
1021#define op_orderI(L,opi,opf,inv,tm) { \ 1028#define op_orderI(L,opi,opf,inv,tm) { \
1022 int cond; \ 1029 StkId ra = RA(i); \
1023 int im = GETARG_sB(i); \ 1030 int cond; \
1024 if (ttisinteger(s2v(ra))) \ 1031 int im = GETARG_sB(i); \
1025 cond = opi(ivalue(s2v(ra)), im); \ 1032 if (ttisinteger(s2v(ra))) \
1026 else if (ttisfloat(s2v(ra))) { \ 1033 cond = opi(ivalue(s2v(ra)), im); \
1027 lua_Number fa = fltvalue(s2v(ra)); \ 1034 else if (ttisfloat(s2v(ra))) { \
1028 lua_Number fim = cast_num(im); \ 1035 lua_Number fa = fltvalue(s2v(ra)); \
1029 cond = opf(fa, fim); \ 1036 lua_Number fim = cast_num(im); \
1030 } \ 1037 cond = opf(fa, fim); \
1031 else { \ 1038 } \
1032 int isf = GETARG_C(i); \ 1039 else { \
1033 Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ 1040 int isf = GETARG_C(i); \
1034 } \ 1041 Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \
1035 docondjump(); } 1042 } \
1043 docondjump(); }
1036 1044
1037/* }================================================================== */ 1045/* }================================================================== */
1038 1046
@@ -1128,7 +1136,6 @@ void luaV_finishOp (lua_State *L) {
1128 updatebase(ci); /* correct stack */ \ 1136 updatebase(ci); /* correct stack */ \
1129 } \ 1137 } \
1130 i = *(pc++); \ 1138 i = *(pc++); \
1131 ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \
1132} 1139}
1133 1140
1134#define vmdispatch(o) switch(o) 1141#define vmdispatch(o) switch(o)
@@ -1164,7 +1171,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1164 /* main loop of interpreter */ 1171 /* main loop of interpreter */
1165 for (;;) { 1172 for (;;) {
1166 Instruction i; /* instruction being executed */ 1173 Instruction i; /* instruction being executed */
1167 StkId ra; /* instruction's A register */
1168 vmfetch(); 1174 vmfetch();
1169 #if 0 1175 #if 0
1170 /* low-level line tracing for debugging Lua */ 1176 /* low-level line tracing for debugging Lua */
@@ -1176,44 +1182,53 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1176 lua_assert(isIT(i) || (cast_void(L->top = base), 1)); 1182 lua_assert(isIT(i) || (cast_void(L->top = base), 1));
1177 vmdispatch (GET_OPCODE(i)) { 1183 vmdispatch (GET_OPCODE(i)) {
1178 vmcase(OP_MOVE) { 1184 vmcase(OP_MOVE) {
1185 StkId ra = RA(i);
1179 setobjs2s(L, ra, RB(i)); 1186 setobjs2s(L, ra, RB(i));
1180 vmbreak; 1187 vmbreak;
1181 } 1188 }
1182 vmcase(OP_LOADI) { 1189 vmcase(OP_LOADI) {
1190 StkId ra = RA(i);
1183 lua_Integer b = GETARG_sBx(i); 1191 lua_Integer b = GETARG_sBx(i);
1184 setivalue(s2v(ra), b); 1192 setivalue(s2v(ra), b);
1185 vmbreak; 1193 vmbreak;
1186 } 1194 }
1187 vmcase(OP_LOADF) { 1195 vmcase(OP_LOADF) {
1196 StkId ra = RA(i);
1188 int b = GETARG_sBx(i); 1197 int b = GETARG_sBx(i);
1189 setfltvalue(s2v(ra), cast_num(b)); 1198 setfltvalue(s2v(ra), cast_num(b));
1190 vmbreak; 1199 vmbreak;
1191 } 1200 }
1192 vmcase(OP_LOADK) { 1201 vmcase(OP_LOADK) {
1202 StkId ra = RA(i);
1193 TValue *rb = k + GETARG_Bx(i); 1203 TValue *rb = k + GETARG_Bx(i);
1194 setobj2s(L, ra, rb); 1204 setobj2s(L, ra, rb);
1195 vmbreak; 1205 vmbreak;
1196 } 1206 }
1197 vmcase(OP_LOADKX) { 1207 vmcase(OP_LOADKX) {
1208 StkId ra = RA(i);
1198 TValue *rb; 1209 TValue *rb;
1199 rb = k + GETARG_Ax(*pc); pc++; 1210 rb = k + GETARG_Ax(*pc); pc++;
1200 setobj2s(L, ra, rb); 1211 setobj2s(L, ra, rb);
1201 vmbreak; 1212 vmbreak;
1202 } 1213 }
1203 vmcase(OP_LOADFALSE) { 1214 vmcase(OP_LOADFALSE) {
1215 StkId ra = RA(i);
1204 setbfvalue(s2v(ra)); 1216 setbfvalue(s2v(ra));
1205 vmbreak; 1217 vmbreak;
1206 } 1218 }
1207 vmcase(OP_LFALSESKIP) { 1219 vmcase(OP_LFALSESKIP) {
1220 StkId ra = RA(i);
1208 setbfvalue(s2v(ra)); 1221 setbfvalue(s2v(ra));
1209 pc++; /* skip next instruction */ 1222 pc++; /* skip next instruction */
1210 vmbreak; 1223 vmbreak;
1211 } 1224 }
1212 vmcase(OP_LOADTRUE) { 1225 vmcase(OP_LOADTRUE) {
1226 StkId ra = RA(i);
1213 setbtvalue(s2v(ra)); 1227 setbtvalue(s2v(ra));
1214 vmbreak; 1228 vmbreak;
1215 } 1229 }
1216 vmcase(OP_LOADNIL) { 1230 vmcase(OP_LOADNIL) {
1231 StkId ra = RA(i);
1217 int b = GETARG_B(i); 1232 int b = GETARG_B(i);
1218 do { 1233 do {
1219 setnilvalue(s2v(ra++)); 1234 setnilvalue(s2v(ra++));
@@ -1221,17 +1236,20 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1221 vmbreak; 1236 vmbreak;
1222 } 1237 }
1223 vmcase(OP_GETUPVAL) { 1238 vmcase(OP_GETUPVAL) {
1239 StkId ra = RA(i);
1224 int b = GETARG_B(i); 1240 int b = GETARG_B(i);
1225 setobj2s(L, ra, cl->upvals[b]->v); 1241 setobj2s(L, ra, cl->upvals[b]->v);
1226 vmbreak; 1242 vmbreak;
1227 } 1243 }
1228 vmcase(OP_SETUPVAL) { 1244 vmcase(OP_SETUPVAL) {
1245 StkId ra = RA(i);
1229 UpVal *uv = cl->upvals[GETARG_B(i)]; 1246 UpVal *uv = cl->upvals[GETARG_B(i)];
1230 setobj(L, uv->v, s2v(ra)); 1247 setobj(L, uv->v, s2v(ra));
1231 luaC_barrier(L, uv, s2v(ra)); 1248 luaC_barrier(L, uv, s2v(ra));
1232 vmbreak; 1249 vmbreak;
1233 } 1250 }
1234 vmcase(OP_GETTABUP) { 1251 vmcase(OP_GETTABUP) {
1252 StkId ra = RA(i);
1235 const TValue *slot; 1253 const TValue *slot;
1236 TValue *upval = cl->upvals[GETARG_B(i)]->v; 1254 TValue *upval = cl->upvals[GETARG_B(i)]->v;
1237 TValue *rc = KC(i); 1255 TValue *rc = KC(i);
@@ -1244,6 +1262,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1244 vmbreak; 1262 vmbreak;
1245 } 1263 }
1246 vmcase(OP_GETTABLE) { 1264 vmcase(OP_GETTABLE) {
1265 StkId ra = RA(i);
1247 const TValue *slot; 1266 const TValue *slot;
1248 TValue *rb = vRB(i); 1267 TValue *rb = vRB(i);
1249 TValue *rc = vRC(i); 1268 TValue *rc = vRC(i);
@@ -1258,6 +1277,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1258 vmbreak; 1277 vmbreak;
1259 } 1278 }
1260 vmcase(OP_GETI) { 1279 vmcase(OP_GETI) {
1280 StkId ra = RA(i);
1261 const TValue *slot; 1281 const TValue *slot;
1262 TValue *rb = vRB(i); 1282 TValue *rb = vRB(i);
1263 int c = GETARG_C(i); 1283 int c = GETARG_C(i);
@@ -1272,6 +1292,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1272 vmbreak; 1292 vmbreak;
1273 } 1293 }
1274 vmcase(OP_GETFIELD) { 1294 vmcase(OP_GETFIELD) {
1295 StkId ra = RA(i);
1275 const TValue *slot; 1296 const TValue *slot;
1276 TValue *rb = vRB(i); 1297 TValue *rb = vRB(i);
1277 TValue *rc = KC(i); 1298 TValue *rc = KC(i);
@@ -1297,6 +1318,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1297 vmbreak; 1318 vmbreak;
1298 } 1319 }
1299 vmcase(OP_SETTABLE) { 1320 vmcase(OP_SETTABLE) {
1321 StkId ra = RA(i);
1300 const TValue *slot; 1322 const TValue *slot;
1301 TValue *rb = vRB(i); /* key (table is in 'ra') */ 1323 TValue *rb = vRB(i); /* key (table is in 'ra') */
1302 TValue *rc = RKC(i); /* value */ 1324 TValue *rc = RKC(i); /* value */
@@ -1311,6 +1333,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1311 vmbreak; 1333 vmbreak;
1312 } 1334 }
1313 vmcase(OP_SETI) { 1335 vmcase(OP_SETI) {
1336 StkId ra = RA(i);
1314 const TValue *slot; 1337 const TValue *slot;
1315 int c = GETARG_B(i); 1338 int c = GETARG_B(i);
1316 TValue *rc = RKC(i); 1339 TValue *rc = RKC(i);
@@ -1325,6 +1348,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1325 vmbreak; 1348 vmbreak;
1326 } 1349 }
1327 vmcase(OP_SETFIELD) { 1350 vmcase(OP_SETFIELD) {
1351 StkId ra = RA(i);
1328 const TValue *slot; 1352 const TValue *slot;
1329 TValue *rb = KB(i); 1353 TValue *rb = KB(i);
1330 TValue *rc = RKC(i); 1354 TValue *rc = RKC(i);
@@ -1337,6 +1361,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1337 vmbreak; 1361 vmbreak;
1338 } 1362 }
1339 vmcase(OP_NEWTABLE) { 1363 vmcase(OP_NEWTABLE) {
1364 StkId ra = RA(i);
1340 int b = GETARG_B(i); /* log2(hash size) + 1 */ 1365 int b = GETARG_B(i); /* log2(hash size) + 1 */
1341 int c = GETARG_C(i); /* array size */ 1366 int c = GETARG_C(i); /* array size */
1342 Table *t; 1367 Table *t;
@@ -1355,6 +1380,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1355 vmbreak; 1380 vmbreak;
1356 } 1381 }
1357 vmcase(OP_SELF) { 1382 vmcase(OP_SELF) {
1383 StkId ra = RA(i);
1358 const TValue *slot; 1384 const TValue *slot;
1359 TValue *rb = vRB(i); 1385 TValue *rb = vRB(i);
1360 TValue *rc = RKC(i); 1386 TValue *rc = RKC(i);
@@ -1412,6 +1438,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1412 vmbreak; 1438 vmbreak;
1413 } 1439 }
1414 vmcase(OP_SHRI) { 1440 vmcase(OP_SHRI) {
1441 StkId ra = RA(i);
1415 TValue *rb = vRB(i); 1442 TValue *rb = vRB(i);
1416 int ic = GETARG_sC(i); 1443 int ic = GETARG_sC(i);
1417 lua_Integer ib; 1444 lua_Integer ib;
@@ -1421,6 +1448,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1421 vmbreak; 1448 vmbreak;
1422 } 1449 }
1423 vmcase(OP_SHLI) { 1450 vmcase(OP_SHLI) {
1451 StkId ra = RA(i);
1424 TValue *rb = vRB(i); 1452 TValue *rb = vRB(i);
1425 int ic = GETARG_sC(i); 1453 int ic = GETARG_sC(i);
1426 lua_Integer ib; 1454 lua_Integer ib;
@@ -1478,6 +1506,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1478 vmbreak; 1506 vmbreak;
1479 } 1507 }
1480 vmcase(OP_MMBIN) { 1508 vmcase(OP_MMBIN) {
1509 StkId ra = RA(i);
1481 Instruction pi = *(pc - 2); /* original arith. expression */ 1510 Instruction pi = *(pc - 2); /* original arith. expression */
1482 TValue *rb = vRB(i); 1511 TValue *rb = vRB(i);
1483 TMS tm = (TMS)GETARG_C(i); 1512 TMS tm = (TMS)GETARG_C(i);
@@ -1487,6 +1516,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1487 vmbreak; 1516 vmbreak;
1488 } 1517 }
1489 vmcase(OP_MMBINI) { 1518 vmcase(OP_MMBINI) {
1519 StkId ra = RA(i);
1490 Instruction pi = *(pc - 2); /* original arith. expression */ 1520 Instruction pi = *(pc - 2); /* original arith. expression */
1491 int imm = GETARG_sB(i); 1521 int imm = GETARG_sB(i);
1492 TMS tm = (TMS)GETARG_C(i); 1522 TMS tm = (TMS)GETARG_C(i);
@@ -1496,6 +1526,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1496 vmbreak; 1526 vmbreak;
1497 } 1527 }
1498 vmcase(OP_MMBINK) { 1528 vmcase(OP_MMBINK) {
1529 StkId ra = RA(i);
1499 Instruction pi = *(pc - 2); /* original arith. expression */ 1530 Instruction pi = *(pc - 2); /* original arith. expression */
1500 TValue *imm = KB(i); 1531 TValue *imm = KB(i);
1501 TMS tm = (TMS)GETARG_C(i); 1532 TMS tm = (TMS)GETARG_C(i);
@@ -1505,6 +1536,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1505 vmbreak; 1536 vmbreak;
1506 } 1537 }
1507 vmcase(OP_UNM) { 1538 vmcase(OP_UNM) {
1539 StkId ra = RA(i);
1508 TValue *rb = vRB(i); 1540 TValue *rb = vRB(i);
1509 lua_Number nb; 1541 lua_Number nb;
1510 if (ttisinteger(rb)) { 1542 if (ttisinteger(rb)) {
@@ -1519,6 +1551,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1519 vmbreak; 1551 vmbreak;
1520 } 1552 }
1521 vmcase(OP_BNOT) { 1553 vmcase(OP_BNOT) {
1554 StkId ra = RA(i);
1522 TValue *rb = vRB(i); 1555 TValue *rb = vRB(i);
1523 lua_Integer ib; 1556 lua_Integer ib;
1524 if (tointegerns(rb, &ib)) { 1557 if (tointegerns(rb, &ib)) {
@@ -1529,6 +1562,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1529 vmbreak; 1562 vmbreak;
1530 } 1563 }
1531 vmcase(OP_NOT) { 1564 vmcase(OP_NOT) {
1565 StkId ra = RA(i);
1532 TValue *rb = vRB(i); 1566 TValue *rb = vRB(i);
1533 if (l_isfalse(rb)) 1567 if (l_isfalse(rb))
1534 setbtvalue(s2v(ra)); 1568 setbtvalue(s2v(ra));
@@ -1537,10 +1571,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1537 vmbreak; 1571 vmbreak;
1538 } 1572 }
1539 vmcase(OP_LEN) { 1573 vmcase(OP_LEN) {
1574 StkId ra = RA(i);
1540 Protect(luaV_objlen(L, ra, vRB(i))); 1575 Protect(luaV_objlen(L, ra, vRB(i)));
1541 vmbreak; 1576 vmbreak;
1542 } 1577 }
1543 vmcase(OP_CONCAT) { 1578 vmcase(OP_CONCAT) {
1579 StkId ra = RA(i);
1544 int n = GETARG_B(i); /* number of elements to concatenate */ 1580 int n = GETARG_B(i); /* number of elements to concatenate */
1545 L->top = ra + n; /* mark the end of concat operands */ 1581 L->top = ra + n; /* mark the end of concat operands */
1546 ProtectNT(luaV_concat(L, n)); 1582 ProtectNT(luaV_concat(L, n));
@@ -1548,10 +1584,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1548 vmbreak; 1584 vmbreak;
1549 } 1585 }
1550 vmcase(OP_CLOSE) { 1586 vmcase(OP_CLOSE) {
1587 StkId ra = RA(i);
1551 Protect(luaF_close(L, ra, LUA_OK, 1)); 1588 Protect(luaF_close(L, ra, LUA_OK, 1));
1552 vmbreak; 1589 vmbreak;
1553 } 1590 }
1554 vmcase(OP_TBC) { 1591 vmcase(OP_TBC) {
1592 StkId ra = RA(i);
1555 /* create new to-be-closed upvalue */ 1593 /* create new to-be-closed upvalue */
1556 halfProtect(luaF_newtbcupval(L, ra)); 1594 halfProtect(luaF_newtbcupval(L, ra));
1557 vmbreak; 1595 vmbreak;
@@ -1561,6 +1599,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1561 vmbreak; 1599 vmbreak;
1562 } 1600 }
1563 vmcase(OP_EQ) { 1601 vmcase(OP_EQ) {
1602 StkId ra = RA(i);
1564 int cond; 1603 int cond;
1565 TValue *rb = vRB(i); 1604 TValue *rb = vRB(i);
1566 Protect(cond = luaV_equalobj(L, s2v(ra), rb)); 1605 Protect(cond = luaV_equalobj(L, s2v(ra), rb));
@@ -1576,6 +1615,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1576 vmbreak; 1615 vmbreak;
1577 } 1616 }
1578 vmcase(OP_EQK) { 1617 vmcase(OP_EQK) {
1618 StkId ra = RA(i);
1579 TValue *rb = KB(i); 1619 TValue *rb = KB(i);
1580 /* basic types do not use '__eq'; we can use raw equality */ 1620 /* basic types do not use '__eq'; we can use raw equality */
1581 int cond = luaV_rawequalobj(s2v(ra), rb); 1621 int cond = luaV_rawequalobj(s2v(ra), rb);
@@ -1583,6 +1623,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1583 vmbreak; 1623 vmbreak;
1584 } 1624 }
1585 vmcase(OP_EQI) { 1625 vmcase(OP_EQI) {
1626 StkId ra = RA(i);
1586 int cond; 1627 int cond;
1587 int im = GETARG_sB(i); 1628 int im = GETARG_sB(i);
1588 if (ttisinteger(s2v(ra))) 1629 if (ttisinteger(s2v(ra)))
@@ -1611,11 +1652,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1611 vmbreak; 1652 vmbreak;
1612 } 1653 }
1613 vmcase(OP_TEST) { 1654 vmcase(OP_TEST) {
1655 StkId ra = RA(i);
1614 int cond = !l_isfalse(s2v(ra)); 1656 int cond = !l_isfalse(s2v(ra));
1615 docondjump(); 1657 docondjump();
1616 vmbreak; 1658 vmbreak;
1617 } 1659 }
1618 vmcase(OP_TESTSET) { 1660 vmcase(OP_TESTSET) {
1661 StkId ra = RA(i);
1619 TValue *rb = vRB(i); 1662 TValue *rb = vRB(i);
1620 if (l_isfalse(rb) == GETARG_k(i)) 1663 if (l_isfalse(rb) == GETARG_k(i))
1621 pc++; 1664 pc++;
@@ -1626,6 +1669,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1626 vmbreak; 1669 vmbreak;
1627 } 1670 }
1628 vmcase(OP_CALL) { 1671 vmcase(OP_CALL) {
1672 StkId ra = RA(i);
1629 CallInfo *newci; 1673 CallInfo *newci;
1630 int b = GETARG_B(i); 1674 int b = GETARG_B(i);
1631 int nresults = GETARG_C(i) - 1; 1675 int nresults = GETARG_C(i) - 1;
@@ -1642,6 +1686,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1642 vmbreak; 1686 vmbreak;
1643 } 1687 }
1644 vmcase(OP_TAILCALL) { 1688 vmcase(OP_TAILCALL) {
1689 StkId ra = RA(i);
1645 int b = GETARG_B(i); /* number of arguments + 1 (function) */ 1690 int b = GETARG_B(i); /* number of arguments + 1 (function) */
1646 int n; /* number of results when calling a C function */ 1691 int n; /* number of results when calling a C function */
1647 int nparams1 = GETARG_C(i); 1692 int nparams1 = GETARG_C(i);
@@ -1667,6 +1712,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1667 } 1712 }
1668 } 1713 }
1669 vmcase(OP_RETURN) { 1714 vmcase(OP_RETURN) {
1715 StkId ra = RA(i);
1670 int n = GETARG_B(i) - 1; /* number of results */ 1716 int n = GETARG_B(i) - 1; /* number of results */
1671 int nparams1 = GETARG_C(i); 1717 int nparams1 = GETARG_C(i);
1672 if (n < 0) /* not fixed? */ 1718 if (n < 0) /* not fixed? */
@@ -1689,6 +1735,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1689 } 1735 }
1690 vmcase(OP_RETURN0) { 1736 vmcase(OP_RETURN0) {
1691 if (l_unlikely(L->hookmask)) { 1737 if (l_unlikely(L->hookmask)) {
1738 StkId ra = RA(i);
1692 L->top = ra; 1739 L->top = ra;
1693 savepc(ci); 1740 savepc(ci);
1694 luaD_poscall(L, ci, 0); /* no hurry... */ 1741 luaD_poscall(L, ci, 0); /* no hurry... */
@@ -1705,6 +1752,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1705 } 1752 }
1706 vmcase(OP_RETURN1) { 1753 vmcase(OP_RETURN1) {
1707 if (l_unlikely(L->hookmask)) { 1754 if (l_unlikely(L->hookmask)) {
1755 StkId ra = RA(i);
1708 L->top = ra + 1; 1756 L->top = ra + 1;
1709 savepc(ci); 1757 savepc(ci);
1710 luaD_poscall(L, ci, 1); /* no hurry... */ 1758 luaD_poscall(L, ci, 1); /* no hurry... */
@@ -1716,6 +1764,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1716 if (nres == 0) 1764 if (nres == 0)
1717 L->top = base - 1; /* asked for no results */ 1765 L->top = base - 1; /* asked for no results */
1718 else { 1766 else {
1767 StkId ra = RA(i);
1719 setobjs2s(L, base - 1, ra); /* at least this result */ 1768 setobjs2s(L, base - 1, ra); /* at least this result */
1720 L->top = base; 1769 L->top = base;
1721 for (; l_unlikely(nres > 1); nres--) 1770 for (; l_unlikely(nres > 1); nres--)
@@ -1731,6 +1780,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1731 } 1780 }
1732 } 1781 }
1733 vmcase(OP_FORLOOP) { 1782 vmcase(OP_FORLOOP) {
1783 StkId ra = RA(i);
1734 if (ttisinteger(s2v(ra + 2))) { /* integer loop? */ 1784 if (ttisinteger(s2v(ra + 2))) { /* integer loop? */
1735 lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1))); 1785 lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1)));
1736 if (count > 0) { /* still more iterations? */ 1786 if (count > 0) { /* still more iterations? */
@@ -1749,12 +1799,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1749 vmbreak; 1799 vmbreak;
1750 } 1800 }
1751 vmcase(OP_FORPREP) { 1801 vmcase(OP_FORPREP) {
1802 StkId ra = RA(i);
1752 savestate(L, ci); /* in case of errors */ 1803 savestate(L, ci); /* in case of errors */
1753 if (forprep(L, ra)) 1804 if (forprep(L, ra))
1754 pc += GETARG_Bx(i) + 1; /* skip the loop */ 1805 pc += GETARG_Bx(i) + 1; /* skip the loop */
1755 vmbreak; 1806 vmbreak;
1756 } 1807 }
1757 vmcase(OP_TFORPREP) { 1808 vmcase(OP_TFORPREP) {
1809 StkId ra = RA(i);
1758 /* create to-be-closed upvalue (if needed) */ 1810 /* create to-be-closed upvalue (if needed) */
1759 halfProtect(luaF_newtbcupval(L, ra + 3)); 1811 halfProtect(luaF_newtbcupval(L, ra + 3));
1760 pc += GETARG_Bx(i); 1812 pc += GETARG_Bx(i);
@@ -1763,7 +1815,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1763 goto l_tforcall; 1815 goto l_tforcall;
1764 } 1816 }
1765 vmcase(OP_TFORCALL) { 1817 vmcase(OP_TFORCALL) {
1766 l_tforcall: 1818 l_tforcall: {
1819 StkId ra = RA(i);
1767 /* 'ra' has the iterator function, 'ra + 1' has the state, 1820 /* 'ra' has the iterator function, 'ra + 1' has the state,
1768 'ra + 2' has the control variable, and 'ra + 3' has the 1821 'ra + 2' has the control variable, and 'ra + 3' has the
1769 to-be-closed variable. The call will use the stack after 1822 to-be-closed variable. The call will use the stack after
@@ -1777,16 +1830,18 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1777 i = *(pc++); /* go to next instruction */ 1830 i = *(pc++); /* go to next instruction */
1778 lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); 1831 lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i));
1779 goto l_tforloop; 1832 goto l_tforloop;
1780 } 1833 }}
1781 vmcase(OP_TFORLOOP) { 1834 vmcase(OP_TFORLOOP) {
1782 l_tforloop: 1835 l_tforloop: {
1836 StkId ra = RA(i);
1783 if (!ttisnil(s2v(ra + 4))) { /* continue loop? */ 1837 if (!ttisnil(s2v(ra + 4))) { /* continue loop? */
1784 setobjs2s(L, ra + 2, ra + 4); /* save control variable */ 1838 setobjs2s(L, ra + 2, ra + 4); /* save control variable */
1785 pc -= GETARG_Bx(i); /* jump back */ 1839 pc -= GETARG_Bx(i); /* jump back */
1786 } 1840 }
1787 vmbreak; 1841 vmbreak;
1788 } 1842 }}
1789 vmcase(OP_SETLIST) { 1843 vmcase(OP_SETLIST) {
1844 StkId ra = RA(i);
1790 int n = GETARG_B(i); 1845 int n = GETARG_B(i);
1791 unsigned int last = GETARG_C(i); 1846 unsigned int last = GETARG_C(i);
1792 Table *h = hvalue(s2v(ra)); 1847 Table *h = hvalue(s2v(ra));
@@ -1810,12 +1865,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1810 vmbreak; 1865 vmbreak;
1811 } 1866 }
1812 vmcase(OP_CLOSURE) { 1867 vmcase(OP_CLOSURE) {
1868 StkId ra = RA(i);
1813 Proto *p = cl->p->p[GETARG_Bx(i)]; 1869 Proto *p = cl->p->p[GETARG_Bx(i)];
1814 halfProtect(pushclosure(L, p, cl->upvals, base, ra)); 1870 halfProtect(pushclosure(L, p, cl->upvals, base, ra));
1815 checkGC(L, ra + 1); 1871 checkGC(L, ra + 1);
1816 vmbreak; 1872 vmbreak;
1817 } 1873 }
1818 vmcase(OP_VARARG) { 1874 vmcase(OP_VARARG) {
1875 StkId ra = RA(i);
1819 int n = GETARG_C(i) - 1; /* required results */ 1876 int n = GETARG_C(i) - 1; /* required results */
1820 Protect(luaT_getvarargs(L, ci, ra, n)); 1877 Protect(luaT_getvarargs(L, ci, ra, n));
1821 vmbreak; 1878 vmbreak;