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.)
This commit is contained in:
Roberto Ierusalimschy
2018-12-27 14:32:29 -02:00
parent da37ac9c78
commit ba7da13ec5
12 changed files with 170 additions and 74 deletions

View File

@@ -172,6 +172,7 @@ if not _G._soft then
assert(f() == 'b')
assert(f() == 'a')
end
dofile('cstack.lua')
dofile('nextvar.lua')
dofile('pm.lua')
dofile('utf8.lua')

View File

@@ -107,7 +107,7 @@ function filter (p, g)
end)
end
local x = gen(100)
local x = gen(80)
local a = {}
while 1 do
local n = x()
@@ -116,7 +116,7 @@ while 1 do
x = filter(n, x)
end
assert(#a == 25 and a[#a] == 97)
assert(#a == 22 and a[#a] == 79)
x, a = nil

62
testes/cstack.lua Normal file
View File

@@ -0,0 +1,62 @@
-- $Id: testes/cstack.lua $
-- See Copyright Notice in file all.lua
print"testing C-stack overflow detection"
-- Segmentation faults in these tests probably result from a C-stack
-- overflow. To avoid these errors, recompile Lua with a smaller
-- value for the constant 'LUAI_MAXCCALLS' or else ensure a larger
-- stack for the program.
local function checkerror (msg, f, ...)
local s, err = pcall(f, ...)
assert(not s and string.find(err, msg))
end
do -- simple recursion
local count = 0
local function foo ()
count = count + 1
foo()
end
checkerror("stack overflow", foo)
print(" maximum recursion: " .. count)
end
-- bug since 2.5 (C-stack overflow in recursion inside pattern matching)
do
local function f (size)
local s = string.rep("a", size)
local p = string.rep(".?", size)
return string.match(s, p)
end
local m = f(80)
assert(#m == 80)
checkerror("too complex", f, 200000)
end
-- testing stack-overflow in recursive 'gsub'
do
local count = 0
local function foo ()
count = count + 1
string.gsub("a", ".", foo)
end
checkerror("stack overflow", foo)
print(" maximum 'gsub' nest (calls): " .. count)
-- can be done with metamethods, too
count = 0
local t = setmetatable({}, {__index = foo})
foo = function ()
count = count + 1
string.gsub("a", ".", t)
end
checkerror("stack overflow", foo)
print(" maximum 'gsub' nest (metamethods): " .. count)
end
print'OK'

View File

@@ -237,18 +237,6 @@ checkerror("invalid capture index %%0", string.gsub, "alo", "(%0)", "a")
checkerror("invalid capture index %%1", string.gsub, "alo", "(%1)", "a")
checkerror("invalid use of '%%'", string.gsub, "alo", ".", "%x")
-- bug since 2.5 (C-stack overflow)
do
local function f (size)
local s = string.rep("a", size)
local p = string.rep(".?", size)
return pcall(string.match, s, p)
end
local r, m = f(80)
assert(r and #m == 80)
r, m = f(200000)
assert(not r and string.find(m, "too complex"))
end
if not _soft then
print("big strings")