Skip to content

Commit

Permalink
perf script: Fix allocation of evsel->priv related to per-event dump …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
Tested-by: Ian Rogers <[email protected]>
Cc: Adrian Hunter <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Ravi Bangoria <[email protected]>
Link: https://lore.kernel.org/lkml/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
acmel authored and gregkh committed Jul 23, 2023
1 parent 939bf46 commit ac6c849
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions tools/perf/builtin-script.c
Original file line number Diff line number Diff line change
Expand Up @@ -2260,6 +2260,9 @@ static int process_sample_event(struct perf_tool *tool,
return ret;
}

// Used when scr->per_event_dump is not set
static struct evsel_script es_stdout;

static int process_attr(struct perf_tool *tool, union perf_event *event,
struct evlist **pevlist)
{
Expand All @@ -2268,7 +2271,6 @@ static int process_attr(struct perf_tool *tool, union perf_event *event,
struct evsel *evsel, *pos;
u64 sample_type;
int err;
static struct evsel_script *es;

err = perf_event__process_attr(tool, event, pevlist);
if (err)
Expand All @@ -2278,14 +2280,13 @@ static int process_attr(struct perf_tool *tool, union perf_event *event,
evsel = evlist__last(*pevlist);

if (!evsel->priv) {
if (scr->per_event_dump) {
if (scr->per_event_dump) {
evsel->priv = evsel_script__new(evsel, scr->session->data);
} else {
es = zalloc(sizeof(*es));
if (!es)
if (!evsel->priv)
return -ENOMEM;
es->fp = stdout;
evsel->priv = es;
} else { // Replicate what is done in perf_script__setup_per_event_dump()
es_stdout.fp = stdout;
evsel->priv = &es_stdout;
}
}

Expand Down Expand Up @@ -2591,7 +2592,6 @@ static int perf_script__fopen_per_event_dump(struct perf_script *script)
static int perf_script__setup_per_event_dump(struct perf_script *script)
{
struct evsel *evsel;
static struct evsel_script es_stdout;

if (script->per_event_dump)
return perf_script__fopen_per_event_dump(script);
Expand Down

0 comments on commit ac6c849

Please sign in to comment.