public inbox for linux-perf-users@vger.kernel.org
 help / color / mirror / Atom feed
From: Chun-Tse Shao <ctshao@google.com>
To: linux-kernel@vger.kernel.org
Cc: Chun-Tse Shao <ctshao@google.com>,
	peterz@infradead.org, mingo@redhat.com,  acme@kernel.org,
	namhyung@kernel.org, mark.rutland@arm.com,
	 alexander.shishkin@linux.intel.com, jolsa@kernel.org,
	irogers@google.com,  adrian.hunter@intel.com,
	kan.liang@linux.intel.com, terrelln@fb.com,  leo.yan@arm.com,
	james.clark@linaro.org, christophe.leroy@csgroup.eu,
	 ben.gainey@arm.com, linux-perf-users@vger.kernel.org
Subject: [PATCH v2 2/2] perf record: Fix a asan runtime error in util/maps.c
Date: Mon,  3 Mar 2025 10:32:41 -0800	[thread overview]
Message-ID: <20250303183646.327510-2-ctshao@google.com> (raw)
In-Reply-To: <20250303183646.327510-1-ctshao@google.com>

If I build perf with asan and run Zstd test:

  $ make -C tools/perf O=/tmp/perf DEBUG=1 EXTRA_CFLAGS="-O0 -g -fno-omit-frame-pointer -fsanitize=undefined"
  $ /tmp/perf/perf test "Zstd perf.data compression/decompression" -vv
   83: Zstd perf.data compression/decompression:
  ...
  util/maps.c:1046:5: runtime error: null pointer passed as argument 2, which is declared to never be null
  ...

The issue was caused by `bsearch`. The patch adds a check to ensure
argument 2 and 3 are not NULL and 0.

Testing with the commands above confirms that the runtime error is
resolved.

Signed-off-by: Chun-Tse Shao <ctshao@google.com>
---
 tools/perf/util/maps.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c
index 09c9cc326c08..41a99e1f4b50 100644
--- a/tools/perf/util/maps.c
+++ b/tools/perf/util/maps.c
@@ -1042,10 +1042,13 @@ struct map *maps__find(struct maps *maps, u64 ip)
 	while (!done) {
 		down_read(maps__lock(maps));
 		if (maps__maps_by_address_sorted(maps)) {
-			struct map **mapp =
-				bsearch(&ip, maps__maps_by_address(maps), maps__nr_maps(maps),
-					sizeof(*mapp), map__addr_cmp);
+			struct map **mapp = NULL;
+			struct map **maps_by_address = maps__maps_by_address(maps);
+			unsigned int nr_maps = maps__nr_maps(maps);
 
+			if (maps_by_address && nr_maps)
+				mapp = bsearch(&ip, maps_by_address, nr_maps, sizeof(*mapp),
+					       map__addr_cmp);
 			if (mapp)
 				result = map__get(*mapp);
 			done = true;
-- 
2.48.1.711.g2feabab25a-goog


  reply	other threads:[~2025-03-03 18:38 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-03 18:32 [PATCH v2 1/2] perf record: Add 8-byte aligned event type PERF_RECORD_COMPRESSED2 Chun-Tse Shao
2025-03-03 18:32 ` Chun-Tse Shao [this message]
2025-03-15  1:27 ` Namhyung Kim
2025-03-17 15:52   ` Arnaldo Carvalho de Melo
2025-03-17 16:17     ` Arnaldo Carvalho de Melo
2025-03-17 16:32       ` Ian Rogers
2025-03-17 19:36         ` Arnaldo Carvalho de Melo
2025-03-17 20:24           ` Arnaldo Carvalho de Melo
2025-03-17 16:46       ` Namhyung Kim
2025-03-17 20:35         ` Arnaldo Carvalho de Melo
2025-03-17 21:45           ` Chun-Tse Shao
2025-03-17 21:49             ` Arnaldo Carvalho de Melo
2025-03-18  5:13               ` Namhyung Kim
2025-05-16 17:10                 ` Chun-Tse Shao
2025-05-16 19:16                   ` Arnaldo Carvalho de Melo
2025-05-16 21:44                     ` Chun-Tse Shao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250303183646.327510-2-ctshao@google.com \
    --to=ctshao@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=ben.gainey@arm.com \
    --cc=christophe.leroy@csgroup.eu \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=leo.yan@arm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=terrelln@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox