Skip to content

Commit afac0b9

Browse files
committed
tests/lapi: add coroutine test
The patch adds a fuzzing test for the coroutine library.
1 parent 8ef72ef commit afac0b9

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

tests/lapi/coroutine_test.lua

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
2.6 – Coroutines
6+
https://www.lua.org/manual/5.3/manual.html#2.6
7+
8+
Computation of stack limit when entering a coroutine is wrong,
9+
https://github.com/lua/lua/commit/e1d8770f12542d34a3e32b825c95b93f8a341ee1
10+
11+
C-stack overflow with deep nesting of coroutine.close,
12+
https://www.lua.org/bugs.html#5.4.4-9
13+
14+
C stack overflow (again),
15+
https://github.com/lua/lua/commit/34affe7a63fc5d842580a9f23616d057e17dfe27
16+
17+
When a coroutine tries to resume a non-suspended coroutine,
18+
it can do some mess (and break C assertions) before detecting the error,
19+
https://www.lua.org/bugs.html#5.3.3-4
20+
21+
debug.getlocal on a coroutine suspended in a hook can crash the interpreter,
22+
https://www.lua.org/bugs.html#5.3.0-2
23+
24+
Suspended __le metamethod can give wrong result,
25+
https://www.lua.org/bugs.html#5.3.0-3
26+
27+
Resuming the running coroutine makes it unyieldable,
28+
https://www.lua.org/bugs.html#5.2.2-8
29+
30+
pcall may not restore previous error function when inside coroutines,
31+
https://www.lua.org/bugs.html#5.2.1-2
32+
33+
Wrong handling of nCcalls in coroutines,
34+
https://www.lua.org/bugs.html#5.2.0-4
35+
36+
coroutine.resume pushes element without ensuring stack size,
37+
https://www.lua.org/bugs.html#5.1.3-2
38+
39+
Recursive coroutines may overflow C stack,
40+
https://www.lua.org/bugs.html#5.1.2-4
41+
42+
Stand-alone interpreter shows incorrect error message when the
43+
"message" is a coroutine,
44+
https://www.lua.org/bugs.html#5.1.2-12
45+
46+
Debug hooks may get wrong when mixed with coroutines,
47+
https://www.lua.org/bugs.html#5.1-7
48+
49+
Values held in open upvalues of suspended threads may be
50+
incorrectly collected,
51+
https://www.lua.org/bugs.html#5.0.2-3
52+
53+
Attempt to resume a running coroutine crashes Lua,
54+
https://www.lua.org/bugs.html#5.0-2
55+
56+
debug.getlocal on a coroutine suspended in a hook can crash the interpreter,
57+
https://www.lua.org/bugs.html#5.3.0-2
58+
59+
debug.sethook/gethook may overflow the thread's stack,
60+
https://www.lua.org/bugs.html#5.1.2-13
61+
62+
Memory hoarding when creating Lua hooks for coroutines,
63+
https://www.lua.org/bugs.html#5.2.0-1
64+
65+
Synopsis:
66+
67+
coroutine.close(co)
68+
coroutine.create(f)
69+
coroutine.isyieldable([co])
70+
coroutine.resume(co [, val1, ...])
71+
coroutine.running()
72+
coroutine.status(co)
73+
coroutine.wrap(f)
74+
coroutine.yield(...)
75+
]]
76+
77+
local luzer = require("luzer")
78+
local test_lib = require("lib")
79+
80+
local CORO_OBJECTS = {}
81+
82+
-- The function `coroutine.isyieldable()` is checked together with
83+
-- `coroutine.yield()`, `coroutine.status()` is used inside the
84+
-- target function.
85+
local CORO_ACTION_NAME = {
86+
"create",
87+
"resume",
88+
"yield",
89+
}
90+
-- `coroutine.close()` is introduced in the Lua 5.4,
91+
-- https://www.lua.org/manual/5.4/manual.html#pdf-coroutine.close.
92+
if test_lib.lua_current_version_ge_than(5, 4) then
93+
table.insert(CORO_ACTION_NAME, "close")
94+
end
95+
96+
-- Forward declaration.
97+
local coro_function
98+
99+
local function hook_func(_event)
100+
-- Accessing Locals,
101+
-- https://www.lua.org/pil/23.1.1.html.
102+
local level = 2
103+
local i = 1
104+
while true do
105+
local name, _ = debug.getlocal(level, i)
106+
if not name then break end
107+
i = i + 1
108+
end
109+
-- Accessing Upvalues,
110+
-- https://www.lua.org/pil/23.1.2.html.
111+
local func = debug.getinfo(level).func
112+
i = 1
113+
while true do
114+
local name, _ = debug.getupvalue(func, i)
115+
if not name then break end
116+
i = i + 1
117+
end
118+
end
119+
120+
local function sethook(co, fdp)
121+
local set_hook = fdp:consume_boolean()
122+
local hook_args = {}
123+
if not set_hook then
124+
return
125+
end
126+
table.insert(hook_args, hook_func)
127+
table.insert(hook_args, fdp:oneof({"c", "r", "l"}))
128+
debug.sethook(co, unpack(hook_args))
129+
end
130+
131+
local function coro_random_action(fdp, coro_max_number)
132+
local action = fdp:oneof(CORO_ACTION_NAME)
133+
if action == "create" and #CORO_OBJECTS < coro_max_number then
134+
local co = coroutine.create(coro_function)
135+
table.insert(CORO_OBJECTS, co)
136+
return
137+
end
138+
action = fdp:oneof(CORO_ACTION_NAME)
139+
local co, co_idx = fdp:oneof(CORO_OBJECTS)
140+
sethook(co, fdp)
141+
if coroutine.status(co) == "dead" then
142+
table.remove(CORO_OBJECTS, co_idx)
143+
return
144+
elseif action == "close" then
145+
coroutine.close(co)
146+
elseif action == "yield" and coroutine.isyieldable(co) then
147+
coroutine.yield(co)
148+
elseif action == "resume" then
149+
coroutine.resume(co)
150+
end
151+
end
152+
153+
coro_function = function(fdp, coro_max_number)
154+
local MAX_N = 1000
155+
local iter = fdp:consume_integer(1, MAX_N)
156+
for _ = 1, iter do
157+
coro_random_action(fdp, coro_max_number)
158+
end
159+
end
160+
161+
local function TestOneInput(buf, _size)
162+
CORO_OBJECTS = {}
163+
local fdp = luzer.FuzzedDataProvider(buf)
164+
local MAX_N = 1000
165+
local coro_max_number = fdp:consume_integer(1, MAX_N)
166+
local co = coroutine.create(coro_function)
167+
table.insert(CORO_OBJECTS, co)
168+
-- The function `coroutine.resume` starts the execution of
169+
-- a coroutine, changing its state from suspended to running.
170+
coroutine.resume(co, fdp, coro_max_number)
171+
end
172+
173+
local args = {
174+
artifact_prefix = "coroutine_",
175+
}
176+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)