diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-27 14:32:29 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-27 14:32:29 -0200 |
| commit | ba7da13ec5938f978c37d63aa40a3e340b301f79 (patch) | |
| tree | c1f22403954f6e0c6d17c8495c11509103313c9a /testes/cstack.lua | |
| parent | da37ac9c7894186a0e2e0e6f1f5f00b825fd1555 (diff) | |
| download | lua-ba7da13ec5938f978c37d63aa40a3e340b301f79.tar.gz lua-ba7da13ec5938f978c37d63aa40a3e340b301f79.tar.bz2 lua-ba7da13ec5938f978c37d63aa40a3e340b301f79.zip | |
Changes in the control of C-stack overflow
* unification of the 'nny' and 'nCcalls' counters;
* external C functions ('lua_CFunction') count more "slots" in
the C stack (to allow for their possible use of buffers)
* added a new test script specific for C-stack overflows. (Most
of those tests were already present, but concentrating them
in a single script easies the task of checking whether
'LUAI_MAXCCALLS' is adequate in a system.)
Diffstat (limited to 'testes/cstack.lua')
| -rw-r--r-- | testes/cstack.lua | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/testes/cstack.lua b/testes/cstack.lua new file mode 100644 index 00000000..9e5bbaef --- /dev/null +++ b/testes/cstack.lua | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | -- $Id: testes/cstack.lua $ | ||
| 2 | -- See Copyright Notice in file all.lua | ||
| 3 | |||
| 4 | print"testing C-stack overflow detection" | ||
| 5 | |||
| 6 | -- Segmentation faults in these tests probably result from a C-stack | ||
| 7 | -- overflow. To avoid these errors, recompile Lua with a smaller | ||
| 8 | -- value for the constant 'LUAI_MAXCCALLS' or else ensure a larger | ||
| 9 | -- stack for the program. | ||
| 10 | |||
| 11 | local function checkerror (msg, f, ...) | ||
| 12 | local s, err = pcall(f, ...) | ||
| 13 | assert(not s and string.find(err, msg)) | ||
| 14 | end | ||
| 15 | |||
| 16 | |||
| 17 | do -- simple recursion | ||
| 18 | local count = 0 | ||
| 19 | local function foo () | ||
| 20 | count = count + 1 | ||
| 21 | foo() | ||
| 22 | end | ||
| 23 | checkerror("stack overflow", foo) | ||
| 24 | print(" maximum recursion: " .. count) | ||
| 25 | end | ||
| 26 | |||
| 27 | |||
| 28 | -- bug since 2.5 (C-stack overflow in recursion inside pattern matching) | ||
| 29 | do | ||
| 30 | local function f (size) | ||
| 31 | local s = string.rep("a", size) | ||
| 32 | local p = string.rep(".?", size) | ||
| 33 | return string.match(s, p) | ||
| 34 | end | ||
| 35 | local m = f(80) | ||
| 36 | assert(#m == 80) | ||
| 37 | checkerror("too complex", f, 200000) | ||
| 38 | end | ||
| 39 | |||
| 40 | |||
| 41 | -- testing stack-overflow in recursive 'gsub' | ||
| 42 | do | ||
| 43 | local count = 0 | ||
| 44 | local function foo () | ||
| 45 | count = count + 1 | ||
| 46 | string.gsub("a", ".", foo) | ||
| 47 | end | ||
| 48 | checkerror("stack overflow", foo) | ||
| 49 | print(" maximum 'gsub' nest (calls): " .. count) | ||
| 50 | |||
| 51 | -- can be done with metamethods, too | ||
| 52 | count = 0 | ||
| 53 | local t = setmetatable({}, {__index = foo}) | ||
| 54 | foo = function () | ||
| 55 | count = count + 1 | ||
| 56 | string.gsub("a", ".", t) | ||
| 57 | end | ||
| 58 | checkerror("stack overflow", foo) | ||
| 59 | print(" maximum 'gsub' nest (metamethods): " .. count) | ||
| 60 | end | ||
| 61 | |||
| 62 | print'OK' | ||
