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
|
|
|
|
2018-12-27 19:32:29 +03:00
|
|
|
print"testing C-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
|
|
|
|
local res, msg = xpcall(loop, loop)
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
2018-12-27 19:32:29 +03:00
|
|
|
print'OK'
|