From 0443ad9e288825b6e4441eb11104bcdb4ff4593a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 25 Mar 2019 14:12:06 -0300 Subject: [PATCH] LUAI_MAXCCALLS renamed LUAI_MAXCSTACK The limit LUAI_MAXCCALLS was renamed LUAI_MAXCSTACK, which better represents its meaning. Moreover, its definition was moved to 'luaconf.h', given its importance now that Lua does not use a "stackless" implementation. --- ldo.c | 4 ++-- llimits.h | 9 --------- lstate.c | 16 ++++++++-------- ltests.h | 4 ++-- luaconf.h | 15 +++++++++++++++ 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/ldo.c b/ldo.c index 077109c4..2a98c397 100644 --- a/ldo.c +++ b/ldo.c @@ -521,7 +521,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { incXCcalls(L); - if (getCcalls(L) >= LUAI_MAXCCALLS) /* possible stack overflow? */ + if (getCcalls(L) >= LUAI_MAXCSTACK) /* possible stack overflow? */ luaE_freeCI(L); luaD_call(L, func, nResults); decXCcalls(L); @@ -673,7 +673,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, L->nCcalls = 1; else /* correct 'nCcalls' for this thread */ L->nCcalls = getCcalls(from) - from->nci + L->nci + CSTACKCF; - if (L->nCcalls >= LUAI_MAXCCALLS) + if (L->nCcalls >= LUAI_MAXCSTACK) return resume_error(L, "C stack overflow", nargs); luai_userstateresume(L, nargs); api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); diff --git a/llimits.h b/llimits.h index 155bb160..3df873db 100644 --- a/llimits.h +++ b/llimits.h @@ -168,15 +168,6 @@ typedef LUAI_UACINT l_uacInt; #endif -/* -** maximum depth for nested C calls and syntactical nested non-terminals -** in a program. (Value must fit in an unsigned short int. It must also -** be compatible with the size of the C stack.) -*/ -#if !defined(LUAI_MAXCCALLS) -#define LUAI_MAXCCALLS 2200 -#endif - /* diff --git a/lstate.c b/lstate.c index f5579a66..463a47d2 100644 --- a/lstate.c +++ b/lstate.c @@ -100,13 +100,13 @@ void luaE_setdebt (global_State *g, l_mem debt) { ** Increment count of "C calls" and check for overflows. In case of ** a stack overflow, check appropriate error ("regular" overflow or ** overflow while handling stack overflow). -** If 'nCcalls' is larger than LUAI_MAXCCALLS but smaller than -** LUAI_MAXCCALLS + CSTACKCF (plus 2 to avoid by-one errors), it means +** If 'nCcalls' is larger than LUAI_MAXCSTACK but smaller than +** LUAI_MAXCSTACK + CSTACKCF (plus 2 to avoid by-one errors), it means ** it has just entered the "overflow zone", so the function raises an ** overflow error. -** If 'nCcalls' is larger than LUAI_MAXCCALLS + CSTACKCF + 2 +** If 'nCcalls' is larger than LUAI_MAXCSTACK + CSTACKCF + 2 ** (which means it is already handling an overflow) but smaller than -** 9/8 of LUAI_MAXCCALLS, does not report an error (to allow message +** 9/8 of LUAI_MAXCSTACK, does not report an error (to allow message ** handling to work). ** Otherwise, report a stack overflow while handling a stack overflow ** (probably caused by a repeating error in the message handling @@ -115,16 +115,16 @@ void luaE_setdebt (global_State *g, l_mem debt) { void luaE_enterCcall (lua_State *L) { int ncalls = getCcalls(L); L->nCcalls++; - if (ncalls >= LUAI_MAXCCALLS) { /* possible overflow? */ + if (ncalls >= LUAI_MAXCSTACK) { /* possible overflow? */ luaE_freeCI(L); /* release unused CIs */ ncalls = getCcalls(L); /* update call count */ - if (ncalls >= LUAI_MAXCCALLS) { /* still overflow? */ - if (ncalls <= LUAI_MAXCCALLS + CSTACKCF + 2) { + if (ncalls >= LUAI_MAXCSTACK) { /* still overflow? */ + if (ncalls <= LUAI_MAXCSTACK + CSTACKCF + 2) { /* no error before increments; raise the error now */ L->nCcalls += (CSTACKCF + 4); /* avoid raising it again */ luaG_runerror(L, "C stack overflow"); } - else if (ncalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS >> 3))) + else if (ncalls >= (LUAI_MAXCSTACK + (LUAI_MAXCSTACK >> 3))) luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ } } diff --git a/ltests.h b/ltests.h index 997e1c4b..a22c98e1 100644 --- a/ltests.h +++ b/ltests.h @@ -30,8 +30,8 @@ /* compiled with -O0, Lua uses a lot of C stack space... */ -#undef LUAI_MAXCCALLS -#define LUAI_MAXCCALLS 400 +#undef LUAI_MAXCSTACK +#define LUAI_MAXCSTACK 400 /* to avoid warnings, and to make sure value is really unused */ #define UNUSED(x) (x=0, (void)(x)) diff --git a/luaconf.h b/luaconf.h index 0fc161a4..5c714d4e 100644 --- a/luaconf.h +++ b/luaconf.h @@ -27,6 +27,21 @@ ** ===================================================================== */ +/* +@@ LUAI_MAXCSTACK defines the maximum depth for nested calls and +** also limits the maximum depth of other recursive algorithms in +** the implementation, such as syntactic analysis. A value too +** large may allow the interpreter to crash (C-stack overflow). +** The default value seems ok for regular machines, but may be +** too high for restricted hardware. +** The test file 'cstack.lua' may help finding a good limit. +** (It will crash with a limit too high.) +*/ +#if !defined(LUAI_MAXCSTACK) +#define LUAI_MAXCSTACK 2200 +#endif + + /* @@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You ** can also define LUA_32BITS in the make file, but changing here you