[PATCH 28/28] perf test: Add truncated perf.data robustness test

From: Arnaldo Carvalho de Melo

Date: Sat May 09 2026 - 23:39:03 EST


From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>

Add a shell test that verifies perf report handles truncated
perf.data files gracefully — exiting with an error code rather
than crashing with SIGSEGV or SIGABRT.

The test records a simple workload, then truncates the resulting
perf.data at four offsets that exercise different parsing stages:

8 bytes — file header magic only
64 bytes — partial file header (attr section incomplete)
256 bytes — into the first events (partial event headers)
75% size — mid-stream truncation (partial event data)

For each truncation, perf report is run and the exit code is
checked. Signal-based exits (128+signal) in the range 134-159
indicate a crash and fail the test. Non-zero exits from normal
error handling are expected and acceptable.

This exercises the bounds checking, minimum-size validation,
and error propagation added by the preceding patches in this
series.

Cc: Ian Rogers <irogers@xxxxxxxxxx>
Cc: Adrian Hunter <adrian.hunter@xxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Assisted-by: Claude Opus 4.6 (1M context) <noreply@xxxxxxxxxxxxx>
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
---
tools/perf/tests/shell/data_validation.sh | 59 +++++++++++++++++++++++
1 file changed, 59 insertions(+)
create mode 100755 tools/perf/tests/shell/data_validation.sh

diff --git a/tools/perf/tests/shell/data_validation.sh b/tools/perf/tests/shell/data_validation.sh
new file mode 100755
index 0000000000000000..649f71b6cdb93202
--- /dev/null
+++ b/tools/perf/tests/shell/data_validation.sh
@@ -0,0 +1,59 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Test that perf report handles truncated perf.data gracefully
+# (no crash, no segfault — clean error exit).
+#
+# Exercises the bounds checking and minimum-size validation added
+# by the perf-data-validation hardening series.
+
+err=0
+
+cleanup() {
+ rm -f "${perfdata}" "${truncated}"
+ trap - EXIT TERM INT
+}
+trap cleanup EXIT TERM INT
+
+perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+truncated=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+
+# Record a simple workload
+if ! perf record -o "${perfdata}" -- perf test -w noploop 2>/dev/null; then
+ echo "Skip: perf record failed"
+ cleanup
+ exit 2
+fi
+
+file_size=$(stat -c %s "${perfdata}")
+if [ "${file_size}" -lt 512 ]; then
+ echo "Skip: perf.data too small (${file_size} bytes)"
+ cleanup
+ exit 2
+fi
+
+# Test truncation at various offsets that exercise different
+# parsing stages:
+# 8 — file header magic only, no attrs or data
+# 64 — partial file header (attr section incomplete)
+# 256 — into the first events (partial event headers)
+# 75% — mid-stream truncation (partial event data)
+for cut_at in 8 64 256 $((file_size * 3 / 4)); do
+ if [ "${cut_at}" -ge "${file_size}" ]; then
+ continue
+ fi
+ head -c "${cut_at}" "${perfdata}" > "${truncated}"
+
+ # perf report should exit with an error, not crash.
+ # Suppress stdout/stderr — we only care about the exit code.
+ perf report -i "${truncated}" --stdio > /dev/null 2>&1
+ exit_code=$?
+
+ # 139 = SIGSEGV, 134 = SIGABRT, 136 = SIGFPE
+ if [ ${exit_code} -ge 134 ] && [ ${exit_code} -le 159 ]; then
+ echo "FAIL: perf report crashed (signal $((exit_code - 128))) on ${cut_at}-byte truncated file"
+ err=1
+ fi
+done
+
+cleanup
+exit ${err}
--
2.54.0