[PATCH 17/28] perf header: Propagate feature section processing errors
From: Arnaldo Carvalho de Melo
Date: Sat May 09 2026 - 23:40:45 EST
From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
perf_session__read_header() discards the return value from
perf_header__process_sections(), so any error from a feature
section processor (process_nrcpus, process_compressed, etc.)
is silently ignored and the session opens as if nothing went
wrong.
This defeats the validation added by subsequent commits in this
series: a crafted perf.data that fails a feature section check
would still be processed with partially-initialized state.
Check the return value and fail the session if any feature
section processor returns an error.
For truncated files (data.size == 0, i.e. recording was
interrupted before the header was finalized), skip feature
section processing entirely and clear the feature bitmap so
tools use their "feature not present" fallbacks instead of
accessing uninitialized env fields.
Change the feature processor stubs for optional libraries
(libtraceevent, libbpf) from returning -1 to returning 0,
so that perf.data files containing these features can still be
opened on builds without the optional library — the feature is
simply skipped rather than causing a fatal error.
Also fix evlist__prepare_tracepoint_events() failure to return
-EINVAL instead of -ENOMEM, since the failure is a data
validation issue, not an allocation failure.
Fixes: 1c0b04d12ae9 ("perf tools: Add perf_session__read_header function")
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Ian Rogers <irogers@xxxxxxxxxx>
Assisted-by: Claude Opus 4.6 (1M context) <noreply@xxxxxxxxxxxxx>
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
---
tools/perf/util/header.c | 52 ++++++++++++++++++++++++++++++----------
1 file changed, 39 insertions(+), 13 deletions(-)
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index d253063b581f21e9..5cbeda0335f1140c 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -2748,8 +2748,9 @@ static int process_tracing_data(struct feat_fd *ff __maybe_unused, void *data __
return ret < 0 ? -1 : 0;
#else
- pr_err("ERROR: Trying to read tracing data without libtraceevent support.\n");
- return -1;
+ /* Not an error — the feature is simply unsupported in this build */
+ pr_debug("Tracing data present but libtraceevent not available, skipping.\n");
+ return 0;
#endif
}
@@ -3643,8 +3644,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _
up_write(&env->bpf_progs.lock);
return err;
#else
- pr_err("ERROR: Trying to read bpf_prog_info without libbpf support.\n");
- return -1;
+ /* Not an error — the feature is simply unsupported in this build */
+ pr_debug("BPF prog info present but libbpf not available, skipping.\n");
+ return 0;
#endif // HAVE_LIBBPF_SUPPORT
}
@@ -3712,8 +3714,9 @@ static int process_bpf_btf(struct feat_fd *ff __maybe_unused, void *data __mayb
free(node);
return err;
#else
- pr_err("ERROR: Trying to read btf data without libbpf support.\n");
- return -1;
+ /* Not an error — the feature is simply unsupported in this build */
+ pr_debug("BTF data present but libbpf not available, skipping.\n");
+ return 0;
#endif // HAVE_LIBBPF_SUPPORT
}
@@ -4900,7 +4903,7 @@ int perf_session__read_header(struct perf_session *session)
struct perf_file_header f_header;
struct perf_file_attr f_attr;
u64 f_id;
- int nr_attrs, nr_ids, i, j, err;
+ int nr_attrs, nr_ids, i, j, err = -ENOMEM;
int fd = perf_data__fd(data);
session->evlist = evlist__new();
@@ -4920,6 +4923,8 @@ int perf_session__read_header(struct perf_session *session)
return err;
}
+ err = -ENOMEM;
+
if (perf_file_header__read(&f_header, header, fd) < 0)
return -EINVAL;
@@ -4997,15 +5002,36 @@ int perf_session__read_header(struct perf_session *session)
lseek(fd, tmp, SEEK_SET);
}
+ /*
+ * Skip feature section processing for truncated files
+ * (data.size == 0 means recording was interrupted). The
+ * section table is unreliable in that case, and the event
+ * data can still be processed without the feature headers.
+ * Clear the bitmap so has_feat() returns false and tools
+ * use their "feature not present" fallbacks instead of
+ * accessing uninitialized env fields.
+ */
+ if (f_header.data.size == 0) {
+ bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
+ } else {
#ifdef HAVE_LIBTRACEEVENT
- perf_header__process_sections(header, fd, &session->tevent,
- perf_file_section__process);
+ err = perf_header__process_sections(header, fd, &session->tevent,
+ perf_file_section__process);
+ if (err < 0)
+ goto out_delete_evlist;
- if (evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent))
- goto out_delete_evlist;
+ if (evlist__prepare_tracepoint_events(session->evlist,
+ session->tevent.pevent)) {
+ err = -EINVAL;
+ goto out_delete_evlist;
+ }
#else
- perf_header__process_sections(header, fd, NULL, perf_file_section__process);
+ err = perf_header__process_sections(header, fd, NULL,
+ perf_file_section__process);
+ if (err < 0)
+ goto out_delete_evlist;
#endif
+ }
return 0;
out_errno:
@@ -5014,7 +5040,7 @@ int perf_session__read_header(struct perf_session *session)
out_delete_evlist:
evlist__delete(session->evlist);
session->evlist = NULL;
- return -ENOMEM;
+ return err;
}
int perf_event__process_feature(const struct perf_tool *tool __maybe_unused,
--
2.54.0