Skip to content

Commit 4309bd9

Browse files
acmelgregkh
authored andcommitted
perf script: Fix allocation of evsel->priv related to per-event dump files
[ Upstream commit 36d3e41 ] When printing output we may want to generate per event files, where the --per-event-dump option should be used, creating perf.data.EVENT.dump files instead of printing to stdout. The callback thar processes event thus expects that evsel->priv->fp should point to either the per-event FILE descriptor or to stdout. The a3af66f ("perf script: Fix crash because of missing evsel->priv") changeset fixed a case where evsel->priv wasn't setup, thus set to NULL, causing a segfault when trying to access evsel->priv->fp. But it did it for the non --per-event-dump case by allocating a 'struct perf_evsel_script' just to set its ->fp to stdout. Since evsel->priv is only freed when --per-event-dump is used, we ended up with a memory leak, detected using ASAN. Fix it by using the same method as perf_script__setup_per_event_dump(), and reuse that static 'struct perf_evsel_script'. Also check if evsel_script__new() failed. Fixes: a3af66f ("perf script: Fix crash because of missing evsel->priv") Reported-by: Ian Rogers <irogers@google.com> Tested-by: Ian Rogers <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Ravi Bangoria <ravi.bangoria@linux.ibm.com> Link: https://lore.kernel.org/lkml/ZH+F0wGAWV14zvMP@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 7cfd310 commit 4309bd9

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

tools/perf/builtin-script.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,6 +2390,9 @@ static int process_sample_event(struct perf_tool *tool,
23902390
return ret;
23912391
}
23922392

2393+
// Used when scr->per_event_dump is not set
2394+
static struct evsel_script es_stdout;
2395+
23932396
static int process_attr(struct perf_tool *tool, union perf_event *event,
23942397
struct evlist **pevlist)
23952398
{
@@ -2398,7 +2401,6 @@ static int process_attr(struct perf_tool *tool, union perf_event *event,
23982401
struct evsel *evsel, *pos;
23992402
u64 sample_type;
24002403
int err;
2401-
static struct evsel_script *es;
24022404

24032405
err = perf_event__process_attr(tool, event, pevlist);
24042406
if (err)
@@ -2408,14 +2410,13 @@ static int process_attr(struct perf_tool *tool, union perf_event *event,
24082410
evsel = evlist__last(*pevlist);
24092411

24102412
if (!evsel->priv) {
2411-
if (scr->per_event_dump) {
2413+
if (scr->per_event_dump) {
24122414
evsel->priv = evsel_script__new(evsel, scr->session->data);
2413-
} else {
2414-
es = zalloc(sizeof(*es));
2415-
if (!es)
2415+
if (!evsel->priv)
24162416
return -ENOMEM;
2417-
es->fp = stdout;
2418-
evsel->priv = es;
2417+
} else { // Replicate what is done in perf_script__setup_per_event_dump()
2418+
es_stdout.fp = stdout;
2419+
evsel->priv = &es_stdout;
24192420
}
24202421
}
24212422

@@ -2721,7 +2722,6 @@ static int perf_script__fopen_per_event_dump(struct perf_script *script)
27212722
static int perf_script__setup_per_event_dump(struct perf_script *script)
27222723
{
27232724
struct evsel *evsel;
2724-
static struct evsel_script es_stdout;
27252725

27262726
if (script->per_event_dump)
27272727
return perf_script__fopen_per_event_dump(script);

0 commit comments

Comments
 (0)