[PATCH] gdb/riscv: delete target descriptions when gdb exits

Andrew Burgess andrew.burgess@embecosm.com
Fri Jul 17 09:32:12 GMT 2020


* Simon Marchi <simark@simark.ca> [2020-07-16 15:58:25 -0400]:

> On 2020-07-16 1:29 p.m., Andrew Burgess wrote:
> > It was pointed out on IRC that the RISC-V target allocates target
> > descriptions and stores them in a global map, and doesn't delete these
> > target descriptions when GDB shuts down.
> > 
> > This isn't a particular problem, the total number of target
> > descriptions we can create is very limited so creating these on demand
> > and holding them for the entire run on GDB seems reasonable.
> > 
> > However, not deleting these objects on GDB exit means extra warnings
> > are printed from tools like valgrind, and the address sanitiser,
> > making it harder to spot real issues.  As it's reasonably easy to have
> > GDB correctly delete these objects on exit, lets just do that.
> > 
> > I started by noticing that we already have a target_desc_up type, a
> > wrapper around unique_ptr that calls a function that will correctly
> > delete target descriptions, so I want to use that, but....
> > 
> > ...that type is declared in gdb/target-descriptions.h.  If I try to
> > include that file in gdb/arch/riscv.c I run into a problem, that file
> > is compiled into both GDB and GDBServer.
> > 
> > OK, I could guard the include with #ifdef, but surely we can do
> > better.
> > 
> > So then I decided to move the target_desc_up type into
> > gdbsupport/tdesc.h, this is the interface file for generic code shared
> > between GDB and GDBserver (relating to target descriptions).  The
> > actual implementation for the delete function still lives in
> > gdb/target-description.c, but now gdb/arch/riscv.c can see the
> > declaration.  Problem solved....
> > 
> > ... but, though RISC-V doesn't use it I've now exposed the
> > target_desc_up type to gdbserver, so in future someone _might_ start
> > using it, which is fine, except right now there's no definition of the
> > delete function - remember the delete I used is only defined in GDB
> > code.
> > 
> > No problem, I add an implementation of the delete operator into
> > gdbserver/tdesc.cc, and all is good..... except....
> > 
> > I start getting this error from GCC:
> > 
> >   tdesc.cc:109:10: error: deleting object of polymorphic class type ‘target_desc’ which has non-virtual destructor might cause undefined behavior [-Werror=delete-non-virtual-dtor]
> > 
> > This seems a little strange to me as target_desc is not a polymorphic
> > class as far as I can tell, so I don't quite understand why GCC is
> > complaining about this.
> > 
> > Still by making gdbserver's target_desc class final this seems to
> > resolve this issue.  This should be fine so long as we never try to
> > inherit from target_desc, but if we do we probably should make the
> > destructor virtual, so that wouldn't be an issue.
> 
> I think this is all reasonable.  The unique_ptr has to be a bit awkward
> (have a custom deleter, and distinct implementations in gdb and gdbserver)
> because gdb and gdbserver don't share a target_desc type.  They have the
> same name, but that's about it.
> 
> I guess an alternative would be to templatize everything.  Both users
> (gdb and gdbserver) would pass in their own target_desc type as a template
> parameter.
> 
> Could you also make allocate_target_description return a target_desc_up?
> It could be a separate patch on top.
> 
> > diff --git a/gdbsupport/tdesc.h b/gdbsupport/tdesc.h
> > index 456e8e070b1..e36137cacd0 100644
> > --- a/gdbsupport/tdesc.h
> > +++ b/gdbsupport/tdesc.h
> > @@ -312,6 +312,17 @@ struct tdesc_feature : tdesc_element
> >  
> >  typedef std::unique_ptr<tdesc_feature> tdesc_feature_up;
> >  
> > +/* A deleter adapter for a target desc.  */
> > +
> > +struct target_desc_deleter
> > +{
> > +  void operator() (struct target_desc *desc) const;
> > +};
> 
> It would be good to say in the comment that there are two distinct implementations
> for this, in gdb and gdbserver.

Thanks for the feedback.  Here's an updated patch with an extended
comment.  I did consider the template approach, but unless I'm missing
something I'm not sure it actually changes things much - the calls to
delete still need to live in a location where the full class type is
known, so we'd still end up with two blocks of code, on in gdb and one
in gdbserver.  But maybe I'm not understanding.

Anyway, I'm hoping you'll be happy to have this merged for now, and
any template based improvements could possibly be added later?

Thanks,
Andrew

---

commit 2238967c0d3eb179c1e3ba289c012a61b6910d26
Author: Andrew Burgess <andrew.burgess@embecosm.com>
Date:   Thu Jul 16 17:48:12 2020 +0100

    gdb/riscv: delete target descriptions when gdb exits
    
    It was pointed out on IRC that the RISC-V target allocates target
    descriptions and stores them in a global map, and doesn't delete these
    target descriptions when GDB shuts down.
    
    This isn't a particular problem, the total number of target
    descriptions we can create is very limited so creating these on demand
    and holding them for the entire run on GDB seems reasonable.
    
    However, not deleting these objects on GDB exit means extra warnings
    are printed from tools like valgrind, and the address sanitiser,
    making it harder to spot real issues.  As it's reasonably easy to have
    GDB correctly delete these objects on exit, lets just do that.
    
    I started by noticing that we already have a target_desc_up type, a
    wrapper around unique_ptr that calls a function that will correctly
    delete target descriptions, so I want to use that, but....
    
    ...that type is declared in gdb/target-descriptions.h.  If I try to
    include that file in gdb/arch/riscv.c I run into a problem, that file
    is compiled into both GDB and GDBServer.
    
    OK, I could guard the include with #ifdef, but surely we can do
    better.
    
    So then I decided to move the target_desc_up type into
    gdbsupport/tdesc.h, this is the interface file for generic code shared
    between GDB and GDBserver (relating to target descriptions).  The
    actual implementation for the delete function still lives in
    gdb/target-description.c, but now gdb/arch/riscv.c can see the
    declaration.  Problem solved....
    
    ... but, though RISC-V doesn't use it I've now exposed the
    target_desc_up type to gdbserver, so in future someone _might_ start
    using it, which is fine, except right now there's no definition of the
    delete function - remember the delete I used is only defined in GDB
    code.
    
    No problem, I add an implementation of the delete operator into
    gdbserver/tdesc.cc, and all is good..... except....
    
    I start getting this error from GCC:
    
      tdesc.cc:109:10: error: deleting object of polymorphic class type ‘target_desc’ which has non-virtual destructor might cause undefined behavior [-Werror=delete-non-virtual-dtor]
    
    This seems a little strange to me as target_desc is not a polymorphic
    class as far as I can tell, so I don't quite understand why GCC is
    complaining about this.
    
    Still by making gdbserver's target_desc class final this seems to
    resolve this issue.  This should be fine so long as we never try to
    inherit from target_desc, but if we do we probably should make the
    destructor virtual, so that wouldn't be an issue.
    
    gdb/ChangeLog:
    
            * arch/riscv.c (riscv_tdesc_cache): Change map type.
            (riscv_lookup_target_description): Return pointer out of
            unique_ptr.
            * target-descriptions.c (allocate_target_description): Add
            comment.
            (target_desc_deleter::operator()): Likewise.
            * target-descriptions.h (struct target_desc_deleter): Moved to
            gdbsupport/tdesc.h.
            (target_desc_up): Likewise.
    
    gdbserver/ChangeLog:
    
            * tdesc.cc (allocate_target_description): Add header comment.
            (target_desc_deleter::operator()): New function.
            * tdesc.h (struct target_desc): Declare as final.
    
    gdbsupport/ChangeLog:
    
            * tdesc.h (struct target_desc_deleter): Moved here
            from gdb/target-descriptions.h, extend comment.
            (target_desc_up): Likewise.

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index f30edafdf92..9195a396a20 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,15 @@
+2020-07-16  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+	* arch/riscv.c (riscv_tdesc_cache): Change map type.
+	(riscv_lookup_target_description): Return pointer out of
+	unique_ptr.
+	* target-descriptions.c (allocate_target_description): Add
+	comment.
+	(target_desc_deleter::operator()): Likewise.
+	* target-descriptions.h (struct target_desc_deleter): Moved to
+	gdbsupport/tdesc.h.
+	(target_desc_up): Likewise.
+
 2020-07-15  Andrew Burgess  <andrew.burgess@embecosm.com>
 
 	* f-typeprint.c (f_type_print_base): Allow for dynamic types not
diff --git a/gdb/arch/riscv.c b/gdb/arch/riscv.c
index a02c18b4c91..e43aafc1c2f 100644
--- a/gdb/arch/riscv.c
+++ b/gdb/arch/riscv.c
@@ -91,7 +91,7 @@ struct riscv_gdbarch_features_hasher
 /* Cache of previously seen target descriptions, indexed by the feature set
    that created them.  */
 static std::unordered_map<riscv_gdbarch_features,
-			  const target_desc *,
+			  const target_desc_up,
 			  riscv_gdbarch_features_hasher> riscv_tdesc_cache;
 
 /* See arch/riscv.h.  */
@@ -99,10 +99,12 @@ static std::unordered_map<riscv_gdbarch_features,
 const target_desc *
 riscv_lookup_target_description (const struct riscv_gdbarch_features features)
 {
-  /* Lookup in the cache.  */
+  /* Lookup in the cache.  If we find it then return the pointer out of
+     the target_desc_up (which is a unique_ptr).  This is safe as the
+     riscv_tdesc_cache will exist until GDB exits.  */
   const auto it = riscv_tdesc_cache.find (features);
   if (it != riscv_tdesc_cache.end ())
-    return it->second;
+    return it->second.get ();
 
   target_desc *tdesc = riscv_create_target_description (features);
 
diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c
index 2f4b17727b8..20d624c0c65 100644
--- a/gdb/target-descriptions.c
+++ b/gdb/target-descriptions.c
@@ -1206,12 +1206,16 @@ tdesc_create_feature (struct target_desc *tdesc, const char *name)
   return new_feature;
 }
 
+/* See gdbsupport/tdesc.h.  */
+
 struct target_desc *
 allocate_target_description (void)
 {
   return new target_desc ();
 }
 
+/* See gdbsupport/tdesc.h.  */
+
 void
 target_desc_deleter::operator() (struct target_desc *target_desc) const
 {
diff --git a/gdb/target-descriptions.h b/gdb/target-descriptions.h
index 6d842bf07ed..66a2c213dc2 100644
--- a/gdb/target-descriptions.h
+++ b/gdb/target-descriptions.h
@@ -225,18 +225,6 @@ struct type *tdesc_find_type (struct gdbarch *gdbarch, const char *id);
 int tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
 				  struct reggroup *reggroup);
 
-
-/* A deleter adapter for a target desc.  */
-
-struct target_desc_deleter
-{
-  void operator() (struct target_desc *desc) const;
-};
-
-/* A unique pointer specialization that holds a target_desc.  */
-
-typedef std::unique_ptr<target_desc, target_desc_deleter> target_desc_up;
-
 /* Methods for constructing a target description.  */
 
 void set_tdesc_architecture (struct target_desc *,
diff --git a/gdbserver/ChangeLog b/gdbserver/ChangeLog
index 1ee716f52d7..402944419cc 100644
--- a/gdbserver/ChangeLog
+++ b/gdbserver/ChangeLog
@@ -1,3 +1,9 @@
+2020-07-16  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+	* tdesc.cc (allocate_target_description): Add header comment.
+	(target_desc_deleter::operator()): New function.
+	* tdesc.h (struct target_desc): Declare as final.
+
 2020-07-13  Simon Marchi  <simon.marchi@polymtl.ca>
 
 	* server.cc (handle_query): Use std::vector of
diff --git a/gdbserver/tdesc.cc b/gdbserver/tdesc.cc
index d21688b932b..e639017cc31 100644
--- a/gdbserver/tdesc.cc
+++ b/gdbserver/tdesc.cc
@@ -93,12 +93,22 @@ init_target_desc (struct target_desc *tdesc,
 #endif
 }
 
+/* See gdbsupport/tdesc.h.  */
+
 struct target_desc *
 allocate_target_description (void)
 {
   return new target_desc ();
 }
 
+/* See gdbsupport/tdesc.h.  */
+
+void
+target_desc_deleter::operator() (struct target_desc *target_desc) const
+{
+  delete target_desc;
+}
+
 #ifndef IN_PROCESS_AGENT
 
 static const struct target_desc default_description {};
diff --git a/gdbserver/tdesc.h b/gdbserver/tdesc.h
index f9ca478ff6d..681de64d0aa 100644
--- a/gdbserver/tdesc.h
+++ b/gdbserver/tdesc.h
@@ -27,7 +27,7 @@
 /* A target description.  Inherit from tdesc_feature so that target_desc
    can be used as tdesc_feature.  */
 
-struct target_desc : tdesc_element
+struct target_desc final : tdesc_element
 {
   /* A vector of elements of register definitions that
      describe the inferior's register set.  */
diff --git a/gdbsupport/ChangeLog b/gdbsupport/ChangeLog
index e1b040d5827..c148975aa41 100644
--- a/gdbsupport/ChangeLog
+++ b/gdbsupport/ChangeLog
@@ -1,3 +1,9 @@
+2020-07-16  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+	* tdesc.h (struct target_desc_deleter): Moved here
+	from gdb/target-descriptions.h, extend comment.
+	(target_desc_up): Likewise.
+
 2020-06-30  Tom Tromey  <tromey@adacore.com>
 
 	PR build/26183:
diff --git a/gdbsupport/tdesc.h b/gdbsupport/tdesc.h
index 456e8e070b1..025aa744bfb 100644
--- a/gdbsupport/tdesc.h
+++ b/gdbsupport/tdesc.h
@@ -312,6 +312,20 @@ struct tdesc_feature : tdesc_element
 
 typedef std::unique_ptr<tdesc_feature> tdesc_feature_up;
 
+/* A deleter adapter for a target desc.  There are different
+   implementations of this class in gdb and gdbserver because even thought
+   the  target_desc name is shared between the two projects, the actual
+   implementations of target_desc are completely different.  */
+
+struct target_desc_deleter
+{
+  void operator() (struct target_desc *desc) const;
+};
+
+/* A unique pointer specialization that holds a target_desc.  */
+
+typedef std::unique_ptr<target_desc, target_desc_deleter> target_desc_up;
+
 /* Allocate a new target_desc.  */
 target_desc *allocate_target_description (void);
 


More information about the Gdb-patches mailing list