]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
struct packed: add fallback byte array implementation, use gcc_struct on Windows... users/palves/packed
authorPedro Alves <pedro@palves.net>
Mon, 18 Jul 2022 23:26:33 +0000 (00:26 +0100)
committerPedro Alves <pedro@palves.net>
Tue, 19 Jul 2022 14:44:07 +0000 (15:44 +0100)
https://sourceware.org/bugzilla/show_bug.cgi?id=29373

- Windows hosts need attribute gcc_struct.

- attribute gcc_struct seemingly doesn't work with Clang, so add a
  fallback standard-conforming implementation based on arrays.

- add unit tests to make sure both implementations behave the same
  way.

- add more relational operators while at it.

Change-Id: I023315ee03622c59c397bf4affc0b68179c32374

gdb/Makefile.in
gdb/unittests/packed-selftests.c [new file with mode: 0644]
gdbsupport/packed.h

index 911daa2607bbc23bd722b3256def17a477f547c2..b0daa649bd1d30e2310fc945aa14c9997c4fa3ff 100644 (file)
@@ -464,6 +464,7 @@ SELFTESTS_SRCS = \
        unittests/offset-type-selftests.c \
        unittests/observable-selftests.c \
        unittests/optional-selftests.c \
+       unittests/packed-selftests.c \
        unittests/parallel-for-selftests.c \
        unittests/parse-connection-spec-selftests.c \
        unittests/path-join-selftests.c \
diff --git a/gdb/unittests/packed-selftests.c b/gdb/unittests/packed-selftests.c
new file mode 100644 (file)
index 0000000..172d601
--- /dev/null
@@ -0,0 +1,125 @@
+/* Self tests for packed for GDB, the GNU debugger.
+
+   Copyright (C) 2022 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "gdbsupport/selftest.h"
+#include "gdbsupport/packed.h"
+#include <atomic>
+
+namespace selftests {
+namespace packed_tests {
+
+gdb_static_assert (sizeof (packed<unsigned int, 1>) == 1);
+gdb_static_assert (sizeof (packed<unsigned int, 2>) == 2);
+gdb_static_assert (sizeof (packed<unsigned int, 3>) == 3);
+gdb_static_assert (sizeof (packed<unsigned int, 4>) == 4);
+
+gdb_static_assert (alignof (packed<unsigned int, 1>) == 1);
+gdb_static_assert (alignof (packed<unsigned int, 2>) == 1);
+gdb_static_assert (alignof (packed<unsigned int, 3>) == 1);
+gdb_static_assert (alignof (packed<unsigned int, 4>) == 1);
+
+/* Triviality checks.  */
+#define CHECK_TRAIT(TRAIT)                     \
+  static_assert (std::TRAIT<packed<unsigned int, 1>>::value, "")
+
+#if HAVE_IS_TRIVIALLY_COPYABLE
+
+CHECK_TRAIT (is_trivially_copyable);
+CHECK_TRAIT (is_trivially_copy_constructible);
+CHECK_TRAIT (is_trivially_move_constructible);
+CHECK_TRAIT (is_trivially_copy_assignable);
+CHECK_TRAIT (is_trivially_move_assignable);
+
+#endif
+
+#undef CHECK_TRAIT
+
+/* Entry point.  */
+
+static void
+run_tests ()
+{
+  typedef packed<unsigned int, 2> packed_2;
+
+  packed_2 p1;
+  packed_2 p2 (0x0102);
+  p1 = 0x0102;
+
+  SELF_CHECK (p1 == p1);
+  SELF_CHECK (p1 == p2);
+  SELF_CHECK (p1 == 0x0102);
+  SELF_CHECK (0x0102 == p1);
+
+  SELF_CHECK (p1 != 0);
+  SELF_CHECK (0 != p1);
+
+  SELF_CHECK (p1 != 0x0103);
+  SELF_CHECK (0x0103 != p1);
+
+  SELF_CHECK (p1 != 0x01020102);
+  SELF_CHECK (0x01020102 != p1);
+
+  SELF_CHECK (p1 != 0x01020000);
+  SELF_CHECK (0x01020000 != p1);
+
+  /* Check truncation.  */
+  p1 = 0x030102;
+  SELF_CHECK (p1 == 0x0102);
+  SELF_CHECK (p1 != 0x030102);
+
+  /* Check that the custom std::atomic/packed/T relational operators
+     work as intended.  No need for fully comprehensive tests, as all
+     operators are defined in the same way, via a macro.  We just want
+     to make sure that we can (non-atomically) compare atomic-wrapped
+     packed, with packed, and with the packed underlying type.  */
+
+  std::atomic<packed<unsigned int, 2>> atomic_packed_2 (0x0102);
+
+  SELF_CHECK (atomic_packed_2 == atomic_packed_2);
+  SELF_CHECK (atomic_packed_2 == p1);
+  SELF_CHECK (p1 == atomic_packed_2);
+  SELF_CHECK (atomic_packed_2 == 0x0102u);
+  SELF_CHECK (0x0102u == atomic_packed_2);
+
+  SELF_CHECK (atomic_packed_2 >= 0x0102u);
+  SELF_CHECK (atomic_packed_2 <= 0x0102u);
+  SELF_CHECK (atomic_packed_2 > 0u);
+  SELF_CHECK (atomic_packed_2 < 0x0103u);
+  SELF_CHECK (atomic_packed_2 >= 0u);
+  SELF_CHECK (atomic_packed_2 <= 0x0102u);
+  SELF_CHECK (!(atomic_packed_2 > 0x0102u));
+  SELF_CHECK (!(atomic_packed_2 < 0x0102u));
+
+  /* Check std::atomic<packed> truncation behaves the same as without
+     std::atomic.  */
+  atomic_packed_2 = 0x030102;
+  SELF_CHECK (atomic_packed_2 == 0x0102u);
+  SELF_CHECK (atomic_packed_2 != 0x030102u);
+}
+
+} /* namespace packed_tests */
+} /* namespace selftests */
+
+void _initialize_packed_selftests ();
+void
+_initialize_packed_selftests ()
+{
+  selftests::register_test ("packed", selftests::packed_tests::run_tests);
+}
index 3468cf442070002095960fb9ab1ee92807c07f2a..d65e0bc33f077651f597669878a1a5d44138dfc1 100644 (file)
    bit-fields (and ENUM_BITFIELD), when the fields must have separate
    memory locations to avoid data races.  */
 
+/* There are two implementations here -- one standard compliant, using
+   a byte array for internal representation, and another that relies on
+   bitfields and attribute packed (and attribute gcc_struct on
+   Windows).  The latter is preferable, as it is more convenient to
+   debug -- an enum wrapped in struct packed is printed by GDB as an
+   enum -- but may not work on all compilers.  */
+
+/* Clang does not support attribute gcc_struct.  */
+#if defined _WIN32 && defined __clang__
+# define PACKED_USE_ARRAY 1
+#else
+# define PACKED_USE_ARRAY 0
+#endif
+
+#if !PACKED_USE_ARRAY && defined _WIN32
+# define ATTRIBUTE_GCC_STRUCT  __attribute__((__gcc_struct__))
+#else
+# define ATTRIBUTE_GCC_STRUCT
+#endif
+
 template<typename T, size_t Bytes = sizeof (T)>
 struct packed
 {
@@ -35,7 +55,18 @@ public:
 
   packed (T val)
   {
+    gdb_static_assert (sizeof (size_t) >= sizeof (T));
+
+#if PACKED_USE_ARRAY
+    size_t tmp = val;
+    for (int i = (Bytes - 1); i >= 0; --i)
+      {
+       m_bytes[i] = tmp & 0xff;
+       tmp >>= 8;
+      }
+#else
     m_val = val;
+#endif
 
     /* Ensure size and aligment are what we expect.  */
     gdb_static_assert (sizeof (packed) == Bytes);
@@ -53,44 +84,78 @@ public:
 
   operator T () const noexcept
   {
+#if PACKED_USE_ARRAY
+    size_t tmp = 0;
+    for (int i = 0;;)
+      {
+       tmp |= m_bytes[i];
+       if (++i == Bytes)
+         break;
+       tmp <<= 8;
+      }
+    return (T) tmp;
+#else
     return m_val;
+#endif
   }
 
 private:
+#if PACKED_USE_ARRAY
+  gdb_byte m_bytes[Bytes];
+#else
   T m_val : (Bytes * HOST_CHAR_BIT) ATTRIBUTE_PACKED;
-};
-
-/* Add some comparisons between std::atomic<packed<T>> and T.  We need
-   this because the regular comparisons would require two implicit
-   conversions to go from T to std::atomic<packed<T>>:
-
-     T         -> packed<T>
-     packed<T> -> std::atomic<packed<T>>
-
-   and C++ only does one.  */
-
-template<typename T, size_t Bytes>
-bool operator== (T lhs, const std::atomic<packed<T, Bytes>> &rhs)
-{
-  return lhs == rhs.load ();
-}
-
-template<typename T, size_t Bytes>
-bool operator== (const std::atomic<packed<T, Bytes>> &lhs, T rhs)
-{
-  return lhs.load () == rhs;
-}
+#endif
+} ATTRIBUTE_GCC_STRUCT;
+
+/* Add some (non-atomic) comparisons between std::atomic<packed<T>>
+   and packed<T> and T.  We need this because even though
+   std::atomic<T> doesn't define these operators, the relational
+   expressions still work via implicit conversions.  Thos wouldn't
+   work when wrapped in packed without these operators, because we'd
+   require two implicit conversions to go from T to packed<T> to
+   std::atomic<packed<T>> (and back), and C++ only does one.  */
+
+#define PACKED_ATOMIC_OP(OP)                                           \
+  template<typename T, size_t Bytes>                                   \
+  bool operator OP (const std::atomic<packed<T, Bytes>> &lhs,          \
+                   const std::atomic<packed<T, Bytes>> &rhs)           \
+  {                                                                    \
+    return lhs.load () OP rhs.load ();                                 \
+  }                                                                    \
+                                                                       \
+  template<typename T, size_t Bytes>                                   \
+  bool operator OP (T lhs, const std::atomic<packed<T, Bytes>> &rhs)   \
+  {                                                                    \
+    return lhs OP rhs.load ();                                         \
+  }                                                                    \
+                                                                       \
+  template<typename T, size_t Bytes>                                   \
+  bool operator OP (const std::atomic<packed<T, Bytes>> &lhs, T rhs)   \
+  {                                                                    \
+    return lhs.load () OP rhs;                                         \
+  }                                                                    \
+                                                                       \
+  template<typename T, size_t Bytes>                                   \
+  bool operator OP (const std::atomic<packed<T, Bytes>> &lhs,          \
+                   packed<T, Bytes> rhs)                               \
+  {                                                                    \
+    return lhs.load () OP rhs;                                         \
+  }                                                                    \
+                                                                       \
+  template<typename T, size_t Bytes>                                   \
+  bool operator OP (packed<T, Bytes> lhs,                              \
+                   const std::atomic<packed<T, Bytes>> &rhs)           \
+  {                                                                    \
+    return lhs OP rhs.load ();                                         \
+  }
 
-template<typename T, size_t Bytes>
-bool operator!= (T lhs, const std::atomic<packed<T, Bytes>> &rhs)
-{
-  return !(lhs == rhs);
-}
+PACKED_ATOMIC_OP (==)
+PACKED_ATOMIC_OP (!=)
+PACKED_ATOMIC_OP (>)
+PACKED_ATOMIC_OP (<)
+PACKED_ATOMIC_OP (>=)
+PACKED_ATOMIC_OP (<=)
 
-template<typename T, size_t Bytes>
-bool operator!= (const std::atomic<packed<T, Bytes>> &lhs, T rhs)
-{
-  return !(lhs == rhs);
-}
+#undef PACKED_ATOMIC_OP
 
 #endif