]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Handle "info symbol" in Rust language mode
authorTom Tromey <tom@tromey.com>
Wed, 27 Mar 2024 16:43:57 +0000 (10:43 -0600)
committerTom Tromey <tom@tromey.com>
Thu, 20 Jun 2024 16:45:05 +0000 (10:45 -0600)
When I changed the Rust parser to handle 128-bit ints, this
inadvertently broke some other gdb commands.  For example, "info
symbol 0xffffffffffffffff" now fails, because the resulting value is
128 bits, but this is rejected by extract_integer.

This patch fixes the problem by changing extract_integer to allow
over-long integers as long as the high bytes are either 0, or (for
signed types) 0xff.

Regression tested on x86-64 Fedora 38.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31565
Approved-By: Andrew Burgess <aburgess@redhat.com>
gdb/extract-store-integer.c
gdb/extract-store-integer.h
gdb/testsuite/gdb.rust/simple.exp

index b2892e4084d6894291391488e283ecdfaf9033c3..644273c96392c926b3f8f42f5a112bc0efb85368 100644 (file)
@@ -26,9 +26,61 @@ extract_integer (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order
 {
   typename std::make_unsigned<T>::type retval = 0;
 
+  /* It is ok if BUF is wider than T, but only if the value is
+     representable.  */
+  bool bad_repr = false;
   if (buf.size () > (int) sizeof (T))
-    error (_("\
-That operation is not available on integers of more than %d bytes."),
+    {
+      const size_t end = buf.size () - sizeof (T);
+      if (byte_order == BFD_ENDIAN_BIG)
+       {
+         for (size_t i = 0; i < end; ++i)
+           {
+             /* High bytes == 0 are always ok, and high bytes == 0xff
+                are ok when the type is signed.  */
+             if ((buf[i] == 0
+                  || (std::is_signed<T>::value && buf[i] == 0xff))
+                 /* All the high bytes must be the same, no
+                    alternating 0 and 0xff.  */
+                 && (i == 0 || buf[i - 1] == buf[i]))
+               {
+                 /* Ok.  */
+               }
+             else
+               {
+                 bad_repr = true;
+                 break;
+               }
+           }
+         buf = buf.slice (end);
+       }
+      else
+       {
+         size_t bufsz = buf.size () - 1;
+         for (size_t i = bufsz; i >= end; --i)
+           {
+             /* High bytes == 0 are always ok, and high bytes == 0xff
+                are ok when the type is signed.  */
+             if ((buf[i] == 0
+                  || (std::is_signed<T>::value && buf[i] == 0xff))
+                 /* All the high bytes must be the same, no
+                    alternating 0 and 0xff.  */
+                 && (i == bufsz || buf[i] == buf[i + 1]))
+               {
+                 /* Ok.  */
+               }
+             else
+               {
+                 bad_repr = true;
+                 break;
+               }
+           }
+         buf = buf.slice (0, end);
+       }
+    }
+
+  if (bad_repr)
+    error (_("Value cannot be represented as integer of %d bytes."),
           (int) sizeof (T));
 
   /* Start at the most significant end of the integer, and work towards
@@ -240,6 +292,47 @@ copy_integer_to_size_test ()
   do_cint_test (0xff2112345678, 0xffffff2112345678, 8, 0xffffff2112345678, 6);
 }
 
+template<typename T>
+void
+do_extract_test (gdb_byte byte1, gdb_byte byte2, enum bfd_endian endian,
+                std::optional<T> expected)
+{
+  std::optional<T> result;
+
+  try
+    {
+      const gdb_byte val[2] = { byte1, byte2 };
+      result = extract_integer<T> (gdb::make_array_view (val, 2), endian);
+    }
+  catch (const gdb_exception_error &)
+    {
+    }
+
+  SELF_CHECK (result == expected);
+}
+
+template<typename T>
+void
+do_extract_tests (gdb_byte low, gdb_byte high, std::optional<T> expected)
+{
+  do_extract_test (low, high, BFD_ENDIAN_LITTLE, expected);
+  do_extract_test (high, low, BFD_ENDIAN_BIG, expected);
+}
+
+static void
+extract_integer_test ()
+{
+  do_extract_tests<uint8_t> (0x00, 0xff, {});
+  do_extract_tests<uint8_t> (0x7f, 0x23, {});
+  do_extract_tests<uint8_t> (0x80, 0xff, {});
+  do_extract_tests<uint8_t> (0x00, 0x00, 0x00);
+
+  do_extract_tests<int8_t> (0xff, 0x00, 0xff);
+  do_extract_tests<int8_t> (0x7f, 0x23, {});
+  do_extract_tests<int8_t> (0x80, 0xff, 0x80);
+  do_extract_tests<int8_t> (0x00, 0x00, 0x00);
+}
+
 } // namespace selftests
 
 #endif
@@ -251,5 +344,7 @@ _initialize_extract_store_integer ()
 #if GDB_SELF_TEST
   selftests::register_test ("copy_integer_to_size",
                            selftests::copy_integer_to_size_test);
+  selftests::register_test ("extract_integer",
+                           selftests::extract_integer_test);
 #endif
 }
index b2c0f35f5c10525f49db1053561b1e32e995322e..a51ef3d09429de6e15c3186bcf43471f9ff33a0d 100644 (file)
@@ -18,9 +18,9 @@
 #ifndef GDB_EXTRACT_STORE_INTEGER_H
 #define GDB_EXTRACT_STORE_INTEGER_H
 
-#include "gdbsupport/traits.h"
+#include <type_traits>
 
-template<typename T, typename = RequireLongest<T>>
+template<typename T, typename = std::is_integral<T>>
 T extract_integer (gdb::array_view<const gdb_byte>, enum bfd_endian byte_order);
 
 static inline LONGEST
index 1e6fc94400e10ec113216e467771657ddcf0929f..37a2e079b6c4e3577ca29d5ec0b48a4e6d9263a3 100644 (file)
@@ -421,3 +421,6 @@ gdb_test "print 4 - 3 - 1" " = 0"
 
 # Another operator precedence bug.
 gdb_test "print \$one = \$two = 75" " = \\\(\\\)"
+
+gdb_test "info symbol 0xffffffffffffffff" \
+    "No symbol matches 0xffffffffffffffff."