Commit Diff


commit - 51d08cf572256c9fbec8a23a551a05a901917518
commit + 42ef050bb0b3810f1e81465316ad8688ce573f5a
blob - /dev/null
blob + 9eeb5ed6511b1e61c4ce79ae26fab52dc703b3cb (mode 644)
--- /dev/null
+++ tests/lapi/jit_p_test.lua
@@ -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)