From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f202.google.com (mail-dy1-f202.google.com [74.125.82.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDFBD3C2B9B for ; Mon, 15 Jun 2026 22:31:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781562666; cv=none; b=Hana4iHgNfWBfiBfrzXw7IDFFHkkGXb2ep6MZKk9qmuPOrHQCd6V5BfyamvRAl6+gKpUi9DSuNZo6GpP6hz5X0P0/gW7QYArqteUpzLXSLiiaEB66hjrCCzy9HhSEUN6Y68QjydiMLiTwCu7/gu36oExWkWnIEO11XzdjJ4OuLk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781562666; c=relaxed/simple; bh=b+zFPPddi5vsuvVelPM3s+QHu6ScPZ/Q3IhGSJ9QvrI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OA12qy2FIcrnA/QQmN+MCvBz7Pk5VtCLAwdIQnk4vKFM8/y3MVa3BrqrLbDsnq/jsFo/1Hse+fKhuSdHUWwjUapFiP3MIe5z86SCClrEeiTE8oHtb0h3HJcjavGp5p/IgalGLKrp0qtiZkCx4kstxfK4NrKtywto53Ct7bZMJqI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ObC2gyTN; arc=none smtp.client-ip=74.125.82.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ObC2gyTN" Received: by mail-dy1-f202.google.com with SMTP id 5a478bee46e88-304df51ff3eso8204465eec.0 for ; Mon, 15 Jun 2026 15:31:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781562664; x=1782167464; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=DhNmHmJTzevaW5Ie0slFVTtwGbX/K4JKnDxjqTLGpkg=; b=ObC2gyTN1q42RBBG+K8RgEQEQkCQzpPVFnzh97GOuInygy4E6BxkGnsggfhQyh1EQj fIjF2GFvKuJDm/17qYydUeTqRTnXKFuPddVLShd09/SDNgeXSjVubo5bDQ7MO05lMyDy QswsUzA48gLSYGjas9HWgmiolGt5dWR0tLS3bZppely5HCKmzPzZxY1+9IkroNSAYTiP fn25Lx3Kiiy8Thps/zsDlNl13oOkEwJgPPbxhUxvnPFJb/1nritfrwukZ+BGTwBYfp+V 3zZ582MhTP1qRvnPhodby/JxkOCNYATw9f0fVSYnHo2kuSuuCAjtndz/gyDfO41IHAOp Bqug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781562664; x=1782167464; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DhNmHmJTzevaW5Ie0slFVTtwGbX/K4JKnDxjqTLGpkg=; b=SDctZNlE+l6pIZcQHDHRXuGGN+jRPjt4UJK6x8axpoxVGJFIJj9uDBD/NaWgWWQUrv Hw0/vSKUHvaH6brPp6Dr81KSegwDBXbO7U61Y5TyhEh3QJo3wEobmhikO+zrUNPVKuq2 PfwKaGTOQQoEMQY5dnro3mul3aN3rR63pH02DcdFu0zJrmKKfClefjvyIASMVBaxnAa0 trBizGDTydMzUe4l+5gsdKp94gWIce3iC58773ExbYj3cT417JOmPscutbomHK3gWz7J uZH2vwfGU5Zplp1NF0fhX1wfkQtZ9krGU65YNS5O2io7h8U1UA691vr7+EOQc7/+1pMF t4LA== X-Forwarded-Encrypted: i=1; AFNElJ9rU/gTESqkcCr2t8PYu95uF8BG9TNUk0ASelEufLXBNaBj0A/se63Wcu2mYTfGUkjScCszzjBnEqpN3wQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxlUgbgqbo+objy4ofmxTJCmVYKeQBJFMncFpTcEuNztoj2IVpx m2cCR7aasqv70Lhy+j1OdTaqcLjPnXGlKQSUTn5WV2BwB/KLliqMc2+xcCDxHrmGXWt1eJC8C5P PNECyfr4usg== X-Received: from dybbs28.prod.google.com ([2002:a05:7300:a21c:b0:307:bbdd:4346]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:cd93:b0:304:db3a:3919 with SMTP id 5a478bee46e88-3081ff4b664mr8334433eec.6.1781562663736; Mon, 15 Jun 2026 15:31:03 -0700 (PDT) Date: Mon, 15 Jun 2026 15:30:08 -0700 In-Reply-To: <20260615223011.3873955-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260615061631.2382079-1-irogers@google.com> <20260615223011.3873955-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.1136.gdb2ca164c4-goog Message-ID: <20260615223011.3873955-19-irogers@google.com> Subject: [PATCH v20 18/21] perf python: Handle Py_None for thread and cpu maps From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, alice.mei.rogers@gmail.com, dapeng1.mi@linux.intel.com, james.clark@linaro.org, leo.yan@linux.dev, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, tmricht@linux.ibm.com Content-Type: text/plain; charset="UTF-8" The python stubs allow passing None for threads and cpus to the perf.parse_events() and perf.parse_metrics() bindings. However, PyArg_ParseTuple parses None into a Py_None object, which is not a NULL pointer. Because the C code lacked an explicit check for Py_None, it would cast Py_None to a pyrf_thread_map/pyrf_cpu_map struct pointer and dereference it, causing a memory corruption crash. Fix this pre-existing issue by explicitly checking for Py_None alongside NULL in pyrf__parse_events, pyrf__parse_metrics, and pyrf_evsel__open. Signed-off-by: Ian Rogers --- tools/perf/util/python.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index 3a9f4e35d0bb..4ec5a91d45c0 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -2091,10 +2091,10 @@ static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, &pcpus, &pthreads, &group, &inherit)) return NULL; - if (pthreads != NULL) + if (pthreads != NULL && pthreads != Py_None) threads = ((struct pyrf_thread_map *)pthreads)->threads; - if (pcpus != NULL) + if (pcpus != NULL && pcpus != Py_None) cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; evsel->core.attr.inherit = inherit; @@ -3341,8 +3341,10 @@ static PyObject *pyrf__parse_events(PyObject *self, PyObject *args) return NULL; } - threads = pthreads ? ((struct pyrf_thread_map *)pthreads)->threads : NULL; - cpus = pcpus ? ((struct pyrf_cpu_map *)pcpus)->cpus : NULL; + threads = (pthreads && pthreads != Py_None) ? + ((struct pyrf_thread_map *)pthreads)->threads : NULL; + cpus = (pcpus && pcpus != Py_None) ? + ((struct pyrf_cpu_map *)pcpus)->cpus : NULL; parse_events_error__init(&err); perf_evlist__set_maps(evlist__core(evlist), cpus, threads); @@ -3375,8 +3377,10 @@ static PyObject *pyrf__parse_metrics(PyObject *self, PyObject *args) return NULL; } - threads = pthreads ? ((struct pyrf_thread_map *)pthreads)->threads : NULL; - cpus = pcpus ? ((struct pyrf_cpu_map *)pcpus)->cpus : NULL; + threads = (pthreads && pthreads != Py_None) ? + ((struct pyrf_thread_map *)pthreads)->threads : NULL; + cpus = (pcpus && pcpus != Py_None) ? + ((struct pyrf_cpu_map *)pcpus)->cpus : NULL; perf_evlist__set_maps(evlist__core(evlist), cpus, threads); ret = metricgroup__parse_groups(evlist, pmu ?: "all", input, -- 2.54.0.1136.gdb2ca164c4-goog