public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Compute proper length for dynamic types of TYPE_CODE_TYPEDEF
@ 2020-06-10 11:35 Tom de Vries
  2020-06-11 10:46 ` Pedro Alves
  0 siblings, 1 reply; 5+ messages in thread
From: Tom de Vries @ 2020-06-10 11:35 UTC (permalink / raw)
  To: gdb-patches; +Cc: Keith Seitz, Pedro Alves

Hi,

[ This patch was submitted earlier here (
https://sourceware.org/legacy-ml/gdb-patches/2017-06/msg00679.html ).

I've:
- made the patch applicable to current master
- made the log message narrower
- added a missing initialization in the test-case:
    vla_struct_typedef_struct_member_object.something = n * 2;
- renamed things in the test-case as suggested by Pedro's review comments
  ( https://sourceware.org/legacy-ml/gdb-patches/2017-06/msg00696.html ).
- re-tested on x86_64-linux. 
]

This patch fixes gdb/21356 in which we hit an assertion in
value_contents_bits_eq:

(gdb) p container_object2
(gdb) p container_object2
$1 = {_container_member2 = 15, _vla_struct_object2 = {_some_member = 0,
    _vla_field = {
../../src/gdb/value.c:829: internal-error: \
  int value_contents_bits_eq(const value*, int, const value*, int, int): \
  Assertion `offset1 + length \
             <= TYPE_LENGTH (val1->enclosing_type) * TARGET_CHAR_BIT' failed.

This is happening because TYPE_LENGTH (val1->enclosing_type) is erroneously
based on enclosing_type, which is a typedef, instead of the actual underlying
type.

This can be traced back to resolve_dynamic_struct, where the size of the
type is computed:
...
        TYPE_FIELD_TYPE (resolved_type, i)
          = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
                                           &pinfo, 0);
        gdb_assert (TYPE_FIELD_LOC_KIND (resolved_type, i)
                    == FIELD_LOC_KIND_BITPOS);

        new_bit_length = TYPE_FIELD_BITPOS (resolved_type, i);
        if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
          new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
        else
          new_bit_length += (TYPE_LENGTH (TYPE_FIELD_TYPE (resolved_type, i))
                             * TARGET_CHAR_BIT);
...

In this function, resolved_type is TYPE_CODE_TYPEDEF which is not what we
want to use to calculate the size of the actual field.

This patch fixes this and the similar problem in resolve_dynamic_union.

Any comments?

Thanks,
- Tom

Compute proper length for dynamic types of TYPE_CODE_TYPEDEF

gdb/ChangeLog:
yyyy-mm-dd  Keith Seitz  <keiths@redhat.com>

	PR gdb/21356
	* gdbtypes.c (resolve_dynamic_union, resolve_dynamic_struct):
	Resolve typedefs for type length calculations.

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Keith Seitz  <keiths@redhat.com>

	PR gdb/21356
	* gdb.base/vla-datatypes.c (vla_factory): Add typedef for struct
	vla_struct.
	Add new struct vla_typedef and union vla_typedef_union and
	corresponding instantiation objects.
	Initialize new objects.
	* gdb.base/vla-datatypes.exp: Add tests for vla_typedef_struct_object
	and vla_typedef_union_object.
	Fixup type for vla_struct_object.

---
 gdb/gdbtypes.c                           | 14 ++++++++++----
 gdb/testsuite/gdb.base/vla-datatypes.c   | 23 ++++++++++++++++++++++-
 gdb/testsuite/gdb.base/vla-datatypes.exp |  6 +++++-
 3 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index b5a13107ed..cdf88a4a7d 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -2264,8 +2264,10 @@ resolve_dynamic_union (struct type *type,
       t = resolve_dynamic_type_internal (resolved_type->field (i).type (),
 					 addr_stack, 0);
       resolved_type->field (i).set_type (t);
-      if (TYPE_LENGTH (t) > max_len)
-	max_len = TYPE_LENGTH (t);
+
+      struct type *real_type = check_typedef (t);
+      if (TYPE_LENGTH (real_type) > max_len)
+	max_len = TYPE_LENGTH (real_type);
     }
 
   TYPE_LENGTH (resolved_type) = max_len;
@@ -2521,8 +2523,12 @@ resolve_dynamic_struct (struct type *type,
       if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
 	new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
       else
-	new_bit_length += (TYPE_LENGTH (resolved_type->field (i).type ())
-			   * TARGET_CHAR_BIT);
+	{
+	  struct type *real_type
+	    = check_typedef (resolved_type->field (i).type ());
+
+	  new_bit_length += (TYPE_LENGTH (real_type) * TARGET_CHAR_BIT);
+	}
 
       /* Normally, we would use the position and size of the last field
 	 to determine the size of the enclosing structure.  But GCC seems
diff --git a/gdb/testsuite/gdb.base/vla-datatypes.c b/gdb/testsuite/gdb.base/vla-datatypes.c
index ecdcf4313c..6f95ea0649 100644
--- a/gdb/testsuite/gdb.base/vla-datatypes.c
+++ b/gdb/testsuite/gdb.base/vla-datatypes.c
@@ -50,7 +50,9 @@ vla_factory (int n)
   {
     int something;
     int vla_field[n];
-  } vla_struct_object;
+  };
+  typedef struct vla_struct vla_struct_typedef;
+  vla_struct_typedef vla_struct_object;
 
   struct inner_vla_struct
   {
@@ -59,14 +61,29 @@ vla_factory (int n)
     int after;
   } inner_vla_struct_object;
 
+  struct vla_struct_typedef_struct_member
+  {
+    int something;
+    vla_struct_typedef vla_object;
+  } vla_struct_typedef_struct_member_object;
+
   union vla_union
   {
     int vla_field[n];
   } vla_union_object;
 
+  union vla_struct_typedef_union_member
+  {
+    int something;
+    vla_struct_typedef vla_object;
+  } vla_struct_typedef_union_member_object;
+
   vla_struct_object.something = n;
   inner_vla_struct_object.something = n;
   inner_vla_struct_object.after = n;
+  vla_struct_typedef_struct_member_object.something = n * 2;
+  vla_struct_typedef_struct_member_object.vla_object.something = n * 3;
+  vla_struct_typedef_union_member_object.vla_object.something = n + 1;
   for (i = 0; i < n; i++)
     {
       int_vla[i] = i*2;
@@ -85,6 +102,10 @@ vla_factory (int n)
       vla_struct_object.vla_field[i] = i*2;
       vla_union_object.vla_field[i] = i*2;
       inner_vla_struct_object.vla_field[i] = i*2;
+      vla_struct_typedef_struct_member_object.vla_object.vla_field[i]
+	= i * 3;
+      vla_struct_typedef_union_member_object.vla_object.vla_field[i]
+	= i * 3 - 1;
     }
 
   size_t int_size        = sizeof(int_vla);     /* vlas_filled */
diff --git a/gdb/testsuite/gdb.base/vla-datatypes.exp b/gdb/testsuite/gdb.base/vla-datatypes.exp
index fbc2c97936..e8d84579fe 100644
--- a/gdb/testsuite/gdb.base/vla-datatypes.exp
+++ b/gdb/testsuite/gdb.base/vla-datatypes.exp
@@ -45,6 +45,10 @@ gdb_test "print vla_struct_object" \
     "\\\{something = 5, vla_field = \\\{0, 2, 4, 6, 8\\\}\\\}"
 gdb_test "print vla_union_object" \
     "\\\{vla_field = \\\{0, 2, 4, 6, 8\\\}\\\}"
+gdb_test "print vla_struct_typedef_struct_member_object" \
+    "\\\{something = 10, vla_object = \\\{something = 15, vla_field = \\\{0, 3, 6, 9, 12\\\}\\\}\\\}"
+gdb_test "print vla_struct_typedef_union_member_object" \
+    "\\\{something = 6, vla_object = \\\{something = 6, vla_field = \\\{-1, 2, 5, 8, 11\\\}\\\}\\\}"
 
 # Check whatis of VLA's.
 gdb_test "whatis int_vla" "type = int \\\[5\\\]"
@@ -61,7 +65,7 @@ gdb_test "whatis unsigned_short_vla" \
 gdb_test "whatis unsigned_char_vla" "type = unsigned char \\\[5\\\]"
 gdb_test "whatis foo_vla" "type = struct foo \\\[5\\\]"
 gdb_test "whatis bar_vla" "type = BAR \\\[5\\\]"
-gdb_test "whatis vla_struct_object" "type = struct vla_struct"
+gdb_test "whatis vla_struct_object" "type = vla_struct_typedef"
 gdb_test "whatis vla_union_object" "type = union vla_union"
 
 # Check ptype of VLA's.

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

* Re: [PATCH] Compute proper length for dynamic types of TYPE_CODE_TYPEDEF
  2020-06-10 11:35 [PATCH] Compute proper length for dynamic types of TYPE_CODE_TYPEDEF Tom de Vries
@ 2020-06-11 10:46 ` Pedro Alves
  2020-06-11 12:38   ` Tom de Vries
  0 siblings, 1 reply; 5+ messages in thread
From: Pedro Alves @ 2020-06-11 10:46 UTC (permalink / raw)
  To: Tom de Vries, gdb-patches

On 6/10/20 12:35 PM, Tom de Vries wrote:
> Hi,
> 
> [ This patch was submitted earlier here (
> https://sourceware.org/legacy-ml/gdb-patches/2017-06/msg00679.html ).
> 
> I've:
> - made the patch applicable to current master
> - made the log message narrower
> - added a missing initialization in the test-case:
>     vla_struct_typedef_struct_member_object.something = n * 2;
> - renamed things in the test-case as suggested by Pedro's review comments
>   ( https://sourceware.org/legacy-ml/gdb-patches/2017-06/msg00696.html ).
> - re-tested on x86_64-linux. 
> ]
> 
> 
> Any comments?

LGTM.

Thanks,
Pedro Alves


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

* Re: [PATCH] Compute proper length for dynamic types of TYPE_CODE_TYPEDEF
  2020-06-11 10:46 ` Pedro Alves
@ 2020-06-11 12:38   ` Tom de Vries
  0 siblings, 0 replies; 5+ messages in thread
From: Tom de Vries @ 2020-06-11 12:38 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 11-06-2020 12:46, Pedro Alves wrote:
> On 6/10/20 12:35 PM, Tom de Vries wrote:
>> Hi,
>>
>> [ This patch was submitted earlier here (
>> https://sourceware.org/legacy-ml/gdb-patches/2017-06/msg00679.html ).
>>
>> I've:
>> - made the patch applicable to current master
>> - made the log message narrower
>> - added a missing initialization in the test-case:
>>     vla_struct_typedef_struct_member_object.something = n * 2;
>> - renamed things in the test-case as suggested by Pedro's review comments
>>   ( https://sourceware.org/legacy-ml/gdb-patches/2017-06/msg00696.html ).
>> - re-tested on x86_64-linux. 
>> ]
>>
>>
>> Any comments?
> 
> LGTM.
> 

Thanks for the review.

I've just realized that Keith submitted a v2 here (
https://sourceware.org/pipermail/gdb-patches/2017-December/145898.html
), so I seem to have duplicated some work.

Anyway, I've copied the comments in the test-case from that version, and
committed.

Thanks,
- Tom

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

* Re: [PATCH] Compute proper length for dynamic types of TYPE_CODE_TYPEDEF
  2017-06-24  0:59 Keith Seitz
@ 2017-06-26 11:01 ` Pedro Alves
  0 siblings, 0 replies; 5+ messages in thread
From: Pedro Alves @ 2017-06-26 11:01 UTC (permalink / raw)
  To: Keith Seitz, gdb-patches

Hi Keith,

On 06/24/2017 01:59 AM, Keith Seitz wrote:
> --- a/gdb/testsuite/gdb.base/vla-datatypes.c
> +++ b/gdb/testsuite/gdb.base/vla-datatypes.c
> @@ -46,11 +46,12 @@ vla_factory (int n)
>    BAR             bar_vla[n];
>    int i;
>  
> -  struct vla_struct
> +  typedef struct vla_struct
>    {
>      int something;
>      int vla_field[n];
> -  } vla_struct_object;
> +  } vla_s;
> +  vla_s vla_struct_object;

This object is now defined with a typedef type, and that confused me a lot.
After staring at this for a while, I think you did intend that.  I just
find the naming very confusing.  Can we improve on that?  Maybe add
some comments too.

>  
>    struct inner_vla_struct
>    {
> @@ -59,14 +60,28 @@ vla_factory (int n)
>      int after;
>    } inner_vla_struct_object;
>  
> +  struct vla_typedef

... because this type is called "typedef", while
the above isn't.

> +  {
> +    int something;
> +    vla_s vla_object;
> +  } vla_typedef_struct_object;

... and this object here is not a a type that is 
defined as a typedef, while it's named "..._typedef_...".
So at first is looked like you have the "typedef vs non-typedef"
cases backwards.  I understand now that you're referring to
the "vla_object" member, but it was totally non-obvious to me.

Can we rename things a bit to avoid this confusion?

Maybe

  struct vla_typedef -> struct vla_s_struct_member
  union vla_typedef_union -> union vla_s_union_member

?

and add some comment about using "vla_s" throughout
because it's a typedef.  And/or rename it
to "vla_struct_typedef".

Thanks,
Pedro Alves

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

* [PATCH] Compute proper length for dynamic types of TYPE_CODE_TYPEDEF
@ 2017-06-24  0:59 Keith Seitz
  2017-06-26 11:01 ` Pedro Alves
  0 siblings, 1 reply; 5+ messages in thread
From: Keith Seitz @ 2017-06-24  0:59 UTC (permalink / raw)
  To: gdb-patches

This patch fixes gdb/21356 in which we hit an assertion in
value_contents_bits_eq:

(gdb) p container_object2
(gdb) p container_object2
$1 = {_container_member2 = 15, _vla_struct_object2 = {_some_member = 0,
    _vla_field = {
../../src/gdb/value.c:829: internal-error: int value_contents_bits_eq(const value*, int, const value*, int, int): Assertion `offset1 + length <= TYPE_LENGTH (val1->enclosing_type) * TARGET_CHAR_BIT' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Quit this debugging session? (y or n)

This is happening because TYPE_LENGTH (val1->enclosing_type) is erroneously
based on enclosing_type, which is a typedef, instead of the actual underlying
type.

This can be traced back to resolve_dynamic_struct, where the size of the
type is computed:

  2093        TYPE_FIELD_TYPE (resolved_type, i)
  2094          = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
  2095                                           &pinfo, 0);
  2096        gdb_assert (TYPE_FIELD_LOC_KIND (resolved_type, i)
  2097                    == FIELD_LOC_KIND_BITPOS);
  2098
  2099        new_bit_length = TYPE_FIELD_BITPOS (resolved_type, i);
  2100        if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
  2101          new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
  2102        else
  2103          new_bit_length += (TYPE_LENGTH (TYPE_FIELD_TYPE (resolved_type, i))
  2104                             * TARGET_CHAR_BIT);

In this function, resolved_type is TYPE_CODE_TYPEDEF which is not what we
want to use to calculate the size of the actual field.

This patch fixes this and the similar problem in resolve_dynamic_union.

gdb/ChangeLog:
yyyy-mm-dd  Keith Seitz  <keiths@redhat.com>

	PR gdb/21356
	* gdbtypes.c (resolve_dynamic_union, resolve_dynamic_struct):
	Resolve typedefs for type length calculations.

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Keith Seitz  <keiths@redhat.com>

	PR gdb/21356
	* gdb.base/vla-datatypes.c (vla_factory): Add typedef for struct
	vla_struct.
	Add new struct vla_typedef and unin vla_typedef_union and
	corresponding instantiation objects.
	Initialize new objects.
	* gdb.base/vla-datatypes.exp: Add tests for vla_typedef_struct_object
	and vla_typedef_union_object.
	Fixup type for vla_struct_object.
---
 gdb/gdbtypes.c                           | 14 ++++++++++----
 gdb/testsuite/gdb.base/vla-datatypes.c   | 21 +++++++++++++++++++--
 gdb/testsuite/gdb.base/vla-datatypes.exp |  6 +++++-
 3 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index c9a9b3d..45dc141 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -2033,8 +2033,10 @@ resolve_dynamic_union (struct type *type,
       t = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
 					 addr_stack, 0);
       TYPE_FIELD_TYPE (resolved_type, i) = t;
-      if (TYPE_LENGTH (t) > max_len)
-	max_len = TYPE_LENGTH (t);
+
+      struct type *real_type = check_typedef (t);
+      if (TYPE_LENGTH (real_type) > max_len)
+	max_len = TYPE_LENGTH (real_type);
     }
 
   TYPE_LENGTH (resolved_type) = max_len;
@@ -2100,8 +2102,12 @@ resolve_dynamic_struct (struct type *type,
       if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
 	new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
       else
-	new_bit_length += (TYPE_LENGTH (TYPE_FIELD_TYPE (resolved_type, i))
-			   * TARGET_CHAR_BIT);
+	{
+	  struct type *real_type
+	    = check_typedef (TYPE_FIELD_TYPE (resolved_type, i));
+
+	  new_bit_length += (TYPE_LENGTH (real_type) * TARGET_CHAR_BIT);
+	}
 
       /* Normally, we would use the position and size of the last field
 	 to determine the size of the enclosing structure.  But GCC seems
diff --git a/gdb/testsuite/gdb.base/vla-datatypes.c b/gdb/testsuite/gdb.base/vla-datatypes.c
index 4902282..e7aee85 100644
--- a/gdb/testsuite/gdb.base/vla-datatypes.c
+++ b/gdb/testsuite/gdb.base/vla-datatypes.c
@@ -46,11 +46,12 @@ vla_factory (int n)
   BAR             bar_vla[n];
   int i;
 
-  struct vla_struct
+  typedef struct vla_struct
   {
     int something;
     int vla_field[n];
-  } vla_struct_object;
+  } vla_s;
+  vla_s vla_struct_object;
 
   struct inner_vla_struct
   {
@@ -59,14 +60,28 @@ vla_factory (int n)
     int after;
   } inner_vla_struct_object;
 
+  struct vla_typedef
+  {
+    int something;
+    vla_s vla_object;
+  } vla_typedef_struct_object;
+
   union vla_union
   {
     int vla_field[n];
   } vla_union_object;
 
+  union vla_typedef_union
+  {
+    int something;
+    vla_s vla_object;
+  } vla_typedef_union_object;
+
   vla_struct_object.something = n;
   inner_vla_struct_object.something = n;
   inner_vla_struct_object.after = n;
+  vla_typedef_struct_object.something = n * 2;
+  vla_typedef_struct_object.vla_object.something = n * 3;
   for (i = 0; i < n; i++)
     {
       int_vla[i] = i*2;
@@ -85,6 +100,8 @@ vla_factory (int n)
       vla_struct_object.vla_field[i] = i*2;
       vla_union_object.vla_field[i] = i*2;
       inner_vla_struct_object.vla_field[i] = i*2;
+      vla_typedef_struct_object.vla_object.vla_field[i] = i * 3;
+      vla_typedef_union_object.vla_object.vla_field[i] = i * 3 - 1;
     }
 
   size_t int_size        = sizeof(int_vla);     /* vlas_filled */
diff --git a/gdb/testsuite/gdb.base/vla-datatypes.exp b/gdb/testsuite/gdb.base/vla-datatypes.exp
index d32ed5a..93997ed 100644
--- a/gdb/testsuite/gdb.base/vla-datatypes.exp
+++ b/gdb/testsuite/gdb.base/vla-datatypes.exp
@@ -57,6 +57,10 @@ gdb_test "print vla_struct_object" \
     "\\\{something = 5, vla_field = \\\{0, 2, 4, 6, 8\\\}\\\}"
 gdb_test "print vla_union_object" \
     "\\\{vla_field = \\\{0, 2, 4, 6, 8\\\}\\\}"
+gdb_test "print vla_typedef_struct_object" \
+    "\\\{something = 10, vla_object = \\\{something = 15, vla_field = \\\{0, 3, 6, 9, 12\\\}\\\}\\\}"
+gdb_test "print vla_typedef_union_object" \
+    "\\\{something = 6, vla_object = \\\{something = 6, vla_field = \\\{-1, 2, 5, 8, 11\\\}\\\}\\\}"
 
 # Check whatis of VLA's.
 gdb_test "whatis int_vla" "type = int \\\[5\\\]" "whatis int_vla"
@@ -78,7 +82,7 @@ gdb_test "whatis unsigned_char_vla" "type = unsigned char \\\[5\\\]" \
          "whatis unsigned_char_vla"
 gdb_test "whatis foo_vla" "type = struct foo \\\[5\\\]" "whatis foo_vla"
 gdb_test "whatis bar_vla" "type = BAR \\\[5\\\]" "whatis bar_vla"
-gdb_test "whatis vla_struct_object" "type = struct vla_struct"
+gdb_test "whatis vla_struct_object" "type = vla_s"
 gdb_test "whatis vla_union_object" "type = union vla_union"
 
 # Check ptype of VLA's.
-- 
2.1.0

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

end of thread, other threads:[~2020-06-11 12:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-10 11:35 [PATCH] Compute proper length for dynamic types of TYPE_CODE_TYPEDEF Tom de Vries
2020-06-11 10:46 ` Pedro Alves
2020-06-11 12:38   ` Tom de Vries
  -- strict thread matches above, loose matches on Subject: below --
2017-06-24  0:59 Keith Seitz
2017-06-26 11:01 ` Pedro Alves

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