Visitar URL original
Fixed bugs of stack reallocation x GC · lua/lua@eb41999 · GitHub
Skip to content

Commit eb41999

Browse files
committed
Fixed bugs of stack reallocation x GC
Macro 'checkstackGC' was doing a GC step after resizing the stack; the GC could shrink the stack and undo the resize. Moreover, macro 'checkstackp' also does a GC step, which could remove the preallocated CallInfo when calling a function. (Its name has been changed to 'checkstackGCp' to emphasize that it calls the GC.)
1 parent 314c605 commit eb41999

4 files changed

Lines changed: 14 additions & 11 deletions

File tree

‎ldo.c‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,13 @@ void luaD_call (lua_State *L, StkId func, int nresults) {
465465
f = fvalue(s2v(func));
466466
Cfunc: {
467467
int n; /* number of returns */
468-
CallInfo *ci = next_ci(L);
469-
checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
468+
CallInfo *ci;
469+
checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
470+
L->ci = ci = next_ci(L);
470471
ci->nresults = nresults;
471472
ci->callstatus = CIST_C;
472473
ci->top = L->top + LUA_MINSTACK;
473474
ci->func = func;
474-
L->ci = ci;
475475
lua_assert(ci->top <= L->stack_last);
476476
if (L->hookmask & LUA_MASKCALL) {
477477
int narg = cast_int(L->top - func) - 1;
@@ -485,12 +485,13 @@ void luaD_call (lua_State *L, StkId func, int nresults) {
485485
break;
486486
}
487487
case LUA_VLCL: { /* Lua function */
488-
CallInfo *ci = next_ci(L);
488+
CallInfo *ci;
489489
Proto *p = clLvalue(s2v(func))->p;
490490
int narg = cast_int(L->top - func) - 1; /* number of real arguments */
491491
int nfixparams = p->numparams;
492492
int fsize = p->maxstacksize; /* frame size */
493-
checkstackp(L, fsize, func);
493+
checkstackGCp(L, fsize, func);
494+
L->ci = ci = next_ci(L);
494495
ci->nresults = nresults;
495496
ci->u.l.savedpc = p->code; /* starting point */
496497
ci->callstatus = 0;
@@ -504,7 +505,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) {
504505
break;
505506
}
506507
default: { /* not a function */
507-
checkstackp(L, 1, func); /* space for metamethod */
508+
checkstackGCp(L, 1, func); /* space for metamethod */
508509
luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */
509510
goto retry; /* try again with metamethod */
510511
}

‎ldo.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
** Macro to check stack size and grow stack if needed. Parameters
1818
** 'pre'/'pos' allow the macro to preserve a pointer into the
1919
** stack across reallocations, doing the work only when needed.
20+
** It also allows the running of one GC step when the stack is
21+
** reallocated.
2022
** 'condmovestack' is used in heavy tests to force a stack reallocation
2123
** at every check.
2224
*/
@@ -35,7 +37,7 @@
3537

3638

3739
/* macro to check stack size, preserving 'p' */
38-
#define checkstackp(L,n,p) \
40+
#define checkstackGCp(L,n,p) \
3941
luaD_checkstackaux(L, n, \
4042
ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \
4143
luaC_checkGC(L), /* stack grow uses memory */ \
@@ -44,7 +46,7 @@
4446

4547
/* macro to check stack size and GC */
4648
#define checkstackGC(L,fsize) \
47-
luaD_checkstackaux(L, (fsize), (void)0, luaC_checkGC(L))
49+
luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0)
4850

4951

5052
/* type of protected functions, to be ran by 'runprotected' */

‎ltm.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
240240
int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */
241241
int nextra = actual - nfixparams; /* number of extra arguments */
242242
ci->u.l.nextraargs = nextra;
243-
checkstackGC(L, p->maxstacksize + 1);
243+
luaD_checkstack(L, p->maxstacksize + 1);
244244
/* copy function to the top of the stack */
245245
setobjs2s(L, L->top++, ci->func);
246246
/* move fixed parameters to the top of the stack */
@@ -259,7 +259,7 @@ void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
259259
int nextra = ci->u.l.nextraargs;
260260
if (wanted < 0) {
261261
wanted = nextra; /* get all extra arguments available */
262-
checkstackp(L, nextra, where); /* ensure stack space */
262+
checkstackGCp(L, nextra, where); /* ensure stack space */
263263
L->top = where + nextra; /* next instruction will need top */
264264
}
265265
for (i = 0; i < wanted && i < nextra; i++)

‎lvm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
16341634
while (!ttisfunction(s2v(ra))) { /* not a function? */
16351635
luaD_tryfuncTM(L, ra); /* try '__call' metamethod */
16361636
b++; /* there is now one extra argument */
1637-
checkstackp(L, 1, ra);
1637+
checkstackGCp(L, 1, ra);
16381638
}
16391639
if (!ttisLclosure(s2v(ra))) { /* C function? */
16401640
luaD_call(L, ra, LUA_MULTRET); /* call it */

0 commit comments

Comments
 (0)