2018-12-27 19:32:29 +03:00
|
|
|
-- $Id: testes/cstack.lua $
|
|
|
|
-- See Copyright Notice in file all.lua
|
|
|
|
|
2020-09-21 16:31:03 +03:00
|
|
|
do return end
|
|
|
|
|
2019-06-18 22:52:22 +03:00
|
|
|
local debug = require "debug"
|
|
|
|
|
2018-12-27 19:32:29 +03:00
|
|
|
print"testing C-stack overflow detection"
|
2019-12-30 17:45:08 +03:00
|
|
|
print"If this test crashes, see its file ('cstack.lua')"
|
2019-06-26 19:26:36 +03:00
|
|
|
|
|
|
|
-- Segmentation faults in these tests probably result from a C-stack
|
|
|
|
-- overflow. To avoid these errors, you can use the function
|
2019-09-24 20:31:06 +03:00
|
|
|
-- 'debug.setcstacklimit' to set a smaller limit for the use of
|
2019-06-26 19:26:36 +03:00
|
|
|
-- 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.
|
|
|
|
|
2018-12-27 19:32:29 +03:00
|
|
|
|
2019-12-30 17:45:08 +03:00
|
|
|
-- get and print original limit
|
2020-05-04 20:17:15 +03:00
|
|
|
local origlimit <const> = debug.setcstacklimit(400)
|
2019-06-26 19:26:36 +03:00
|
|
|
print("default stack limit: " .. origlimit)
|
|
|
|
|
2020-05-04 20:17:15 +03:00
|
|
|
|
2019-12-30 17:45:08 +03:00
|
|
|
-- Do the tests using the original limit. Or else you may want to change
|
|
|
|
-- 'currentlimit' to lower values to avoid a seg. fault or to higher
|
|
|
|
-- values to check whether they are reliable.
|
2020-05-04 20:17:15 +03:00
|
|
|
local currentlimit <const> = origlimit
|
2019-09-24 20:31:06 +03:00
|
|
|
debug.setcstacklimit(currentlimit)
|
2019-06-26 19:26:36 +03:00
|
|
|
print("current stack limit: " .. currentlimit)
|
2019-06-18 22:52:22 +03:00
|
|
|
|
2018-12-27 19:32:29 +03:00
|
|
|
|
|
|
|
local function checkerror (msg, f, ...)
|
|
|
|
local s, err = pcall(f, ...)
|
|
|
|
assert(not s and string.find(err, msg))
|
|
|
|
end
|
|
|
|
|
2019-12-30 17:45:08 +03:00
|
|
|
-- auxiliary function to keep 'count' on the screen even if the program
|
|
|
|
-- crashes.
|
2019-06-03 17:34:32 +03:00
|
|
|
local count
|
|
|
|
local back = string.rep("\b", 8)
|
|
|
|
local function progress ()
|
|
|
|
count = count + 1
|
|
|
|
local n = string.format("%-8d", count)
|
2019-12-30 17:45:08 +03:00
|
|
|
io.stderr:write(back, n) -- erase previous value and write new one
|
2019-06-03 17:34:32 +03:00
|
|
|
end
|
|
|
|
|
2018-12-27 19:32:29 +03:00
|
|
|
|
2019-06-03 17:34:32 +03:00
|
|
|
do print("testing simple recursion:")
|
|
|
|
count = 0
|
2018-12-27 19:32:29 +03:00
|
|
|
local function foo ()
|
2019-06-03 17:34:32 +03:00
|
|
|
progress()
|
2019-12-30 17:45:08 +03:00
|
|
|
foo() -- do recursive calls until a stack error (or crash)
|
2018-12-27 19:32:29 +03:00
|
|
|
end
|
|
|
|
checkerror("stack overflow", foo)
|
2019-06-03 17:34:32 +03:00
|
|
|
print("\tfinal count: ", count)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
do print("testing stack overflow in message handling")
|
|
|
|
count = 0
|
|
|
|
local function loop (x, y, z)
|
|
|
|
progress()
|
|
|
|
return 1 + loop(x, y, z)
|
|
|
|
end
|
|
|
|
local res, msg = xpcall(loop, loop)
|
|
|
|
assert(msg == "error in error handling")
|
|
|
|
print("\tfinal count: ", count)
|
2018-12-27 19:32:29 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- bug since 2.5 (C-stack overflow in recursion inside pattern matching)
|
2019-06-03 17:34:32 +03:00
|
|
|
do print("testing recursion inside pattern matching")
|
2018-12-27 19:32:29 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-06-03 17:34:32 +03:00
|
|
|
do print("testing stack-overflow in recursive 'gsub'")
|
|
|
|
count = 0
|
2018-12-27 19:32:29 +03:00
|
|
|
local function foo ()
|
2019-06-03 17:34:32 +03:00
|
|
|
progress()
|
2018-12-27 19:32:29 +03:00
|
|
|
string.gsub("a", ".", foo)
|
|
|
|
end
|
|
|
|
checkerror("stack overflow", foo)
|
2019-06-03 17:34:32 +03:00
|
|
|
print("\tfinal count: ", count)
|
2018-12-27 19:32:29 +03:00
|
|
|
|
2019-06-03 17:34:32 +03:00
|
|
|
print("testing stack-overflow in recursive 'gsub' with metatables")
|
2018-12-27 19:32:29 +03:00
|
|
|
count = 0
|
|
|
|
local t = setmetatable({}, {__index = foo})
|
|
|
|
foo = function ()
|
|
|
|
count = count + 1
|
2019-06-03 17:34:32 +03:00
|
|
|
progress(count)
|
2018-12-27 19:32:29 +03:00
|
|
|
string.gsub("a", ".", t)
|
|
|
|
end
|
|
|
|
checkerror("stack overflow", foo)
|
2019-06-03 17:34:32 +03:00
|
|
|
print("\tfinal count: ", count)
|
2018-12-27 19:32:29 +03:00
|
|
|
end
|
|
|
|
|
2020-07-13 19:39:02 +03:00
|
|
|
do -- bug in 5.4.0
|
|
|
|
print("testing limits in coroutines inside deep calls")
|
|
|
|
count = 0
|
|
|
|
local lim = 1000
|
|
|
|
local function stack (n)
|
|
|
|
progress()
|
|
|
|
if n > 0 then return stack(n - 1) + 1
|
|
|
|
else coroutine.wrap(function ()
|
|
|
|
stack(lim)
|
|
|
|
end)()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
print(xpcall(stack, function () return "ok" end, lim))
|
|
|
|
end
|
|
|
|
|
2019-06-18 22:52:22 +03:00
|
|
|
|
|
|
|
do print("testing changes in C-stack limit")
|
|
|
|
|
2020-05-04 20:17:15 +03:00
|
|
|
-- Just an alternative limit, different from the current one
|
|
|
|
-- (smaller to avoid stack overflows)
|
|
|
|
local alterlimit <const> = currentlimit * 8 // 10
|
|
|
|
|
2019-09-24 20:31:06 +03:00
|
|
|
assert(not debug.setcstacklimit(0)) -- limit too small
|
|
|
|
assert(not debug.setcstacklimit(50000)) -- limit too large
|
2019-06-18 22:52:22 +03:00
|
|
|
local co = coroutine.wrap (function ()
|
2020-05-04 20:17:15 +03:00
|
|
|
return debug.setcstacklimit(alterlimit)
|
2019-06-18 22:52:22 +03:00
|
|
|
end)
|
2020-05-04 20:17:15 +03:00
|
|
|
assert(not co()) -- cannot change C stack inside coroutine
|
2019-06-18 22:52:22 +03:00
|
|
|
|
|
|
|
local n
|
|
|
|
local function foo () n = n + 1; foo () end
|
|
|
|
|
|
|
|
local function check ()
|
|
|
|
n = 0
|
|
|
|
pcall(foo)
|
|
|
|
return n
|
|
|
|
end
|
|
|
|
|
2020-05-04 20:17:15 +03:00
|
|
|
-- set limit to 'alterlimit'
|
|
|
|
assert(debug.setcstacklimit(alterlimit) == currentlimit)
|
|
|
|
local limalter <const> = check()
|
2019-12-30 17:45:08 +03:00
|
|
|
-- set a very low limit (given that there are already several active
|
|
|
|
-- calls to arrive here)
|
2020-05-04 20:17:15 +03:00
|
|
|
local lowlimit <const> = 38
|
|
|
|
assert(debug.setcstacklimit(lowlimit) == alterlimit)
|
|
|
|
-- usable limit is much lower, due to active calls
|
|
|
|
local actuallow = check()
|
|
|
|
assert(actuallow < lowlimit - 30)
|
|
|
|
-- now, add 'lowlimit' extra slots, which should all be available
|
|
|
|
assert(debug.setcstacklimit(lowlimit + lowlimit) == lowlimit)
|
|
|
|
local lim2 <const> = check()
|
|
|
|
assert(lim2 == actuallow + lowlimit)
|
2019-06-18 22:52:22 +03:00
|
|
|
|
|
|
|
|
2019-09-24 20:31:06 +03:00
|
|
|
-- 'setcstacklimit' works inside protected calls. (The new stack
|
2019-06-18 22:52:22 +03:00
|
|
|
-- limit is kept when 'pcall' returns.)
|
|
|
|
assert(pcall(function ()
|
2020-05-04 20:17:15 +03:00
|
|
|
assert(debug.setcstacklimit(alterlimit) == lowlimit * 2)
|
|
|
|
assert(check() <= limalter)
|
2019-06-18 22:52:22 +03:00
|
|
|
end))
|
|
|
|
|
2020-05-04 20:17:15 +03:00
|
|
|
assert(check() == limalter)
|
|
|
|
-- restore original limit
|
|
|
|
assert(debug.setcstacklimit(origlimit) == alterlimit)
|
2019-06-18 22:52:22 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-12-27 19:32:29 +03:00
|
|
|
print'OK'
|