public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Allow cast of 128-bit integer to pointer
@ 2023-11-25  2:25 Tom Tromey
  2023-12-08 15:15 ` Tom Tromey
  0 siblings, 1 reply; 2+ messages in thread
From: Tom Tromey @ 2023-11-25  2:25 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

PR rust/31082 points out that casting a 128-bit integer to a pointer
will fail.  This happens because a case in value_cast was not
converted to use GMP.

This patch fixes the problem.  I am not really sure that testing
against the negative value here makes sense, but I opted to just
preserve the existing behavior rather than change it.

Regression tested on x86-64 Fedora 38.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31082
---
 gdb/gmp-utils.h                 | 15 ++++++++++++++-
 gdb/testsuite/gdb.rust/expr.exp |  5 +++++
 gdb/valops.c                    | 14 ++++++--------
 3 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index a5c27feb59f..52052e30d26 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -251,19 +251,32 @@ struct gdb_mpz
     return result;
   }
 
+  gdb_mpz operator- () const
+  {
+    gdb_mpz result;
+    mpz_neg (result.m_val, m_val);
+    return result;
+  }
+
   gdb_mpz &operator<<= (unsigned long nbits)
   {
     mpz_mul_2exp (m_val, m_val, nbits);
     return *this;
   }
 
-  gdb_mpz operator<< (unsigned long nbits) const
+  gdb_mpz operator<< (unsigned long nbits) const &
   {
     gdb_mpz result;
     mpz_mul_2exp (result.m_val, m_val, nbits);
     return result;
   }
 
+  gdb_mpz operator<< (unsigned long nbits) &&
+  {
+    mpz_mul_2exp (m_val, m_val, nbits);
+    return *this;
+  }
+
   gdb_mpz operator>> (unsigned long nbits) const
   {
     gdb_mpz result;
diff --git a/gdb/testsuite/gdb.rust/expr.exp b/gdb/testsuite/gdb.rust/expr.exp
index ce2cce2677c..cec3c1fccd8 100644
--- a/gdb/testsuite/gdb.rust/expr.exp
+++ b/gdb/testsuite/gdb.rust/expr.exp
@@ -169,3 +169,8 @@ gdb_test "with debug expression 1 -- print \"foo\"" \
 	 " Constant: 0" \
 	 "evaluation of this expression requires the target program to be active"] \
     "print a string with expression debug turned on"
+
+# PR rust/31082 - truncating to a pointer would fail.  Depending on
+# the default host architecture, this may or may not print a warning.
+gdb_test "print (0xffffffd00000009a as *mut u64)" \
+    "(warning: value truncated\[\r\n\]+)?.* = \\(\\*mut u64\\) $hex"
diff --git a/gdb/valops.c b/gdb/valops.c
index a8760ccf3e4..bf376921ca3 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -603,15 +603,13 @@ value_cast (struct type *type, struct value *arg2)
 	 pointers and four byte addresses.  */
 
       int addr_bit = gdbarch_addr_bit (type2->arch ());
-      LONGEST longest = value_as_long (arg2);
+      gdb_mpz longest = value_as_mpz (arg2);
 
-      if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
-	{
-	  if (longest >= ((LONGEST) 1 << addr_bit)
-	      || longest <= -((LONGEST) 1 << addr_bit))
-	    warning (_("value truncated"));
-	}
-      return value_from_longest (to_type, longest);
+      gdb_mpz addr_val = gdb_mpz (1) << addr_bit;
+      if (longest >= addr_val || longest <= -addr_val)
+	warning (_("value truncated"));
+
+      return value_from_mpz (to_type, longest);
     }
   else if (code1 == TYPE_CODE_METHODPTR && code2 == TYPE_CODE_INT
 	   && value_as_long (arg2) == 0)
-- 
2.41.0


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

* Re: [PATCH] Allow cast of 128-bit integer to pointer
  2023-11-25  2:25 [PATCH] Allow cast of 128-bit integer to pointer Tom Tromey
@ 2023-12-08 15:15 ` Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2023-12-08 15:15 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

Tom> PR rust/31082 points out that casting a 128-bit integer to a pointer
Tom> will fail.  This happens because a case in value_cast was not
Tom> converted to use GMP.

Tom> This patch fixes the problem.  I am not really sure that testing
Tom> against the negative value here makes sense, but I opted to just
Tom> preserve the existing behavior rather than change it.

I'm checking this in.

Tom

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

end of thread, other threads:[~2023-12-08 15:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-25  2:25 [PATCH] Allow cast of 128-bit integer to pointer Tom Tromey
2023-12-08 15:15 ` Tom Tromey

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