1
0
mirror of https://github.com/lua/lua.git synced 2025-01-14 05:43:00 +08:00

Small changes around C-stack limit

- Better documentation in 'testes/cstack.lua' about using
'debug.setCstacklimit' to find a good limit.

- Constant LUAI_MAXCSTACK gets added CSTACKERR (extra stack for
error handling), so that it is compatible with the argument to
'debug.setCstacklimit'.
This commit is contained in:
Roberto Ierusalimschy 2019-06-26 13:26:36 -03:00
parent c1a63c45f8
commit 8b7cfee26b
4 changed files with 25 additions and 11 deletions

View File

@ -388,7 +388,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
preinit_thread(L, g); preinit_thread(L, g);
g->allgc = obj2gco(L); /* by now, only object is the main thread */ g->allgc = obj2gco(L); /* by now, only object is the main thread */
L->next = NULL; L->next = NULL;
g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK; g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK + CSTACKERR;
g->frealloc = f; g->frealloc = f;
g->ud = ud; g->ud = ud;
g->warnf = NULL; g->warnf = NULL;

View File

@ -31,7 +31,7 @@
/* compiled with -O0, Lua uses a lot of C stack space... */ /* compiled with -O0, Lua uses a lot of C stack space... */
#undef LUAI_MAXCSTACK #undef LUAI_MAXCSTACK
#define LUAI_MAXCSTACK (400 + CSTACKERR) #define LUAI_MAXCSTACK 400
/* to avoid warnings, and to make sure value is really unused */ /* to avoid warnings, and to make sure value is really unused */
#define UNUSED(x) (x=0, (void)(x)) #define UNUSED(x) (x=0, (void)(x))

View File

@ -47,7 +47,7 @@
** (It will crash with a limit too high.) ** (It will crash with a limit too high.)
*/ */
#if !defined(LUAI_MAXCSTACK) #if !defined(LUAI_MAXCSTACK)
#define LUAI_MAXCSTACK 2200 #define LUAI_MAXCSTACK 2000
#endif #endif

View File

@ -4,15 +4,29 @@
local debug = require "debug" local debug = require "debug"
print"testing C-stack overflow detection" print"testing C-stack overflow detection"
print"If this test craches, see its file ('cstack.lua')"
local origlimit = debug.setCstacklimit(400)
print("current stack limit: " .. origlimit)
debug.setCstacklimit(origlimit)
-- Segmentation faults in these tests probably result from a C-stack -- Segmentation faults in these tests probably result from a C-stack
-- overflow. To avoid these errors, recompile Lua with a smaller -- overflow. To avoid these errors, you can use the function
-- value for the constant 'LUAI_MAXCCALLS' or else ensure a larger -- 'debug.setCstacklimit' to set a smaller limit for the use of
-- stack for the program. -- C stack by Lua. After finding a reliable limit, you might want
-- to recompile Lua with this limit as the value for
-- the constant 'LUAI_MAXCCALLS', which defines the default limit.
-- (The default limit is printed by this test.)
-- Alternatively, you can ensure a larger stack for the program.
-- For Linux, a limit up to 30_000 seems Ok. Windows cannot go much
-- higher than 2_000.
local origlimit = debug.setCstacklimit(400)
print("default stack limit: " .. origlimit)
-- change this value for different limits for this test suite
local currentlimit = origlimit
debug.setCstacklimit(currentlimit)
print("current stack limit: " .. currentlimit)
local function checkerror (msg, f, ...) local function checkerror (msg, f, ...)
local s, err = pcall(f, ...) local s, err = pcall(f, ...)
@ -104,7 +118,7 @@ do print("testing changes in C-stack limit")
return n return n
end end
assert(debug.setCstacklimit(400) == origlimit) assert(debug.setCstacklimit(400) == currentlimit)
local lim400 = check() local lim400 = check()
-- a very low limit (given that the several calls to arive here) -- a very low limit (given that the several calls to arive here)
local lowlimit = 38 local lowlimit = 38