diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-06-26 13:26:36 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-06-26 13:26:36 -0300 |
commit | 8b7cfee26b71e66de2cef9f8db9d9e18f5439afd (patch) | |
tree | 8590bc5eba700f6c03da032bff8a925de257c726 | |
parent | c1a63c45f8ec5932993c8cec40d3c5ec0743349c (diff) | |
download | lua-8b7cfee26b71e66de2cef9f8db9d9e18f5439afd.tar.gz lua-8b7cfee26b71e66de2cef9f8db9d9e18f5439afd.tar.bz2 lua-8b7cfee26b71e66de2cef9f8db9d9e18f5439afd.zip |
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'.
-rw-r--r-- | lstate.c | 2 | ||||
-rw-r--r-- | ltests.h | 2 | ||||
-rw-r--r-- | luaconf.h | 2 | ||||
-rw-r--r-- | testes/cstack.lua | 28 |
4 files changed, 24 insertions, 10 deletions
@@ -388,7 +388,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
388 | preinit_thread(L, g); | 388 | preinit_thread(L, g); |
389 | g->allgc = obj2gco(L); /* by now, only object is the main thread */ | 389 | g->allgc = obj2gco(L); /* by now, only object is the main thread */ |
390 | L->next = NULL; | 390 | L->next = NULL; |
391 | g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK; | 391 | g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK + CSTACKERR; |
392 | g->frealloc = f; | 392 | g->frealloc = f; |
393 | g->ud = ud; | 393 | g->ud = ud; |
394 | g->warnf = NULL; | 394 | g->warnf = NULL; |
@@ -31,7 +31,7 @@ | |||
31 | 31 | ||
32 | /* compiled with -O0, Lua uses a lot of C stack space... */ | 32 | /* compiled with -O0, Lua uses a lot of C stack space... */ |
33 | #undef LUAI_MAXCSTACK | 33 | #undef LUAI_MAXCSTACK |
34 | #define LUAI_MAXCSTACK (400 + CSTACKERR) | 34 | #define LUAI_MAXCSTACK 400 |
35 | 35 | ||
36 | /* to avoid warnings, and to make sure value is really unused */ | 36 | /* to avoid warnings, and to make sure value is really unused */ |
37 | #define UNUSED(x) (x=0, (void)(x)) | 37 | #define UNUSED(x) (x=0, (void)(x)) |
@@ -47,7 +47,7 @@ | |||
47 | ** (It will crash with a limit too high.) | 47 | ** (It will crash with a limit too high.) |
48 | */ | 48 | */ |
49 | #if !defined(LUAI_MAXCSTACK) | 49 | #if !defined(LUAI_MAXCSTACK) |
50 | #define LUAI_MAXCSTACK 2200 | 50 | #define LUAI_MAXCSTACK 2000 |
51 | #endif | 51 | #endif |
52 | 52 | ||
53 | 53 | ||
diff --git a/testes/cstack.lua b/testes/cstack.lua index c7aa740f..2a55ce21 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua | |||
@@ -4,15 +4,29 @@ | |||
4 | local debug = require "debug" | 4 | local debug = require "debug" |
5 | 5 | ||
6 | print"testing C-stack overflow detection" | 6 | print"testing C-stack overflow detection" |
7 | print"If this test craches, see its file ('cstack.lua')" | ||
8 | |||
9 | -- Segmentation faults in these tests probably result from a C-stack | ||
10 | -- overflow. To avoid these errors, you can use the function | ||
11 | -- 'debug.setCstacklimit' to set a smaller limit for the use of | ||
12 | -- C stack by Lua. After finding a reliable limit, you might want | ||
13 | -- to recompile Lua with this limit as the value for | ||
14 | -- the constant 'LUAI_MAXCCALLS', which defines the default limit. | ||
15 | -- (The default limit is printed by this test.) | ||
16 | -- Alternatively, you can ensure a larger stack for the program. | ||
17 | |||
18 | -- For Linux, a limit up to 30_000 seems Ok. Windows cannot go much | ||
19 | -- higher than 2_000. | ||
20 | |||
7 | 21 | ||
8 | local origlimit = debug.setCstacklimit(400) | 22 | local origlimit = debug.setCstacklimit(400) |
9 | print("current stack limit: " .. origlimit) | 23 | print("default stack limit: " .. origlimit) |
10 | debug.setCstacklimit(origlimit) | 24 | |
25 | -- change this value for different limits for this test suite | ||
26 | local currentlimit = origlimit | ||
27 | debug.setCstacklimit(currentlimit) | ||
28 | print("current stack limit: " .. currentlimit) | ||
11 | 29 | ||
12 | -- Segmentation faults in these tests probably result from a C-stack | ||
13 | -- overflow. To avoid these errors, recompile Lua with a smaller | ||
14 | -- value for the constant 'LUAI_MAXCCALLS' or else ensure a larger | ||
15 | -- stack for the program. | ||
16 | 30 | ||
17 | local function checkerror (msg, f, ...) | 31 | local function checkerror (msg, f, ...) |
18 | local s, err = pcall(f, ...) | 32 | local s, err = pcall(f, ...) |
@@ -104,7 +118,7 @@ do print("testing changes in C-stack limit") | |||
104 | return n | 118 | return n |
105 | end | 119 | end |
106 | 120 | ||
107 | assert(debug.setCstacklimit(400) == origlimit) | 121 | assert(debug.setCstacklimit(400) == currentlimit) |
108 | local lim400 = check() | 122 | local lim400 = check() |
109 | -- a very low limit (given that the several calls to arive here) | 123 | -- a very low limit (given that the several calls to arive here) |
110 | local lowlimit = 38 | 124 | local lowlimit = 38 |