gdb: care for dynamic objfiles in build_id_bfd_get ()

Message ID 20230109211948.604669-1-jan.vrany@labware.com
State New
Headers
Series gdb: care for dynamic objfiles in build_id_bfd_get () |

Commit Message

Jan Vrany Jan. 9, 2023, 9:19 p.m. UTC
  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(+)
  

Comments

Tom Tromey Jan. 10, 2023, 2:14 p.m. UTC | #1
>>>>> "Jan" == Jan Vrany via Gdb-patches <gdb-patches@sourceware.org> writes:

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

Is there a patch already for the fullname bug?

Jan> +  /* Dynamic objfiles such as ones created by JIT reader API
Jan> +     have no underlaying bfd structure (that is, objfile->obfd
Jan> +     is NULL).  */
Jan> +  if (abfd == nullptr)
Jan> +    return NULL;

Use 'nullptr' in new code.  This is ok with this change.

thank you,
Tom
  
Jan Vrany Jan. 11, 2023, 12:50 p.m. UTC | #2
On Tue, 2023-01-10 at 07:14 -0700, Tom Tromey wrote:
> > > > > > "Jan" == Jan Vrany via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Jan> Accessing gdb.Objfile.build_id caused GDB to crash when objfile is
> Jan> dynamic, that is created by JIT reader API. Similarly, invoking
> Jan> gdb.Symtab.fullname() caused a crash. Possibly there are other paths
> Jan> that may trigger the crash.
> 
> Is there a patch already for the fullname bug?

Hmm, it's been a while I wrote that patch (when working on Python
alternative to JIT-reader API) and I believe I encountered a case
case where fullname() crashed since it (indirectly) called 
build_id_bfd_get(). 

I tried to reproduce it now and could not, perhaps I can be reproduced
once other patches are in. 

I'll remove that sentence from commit message to avoid confusion:


    gdb: care for dynamic objfiles in build_id_bfd_get ()
    
    Accessing gdb.Objfile.build_id caused GDB to crash when objfile is
    dynamic, that is created by JIT reader API.
    
    The issue was NULL-pointer dereferencing in build_id_bfd_get () because
    dynamic objfiles have no underlaying BFD structure. This commit fixes

Jan

> 
> Jan> +  /* Dynamic objfiles such as ones created by JIT reader API
> Jan> +     have no underlaying bfd structure (that is, objfile->obfd
> Jan> +     is NULL).  */
> Jan> +  if (abfd == nullptr)
> Jan> +    return NULL;
> 
> Use 'nullptr' in new code.  This is ok with this change.
> 
> thank you,
> Tom
>
  

Patch

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"
 	    }
 	}
     }