public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Jan Vrany <jan.vrany@labware.com>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan.vrany@labware.com>
Subject: [PATCH] gdb: care for dynamic objfiles in build_id_bfd_get ()
Date: Mon,  9 Jan 2023 21:19:48 +0000	[thread overview]
Message-ID: <20230109211948.604669-1-jan.vrany@labware.com> (raw)

Accessing gdb.Objfile.build_id caused GDB to crash when objfile is
dynamic, that is created by JIT reader API. Similarly, invoking
gdb.Symtab.fullname() caused a crash. Possibly there are other paths
that may trigger the crash.

The issue was NULL-pointer dereferencing in build_id_bfd_get () because
dynamic objfiles have no underlaying BFD structure. This commit fixes
the problem by a NULL-check in build_id_bfd_get ().
---
 gdb/build-id.c                        | 6 ++++++
 gdb/testsuite/gdb.base/jit-reader.exp | 4 ++++
 2 files changed, 10 insertions(+)

diff --git a/gdb/build-id.c b/gdb/build-id.c
index c82f96402c8..671cbc1b545 100644
--- a/gdb/build-id.c
+++ b/gdb/build-id.c
@@ -32,6 +32,12 @@
 const struct bfd_build_id *
 build_id_bfd_get (bfd *abfd)
 {
+  /* Dynamic objfiles such as ones created by JIT reader API
+     have no underlaying bfd structure (that is, objfile->obfd
+     is NULL).  */
+  if (abfd == nullptr)
+    return NULL;
+
   if (!bfd_check_format (abfd, bfd_object)
       && !bfd_check_format (abfd, bfd_core))
     return NULL;
diff --git a/gdb/testsuite/gdb.base/jit-reader.exp b/gdb/testsuite/gdb.base/jit-reader.exp
index 5f8b6b0343c..d8d561cd4df 100644
--- a/gdb/testsuite/gdb.base/jit-reader.exp
+++ b/gdb/testsuite/gdb.base/jit-reader.exp
@@ -233,6 +233,10 @@ proc jit_reader_test {} {
 		gdb_test "python print(list(map(lambda objf : objf.filename, gdb.objfiles())))" \
 		    "$any'<< JIT compiled code at $hex >>'$any" \
 		    "python gdb.Objfile.filename"
+
+		gdb_test "python print( \[o for o in gdb.objfiles() if o.filename.startswith('<< JIT compiled code')\]\[0\].build_id )" \
+		    "None" \
+		    "python gdb.Objfile.build_id"
 	    }
 	}
     }
-- 
2.39.0


             reply	other threads:[~2023-01-09 21:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-09 21:19 Jan Vrany [this message]
2023-01-10 14:14 ` Tom Tromey
2023-01-11 12:50   ` Jan Vraný

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230109211948.604669-1-jan.vrany@labware.com \
    --to=jan.vrany@labware.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).