2018-12-27 19:32:29 +03:00
|
|
|
-- $Id: testes/cstack.lua $
|
|
|
|
-- See Copyright Notice in file all.lua
|
|
|
|
|
2019-06-18 22:52:22 +03:00
|
|
|
|
2021-03-01 19:54:29 +03:00
|
|
|
local tracegc = require"tracegc"
|
|
|
|
|
2020-10-06 19:37:41 +03:00
|
|
|
print"testing stack overflow detection"
|
2019-06-26 19:26:36 +03:00
|
|
|
|
|
|
|
-- Segmentation faults in these tests probably result from a C-stack
|
2020-09-23 16:18:01 +03:00
|
|
|
-- overflow. To avoid these errors, you should set a smaller limit for
|
|
|
|
-- the use of C stack by Lua, by changing the constant 'LUAI_MAXCCALLS'.
|
2019-06-26 19:26:36 +03:00
|
|
|
-- Alternatively, you can ensure a larger stack for the program.
|
|
|
|
|
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-06-03 17:34:32 +03:00
|
|
|
do print("testing stack overflow in message handling")
|
2020-09-23 16:18:01 +03:00
|
|
|
local count = 0
|
2019-06-03 17:34:32 +03:00
|
|
|
local function loop (x, y, z)
|
2020-09-23 16:18:01 +03:00
|
|
|
count = count + 1
|
2019-06-03 17:34:32 +03:00
|
|
|
return 1 + loop(x, y, z)
|
|
|
|
end
|
2021-03-01 19:54:29 +03:00
|
|
|
tracegc.stop() -- __gc should not be called with a full stack
|
2019-06-03 17:34:32 +03:00
|
|
|
local res, msg = xpcall(loop, loop)
|
2021-03-01 19:54:29 +03:00
|
|
|
tracegc.start()
|
2019-06-03 17:34:32 +03:00
|
|
|
assert(msg == "error in error handling")
|
2020-09-23 16:18:01 +03:00
|
|
|
print("final 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)
|
2020-09-23 16:18:01 +03:00
|
|
|
checkerror("too complex", f, 2000)
|
2018-12-27 19:32:29 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-06-03 17:34:32 +03:00
|
|
|
do print("testing stack-overflow in recursive 'gsub'")
|
2020-09-23 16:18:01 +03:00
|
|
|
local count = 0
|
2018-12-27 19:32:29 +03:00
|
|
|
local function foo ()
|
2020-09-23 16:18:01 +03:00
|
|
|
count = count + 1
|
2018-12-27 19:32:29 +03:00
|
|
|
string.gsub("a", ".", foo)
|
|
|
|
end
|
|
|
|
checkerror("stack overflow", foo)
|
2020-09-23 16:18:01 +03:00
|
|
|
print("final 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")
|
2020-09-23 16:18:01 +03:00
|
|
|
local count = 0
|
2018-12-27 19:32:29 +03:00
|
|
|
local t = setmetatable({}, {__index = foo})
|
|
|
|
foo = function ()
|
|
|
|
count = count + 1
|
|
|
|
string.gsub("a", ".", t)
|
|
|
|
end
|
|
|
|
checkerror("stack overflow", foo)
|
2020-09-23 16:18:01 +03:00
|
|
|
print("final count: ", count)
|
2018-12-27 19:32:29 +03:00
|
|
|
end
|
|
|
|
|
2020-09-23 16:18:01 +03:00
|
|
|
|
2020-07-13 19:39:02 +03:00
|
|
|
do -- bug in 5.4.0
|
|
|
|
print("testing limits in coroutines inside deep calls")
|
2020-09-23 16:18:01 +03:00
|
|
|
local count = 0
|
2020-07-13 19:39:02 +03:00
|
|
|
local lim = 1000
|
|
|
|
local function stack (n)
|
|
|
|
if n > 0 then return stack(n - 1) + 1
|
|
|
|
else coroutine.wrap(function ()
|
2020-09-23 16:18:01 +03:00
|
|
|
count = count + 1
|
2020-07-13 19:39:02 +03:00
|
|
|
stack(lim)
|
|
|
|
end)()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-23 16:18:01 +03:00
|
|
|
local st, msg = xpcall(stack, function () return "ok" end, lim)
|
|
|
|
assert(not st and msg == "ok")
|
|
|
|
print("final count: ", count)
|
2020-07-13 19:39:02 +03:00
|
|
|
end
|
|
|
|
|
2019-06-18 22:52:22 +03:00
|
|
|
|
2022-10-25 22:44:06 +03:00
|
|
|
do -- bug since 5.4.0
|
|
|
|
local count = 0
|
|
|
|
print("chain of 'coroutine.close'")
|
|
|
|
-- create N coroutines forming a list so that each one, when closed,
|
|
|
|
-- closes the previous one. (With a large enough N, previous Lua
|
|
|
|
-- versions crash in this test.)
|
|
|
|
local coro = false
|
|
|
|
for i = 1, 1000 do
|
|
|
|
local previous = coro
|
|
|
|
coro = coroutine.create(function()
|
|
|
|
local cc <close> = setmetatable({}, {__close=function()
|
|
|
|
count = count + 1
|
|
|
|
if previous then
|
|
|
|
assert(coroutine.close(previous))
|
|
|
|
end
|
|
|
|
end})
|
|
|
|
coroutine.yield() -- leaves 'cc' pending to be closed
|
|
|
|
end)
|
|
|
|
assert(coroutine.resume(coro)) -- start it and run until it yields
|
|
|
|
end
|
|
|
|
local st, msg = coroutine.close(coro)
|
|
|
|
assert(not st and string.find(msg, "C stack overflow"))
|
|
|
|
print("final count: ", count)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-09-23 16:18:01 +03:00
|
|
|
do
|
|
|
|
print("nesting of resuming yielded coroutines")
|
|
|
|
local count = 0
|
2019-06-18 22:52:22 +03:00
|
|
|
|
2020-09-23 16:18:01 +03:00
|
|
|
local function body ()
|
|
|
|
coroutine.yield()
|
|
|
|
local f = coroutine.wrap(body)
|
|
|
|
f(); -- start new coroutine (will stop in previous yield)
|
|
|
|
count = count + 1
|
|
|
|
f() -- call it recursively
|
2019-06-18 22:52:22 +03:00
|
|
|
end
|
|
|
|
|
2020-09-23 16:18:01 +03:00
|
|
|
local f = coroutine.wrap(body)
|
|
|
|
f()
|
|
|
|
assert(not pcall(f))
|
|
|
|
print("final count: ", count)
|
2019-06-18 22:52:22 +03:00
|
|
|
end
|
|
|
|
|
2020-10-06 19:37:41 +03:00
|
|
|
|
2021-11-03 21:04:18 +03:00
|
|
|
do -- bug in 5.4.2
|
|
|
|
print("nesting coroutines running after recoverable errors")
|
|
|
|
local count = 0
|
|
|
|
local function foo()
|
|
|
|
count = count + 1
|
|
|
|
pcall(1) -- create an error
|
|
|
|
-- running now inside 'precover' ("protected recover")
|
|
|
|
coroutine.wrap(foo)() -- call another coroutine
|
|
|
|
end
|
|
|
|
checkerror("C stack overflow", foo)
|
|
|
|
print("final count: ", count)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-10-06 19:37:41 +03:00
|
|
|
if T then
|
|
|
|
print("testing stack recovery")
|
|
|
|
local N = 0 -- trace number of calls
|
|
|
|
local LIM = -1 -- will store N just before stack overflow
|
|
|
|
|
|
|
|
-- trace stack size; after stack overflow, it should be
|
|
|
|
-- the maximum allowed stack size.
|
|
|
|
local stack1
|
|
|
|
local dummy
|
|
|
|
|
|
|
|
local function err(msg)
|
|
|
|
assert(string.find(msg, "stack overflow"))
|
|
|
|
local _, stacknow = T.stacklevel()
|
|
|
|
assert(stacknow == stack1 + 200)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- When LIM==-1, the 'if' is not executed, so this function only
|
|
|
|
-- counts and stores the stack limits up to overflow. Then, LIM
|
|
|
|
-- becomes N, and then the 'if' code is run when the stack is
|
|
|
|
-- full. Then, there is a stack overflow inside 'xpcall', after which
|
|
|
|
-- the stack must have been restored back to its maximum normal size.
|
|
|
|
local function f()
|
|
|
|
dummy, stack1 = T.stacklevel()
|
|
|
|
if N == LIM then
|
|
|
|
xpcall(f, err)
|
|
|
|
local _, stacknow = T.stacklevel()
|
|
|
|
assert(stacknow == stack1)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
N = N + 1
|
|
|
|
f()
|
|
|
|
end
|
|
|
|
|
|
|
|
local topB, sizeB -- top and size Before overflow
|
|
|
|
local topA, sizeA -- top and size After overflow
|
|
|
|
topB, sizeB = T.stacklevel()
|
2021-03-01 19:54:29 +03:00
|
|
|
tracegc.stop() -- __gc should not be called with a full stack
|
2020-10-06 19:37:41 +03:00
|
|
|
xpcall(f, err)
|
2021-03-01 19:54:29 +03:00
|
|
|
tracegc.start()
|
2020-10-06 19:37:41 +03:00
|
|
|
topA, sizeA = T.stacklevel()
|
|
|
|
-- sizes should be comparable
|
|
|
|
assert(topA == topB and sizeA < sizeB * 2)
|
|
|
|
print(string.format("maximum stack size: %d", stack1))
|
|
|
|
LIM = N -- will stop recursion at maximum level
|
|
|
|
N = 0 -- to count again
|
2021-03-01 19:54:29 +03:00
|
|
|
tracegc.stop() -- __gc should not be called with a full stack
|
2020-10-06 19:37:41 +03:00
|
|
|
f()
|
2021-03-01 19:54:29 +03:00
|
|
|
tracegc.start()
|
2020-10-06 19:37:41 +03:00
|
|
|
print"+"
|
|
|
|
end
|
|
|
|
|
2018-12-27 19:32:29 +03:00
|
|
|
print'OK'
|