issue #163 Fix bug in OP_RETURN in interpreter and MIR JIT backend; we need to reload RA after call to luaF_close() as stack may been reallocated

mini
Dibyendu Majumdar 4 years ago
parent e483fe40ee
commit 9e2a4f4645

@ -1809,8 +1809,10 @@ int luaV_execute (lua_State *L) {
vmcase(OP_RETURN) {
int b = GETARG_B(i);
#ifdef RAVI_DEFER_STATEMENT
if (cl->p->sizep > 0)
if (cl->p->sizep > 0) {
Protect_base(luaF_close(L, base, LUA_OK));
ra = RA(i);
}
#else
if (cl->p->sizep > 0) luaF_close(L, base);
#endif

@ -1062,7 +1062,6 @@ static void emit_op_loadk(struct function *fn, int A, int Bx, int pc) {
static void emit_op_return(struct function *fn, int A, int B, int pc) {
(void)pc;
emit_reg(fn, "ra", A);
#ifdef RAVI_DEFER_STATEMENT
membuff_add_string(&fn->body, "if (cl->p->sizep > 0) {\n luaF_close(L, base, LUA_OK);\n");
membuff_add_string(&fn->body, " base = ci->u.l.base;\n");
@ -1070,6 +1069,7 @@ static void emit_op_return(struct function *fn, int A, int B, int pc) {
#else
membuff_add_string(&fn->body, "if (cl->p->sizep > 0) luaF_close(L, base);\n");
#endif
emit_reg(fn, "ra", A);
membuff_add_fstring(&fn->body, "result = (%d != 0 ? %d - 1 : cast_int(L->top - ra));\n", B, B);
membuff_add_string(&fn->body, "return luaD_poscall(L, ci, ra, result);\n");
}

@ -1891,6 +1891,24 @@ compile(x)
x()
print 'Test 83 OK'
--- Test stack reallocation in defer statement
function x(a) if a <= 0 then return else x(a-1) end end
y = ravi.jit and 100 or 1000
function z(...)
-- recursive call to make stack
defer x(y) end
return ...
end
do
local a,b,c = z(1,2,3)
assert(a == 1 and b == 2 and c == 3)
compile(x)
compile(z)
a,b,c = z(3,2,1)
assert(a == 3 and b == 2 and c == 1)
end
print 'Test 84 OK'
for k,v in pairs(opcodes_coverage)
do
print(k, v)

Loading…
Cancel
Save