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 56BF63D6CC2 for ; Sat, 13 Jun 2026 07:11:41 +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=1781334708; cv=none; b=RwTtUmKRuTdWGeZLcIMa+ulytctG+xotaSIoMRiGiDuZtHfmCt4YtLoeuxSTBBbAIDfmPB9EzJNbibIHhxS1uV5Zt8q54FCuFF+xx8ajPU2+mt/QU/CT7BKcbUciUKE5Hhydrf5W4xRb9OFWnDAL9agvm3UspYDrkudXnBAu2Gw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781334708; c=relaxed/simple; bh=PLF4WJXZBpORTZ2JlH+GCFFWzFf6msVG7RwS3W4YDmg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IKBtGLox0NzsPZ9YBaU6XeNaKVFXemT8M2bV5HbqYLsYoZJRW4K29JlLwxdbknYTKMruBjk18xr14pNB2zJgEUfAk5853b7n0x/8vI+Z+UDHmQCwbf1NHNFz95ramDZcDvL18TA+aGoH0DMZAv0G3+2I2x0S60V+BWmP14HJRkQ= 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=B9eSIFs7; 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="B9eSIFs7" Received: by mail-dy1-f202.google.com with SMTP id 5a478bee46e88-304e7fc90b1so1998326eec.1 for ; Sat, 13 Jun 2026 00:11:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781334700; x=1781939500; 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=5FmwGLPyViUdq322mofgfDRvlY2+s3k9Pg5JyAuFQcQ=; b=B9eSIFs7l0lFHRfif1ABlwRcJMDv7DdIs0lz/00xFabeEKua04ek7G+V6gW5HiP3IA S4GWC71/GomhnKzQFAaCx4Jf9pxOeBanR21tIsTShYYYUL//Nn/33m8CCmjZsuO/oISE WPYMMUdVe5kZmvxFOYko+UR3HpPYoKP7aBxPqeCO6zxnjxIvXZuIVP141om1ylfjzVIV HY4yPSX/pWfVts7cP0n3b0LtfcfkFqhGRpvzrxjnVG1MldIg+BdrgJ64s9tT7aXfmkit 8n51uc+xm3np2RxO4+Gkmjvz+e15RRApirE7brqXvAm+Hbu/7aOGbHWX+/RoHy6mjEwi JA7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781334700; x=1781939500; 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=5FmwGLPyViUdq322mofgfDRvlY2+s3k9Pg5JyAuFQcQ=; b=aVV9vF79Y8E6Fx4u7Yt/pq9n/2R75zDFIePuyp5MBjrcsf5YYdVaaccnosfSYis68j /g+EJSYhANcLC64HbCx6E0a+5b3ry7XfEr+Hs6BSAsf00Sx6PvU/zuhk074YT+hdFPio QEOQuvYLLBxvSueLNhw49tj7FKbGogqllUYEQC9MqiarKABzfrX52LA0T8CzJcNK7O8k 3XUkHgMbvZ9y6fio9zt6fUNHvS8U3II74Zhk5jDKTQno0Mq4qaNBF250j5DKOvQ1apdV 2ueuQKPIebyu6WTw60K6+QtGIFAmlme1RTjCooET/sj/QaqTBkIjPwfgVxlmr/PMzucB 4fDQ== X-Forwarded-Encrypted: i=1; AFNElJ+hX57p41DEZoGaeZ3g7rXCwiNEC5J6FDybP/BY42d9qO/vrh4x5gOtmSDxmVkNAdC1Bm39XMS5KQ72wnM=@vger.kernel.org X-Gm-Message-State: AOJu0YyC9+3paKq2Jcq9fqarFDJGe5gJhJ6Vnv9NuwxCOVeDFXX1k2Mh AdrIVEYu5PX7mq+AUL47yzAD/3udQXVPOVD4cJnRMrMrLh/349JPAh86L9WE+Ws0A6AZqg2Iwmt pWg3X1wmM9g== X-Received: from dybvc22.prod.google.com ([2002:a05:7301:5156:b0:2f4:b34a:2550]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:511:b0:304:de94:1c55 with SMTP id 5a478bee46e88-30940542b00mr1654679eec.35.1781334700147; Sat, 13 Jun 2026 00:11:40 -0700 (PDT) Date: Sat, 13 Jun 2026 00:10:58 -0700 In-Reply-To: <20260613071100.1508192-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: <20260613052722.1424093-1-irogers@google.com> <20260613071100.1508192-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.1136.gdb2ca164c4-goog Message-ID: <20260613071100.1508192-19-irogers@google.com> Subject: [PATCH v17 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 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index ee88b3af98e3..8d4877148aa6 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -2086,10 +2086,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; @@ -3335,8 +3335,8 @@ 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); @@ -3369,8 +3369,8 @@ 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