OSDN Git Service

perf record: Fix a segfault in record__read_lost_samples()
authorNamhyung Kim <namhyung@kernel.org>
Fri, 9 Sep 2022 23:50:24 +0000 (16:50 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 4 Oct 2022 11:55:22 +0000 (08:55 -0300)
When it fails to open events record__open() returns without setting the
session->evlist.  Then it gets a segfault in the function trying to read
lost sample counts.  You can easily reproduce it as a normal user like:

  $ perf record -p 1 true
  ...
  perf: Segmentation fault
  ...

Skip the function if it has no evlist.  And add more protection for evsels
which are not properly initialized.

Fixes: a49aa8a54e861af1 ("perf record: Read and inject LOST_SAMPLES events")
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Leo Yan <leo.yan@linaro.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20220909235024.278281-1-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-record.c

index 741e763..f4f1619 100644 (file)
@@ -1888,6 +1888,10 @@ static void record__read_lost_samples(struct record *rec)
        struct perf_record_lost_samples *lost;
        struct evsel *evsel;
 
+       /* there was an error during record__open */
+       if (session->evlist == NULL)
+               return;
+
        lost = zalloc(PERF_SAMPLE_MAX_SIZE);
        if (lost == NULL) {
                pr_debug("Memory allocation failed\n");
@@ -1899,6 +1903,8 @@ static void record__read_lost_samples(struct record *rec)
        evlist__for_each_entry(session->evlist, evsel) {
                struct xyarray *xy = evsel->core.sample_id;
 
+               if (xy == NULL || evsel->core.fd == NULL)
+                       continue;
                if (xyarray__max_x(evsel->core.fd) != xyarray__max_x(xy) ||
                    xyarray__max_y(evsel->core.fd) != xyarray__max_y(xy)) {
                        pr_debug("Unmatched FD vs. sample ID: skip reading LOST count\n");