From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f201.google.com (mail-dy1-f201.google.com [74.125.82.201]) (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 35C933AFD02 for ; Mon, 15 Jun 2026 06:17:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781504233; cv=none; b=juoJvrdqLJwpZgF5N8jjgVFEXd/XsqzKQxwMIfMF/oQMmbsifUbXaMqqHs/rhGDVhpgf4RJS+rziiDIwqoakAr4l/tZJXL2IOnD8DE/QHSpII8xnAZq4FWuonwHlwQtta1DyiBzYi1Qk4Rq2E6K1lDexpx4ttrEh0MgTDqDGnNI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781504233; c=relaxed/simple; bh=7Ou4Ehb3EB5Nvzu1qULu8OlFOd9EWLp6NCml3x1Ym5s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HDEGFPeVZP/IaY2/KwtqYR6y4dOy38cpx478LFBx7Lf5Sm1RblOMSBbLr1/DmzYneeIZCP+ra29sQgNRE3T/JTwz8/LcTutaoA/j6ypFWbbUj3/kFlYIhHpIEofEBUVRD4sUCbUEag8LPjjWxKj4sjLUm5OS19ibI/ezL/hlN28= 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=Je2m1x/6; arc=none smtp.client-ip=74.125.82.201 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="Je2m1x/6" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-304ea42b025so1655681eec.0 for ; Sun, 14 Jun 2026 23:17:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781504231; x=1782109031; 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=XD26SctRAczF8gjk3e/dIIjJ4oWi9jYsbtNxYyZiuQs=; b=Je2m1x/6fxRHL+sVCabTuwB1ak76tAlHJiUXEG24m/YdxMqyFcqm3i3PgRO3kkq93p sKOhhjAptbWlZVdvfNMBWcAwpQ2nT7/LQavpJ5VMHyeO5sHXMviY23qUJkve7pq/VicH h76uYI82Bkz5HlRwPee4AdzWTPVE+4lrEzf9633LJ3doVOvlV7GJ15stVEaA0RqWVtTp JttrEXiHzHt8RmzfyyY4nO46fe7wIVoWLCu3Y7AUnmIqgOsSU9hQAgEDw8aZRFo0ZBXv BrVYrTthvmeWp/bdKQan59iMKoYoJEiTNYJUNnbg6Xd8fFACKOZhhFS/DrNN7sLucvSn b6IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781504231; x=1782109031; 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=XD26SctRAczF8gjk3e/dIIjJ4oWi9jYsbtNxYyZiuQs=; b=WXiAaqB8UowH41kbo6ZyIQE7yLHxPGz3PShl2FqT/y2Gu40HxBzhedDr1mFNIYdzia Dr3dA2h6p3951w0mmYlaGV0gG2Pv5aZpkXzhZlbmrDsXS4spzasn8n7FQMtlI8vGrqc2 e+k/O9POUow0/bZmCR/pmVteV1cyo9FTlRYb8mt3O857DEIck3y5yEeSqmwVAZBY5h7e wI8Wut0NSAaHSU3VuK5QI3oFuUqkz2xxLNZyNC+LAWE5IbkUjUWYcSDw/Cq7hLlqrTRM gM9beqDGqs1R6Pt4bEU3/Uih3FSS3blEIkdAjcfixoF6YBkTuE2wj95XSxIq49gsuG2Z b9tQ== X-Forwarded-Encrypted: i=1; AFNElJ8018PoxQ40lGlfQo53NRky7W+rRleQoOyfCymXKGDY3JhPrpX3zosIV4aOYMJIlJetnDTXYfSntTB6Bzs=@vger.kernel.org X-Gm-Message-State: AOJu0Yyf3SNX1y/UlxApHU4htvVpLGnieMntssnyAO7ZhPspw+4t/En3 v9ySIK/Cadn5Wfe66+IgSKU5sOndW0bhBW7vNSZ4VrO8saH+Qw9MmfK5Yis1sk/f0/eD17PQpR8 dJ1L+P/a9sw== X-Received: from dycsr10.prod.google.com ([2002:a05:693c:374a:b0:304:ec70:1018]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:4b18:b0:304:6822:2cb2 with SMTP id 5a478bee46e88-30820cc3819mr5668261eec.11.1781504231084; Sun, 14 Jun 2026 23:17:11 -0700 (PDT) Date: Sun, 14 Jun 2026 23:16:28 -0700 In-Reply-To: <20260615061631.2382079-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: <20260615002812.2234795-1-irogers@google.com> <20260615061631.2382079-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.1136.gdb2ca164c4-goog Message-ID: <20260615061631.2382079-19-irogers@google.com> Subject: [PATCH v19 18/20] 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 38a41bd6c9b5..a6593c388a48 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -2089,10 +2089,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; @@ -3338,8 +3338,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); @@ -3372,8 +3374,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