Skip to content

Commit a668cc0

Browse files
ahunter6acmel
authored andcommitted
perf tools: Fix segfault accessing sample_id xyarray
perf_evsel::sample_id is an xyarray which can cause a segfault when accessed beyond its size. e.g. # perf record -e intel_pt// -C 1 sleep 1 Segmentation fault (core dumped) # That is happening because a dummy event is opened to capture text poke events accross all CPUs, however the mmap logic is allocating according to the number of user_requested_cpus. In general, perf sometimes uses the evsel cpus to open events, and sometimes the evlist user_requested_cpus. However, it is not necessary to determine which case is which because the opened event file descriptors are also in an xyarray, the size of whch can be used to correctly allocate the size of the sample_id xyarray, because there is one ID per file descriptor. Note, in the affected code path, perf_evsel fd array is subsequently used to get the file descriptor for the mmap, so it makes sense for the xyarrays to be the same size there. Fixes: d1a1775 ("libperf: Adopt perf_evlist__mmap()/munmap() from tools/perf") Fixes: 246eba8 ("perf tools: Add support for PERF_RECORD_TEXT_POKE") Signed-off-by: Adrian Hunter <adrian.hunter@intel.com> Acked-by: Ian Rogers <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: stable@vger.kernel.org # 5.5+ Link: https://lore.kernel.org/r/20220413114232.26914-1-adrian.hunter@intel.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent d73f5d1 commit a668cc0

1 file changed

Lines changed: 1 addition & 2 deletions

File tree

tools/lib/perf/evlist.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,6 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist,
577577
{
578578
struct perf_evsel *evsel;
579579
const struct perf_cpu_map *cpus = evlist->user_requested_cpus;
580-
const struct perf_thread_map *threads = evlist->threads;
581580

582581
if (!ops || !ops->get || !ops->mmap)
583582
return -EINVAL;
@@ -589,7 +588,7 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist,
589588
perf_evlist__for_each_entry(evlist, evsel) {
590589
if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
591590
evsel->sample_id == NULL &&
592-
perf_evsel__alloc_id(evsel, perf_cpu_map__nr(cpus), threads->nr) < 0)
591+
perf_evsel__alloc_id(evsel, evsel->fd->max_x, evsel->fd->max_y) < 0)
593592
return -ENOMEM;
594593
}
595594

0 commit comments

Comments
 (0)