public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] PR gdb/28480: Improve ambiguous member detection
@ 2021-11-08 18:27 Bruno Larsen
  2021-11-22 13:47 ` [PING] " Bruno Larsen
  2021-11-22 18:00 ` Andrew Burgess
  0 siblings, 2 replies; 9+ messages in thread
From: Bruno Larsen @ 2021-11-08 18:27 UTC (permalink / raw)
  To: gdb-patches

Basic ambiguity detection assumes that when 2 fields with the same name
have the same boffset, it must be an unambiguous request. This is not
always correct. Consider the following code:

class empty { };

class A {
public:
  [[no_unique_address]] empty e;
};

class B {
public:
  int e;
};

class C: public A, public B { };

if we tried to use c.e in code, the compiler would warn of an ambiguity,
however, since A::e does not demand an unique address, it gets the same
address (and thus boffset) of the members, making A::e and B::e have the
same address. however, "print c.e" would fail to report the ambiguity,
and would instead print it as an empty class (first path found).

The new code solves this by checking for other found_fields that have
different m_struct_path.back() (final class that the member was found
in), despite having the same boffset.

The testcase gdb.cp/ambiguous.exp was also changed to test for this
behavior.
---
 gdb/testsuite/gdb.cp/ambiguous.cc  | 19 +++++++++++++++++++
 gdb/testsuite/gdb.cp/ambiguous.exp | 10 ++++++++++
 gdb/valops.c                       | 27 +++++++++++++++++++++++++++
 3 files changed, 56 insertions(+)

diff --git a/gdb/testsuite/gdb.cp/ambiguous.cc b/gdb/testsuite/gdb.cp/ambiguous.cc
index a55686547f2..af2198dcfbc 100644
--- a/gdb/testsuite/gdb.cp/ambiguous.cc
+++ b/gdb/testsuite/gdb.cp/ambiguous.cc
@@ -1,3 +1,4 @@
+class empty { };
 
 class A1 {
 public:
@@ -17,6 +18,17 @@ public:
   int y;
 };
 
+#if !defined (__GNUC__) || __GNUC__ > 7
+# define NO_UNIQUE_ADDRESS [[no_unique_address]]
+#else
+# define NO_UNIQUE_ADDRESS
+#endif
+
+class A4 {
+public:
+    NO_UNIQUE_ADDRESS empty x;
+};
+
 class X : public A1, public A2 {
 public:
   int z;
@@ -77,6 +89,10 @@ public:
   int jva1v;
 };
 
+class JE : public A1, public A4 {
+public:
+};
+
 int main()
 {
   A1 a1;
@@ -92,6 +108,7 @@ int main()
   JVA1 jva1;
   JVA2 jva2;
   JVA1V jva1v;
+  JE je;
   
   int i;
 
@@ -173,5 +190,7 @@ int main()
   jva1v.i = 4;
   jva1v.jva1v = 5;
 
+  je.A1::x = 1;
+
   return 0; /* set breakpoint here */
 }
diff --git a/gdb/testsuite/gdb.cp/ambiguous.exp b/gdb/testsuite/gdb.cp/ambiguous.exp
index 008898c5818..a2a7b02b113 100644
--- a/gdb/testsuite/gdb.cp/ambiguous.exp
+++ b/gdb/testsuite/gdb.cp/ambiguous.exp
@@ -264,3 +264,13 @@ gdb_test "print (A1)(KV)jva1" " = \{x = 3, y = 4\}"
 # JVA1V is derived from A1; A1 is a virtual base indirectly
 # and also directly; must not report ambiguity when a JVA1V is cast to an A1.
 gdb_test "print (A1)jva1v" " = {x = 1, y = 2}"
+
+# C++20 introduced a way to have ambiguous fields with the same boffset.
+# This class explicitly tests for that.
+# if this is tested with a compiler that can't handle [[no_unique_address]]
+# the code should still correctly identify the ambiguity because of
+# different boffsets.
+test_ambiguous "je.x" "x" "JE" {
+    "'int A1::x' (JE -> A1)"
+    "'empty A4::x' (JE -> A4)"
+}
diff --git a/gdb/valops.c b/gdb/valops.c
index 9787cdbb513..2989a93df1a 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -1962,6 +1962,33 @@ struct_field_searcher::update_result (struct value *v, LONGEST boffset)
 	     space.  */
 	  if (m_fields.empty () || m_last_boffset != boffset)
 	    m_fields.push_back ({m_struct_path, v});
+	  else
+	  /* Some fields may occupy the same space and still be ambiguous.
+	     This happens when [[no_unique_address]] is used by a member
+	     of the class.  We assume that this only happens when the types are
+	     different.  This is not necessarily complete, but a situation where
+	     this assumption is incorrect is currently (2021) impossible.  */
+	  {
+	      bool ambiguous = false, insert = true;
+	      for (const found_field& field: m_fields) {
+		  if(field.path.back () != m_struct_path.back ())
+		  {
+		      /* Same boffset points to members of different classes.
+			 We have found an ambiguity and should record it.  */
+		      ambiguous = true;
+		  }
+		  else
+		  {
+		      /* We don't need to insert this value again, because a
+			 non-ambiguous path already leads to it.  */
+		      insert = false;
+		      break;
+		  }
+	      }
+	      if (ambiguous && insert) {
+		  m_fields.push_back ({m_struct_path, v});
+	      }
+	  }
 	}
     }
 }
-- 
2.27.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PING] [PATCH v2] PR gdb/28480: Improve ambiguous member detection
  2021-11-08 18:27 [PATCH v2] PR gdb/28480: Improve ambiguous member detection Bruno Larsen
@ 2021-11-22 13:47 ` Bruno Larsen
  2021-11-22 18:00 ` Andrew Burgess
  1 sibling, 0 replies; 9+ messages in thread
From: Bruno Larsen @ 2021-11-22 13:47 UTC (permalink / raw)
  To: gdb-patches

Ping

On 11/8/21 15:27, Bruno Larsen wrote:
> Basic ambiguity detection assumes that when 2 fields with the same name
> have the same boffset, it must be an unambiguous request. This is not
> always correct. Consider the following code:
> 
> class empty { };
> 
> class A {
> public:
>    [[no_unique_address]] empty e;
> };
> 
> class B {
> public:
>    int e;
> };
> 
> class C: public A, public B { };
> 
> if we tried to use c.e in code, the compiler would warn of an ambiguity,
> however, since A::e does not demand an unique address, it gets the same
> address (and thus boffset) of the members, making A::e and B::e have the
> same address. however, "print c.e" would fail to report the ambiguity,
> and would instead print it as an empty class (first path found).
> 
> The new code solves this by checking for other found_fields that have
> different m_struct_path.back() (final class that the member was found
> in), despite having the same boffset.
> 
> The testcase gdb.cp/ambiguous.exp was also changed to test for this
> behavior.
> ---
>   gdb/testsuite/gdb.cp/ambiguous.cc  | 19 +++++++++++++++++++
>   gdb/testsuite/gdb.cp/ambiguous.exp | 10 ++++++++++
>   gdb/valops.c                       | 27 +++++++++++++++++++++++++++
>   3 files changed, 56 insertions(+)
> 
> diff --git a/gdb/testsuite/gdb.cp/ambiguous.cc b/gdb/testsuite/gdb.cp/ambiguous.cc
> index a55686547f2..af2198dcfbc 100644
> --- a/gdb/testsuite/gdb.cp/ambiguous.cc
> +++ b/gdb/testsuite/gdb.cp/ambiguous.cc
> @@ -1,3 +1,4 @@
> +class empty { };
>   
>   class A1 {
>   public:
> @@ -17,6 +18,17 @@ public:
>     int y;
>   };
>   
> +#if !defined (__GNUC__) || __GNUC__ > 7
> +# define NO_UNIQUE_ADDRESS [[no_unique_address]]
> +#else
> +# define NO_UNIQUE_ADDRESS
> +#endif
> +
> +class A4 {
> +public:
> +    NO_UNIQUE_ADDRESS empty x;
> +};
> +
>   class X : public A1, public A2 {
>   public:
>     int z;
> @@ -77,6 +89,10 @@ public:
>     int jva1v;
>   };
>   
> +class JE : public A1, public A4 {
> +public:
> +};
> +
>   int main()
>   {
>     A1 a1;
> @@ -92,6 +108,7 @@ int main()
>     JVA1 jva1;
>     JVA2 jva2;
>     JVA1V jva1v;
> +  JE je;
>     
>     int i;
>   
> @@ -173,5 +190,7 @@ int main()
>     jva1v.i = 4;
>     jva1v.jva1v = 5;
>   
> +  je.A1::x = 1;
> +
>     return 0; /* set breakpoint here */
>   }
> diff --git a/gdb/testsuite/gdb.cp/ambiguous.exp b/gdb/testsuite/gdb.cp/ambiguous.exp
> index 008898c5818..a2a7b02b113 100644
> --- a/gdb/testsuite/gdb.cp/ambiguous.exp
> +++ b/gdb/testsuite/gdb.cp/ambiguous.exp
> @@ -264,3 +264,13 @@ gdb_test "print (A1)(KV)jva1" " = \{x = 3, y = 4\}"
>   # JVA1V is derived from A1; A1 is a virtual base indirectly
>   # and also directly; must not report ambiguity when a JVA1V is cast to an A1.
>   gdb_test "print (A1)jva1v" " = {x = 1, y = 2}"
> +
> +# C++20 introduced a way to have ambiguous fields with the same boffset.
> +# This class explicitly tests for that.
> +# if this is tested with a compiler that can't handle [[no_unique_address]]
> +# the code should still correctly identify the ambiguity because of
> +# different boffsets.
> +test_ambiguous "je.x" "x" "JE" {
> +    "'int A1::x' (JE -> A1)"
> +    "'empty A4::x' (JE -> A4)"
> +}
> diff --git a/gdb/valops.c b/gdb/valops.c
> index 9787cdbb513..2989a93df1a 100644
> --- a/gdb/valops.c
> +++ b/gdb/valops.c
> @@ -1962,6 +1962,33 @@ struct_field_searcher::update_result (struct value *v, LONGEST boffset)
>   	     space.  */
>   	  if (m_fields.empty () || m_last_boffset != boffset)
>   	    m_fields.push_back ({m_struct_path, v});
> +	  else
> +	  /* Some fields may occupy the same space and still be ambiguous.
> +	     This happens when [[no_unique_address]] is used by a member
> +	     of the class.  We assume that this only happens when the types are
> +	     different.  This is not necessarily complete, but a situation where
> +	     this assumption is incorrect is currently (2021) impossible.  */
> +	  {
> +	      bool ambiguous = false, insert = true;
> +	      for (const found_field& field: m_fields) {
> +		  if(field.path.back () != m_struct_path.back ())
> +		  {
> +		      /* Same boffset points to members of different classes.
> +			 We have found an ambiguity and should record it.  */
> +		      ambiguous = true;
> +		  }
> +		  else
> +		  {
> +		      /* We don't need to insert this value again, because a
> +			 non-ambiguous path already leads to it.  */
> +		      insert = false;
> +		      break;
> +		  }
> +	      }
> +	      if (ambiguous && insert) {
> +		  m_fields.push_back ({m_struct_path, v});
> +	      }
> +	  }
>   	}
>       }
>   }
> 


-- 
Cheers!
Bruno Larsen


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] PR gdb/28480: Improve ambiguous member detection
  2021-11-08 18:27 [PATCH v2] PR gdb/28480: Improve ambiguous member detection Bruno Larsen
  2021-11-22 13:47 ` [PING] " Bruno Larsen
@ 2021-11-22 18:00 ` Andrew Burgess
  2021-11-22 18:35   ` Bruno Larsen
  1 sibling, 1 reply; 9+ messages in thread
From: Andrew Burgess @ 2021-11-22 18:00 UTC (permalink / raw)
  To: Bruno Larsen; +Cc: gdb-patches

* Bruno Larsen via Gdb-patches <gdb-patches@sourceware.org> [2021-11-08 15:27:22 -0300]:

> Basic ambiguity detection assumes that when 2 fields with the same name
> have the same boffset, it must be an unambiguous request. This is not
> always correct. Consider the following code:

You've use "boffset" a few times in this description.  I think it
would be clearer to just say "offset", or to expand to (I guess) "byte
offset".  The 'boffset' comes from the code, and, ideally, I shouldn't
need to be familiar with variable names to understand the commit message.

> 
> class empty { };
> 
> class A {
> public:
>   [[no_unique_address]] empty e;
> };
> 
> class B {
> public:
>   int e;
> };
> 
> class C: public A, public B { };
> 
> if we tried to use c.e in code, the compiler would warn of an ambiguity,
> however, since A::e does not demand an unique address, it gets the same
> address (and thus boffset) of the members, making A::e and B::e have the
> same address. however, "print c.e" would fail to report the ambiguity,
> and would instead print it as an empty class (first path found).
> 
> The new code solves this by checking for other found_fields that have
> different m_struct_path.back() (final class that the member was found
> in), despite having the same boffset.
> 
> The testcase gdb.cp/ambiguous.exp was also changed to test for this
> behavior.

Thanks for working on this.  I had some formatting feedback, and also
I had some questions on some of the comments, more details below.

> ---
>  gdb/testsuite/gdb.cp/ambiguous.cc  | 19 +++++++++++++++++++
>  gdb/testsuite/gdb.cp/ambiguous.exp | 10 ++++++++++
>  gdb/valops.c                       | 27 +++++++++++++++++++++++++++
>  3 files changed, 56 insertions(+)
> 
> diff --git a/gdb/testsuite/gdb.cp/ambiguous.cc b/gdb/testsuite/gdb.cp/ambiguous.cc
> index a55686547f2..af2198dcfbc 100644
> --- a/gdb/testsuite/gdb.cp/ambiguous.cc
> +++ b/gdb/testsuite/gdb.cp/ambiguous.cc
> @@ -1,3 +1,4 @@
> +class empty { };
>  
>  class A1 {
>  public:
> @@ -17,6 +18,17 @@ public:
>    int y;
>  };
>  
> +#if !defined (__GNUC__) || __GNUC__ > 7
> +# define NO_UNIQUE_ADDRESS [[no_unique_address]]
> +#else
> +# define NO_UNIQUE_ADDRESS
> +#endif
> +
> +class A4 {
> +public:
> +    NO_UNIQUE_ADDRESS empty x;
> +};
> +
>  class X : public A1, public A2 {
>  public:
>    int z;
> @@ -77,6 +89,10 @@ public:
>    int jva1v;
>  };
>  
> +class JE : public A1, public A4 {
> +public:
> +};
> +
>  int main()
>  {
>    A1 a1;
> @@ -92,6 +108,7 @@ int main()
>    JVA1 jva1;
>    JVA2 jva2;
>    JVA1V jva1v;
> +  JE je;
>    
>    int i;
>  
> @@ -173,5 +190,7 @@ int main()
>    jva1v.i = 4;
>    jva1v.jva1v = 5;
>  
> +  je.A1::x = 1;
> +
>    return 0; /* set breakpoint here */
>  }
> diff --git a/gdb/testsuite/gdb.cp/ambiguous.exp b/gdb/testsuite/gdb.cp/ambiguous.exp
> index 008898c5818..a2a7b02b113 100644
> --- a/gdb/testsuite/gdb.cp/ambiguous.exp
> +++ b/gdb/testsuite/gdb.cp/ambiguous.exp
> @@ -264,3 +264,13 @@ gdb_test "print (A1)(KV)jva1" " = \{x = 3, y = 4\}"
>  # JVA1V is derived from A1; A1 is a virtual base indirectly
>  # and also directly; must not report ambiguity when a JVA1V is cast to an A1.
>  gdb_test "print (A1)jva1v" " = {x = 1, y = 2}"
> +
> +# C++20 introduced a way to have ambiguous fields with the same boffset.

Same request for the use of "boffset" here.

> +# This class explicitly tests for that.
> +# if this is tested with a compiler that can't handle [[no_unique_address]]
> +# the code should still correctly identify the ambiguity because of
> +# different boffsets.
> +test_ambiguous "je.x" "x" "JE" {
> +    "'int A1::x' (JE -> A1)"
> +    "'empty A4::x' (JE -> A4)"
> +}
> diff --git a/gdb/valops.c b/gdb/valops.c
> index 9787cdbb513..2989a93df1a 100644
> --- a/gdb/valops.c
> +++ b/gdb/valops.c
> @@ -1962,6 +1962,33 @@ struct_field_searcher::update_result (struct value *v, LONGEST boffset)
>  	     space.  */
>  	  if (m_fields.empty () || m_last_boffset != boffset)
>  	    m_fields.push_back ({m_struct_path, v});
> +	  else
> +	  /* Some fields may occupy the same space and still be ambiguous.
> +	     This happens when [[no_unique_address]] is used by a member
> +	     of the class.  We assume that this only happens when the types are
> +	     different.  This is not necessarily complete, but a situation where
> +	     this assumption is incorrect is currently (2021) impossible.  */

This comment should be moved inside the "{ ... }" block.

I found this comment difficult to understand.  When you say "...when
the types are different", I guess this is referring to the path check
below maybe?  In which case I wonder if we can find a different way to
phrase this, rather than "different types" ... "paths to the two
fields are different" maybe?

Additional the whole final sentence just leaves me confused, it seems
to hint that there is a situation not covered by this code "This is
not necessarily complete...", but also that there is no such situation
"... is currently impossible".

I wonder if you are saying that should we ever have two fields of the
same name, in the same class, that occur at the same address, then
this code wouldn't cover that case?  But that seems a pretty weird
thing to worry about, so I assume I'm not understand you correctly.

Could you rephrase the last part please?

> +	  {

The indentation of this is wrong, it should be indented with two tabs.

> +	      bool ambiguous = false, insert = true;
> +	      for (const found_field& field: m_fields) {

I think the GDB style for this would be:

  for (const found_field &field : m_fields)

the '{' should be on the next line, and indented from the 'for'.

> +		  if(field.path.back () != m_struct_path.back ())
> +		  {

New blocks get two additional spaces for indentation, which will mean
reindenting all this if/else code.

> +		      /* Same boffset points to members of different classes.
> +			 We have found an ambiguity and should record it.  */
> +		      ambiguous = true;
> +		  }
> +		  else
> +		  {
> +		      /* We don't need to insert this value again, because a
> +			 non-ambiguous path already leads to it.  */
> +		      insert = false;
> +		      break;
> +		  }
> +	      }
> +	      if (ambiguous && insert) {
> +		  m_fields.push_back ({m_struct_path, v});
> +	      }

Single statement if blocks don't get '{ ... }' around them.

> +	  }
>  	}
>      }
>  }
> -- 
> 2.27.0
> 

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] PR gdb/28480: Improve ambiguous member detection
  2021-11-22 18:00 ` Andrew Burgess
@ 2021-11-22 18:35   ` Bruno Larsen
  2021-11-24 17:09     ` Andrew Burgess
  0 siblings, 1 reply; 9+ messages in thread
From: Bruno Larsen @ 2021-11-22 18:35 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

Hi Andrew,

Thanks for the review. All of the easy addressable comments will be changed.

On 11/22/21 15:00, Andrew Burgess wrote:
> * Bruno Larsen via Gdb-patches <gdb-patches@sourceware.org> [2021-11-08 15:27:22 -0300]:
> 

>> diff --git a/gdb/valops.c b/gdb/valops.c
>> index 9787cdbb513..2989a93df1a 100644
>> --- a/gdb/valops.c
>> +++ b/gdb/valops.c
>> @@ -1962,6 +1962,33 @@ struct_field_searcher::update_result (struct value *v, LONGEST boffset)
>>   	     space.  */
>>   	  if (m_fields.empty () || m_last_boffset != boffset)
>>   	    m_fields.push_back ({m_struct_path, v});
>> +	  else
>> +	  /* Some fields may occupy the same space and still be ambiguous.
>> +	     This happens when [[no_unique_address]] is used by a member
>> +	     of the class.  We assume that this only happens when the types are
>> +	     different.  This is not necessarily complete, but a situation where
>> +	     this assumption is incorrect is currently (2021) impossible.  */
> 
> This comment should be moved inside the "{ ... }" block.
> 
> I found this comment difficult to understand.  When you say "...when
> the types are different", I guess this is referring to the path check
> below maybe?  In which case I wonder if we can find a different way to
> phrase this, rather than "different types" ... "paths to the two
> fields are different" maybe?
> 
> Additional the whole final sentence just leaves me confused, it seems
> to hint that there is a situation not covered by this code "This is
> not necessarily complete...", but also that there is no such situation
> "... is currently impossible".
> 
> I wonder if you are saying that should we ever have two fields of the
> same name, in the same class, that occur at the same address, then
> this code wouldn't cover that case?  But that seems a pretty weird
> thing to worry about, so I assume I'm not understand you correctly.
> 
> Could you rephrase the last part please?

How does the following sound:

Some fields may occupy the same space and still be ambiguous. This happens when [[no_unique_address]] is used in the inferior's code. The current solution assumes that the compiler will only place 2 struct members in the same location if they are of different types. As of 2021, this is mandatory, but this may change in the future

Or I can remove the last sentence, if that is still confusing or unnecessary

-- 
Cheers!
Bruno Larsen


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] PR gdb/28480: Improve ambiguous member detection
  2021-11-22 18:35   ` Bruno Larsen
@ 2021-11-24 17:09     ` Andrew Burgess
  2021-11-25 12:01       ` Bruno Larsen
  2021-12-04 11:31       ` Joel Brobecker
  0 siblings, 2 replies; 9+ messages in thread
From: Andrew Burgess @ 2021-11-24 17:09 UTC (permalink / raw)
  To: Bruno Larsen; +Cc: gdb-patches

* Bruno Larsen via Gdb-patches <gdb-patches@sourceware.org> [2021-11-22 15:35:29 -0300]:

> Hi Andrew,
> 
> Thanks for the review. All of the easy addressable comments will be changed.
> 
> On 11/22/21 15:00, Andrew Burgess wrote:
> > * Bruno Larsen via Gdb-patches <gdb-patches@sourceware.org> [2021-11-08 15:27:22 -0300]:
> > 
> 
> > > diff --git a/gdb/valops.c b/gdb/valops.c
> > > index 9787cdbb513..2989a93df1a 100644
> > > --- a/gdb/valops.c
> > > +++ b/gdb/valops.c
> > > @@ -1962,6 +1962,33 @@ struct_field_searcher::update_result (struct value *v, LONGEST boffset)
> > >   	     space.  */
> > >   	  if (m_fields.empty () || m_last_boffset != boffset)
> > >   	    m_fields.push_back ({m_struct_path, v});
> > > +	  else
> > > +	  /* Some fields may occupy the same space and still be ambiguous.
> > > +	     This happens when [[no_unique_address]] is used by a member
> > > +	     of the class.  We assume that this only happens when the types are
> > > +	     different.  This is not necessarily complete, but a situation where
> > > +	     this assumption is incorrect is currently (2021) impossible.  */
> > 
> > This comment should be moved inside the "{ ... }" block.
> > 
> > I found this comment difficult to understand.  When you say "...when
> > the types are different", I guess this is referring to the path check
> > below maybe?  In which case I wonder if we can find a different way to
> > phrase this, rather than "different types" ... "paths to the two
> > fields are different" maybe?
> > 
> > Additional the whole final sentence just leaves me confused, it seems
> > to hint that there is a situation not covered by this code "This is
> > not necessarily complete...", but also that there is no such situation
> > "... is currently impossible".
> > 
> > I wonder if you are saying that should we ever have two fields of the
> > same name, in the same class, that occur at the same address, then
> > this code wouldn't cover that case?  But that seems a pretty weird
> > thing to worry about, so I assume I'm not understand you correctly.
> > 
> > Could you rephrase the last part please?
> 
> How does the following sound:
> 
> Some fields may occupy the same space and still be ambiguous. This
> happens when [[no_unique_address]] is used in the inferior's
> code. The current solution assumes that the compiler will only place
> 2 struct members in the same location if they are of different
> types. As of 2021, this is mandatory, but this may change in the
> future

I agree with you about what the standard says, but what I'm not
understanding is why that's relevant here.  We're looking up something
by name, right?  And we're trying to handle the case where two things
might exist at the same address _and_ have the same name.

So in this check:

  if(field.path.back () != m_struct_path.back ())
    ...
  else
    ...

We get to the if block when two things have the same address, but
their containing classes are different, in your original example A::e
and B::e, the 'A' and 'B' are different.

We get to the else block when two things have the same address, but
their containing classes are the same, so this might be A::e and A::e,
which I'm pretty sure can only happen when virtual inheritance is in
use, right?

But my point is, where in this logic do we check the type of 'e'?  I
don't see it.  If we imagine a world where A::e and B::e were the same
type, and placed at the same address, I think the existing code would
be fine.

My claim then, is that the following describes your code:

  Fields can occupy the same space and have the same name (be
  ambiguous).  This can happen when fields in two different base
  classes are marked [[no_unique_address]] and have the same name.
  The C++ standard says that such fields can only occupy the same
  space if they are of different type, but we don't rely on that in
  the following code.

If I'm not understanding something, please do let me know.

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] PR gdb/28480: Improve ambiguous member detection
  2021-11-24 17:09     ` Andrew Burgess
@ 2021-11-25 12:01       ` Bruno Larsen
  2021-12-04 11:31       ` Joel Brobecker
  1 sibling, 0 replies; 9+ messages in thread
From: Bruno Larsen @ 2021-11-25 12:01 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

On 11/24/21 14:09, Andrew Burgess wrote:
> * Bruno Larsen via Gdb-patches <gdb-patches@sourceware.org> [2021-11-22 15:35:29 -0300]:
> 
>> Hi Andrew,
>>
>> Thanks for the review. All of the easy addressable comments will be changed.
>>
>> On 11/22/21 15:00, Andrew Burgess wrote:
>>> * Bruno Larsen via Gdb-patches <gdb-patches@sourceware.org> [2021-11-08 15:27:22 -0300]:
>>>
>>
>>>> diff --git a/gdb/valops.c b/gdb/valops.c
>>>> index 9787cdbb513..2989a93df1a 100644
>>>> --- a/gdb/valops.c
>>>> +++ b/gdb/valops.c
>>>> @@ -1962,6 +1962,33 @@ struct_field_searcher::update_result (struct value *v, LONGEST boffset)
>>>>    	     space.  */
>>>>    	  if (m_fields.empty () || m_last_boffset != boffset)
>>>>    	    m_fields.push_back ({m_struct_path, v});
>>>> +	  else
>>>> +	  /* Some fields may occupy the same space and still be ambiguous.
>>>> +	     This happens when [[no_unique_address]] is used by a member
>>>> +	     of the class.  We assume that this only happens when the types are
>>>> +	     different.  This is not necessarily complete, but a situation where
>>>> +	     this assumption is incorrect is currently (2021) impossible.  */
>>>
>>> This comment should be moved inside the "{ ... }" block.
>>>
>>> I found this comment difficult to understand.  When you say "...when
>>> the types are different", I guess this is referring to the path check
>>> below maybe?  In which case I wonder if we can find a different way to
>>> phrase this, rather than "different types" ... "paths to the two
>>> fields are different" maybe?
>>>
>>> Additional the whole final sentence just leaves me confused, it seems
>>> to hint that there is a situation not covered by this code "This is
>>> not necessarily complete...", but also that there is no such situation
>>> "... is currently impossible".
>>>
>>> I wonder if you are saying that should we ever have two fields of the
>>> same name, in the same class, that occur at the same address, then
>>> this code wouldn't cover that case?  But that seems a pretty weird
>>> thing to worry about, so I assume I'm not understand you correctly.
>>>
>>> Could you rephrase the last part please?
>>
>> How does the following sound:
>>
>> Some fields may occupy the same space and still be ambiguous. This
>> happens when [[no_unique_address]] is used in the inferior's
>> code. The current solution assumes that the compiler will only place
>> 2 struct members in the same location if they are of different
>> types. As of 2021, this is mandatory, but this may change in the
>> future
> 
> I agree with you about what the standard says, but what I'm not
> understanding is why that's relevant here.  We're looking up something
> by name, right?  And we're trying to handle the case where two things
> might exist at the same address _and_ have the same name.
> 
> So in this check:
> 
>    if(field.path.back () != m_struct_path.back ())
>      ...
>    else
>      ...
> 
> We get to the if block when two things have the same address, but
> their containing classes are different, in your original example A::e
> and B::e, the 'A' and 'B' are different.
> 
> We get to the else block when two things have the same address, but
> their containing classes are the same, so this might be A::e and A::e,
> which I'm pretty sure can only happen when virtual inheritance is in
> use, right?> 
> But my point is, where in this logic do we check the type of 'e'?  I
> don't see it.  If we imagine a world where A::e and B::e were the same
> type, and placed at the same address, I think the existing code would
> be fine.


Good catch. I thought that field.path included the type of 'e', but reviewing the code, you're right in saying that it doesn't.
However, as far as I understand, this is still fine, seeing as otherwise, we'd have two different A classes in the same address, which is also blocked by the C++ standard.

> 
> My claim then, is that the following describes your code:
> 
>    Fields can occupy the same space and have the same name (be
>    ambiguous).  This can happen when fields in two different base
>    classes are marked [[no_unique_address]] and have the same name.
>    The C++ standard says that such fields can only occupy the same
>    space if they are of different type, but we don't rely on that in
>    the following code.

I feel like this is a good description, I'll add it to the code and push it. Thanks again for the review!

> 
> If I'm not understanding something, please do let me know.
> 
> Thanks,
> Andrew
> 


-- 
Cheers!
Bruno Larsen


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] PR gdb/28480: Improve ambiguous member detection
  2021-11-24 17:09     ` Andrew Burgess
  2021-11-25 12:01       ` Bruno Larsen
@ 2021-12-04 11:31       ` Joel Brobecker
  2021-12-06 11:16         ` Andrew Burgess
  1 sibling, 1 reply; 9+ messages in thread
From: Joel Brobecker @ 2021-12-04 11:31 UTC (permalink / raw)
  To: aburgess; +Cc: Bruno Larsen, Joel Brobecker, gdb-patches

Hi Andrew,

Do you think this patch is safe-enough to be backported to 11.2?
This was requested because it was an apparent regression compared to
GDB 10.

Thanks!
-- 
Joel

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] PR gdb/28480: Improve ambiguous member detection
  2021-12-04 11:31       ` Joel Brobecker
@ 2021-12-06 11:16         ` Andrew Burgess
  2021-12-11  7:50           ` Joel Brobecker
  0 siblings, 1 reply; 9+ messages in thread
From: Andrew Burgess @ 2021-12-06 11:16 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: Bruno Larsen, gdb-patches

* Joel Brobecker <brobecker@adacore.com> [2021-12-04 15:31:16 +0400]:

> Hi Andrew,
> 
> Do you think this patch is safe-enough to be backported to 11.2?
> This was requested because it was an apparent regression compared to
> GDB 10.

I'm happy for this patch to go into 11.2, the change seems pretty self
contained, so I don't think there should be too much risk.

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] PR gdb/28480: Improve ambiguous member detection
  2021-12-06 11:16         ` Andrew Burgess
@ 2021-12-11  7:50           ` Joel Brobecker
  0 siblings, 0 replies; 9+ messages in thread
From: Joel Brobecker @ 2021-12-11  7:50 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: Joel Brobecker, Bruno Larsen, gdb-patches

> > Do you think this patch is safe-enough to be backported to 11.2?
> > This was requested because it was an apparent regression compared to
> > GDB 10.
> 
> I'm happy for this patch to go into 11.2, the change seems pretty self
> contained, so I don't think there should be too much risk.

Thanks Andrew. I've pushed the patch to gdb-11-branch after having
added a ChangeLog entry.

-- 
Joel

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-12-11  7:50 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-08 18:27 [PATCH v2] PR gdb/28480: Improve ambiguous member detection Bruno Larsen
2021-11-22 13:47 ` [PING] " Bruno Larsen
2021-11-22 18:00 ` Andrew Burgess
2021-11-22 18:35   ` Bruno Larsen
2021-11-24 17:09     ` Andrew Burgess
2021-11-25 12:01       ` Bruno Larsen
2021-12-04 11:31       ` Joel Brobecker
2021-12-06 11:16         ` Andrew Burgess
2021-12-11  7:50           ` Joel Brobecker

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).