Skip to content

Commit 6e14ec7

Browse files
committed
tests/lapi: add builtin tests
1 parent 0508724 commit 6e14ec7

32 files changed

+993
-0
lines changed

tests/lapi/builtin_add_test.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
3.1 – Arithmetic Operators
6+
https://www.lua.org/pil/3.1.html
7+
https://www.lua.org/manual/5.1/manual.html#2.5.1
8+
9+
See properties in
10+
"ESA/390 Enhanced Floating Point Support: An Overview"
11+
(The Facts of Floating Point Arithmetic) [1].
12+
13+
1. http://ftpmirror.your.org/pub/misc/ftp.software.ibm.com/software/websphere/awdtools/hlasm/sh93fpov.pdf
14+
]]
15+
16+
local luzer = require("luzer")
17+
18+
local function TestOneInput(_buf)
19+
-- TODO
20+
end
21+
22+
local args = {
23+
artifact_prefix = "builtin_add_",
24+
}
25+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/builtin_assert_test.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
5.1 – Basic Functions
6+
https://www.lua.org/manual/5.1/manual.html
7+
8+
Synopsis: assert(v [, message])
9+
]]
10+
11+
local luzer = require("luzer")
12+
local test_lib = require("lib")
13+
local MAX_INT = test_lib.MAX_INT
14+
15+
local function TestOneInput(buf)
16+
local fdp = luzer.FuzzedDataProvider(buf)
17+
local max_len = fdp:consume_integer(0, MAX_INT)
18+
local message = fdp:consume_string(max_len)
19+
local v = fdp:consume_boolean()
20+
local ok, _ = pcall(assert, v, message)
21+
assert(ok == v)
22+
end
23+
24+
local args = {
25+
artifact_prefix = "builtin_assert_",
26+
}
27+
luzer.Fuzz(TestOneInput, nil, args)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--[=[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
5.1 – Basic Functions
6+
https://www.lua.org/manual/5.1/manual.html
7+
8+
Synopsis: collectgarbage([opt [, arg]])
9+
]]=]
10+
11+
local luzer = require("luzer")
12+
13+
local function TestOneInput(buf)
14+
pcall(collectgarbage, buf)
15+
end
16+
17+
local args = {
18+
artifact_prefix = "builtin_collectgarbage_",
19+
}
20+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/builtin_concat_test.lua

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
2.5.4 – Concatenation
6+
https://www.lua.org/manual/5.1/manual.html
7+
8+
Recording of __concat in GC64 mode,
9+
https://github.com/LuaJIT/LuaJIT/issues/839
10+
11+
Bug: Unbalanced Stack After Hot Instruction error on table concatenation,
12+
https://github.com/LuaJIT/LuaJIT/issues/690
13+
14+
LJ_GC64: Fix lua_concat(),
15+
https://github.com/LuaJIT/LuaJIT/issues/881
16+
17+
Buffer overflow in string concatenation,
18+
https://github.com/lua/lua/commit/5853c37a83ec66ccb45094f9aeac23dfdbcde671
19+
20+
Wrong assert when reporting concatenation errors
21+
(manifests only when Lua is compiled in debug mode).
22+
https://www.lua.org/bugs.html#5.2.2-3
23+
24+
Wrong error message in some concatenations,
25+
https://www.lua.org/bugs.html#5.1.2-5
26+
27+
Concat metamethod converts numbers to strings,
28+
https://www.lua.org/bugs.html#5.1.1-8
29+
30+
String concatenation may cause arithmetic overflow, leading to
31+
a buffer overflow,
32+
https://www.lua.org/bugs.html#5.0.2-1
33+
]]
34+
35+
local luzer = require("luzer")
36+
local test_lib = require("lib")
37+
38+
local function TestOneInput(buf)
39+
local fdp = luzer.FuzzedDataProvider(buf)
40+
local tbl = test_lib.random_table(fdp)
41+
assert(table.concat(tbl) == buf)
42+
43+
-- Build format string.
44+
local fmt_string = ""
45+
for _, v in ipairs(tbl) do
46+
local t = type(v)
47+
if t == "string" then
48+
fmt_string = fmt_string .. "%s"
49+
end
50+
local d = tonumber(v)
51+
if d ~= nil then
52+
-- FIXME: Update a value as well.
53+
fmt_string = fmt_string .. "%d"
54+
end
55+
end
56+
local table_concat = table.concat(tbl)
57+
-- FIXME
58+
-- assert(table_concat == string.format(fmt_string, unpack(tbl)))
59+
60+
local concat_tbl = ""
61+
for _, v in ipairs(tbl) do
62+
concat_tbl = concat_tbl .. v
63+
end
64+
assert(table_concat == concat_tbl)
65+
end
66+
67+
local args = {
68+
artifact_prefix = "builtin_concat_",
69+
}
70+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/builtin_div_test.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
3.1 – Arithmetic Operators
6+
https://www.lua.org/pil/3.1.html
7+
https://www.lua.org/manual/5.1/manual.html#2.5.1
8+
9+
See properties in
10+
"ESA/390 Enhanced Floating Point Support: An Overview"
11+
(The Facts of Floating Point Arithmetic) [1].
12+
13+
1. http://ftpmirror.your.org/pub/misc/ftp.software.ibm.com/software/websphere/awdtools/hlasm/sh93fpov.pdf
14+
]]
15+
16+
local luzer = require("luzer")
17+
local test_lib = require("lib")
18+
local MAX_INT = test_lib.MAX_INT
19+
local MIN_INT = test_lib.MIN_INT
20+
21+
local function TestOneInput(buf)
22+
local fdp = luzer.FuzzedDataProvider(buf)
23+
local x = fdp:consume_integer(MIN_INT, MAX_INT)
24+
local y = fdp:consume_integer(MIN_INT, MAX_INT)
25+
local res = x / y
26+
assert(res * y == x)
27+
end
28+
29+
local args = {
30+
artifact_prefix = "builtin_div_",
31+
}
32+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/builtin_dofile_test.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
5.1 – Basic Functions
6+
https://www.lua.org/manual/5.1/manual.html
7+
8+
Synopsis: dofile([filename])
9+
]]
10+
11+
local luzer = require("luzer")
12+
13+
local function TestOneInput(buf)
14+
local chunk_filename = os.tmpname()
15+
local fh = io.open(chunk_filename, "w")
16+
fh:write(buf)
17+
fh:close()
18+
pcall(dofile, chunk_filename)
19+
os.remove(chunk_filename)
20+
end
21+
22+
local args = {
23+
artifact_prefix = "builtin_dofile_",
24+
}
25+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/builtin_dostring_test.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
5.1 – Basic Functions
6+
https://www.lua.org/manual/5.1/manual.html
7+
]]
8+
9+
local luzer = require("luzer")
10+
local test_lib = require("lib")
11+
local MAX_INT = test_lib.MAX_INT
12+
13+
local function TestOneInput(buf)
14+
local fdp = luzer.FuzzedDataProvider(buf)
15+
local max_len = fdp:consume_integer(0, MAX_INT)
16+
local str = fdp:consume_string(max_len)
17+
pcall(loadstring, str)
18+
end
19+
20+
local args = {
21+
artifact_prefix = "builtin_dostring_",
22+
}
23+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/builtin_error_test.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
5.1 – Basic Functions
6+
https://www.lua.org/manual/5.1/manual.html
7+
8+
Synopsis: error(message [, level])
9+
]]
10+
11+
local luzer = require("luzer")
12+
local test_lib = require("lib")
13+
local MAX_INT = test_lib.MAX_INT
14+
15+
local function TestOneInput(buf)
16+
local fdp = luzer.FuzzedDataProvider(buf)
17+
local level = fdp:consume_integer(0, MAX_INT)
18+
local message_len = fdp:consume_integer(0, MAX_INT)
19+
local message = fdp:consume_string(message_len)
20+
local ok, err = pcall(error, message, level)
21+
assert(ok == false)
22+
assert(err:match(message) == message)
23+
end
24+
25+
local args = {
26+
artifact_prefix = "builtin_error_",
27+
}
28+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/builtin_exp_test.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
3.1 – Arithmetic Operators
6+
https://www.lua.org/pil/3.1.html
7+
https://www.lua.org/manual/5.1/manual.html#2.5.1
8+
9+
> Lua also offers partial support for `^´ (exponentiation).
10+
> One of the design goals of Lua is to have a tiny core.
11+
> An exponentiation operation (implemented through the `pow`
12+
> function in C) would mean that we should always need to link
13+
> Lua with the C mathematical library.
14+
15+
See properties in
16+
"ESA/390 Enhanced Floating Point Support: An Overview"
17+
(The Facts of Floating Point Arithmetic) [1].
18+
19+
1. http://ftpmirror.your.org/pub/misc/ftp.software.ibm.com/software/websphere/awdtools/hlasm/sh93fpov.pdf
20+
]]
21+
22+
local luzer = require("luzer")
23+
local test_lib = require("lib")
24+
local MAX_INT = test_lib.MAX_INT
25+
local MIN_INT = test_lib.MIN_INT
26+
27+
local function TestOneInput(buf)
28+
local fdp = luzer.FuzzedDataProvider(buf)
29+
local x = fdp:consume_integer(MIN_INT, MAX_INT)
30+
local y = fdp:consume_integer(MIN_INT, MAX_INT)
31+
local _ = x ^ y
32+
end
33+
34+
local args = {
35+
artifact_prefix = "builtin_exp_",
36+
}
37+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/builtin_getfenv_test.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
5.1 – Basic Functions
6+
https://www.lua.org/manual/5.1/manual.html
7+
8+
debug.getfenv does not check whether it has an argument,
9+
https://www.lua.org/bugs.html#5.1.4-5
10+
11+
module may change the environment of a C function,
12+
https://www.lua.org/bugs.html#5.1.3-11
13+
14+
UBSan warning for too big/small getfenv/setfenv level,
15+
https://github.com/LuaJIT/LuaJIT/issues/1329
16+
17+
Synopsis: getfenv([f])
18+
]]
19+
20+
local luzer = require("luzer")
21+
local test_lib = require("lib")
22+
local MAX_INT = test_lib.MAX_INT
23+
24+
-- Lua 5.2: Functions setfenv and getfenv were removed, because
25+
-- of the changes in environments.
26+
if test_lib.version() ~= "LuaJIT" then
27+
os.exit(0)
28+
end
29+
30+
local function TestOneInput(buf)
31+
local fdp = luzer.FuzzedDataProvider(buf)
32+
local n = fdp:consume_integer(0, MAX_INT)
33+
setfenv(TestOneInput, { n = n })
34+
local fenv = getfenv(TestOneInput)
35+
assert(fenv.n == n)
36+
end
37+
38+
local args = {
39+
artifact_prefix = "builtin_getfenv_",
40+
}
41+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)