Skip to contentSkip to conversation

perf tools: Fix use before NULL check introduced by map__dso() introduction

PATCH In review Arnaldo Carvalho de Melo 2 participants 2 messages Apr 18, 2023 perf events

lore
Share & tools
lore

Arnaldo Carvalho de Melo

author maintainer
#0 lore raw

PATCH 1/1 perf tools: Fix use before NULL check introduced by map__dso() introduction

James Clark noticed that the recent 63df0e4bc368adbd ("perf map: Add accessor for dso") patch accessed map->dso before the 'map' variable was NULL checked, which is a change in logic that leads to segmentation faults, so comb thru that patch to fix similar cases.

Fixes 63df0e4bc368adbd ("perf map: Add accessor for dso")
Cc Adrian Hunter <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc John Garry <[email protected]>
Cc Leo Yan <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Mathieu Poirier <[email protected]>
Cc Mike Leach <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]
Cc Suzuki K Poulose <[email protected]>
Cc Will Deacon <[email protected]>

To: James Clark <[email protected]>

---
 tools/perf/builtin-script.c    | 7 +++----
 tools/perf/ui/browsers/hists.c | 4 ++--
 tools/perf/util/sort.c         | 2 +-
 3 files changed, 6 insertions(+), 7 deletions(-)
tools/perf/builtin-script.c +3 -4
index 8fba247b798ca307..006f522d0e7f6a18 100644
@@ -1075,8 +1075,7 @@ static int grab_bb(u8 *buffer, u64 start, u64 end,
 		return 0;
 	}
 
-	dso = map__dso(al.map);
-	if (!thread__find_map(thread, *cpumode, start, &al) || !dso) {
+	if (!thread__find_map(thread, *cpumode, start, &al) || (dso = map__dso(al.map)) == NULL) {
 		pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
 		return 0;
 	}
@@ -1106,9 +1105,9 @@ static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struct srcc
 	unsigned line;
 	int len;
 	char *srccode;
-	struct dso *dso = map__dso(map);
+	struct dso *dso;
 
-	if (!map || !dso)
+	if (!map || (dso = map__dso(map)) == NULL)
 		return 0;
 	srcfile = get_srcline_split(dso,
 				    map__rip_2objdump(map, addr),
tools/perf/ui/browsers/hists.c +2 -2
index ab70e5f5fad236e5..69c81759a64f938f 100644
@@ -2499,9 +2499,9 @@ add_annotate_opt(struct hist_browser *browser __maybe_unused,
 		 struct map_symbol *ms,
 		 u64 addr)
 {
-	struct dso *dso = map__dso(ms->map);
+	struct dso *dso;
 
-	if (!ms->map || !dso || dso->annotate_warned)
+	if (!ms->map || (dso = map__dso(ms->map)) == NULL || dso->annotate_warned)
 		return 0;
 
 	if (!ms->sym)
tools/perf/util/sort.c +1 -1
index f2ffaf90648e469e..31b1cd0935e277ba 100644
@@ -1568,7 +1568,7 @@ static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
 
 	if (he->mem_info) {
 		struct map *map = he->mem_info->daddr.ms.map;
-		struct dso *dso = map__dso(map);
+		struct dso *dso = map ? map__dso(map) : NULL;
 
 		addr = cl_address(he->mem_info->daddr.al_addr, chk_double_cl);
 		ms = &he->mem_info->daddr.ms;
Signature · 1 line
2.39.2
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 21 lines · Arnaldo Carvalho de Melo

James Clark noticed that the recent 63df0e4bc368adbd ("perf map: Add accessor for dso") patch accessed map->dso before the 'map' variable was NULL checked, which is a change in logic that leads to segmentation faults, so comb thru that patch to fix similar cases.

Fixes 63df0e4bc368adbd ("perf map: Add accessor for dso")
Cc Adrian Hunter <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc John Garry <[email protected]>
Cc Leo Yan <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Mathieu Poirier <[email protected]>
Cc Mike Leach <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]
Cc Suzuki K Poulose <[email protected]>
Cc Will Deacon <[email protected]>
Quoted 3 lines

To: James Clark <[email protected]>

Sorry for the breakage!

Thanks,
Ian

Quoted 64 lines
---
 tools/perf/builtin-script.c    | 7 +++----
 tools/perf/ui/browsers/hists.c | 4 ++--
 tools/perf/util/sort.c         | 2 +-
 3 files changed, 6 insertions(+), 7 deletions(-)
tools/perf/builtin-script.c +3 -4
index 8fba247b798ca307..006f522d0e7f6a18 100644
@@ -1075,8 +1075,7 @@ static int grab_bb(u8 *buffer, u64 start, u64 end,
                return 0;
        }
 
-       dso = map__dso(al.map);
-       if (!thread__find_map(thread, *cpumode, start, &al) || !dso) {
+       if (!thread__find_map(thread, *cpumode, start, &al) || (dso = map__dso(al.map)) == NULL) {
                pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
                return 0;
        }
@@ -1106,9 +1105,9 @@ static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struct srcc
        unsigned line;
        int len;
        char *srccode;
-       struct dso *dso = map__dso(map);
+       struct dso *dso;
 
-       if (!map || !dso)
+       if (!map || (dso = map__dso(map)) == NULL)
                return 0;
        srcfile = get_srcline_split(dso,
                                    map__rip_2objdump(map, addr),
tools/perf/ui/browsers/hists.c +2 -2
index ab70e5f5fad236e5..69c81759a64f938f 100644
@@ -2499,9 +2499,9 @@ add_annotate_opt(struct hist_browser *browser __maybe_unused,
                 struct map_symbol *ms,
                 u64 addr)
 {
-       struct dso *dso = map__dso(ms->map);
+       struct dso *dso;
 
-       if (!ms->map || !dso || dso->annotate_warned)
+       if (!ms->map || (dso = map__dso(ms->map)) == NULL || dso->annotate_warned)
                return 0;
 
        if (!ms->sym)
tools/perf/util/sort.c +1 -2
index f2ffaf90648e469e..31b1cd0935e277ba 100644
@@ -1568,7 +1568,7 @@ static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
 
        if (he->mem_info) {
                struct map *map = he->mem_info->daddr.ms.map;
-               struct dso *dso = map__dso(map);
+               struct dso *dso = map ? map__dso(map) : NULL;
 
                addr = cl_address(he->mem_info->daddr.al_addr, chk_double_cl);
                ms = &he->mem_info->daddr.ms;
--

2.39.2