Skip to content

Commit 8800a82

Browse files
committed
tests/lapi: add builtin tests
The commit adds tests for Lua 5.1 builtin functions except the following functions: `print`, `pcall`, `xpcall`.
1 parent bfac9d7 commit 8800a82

30 files changed

+1043
-0
lines changed

tests/lapi/builtin_add_test.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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_INT64 = test_lib.MAX_INT64
19+
20+
local function TestOneInput(buf)
21+
local fdp = luzer.FuzzedDataProvider(buf)
22+
local arg1 = fdp:consume_integer(0, MAX_INT64)
23+
local arg2 = fdp:consume_integer(0, MAX_INT64)
24+
local _ = arg1 + arg2
25+
end
26+
27+
local args = {
28+
artifact_prefix = "builtin_add_",
29+
}
30+
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
local test_lib = require("lib")
13+
14+
local gc_mode = {
15+
"collect",
16+
"count",
17+
"restart",
18+
"setpause",
19+
"setstepmul",
20+
"step",
21+
"stop",
22+
}
23+
24+
local function TestOneInput(buf)
25+
local fdp = luzer.FuzzedDataProvider(buf)
26+
local mode = fdp:oneof(gc_mode)
27+
local MAX_INT = test_lib.MAX_INT
28+
local arg
29+
if mode == "step" or
30+
mode == "setpause" or
31+
mode == "setstepmul" then
32+
arg = fdp:consume_integer(0, MAX_INT)
33+
end
34+
collectgarbage(mode, arg)
35+
end
36+
37+
local args = {
38+
artifact_prefix = "builtin_collectgarbage_",
39+
}
40+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/builtin_concat_test.lua

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 str = fdp:consume_string(test_lib.MAX_STR_LEN)
41+
if str == nil then return -1 end
42+
local str_chars = {}
43+
str:gsub(".", function(c) table.insert(str_chars, c) end)
44+
45+
local str_concat = ""
46+
for _, c in ipairs(str_chars) do
47+
str_concat = str_concat .. c
48+
end
49+
assert(str_concat == str)
50+
end
51+
52+
local args = {
53+
artifact_prefix = "builtin_concat_",
54+
}
55+
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(type(res) == "number")
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: 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: 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+
-- lj_bcread.c:123: bcread_byte: buffer read overflow
25+
only_ascii = 1,
26+
}
27+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/builtin_dostring_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+
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+
-- lj_bcread.c:123: bcread_byte: buffer read overflow
23+
only_ascii = 1,
24+
}
25+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/builtin_error_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+
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 escape_pattern(text)
16+
return (text:gsub('[%-%.%+%[%]%(%)%$%^%%%?%*]','%%%1'))
17+
end
18+
19+
local function TestOneInput(buf)
20+
local fdp = luzer.FuzzedDataProvider(buf)
21+
local level = fdp:consume_integer(0, MAX_INT)
22+
local message_len = fdp:consume_integer(0, MAX_INT)
23+
local message = fdp:consume_string(message_len)
24+
local ok, err = pcall(error, message, level)
25+
assert(ok == false)
26+
assert(err:match(escape_pattern(message)) == message, message)
27+
end
28+
29+
local args = {
30+
artifact_prefix = "builtin_error_",
31+
}
32+
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
23+
-- Lua 5.2: Functions setfenv and getfenv were removed, because
24+
-- of the changes in environments.
25+
if test_lib.lua_current_version_ge_than(5, 2) then
26+
os.exit(0)
27+
end
28+
29+
local function TestOneInput(buf)
30+
local fdp = luzer.FuzzedDataProvider(buf)
31+
local level = fdp:consume_integer(0, test_lib.MAX_INT)
32+
local fenv, err = pcall(getfenv, level)
33+
if err then
34+
return -1
35+
end
36+
local magic_str = fdp:consume_string(test_lib.MAX_STR_LEN)
37+
fenv["magic"] = magic_str
38+
setfenv(level, fenv)
39+
assert(getfenv(level).magic == magic_str)
40+
end
41+
42+
local args = {
43+
artifact_prefix = "builtin_getfenv_",
44+
}
45+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)