When perf annotate looks up the build-id cache to find the binary to
pass to objdump, it currently only checks that the cache file exists
and is readable (access(filename, R_OK)).  It does not verify that the
file is actually an ELF.

If the cache entry was populated with a non-ELF file (e.g. a shell
script placeholder left by a test harness such as SPEC CPU), objdump
receives a non-ELF file, produces no output, and perf reports an
error:

  Couldn't annotate <symbol>: Internal error: Invalid -1 error code

Fix this by calling the shared is_valid_elf() helper (introduced in
the previous patch) before accepting the cache entry.  If the check
fails, emit a warning and fall back to the original binary path, which
is the same behaviour as when the cache entry is missing or unreadable.

Reported-by: Narendra Nalli <[email protected]>
Reported-by: Vijay Puliyala <[email protected]>
Signed-off-by: Athira Rajeev <[email protected]>
---
 tools/perf/util/disasm.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index 6cfdbabbb8c7..e263a2a8715d 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -1201,6 +1201,12 @@ static int dso__disassemble_filename(struct dso *dso, 
char *filename, size_t fil
        if (len < 0)
                goto fallback;
 
+       if (!is_valid_elf(filename)) {
+               pr_warning("build-id cache file is not a valid ELF, falling 
back to original binary: %s\n",
+                       filename);
+               goto fallback;
+       }
+
        linkname[len] = '\0';
        if (strstr(linkname, DSO__NAME_KALLSYMS) ||
                access(filename, R_OK)) {
-- 
2.43.0


Reply via email to