public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH] Fix crash with DW_FORM_implicit_const
Date: Thu, 20 Jul 2023 16:11:18 -0600	[thread overview]
Message-ID: <20230720221118.1966110-1-tom@tromey.com> (raw)

Jakub pointed out that using DW_FORM_implicit_const with
DW_AT_bit_size would cause gdb to crash.  This happened because
DW_FORM_implicit_const is not an "unsigned" form, causing as_unsigned
to assert.  This patch fixes the problem.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30651
---
 gdb/dwarf2/read.c                    | 29 ++++++++++++++++------------
 gdb/testsuite/gdb.dwarf2/intbits.exp |  2 +-
 gdb/testsuite/lib/dwarf.exp          |  4 ++++
 3 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 3508f2c29ee..b1f1c1d7257 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -15420,20 +15420,25 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
   if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
     {
       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
-      if (attr != nullptr && attr->as_unsigned () <= 8 * type->length ())
+      if (attr != nullptr && attr->form_is_constant ())
 	{
-	  unsigned real_bit_size = attr->as_unsigned ();
-	  attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
-	  /* Only use the attributes if they make sense together.  */
-	  if (attr == nullptr
-	      || (attr->as_unsigned () + real_bit_size
-		  <= 8 * type->length ()))
+	  unsigned real_bit_size = attr->constant_value (0);
+	  if (real_bit_size >= 0 && real_bit_size <= 8 * type->length ())
 	    {
-	      TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
-		= real_bit_size;
-	      if (attr != nullptr)
-		TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
-		  = attr->as_unsigned ();
+	      attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
+	      /* Only use the attributes if they make sense together.  */
+	      if (attr == nullptr
+		  || (attr->form_is_constant ()
+		      && attr->constant_value (0) >= 0
+		      && (attr->constant_value (0) + real_bit_size
+			  <= 8 * type->length ())))
+		{
+		  TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
+		    = real_bit_size;
+		  if (attr != nullptr)
+		    TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
+		      = attr->constant_value (0);
+		}
 	    }
 	}
     }
diff --git a/gdb/testsuite/gdb.dwarf2/intbits.exp b/gdb/testsuite/gdb.dwarf2/intbits.exp
index c52e709316b..cf44d26b721 100644
--- a/gdb/testsuite/gdb.dwarf2/intbits.exp
+++ b/gdb/testsuite/gdb.dwarf2/intbits.exp
@@ -43,7 +43,7 @@ Dwarf::assemble ${asm_file} {
 		{DW_AT_endianity @DW_END_little}
 		{DW_AT_name "i7"}
 		{DW_AT_byte_size 2 DW_FORM_udata}
-		{DW_AT_bit_size 7 DW_FORM_udata}
+		{DW_AT_bit_size 7 DW_FORM_implicit_const}
 	    }
 
 	    DW_TAG_variable {
diff --git a/gdb/testsuite/lib/dwarf.exp b/gdb/testsuite/lib/dwarf.exp
index 434495df24a..f09da0430ab 100644
--- a/gdb/testsuite/lib/dwarf.exp
+++ b/gdb/testsuite/lib/dwarf.exp
@@ -693,6 +693,7 @@ namespace eval Dwarf {
 		_op .ascii [_quote $value]
 	    }
 
+	    DW_FORM_implicit_const -
 	    DW_FORM_flag_present {
 		# We don't need to emit anything.
 	    }
@@ -917,6 +918,9 @@ namespace eval Dwarf {
 	    }
 	    _op .uleb128 $_constants($attr_name) $attr_name
 	    _op .uleb128 $_constants($attr_form) $attr_form_comment
+	    if {$attr_form eq "DW_FORM_implicit_const"} {
+		_op .sleb128 $attr_value "the constant"
+	    }
 	}
     }
 
-- 
2.41.0


             reply	other threads:[~2023-07-20 22:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-20 22:11 Tom Tromey [this message]
2023-07-21  8:40 ` Andrew Burgess

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=20230720221118.1966110-1-tom@tromey.com \
    --to=tom@tromey.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).