aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lj_state.c17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/lj_state.c b/src/lj_state.c
index 302c721c1..d1eff6174 100644
--- a/src/lj_state.c
+++ b/src/lj_state.c
@@ -34,8 +34,9 @@
34#define LJ_STACK_MAX LUAI_MAXSTACK /* Max. stack size. */ 34#define LJ_STACK_MAX LUAI_MAXSTACK /* Max. stack size. */
35#define LJ_STACK_START (2*LJ_STACK_MIN) /* Starting stack size. */ 35#define LJ_STACK_START (2*LJ_STACK_MIN) /* Starting stack size. */
36#define LJ_STACK_MAXEX (LJ_STACK_MAX + 1 + LJ_STACK_EXTRA) 36#define LJ_STACK_MAXEX (LJ_STACK_MAX + 1 + LJ_STACK_EXTRA)
37#define LJ_STACK_ERREX (1 + 2*LJ_STACK_MIN) /* Extra for error handling. */
37 38
38/* Explanation of LJ_STACK_EXTRA: 39/* Explanation for LJ_STACK_EXTRA:
39** 40**
40** Calls to metamethods store their arguments beyond the current top 41** Calls to metamethods store their arguments beyond the current top
41** without checking for the stack limit. This avoids stack resizes which 42** without checking for the stack limit. This avoids stack resizes which
@@ -47,6 +48,11 @@
47** one extra slot if mobj is not a function. Only lj_meta_tset needs 5 48** one extra slot if mobj is not a function. Only lj_meta_tset needs 5
48** slots above top, but then mobj is always a function. So we can get by 49** slots above top, but then mobj is always a function. So we can get by
49** with 5 extra slots. 50** with 5 extra slots.
51**
52** Explanation for LJ_STACK_ERREX:
53**
54** The 1 is space for the error message, and 2 * LJ_STACK_MIN is for
55** the lj_state_checkstack() call in lj_err_run().
50*/ 56*/
51 57
52/* Resize stack slots and adjust pointers in state. */ 58/* Resize stack slots and adjust pointers in state. */
@@ -78,7 +84,8 @@ static void resizestack(lua_State *L, MSize n)
78/* Relimit stack after error, in case the limit was overdrawn. */ 84/* Relimit stack after error, in case the limit was overdrawn. */
79void lj_state_relimitstack(lua_State *L) 85void lj_state_relimitstack(lua_State *L)
80{ 86{
81 if (L->stacksize > LJ_STACK_MAXEX && L->top-tvref(L->stack) < LJ_STACK_MAX-1) 87 if (L->stacksize > LJ_STACK_MAXEX &&
88 L->top-tvref(L->stack) < LJ_STACK_MAX - 1 - LJ_STACK_ERREX)
82 resizestack(L, LJ_STACK_MAX); 89 resizestack(L, LJ_STACK_MAX);
83} 90}
84 91
@@ -119,11 +126,9 @@ void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need)
119 /* An error handler might want to inspect the stack overflow error, but 126 /* An error handler might want to inspect the stack overflow error, but
120 ** will need some stack space to run in. We give it a stack size beyond 127 ** will need some stack space to run in. We give it a stack size beyond
121 ** the normal limit in order to do so, then rely on lj_state_relimitstack 128 ** the normal limit in order to do so, then rely on lj_state_relimitstack
122 ** calls during unwinding to bring us back to a convential stack size. 129 ** calls during unwinding to bring us back to a conventional stack size.
123 ** The + 1 is space for the error message, and 2 * LUA_MINSTACK is for
124 ** the lj_state_checkstack() call in lj_err_run().
125 */ 130 */
126 resizestack(L, LJ_STACK_MAX + 1 + 2 * LUA_MINSTACK); 131 resizestack(L, LJ_STACK_MAX + LJ_STACK_ERREX);
127 lj_err_stkov(L); /* May invoke an error handler. */ 132 lj_err_stkov(L); /* May invoke an error handler. */
128 } else { 133 } else {
129 /* If we're here, then the stack overflow error handler is requesting 134 /* If we're here, then the stack overflow error handler is requesting