Skip to content

Commit

Permalink
issue #163 Fix bug in OP_RETURN in interpreter and MIR JIT backend; w…
Browse files Browse the repository at this point in the history
…e need to reload RA after call to luaF_close() as stack may been reallocated
  • Loading branch information
dibyendumajumdar committed Jul 4, 2020
1 parent e483fe4 commit 9e2a4f4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/lvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ravi_jitshared.c
Original file line number Diff line number Diff line change
Expand Up @@ -1062,14 +1062,14 @@ 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");
membuff_add_string(&fn->body, "}\n");
#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");
}
Expand Down
18 changes: 18 additions & 0 deletions tests/language/ravi_tests1.ravi
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9e2a4f4

Please sign in to comment.