Skip to content

tests/lapi: add sysprof test #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions tests/lapi/jit_p_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2025, Sergey Bronnikov.

LuaJIT profiler,
https://luajit.org/ext_profiler.html
]]

local luzer = require("luzer")
local test_lib = require("lib")

if test_lib.lua_version() ~= "LuaJIT" then
print("Unsupported version.")
os.exit(0)
end

local sysprof = require("jit.profile")

local MAX_INT = test_lib.MAX_INT
local MIN_INT = test_lib.MIN_INT

local SYSPROF_DEFAULT_INTERVAL = 1 -- ms

local SYSPROF_OPTIONS = {
"f", -- Profile with precision down to the function level.
"l", -- Profile with precision down to the line level.
"i", -- Sampling interval in milliseconds (default 10ms).
}

local DUMPSTACK_FMT = {
"p", -- Preserve the full path for module names.
"f", -- Dump the function name if it can be derived.
"F", -- Ditto, but dump module:name.
"l", -- Dump module:line.
"Z", -- Zap the following characters for the last dumped frame.
}

local function sysprof_dumpstack(fdp)
local fmt = fdp:oneof(DUMPSTACK_FMT)
local depth = fdp:consume_integer(MIN_INT, MAX_INT)
local dump = sysprof.dumpstack(fmt, depth)
assert(dump)
end

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local chunk = fdp:consume_string(test_lib.MAX_STR_LEN)
-- LuaJIT ASSERT lj_bcread.c:123: bcread_byte: buffer read
-- overflow.
local func = load(chunk, "luzer", "t")

local sysprof_option = fdp:oneof(SYSPROF_OPTIONS)
if sysprof_option == "i" then
sysprof_option = ("i%d"):format(SYSPROF_DEFAULT_INTERVAL)
end
local cb = function(_thread, _samples, _vmstate)
-- Nope.
end

sysprof.start(sysprof_option, cb)
pcall(func)
sysprof_dumpstack(fdp)
sysprof.stop()
end

local args = {
artifact_prefix = "jit_p_test",
}
luzer.Fuzz(TestOneInput, nil, args)
41 changes: 41 additions & 0 deletions tests/lapi/misc_sysprof_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2025, Sergey Bronnikov.

Tarantool's platform profiler,
https://www.tarantool.io/en/doc/latest/tooling/luajit_sysprof/
]]

local luzer = require("luzer")
local test_lib = require("lib")

local has_sysprof, sysprof = pcall(require, "misc.sysprof")
if not has_sysprof then
print("Unsupported version.")
os.exit(0)
end

local SYSPROF_DEFAULT_INTERVAL = 1 -- ms

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local chunk = fdp:consume_string(test_lib.MAX_STR_LEN)
-- LuaJIT ASSERT lj_bcread.c:123: bcread_byte: buffer read
-- overflow.
local func = load(chunk, "luzer", "t")
local sysprof_mode = fdp:oneof({"D", "L", "C"})

assert(sysprof.start({
interval = SYSPROF_DEFAULT_INTERVAL,
mode = sysprof_mode,
path = "/dev/null",
}))
pcall(func)
sysprof.report()
sysprof.stop()
end

local args = {
artifact_prefix = "misc_sysprof_",
}
luzer.Fuzz(TestOneInput, nil, args)