[PATCH v3 11/15] perf python: Add thread and PMU uninitialized checks

From: Ian Rogers

Date: Sun Aug 09 2026 - 03:15:47 EST


Add CHECK_INITIALIZED checks to the thread attribute getters
(get_pid, get_tid, get_ppid) to prevent crashes if they are accessed
before being properly initialized.

Fixes: 3b96bf7af60d ("perf python: Add python session abstraction wrapping perf's session")
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/python.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index c70b151428f7..fe5afce17333 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -1678,6 +1678,7 @@ static PyObject *pyrf_pmu__name(PyObject *self)
{
struct pyrf_pmu *ppmu = (void *)self;

+ CHECK_INITIALIZED(ppmu->pmu, "pmu");
return PyUnicode_FromString(ppmu->pmu->name);
}

@@ -1730,6 +1731,8 @@ static PyObject *pyrf_pmu__events(PyObject *self)
PyObject *py_list = PyList_New(0);
int ret;

+ CHECK_INITIALIZED(ppmu->pmu, "pmu");
+
if (!py_list)
return NULL;

@@ -1750,6 +1753,7 @@ static PyObject *pyrf_pmu__repr(PyObject *self)
{
struct pyrf_pmu *ppmu = (void *)self;

+ CHECK_INITIALIZED(ppmu->pmu, "pmu");
return PyUnicode_FromFormat("pmu(%s)", ppmu->pmu->name);
}

@@ -3764,21 +3768,25 @@ static PyMethodDef pyrf_thread__methods[] = {

static PyObject *pyrf_thread__get_pid(struct pyrf_thread *pthread, void *closure __maybe_unused)
{
+ CHECK_INITIALIZED(pthread->thread, "thread");
return PyLong_FromLong(thread__pid(pthread->thread));
}

static PyObject *pyrf_thread__get_tid(struct pyrf_thread *pthread, void *closure __maybe_unused)
{
+ CHECK_INITIALIZED(pthread->thread, "thread");
return PyLong_FromLong(thread__tid(pthread->thread));
}

static PyObject *pyrf_thread__get_ppid(struct pyrf_thread *pthread, void *closure __maybe_unused)
{
+ CHECK_INITIALIZED(pthread->thread, "thread");
return PyLong_FromLong(thread__ppid(pthread->thread));
}

static PyObject *pyrf_thread__get_cpu(struct pyrf_thread *pthread, void *closure __maybe_unused)
{
+ CHECK_INITIALIZED(pthread->thread, "thread");
return PyLong_FromLong(thread__cpu(pthread->thread));
}

--
2.55.0.679.g6767b8d81c-goog