Skip to content

Commit 42ef050

Browse files
committed
tests/lapi: add sysprof test
The patch adds test for sysprof built into LuaJIT.
1 parent 51d08cf commit 42ef050

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

tests/lapi/jit_p_test.lua

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2025, Sergey Bronnikov.
4+
5+
LuaJIT profiler,
6+
https://luajit.org/ext_profiler.html
7+
]]
8+
9+
local luzer = require("luzer")
10+
local test_lib = require("lib")
11+
12+
if test_lib.lua_version() ~= "LuaJIT" then
13+
print("Unsupported version.")
14+
os.exit(0)
15+
end
16+
17+
local sysprof = require("jit.profile")
18+
19+
local MAX_INT = test_lib.MAX_INT
20+
local MIN_INT = test_lib.MIN_INT
21+
22+
local SYSPROF_DEFAULT_INTERVAL = 1 -- ms
23+
24+
local SYSPROF_OPTIONS = {
25+
"f", -- Profile with precision down to the function level.
26+
"l", -- Profile with precision down to the line level.
27+
"i", -- Sampling interval in milliseconds (default 10ms).
28+
}
29+
30+
local DUMPSTACK_FMT = {
31+
"p", -- Preserve the full path for module names.
32+
"f", -- Dump the function name if it can be derived.
33+
"F", -- Ditto, but dump module:name.
34+
"l", -- Dump module:line.
35+
"Z", -- Zap the following characters for the last dumped frame.
36+
}
37+
38+
local function sysprof_dumpstack(fdp)
39+
local fmt = fdp:oneof(DUMPSTACK_FMT)
40+
local depth = fdp:consume_integer(MIN_INT, MAX_INT)
41+
local dump = sysprof.dumpstack(fmt, depth)
42+
assert(dump)
43+
end
44+
45+
local function TestOneInput(buf)
46+
local fdp = luzer.FuzzedDataProvider(buf)
47+
local chunk = fdp:consume_string(test_lib.MAX_STR_LEN)
48+
-- LuaJIT ASSERT lj_bcread.c:123: bcread_byte: buffer read
49+
-- overflow.
50+
local func = load(chunk, "luzer", "t")
51+
52+
local sysprof_option = fdp:oneof(SYSPROF_OPTIONS)
53+
if sysprof_option == "i" then
54+
sysprof_option = ("i%d"):format(SYSPROF_DEFAULT_INTERVAL)
55+
end
56+
local cb = function(_thread, _samples, _vmstate)
57+
-- Nope.
58+
end
59+
60+
sysprof.start(sysprof_option, cb)
61+
pcall(func)
62+
sysprof_dumpstack(fdp)
63+
sysprof.stop()
64+
end
65+
66+
local args = {
67+
artifact_prefix = "jit_p_test",
68+
}
69+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)