public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 3/8] Add truncation mode to gdb_mpz
Date: Fri,  3 Mar 2023 14:12:02 -0700	[thread overview]
Message-ID: <20230303211207.1053037-4-tromey@adacore.com> (raw)
In-Reply-To: <20230303211207.1053037-1-tromey@adacore.com>

This renames gdb_mpz::safe_export to export_bits, and adds a new flag
to export a truncated value.  This is needed by value arithmetic.
---
 gdb/gmp-utils.c | 93 ++++++++++++++++++++++++++++---------------------
 gdb/gmp-utils.h | 29 +++++++++++----
 2 files changed, 76 insertions(+), 46 deletions(-)

diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c
index 57f5f9766b9..0afa344781b 100644
--- a/gdb/gmp-utils.c
+++ b/gdb/gmp-utils.c
@@ -66,18 +66,8 @@ gdb_mpz::read (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order,
 /* See gmp-utils.h.  */
 
 void
-gdb_mpz::write (gdb::array_view<gdb_byte> buf, enum bfd_endian byte_order,
-		bool unsigned_p) const
-{
-  this->safe_export
-    (buf, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */, unsigned_p);
-}
-
-/* See gmp-utils.h.  */
-
-void
-gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
-		      int endian, bool unsigned_p) const
+gdb_mpz::export_bits (gdb::array_view<gdb_byte> buf, int endian, bool unsigned_p,
+		      bool safe) const
 {
   gdb_assert (buf.size () > 0);
 
@@ -92,36 +82,39 @@ gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
       return;
     }
 
-  /* Determine the maximum range of values that our buffer can hold,
-     and verify that VAL is within that range.  */
-
-  gdb_mpz lo, hi;
-  const size_t max_usable_bits = buf.size () * HOST_CHAR_BIT;
-  if (unsigned_p)
-    {
-      lo = 0;
-
-      mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits);
-      mpz_sub_ui (hi.m_val, hi.m_val, 1);
-    }
-  else
+  if (safe)
     {
-      mpz_ui_pow_ui (lo.m_val, 2, max_usable_bits - 1);
-      mpz_neg (lo.m_val, lo.m_val);
-
-      mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits - 1);
-      mpz_sub_ui (hi.m_val, hi.m_val, 1);
+      /* Determine the maximum range of values that our buffer can
+	 hold, and verify that VAL is within that range.  */
+
+      gdb_mpz lo, hi;
+      const size_t max_usable_bits = buf.size () * HOST_CHAR_BIT;
+      if (unsigned_p)
+	{
+	  lo = 0;
+
+	  mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits);
+	  mpz_sub_ui (hi.m_val, hi.m_val, 1);
+	}
+      else
+	{
+	  mpz_ui_pow_ui (lo.m_val, 2, max_usable_bits - 1);
+	  mpz_neg (lo.m_val, lo.m_val);
+
+	  mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits - 1);
+	  mpz_sub_ui (hi.m_val, hi.m_val, 1);
+	}
+
+      if (mpz_cmp (m_val, lo.m_val) < 0 || mpz_cmp (m_val, hi.m_val) > 0)
+	error (_("Cannot export value %s as %zu-bits %s integer"
+		 " (must be between %s and %s)"),
+	       this->str ().c_str (),
+	       max_usable_bits,
+	       unsigned_p ? _("unsigned") : _("signed"),
+	       lo.str ().c_str (),
+	       hi.str ().c_str ());
     }
 
-  if (mpz_cmp (m_val, lo.m_val) < 0 || mpz_cmp (m_val, hi.m_val) > 0)
-    error (_("Cannot export value %s as %zu-bits %s integer"
-	     " (must be between %s and %s)"),
-	   this->str ().c_str (),
-	   max_usable_bits,
-	   unsigned_p ? _("unsigned") : _("signed"),
-	   lo.str ().c_str (),
-	   hi.str ().c_str ());
-
   const gdb_mpz *exported_val = this;
   gdb_mpz un_signed;
   if (sign < 0)
@@ -134,6 +127,28 @@ gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
       exported_val = &un_signed;
     }
 
+  /* If the value is too large, truncate it.  */
+  if (!safe
+      && mpz_sizeinbase (exported_val->m_val, 2) > buf.size () * HOST_CHAR_BIT)
+    {
+      /* If we don't already have a copy, make it now.  */
+      if (exported_val != &un_signed)
+	{
+	  un_signed = *exported_val;
+	  exported_val = &un_signed;
+	}
+
+      un_signed.mask (buf.size () * HOST_CHAR_BIT);
+    }
+
+  /* It's possible that one of the above results in zero, which has to
+     be handled specially.  */
+  if (exported_val->sgn () == 0)
+    {
+      memset (buf.data (), 0, buf.size ());
+      return;
+    }
+
   /* Do the export into a buffer allocated by GMP itself; that way,
      we can detect cases where BUF is not large enough to export
      our value, and thus avoid a buffer overlow.  Normally, this should
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index d4e5015e345..d378499d287 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -137,7 +137,20 @@ struct gdb_mpz
 
      UNSIGNED_P indicates whether the number has an unsigned type.  */
   void write (gdb::array_view<gdb_byte> buf, enum bfd_endian byte_order,
-	      bool unsigned_p) const;
+	      bool unsigned_p) const
+  {
+    export_bits (buf, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */,
+		 unsigned_p, true /* safe */);
+  }
+
+  /* Like write, but truncates the value to the desired number of
+     bytes.  */
+  void truncate (gdb::array_view<gdb_byte> buf, enum bfd_endian byte_order,
+		 bool unsigned_p) const
+  {
+    export_bits (buf, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */,
+		 unsigned_p, false /* safe */);
+  }
 
   /* Return a string containing VAL.  */
   std::string str () const { return gmp_string_printf ("%Zd", m_val); }
@@ -337,10 +350,11 @@ struct gdb_mpz
 	   . -1 for least significant byte first; or
 	   . 0 for native endianness.
 
-    An error is raised if BUF is not large enough to contain the value
-    being exported.  */
-  void safe_export (gdb::array_view<gdb_byte> buf,
-		    int endian, bool unsigned_p) const;
+    If SAFE is true, an error is raised if BUF is not large enough to
+    contain the value being exported.  If SAFE is false, the value is
+    truncated to fit in BUF.  */
+  void export_bits (gdb::array_view<gdb_byte> buf, int endian, bool unsigned_p,
+		    bool safe) const;
 
   friend struct gdb_mpq;
   friend struct gdb_mpf;
@@ -590,9 +604,10 @@ gdb_mpz::as_integer () const
 {
   T result;
 
-  this->safe_export ({(gdb_byte *) &result, sizeof (result)},
+  this->export_bits ({(gdb_byte *) &result, sizeof (result)},
 		     0 /* endian (0 = native) */,
-		     !std::is_signed<T>::value /* unsigned_p */);
+		     !std::is_signed<T>::value /* unsigned_p */,
+		     true /* safe */);
 
   return result;
 }
-- 
2.39.1


  parent reply	other threads:[~2023-03-03 21:12 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-03 21:11 [PATCH 0/8] Arithmetic for 128-bit types Tom Tromey
2023-03-03 21:12 ` [PATCH 1/8] Add many operators to gdb_mpz Tom Tromey
2023-03-07 13:38   ` Alexandra Petlanova Hajkova
2023-03-03 21:12 ` [PATCH 2/8] Avoid a copy in gdb_mpz::safe_export Tom Tromey
2023-03-03 21:12 ` Tom Tromey [this message]
2023-03-03 21:12 ` [PATCH 4/8] Add value_as_mpz and value_from_mpz Tom Tromey
2023-03-08 10:47   ` Lancelot SIX
2023-03-08 15:48     ` Tom Tromey
2023-03-03 21:12 ` [PATCH 5/8] Simplify binop_promote Tom Tromey
2023-03-03 21:12 ` [PATCH 6/8] Use value_true in value_equal and value_less Tom Tromey
2023-03-03 21:12 ` [PATCH 7/8] Use gdb_gmp for scalar arithmetic Tom Tromey
2023-03-03 21:12 ` [PATCH 8/8] Fix 128-bit integer bug in Ada Tom Tromey
2023-03-04  7:04 ` [PATCH 0/8] Arithmetic for 128-bit types Eli Zaretskii
2023-03-08 16:17   ` Tom Tromey
2023-03-27 14:20 ` Tom Tromey

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=20230303211207.1053037-4-tromey@adacore.com \
    --to=tromey@adacore.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).