Skip to content

Commit 5b10c18

Browse files
captain5050acmel
authored andcommitted
perf parse-events: Avoid SEGV if PMU lookup fails for legacy cache terms
libfuzzer found the following command could SEGV: $ perf stat -e cpu/L2,L2/ true This is because the L2 term rewrites the perf_event_attr type to PERF_TYPE_HW_CACHE which then fails the PMU lookup for the second legacy cache term. The new failure is consistent with repeated hardware terms: $ perf stat -e cpu/L2,L2/ true event syntax error: 'cpu/L2,L2/' \___ Failed to find PMU for type 3 Initial error: event syntax error: 'cpu/L2,L2/' \___ Failed to find PMU for type 3 Run 'perf list' for a list of valid events Usage: perf stat [<options>] [<command>] -e, --event <event> event selector. use 'perf list' to list available events $ perf stat -e cpu/cycles,cycles/ true event syntax error: 'cpu/cycles,cycles/' \___ Failed to find PMU for type 0 Initial error: event syntax error: 'cpu/cycles,cycles/' \___ Failed to find PMU for type 0 Run 'perf list' for a list of valid events Usage: perf stat [<options>] [<command>] -e, --event <event> event selector. use 'perf list' to list available events Committer testing: Before: $ perf stat -e cpu/L2,L2/ true Segmentation fault (core dumped) $ After: $ perf stat -e cpu/L2,L2/ true event syntax error: 'cpu/L2,L2/' \___ Failed to find PMU for type 3 Initial error: event syntax error: 'cpu/L2,L2/' \___ Failed to find PMU for type 3 Run 'perf list' for a list of valid events Usage: perf stat [<options>] [<command>] -e, --event <event> event selector. use 'perf list' to list available events $ Fixes: 6fd1e51 ("perf parse-events: Support PMUs for legacy cache events") Signed-off-by: Ian Rogers <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Xing Zhengjun <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 4b96679 commit 5b10c18

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

tools/perf/util/parse-events.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,14 @@ static int config_term_pmu(struct perf_event_attr *attr,
12161216
if (term->type_term == PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE) {
12171217
const struct perf_pmu *pmu = perf_pmus__find_by_type(attr->type);
12181218

1219+
if (!pmu) {
1220+
char *err_str;
1221+
1222+
if (asprintf(&err_str, "Failed to find PMU for type %d", attr->type) >= 0)
1223+
parse_events_error__handle(err, term->err_term,
1224+
err_str, /*help=*/NULL);
1225+
return -EINVAL;
1226+
}
12191227
if (perf_pmu__supports_legacy_cache(pmu)) {
12201228
attr->type = PERF_TYPE_HW_CACHE;
12211229
return parse_events__decode_legacy_cache(term->config, pmu->type,

0 commit comments

Comments
 (0)