diff mbox series

[2/3] nptl: Change tst-typesizes to _Static_assert

Message ID 1508186653-24430-2-git-send-email-adhemerval.zanella@linaro.org
State Superseded
Headers show
Series [1/3] Define __PTHREAD_MUTEX_HAVE_PREV for all architectures [BZ #22298] | expand

Commit Message

Adhemerval Zanella Oct. 16, 2017, 8:44 p.m. UTC
Instead of rely on runtime check to assure correct pthread types
size a better strategy would use _Static_assert to trigger an error
on build time (and thus allowing to check to potentially ABI breakage
on cross-compiling make check).

Checked on x86_64-linux-gnu and with a build check for all affected
ABIs (aarch64-linux-gnu, alpha-linux-gnu, arm-linux-gnueabihf,
hppa-linux-gnu, i686-linux-gnu, ia64-linux-gnu, m68k-linux-gnu,
microblaze-linux-gnu, mips64-linux-gnu, mips64-n32-linux-gnu,
mips-linux-gnu, powerpc64le-linux-gnu, powerpc-linux-gnu,
s390-linux-gnu, s390x-linux-gnu, sh4-linux-gnu, sparc64-linux-gnu,
sparcv9-linux-gnu, tilegx-linux-gnu, tilegx-linux-gnu-x32,
tilepro-linux-gnu, x86_64-linux-gnu, and x86_64-linux-x32).

	* nptl/tst-typesizes.c: Make sizes check _Static_asserts at
	built time.
---
 ChangeLog            |  3 ++
 nptl/tst-typesizes.c | 77 +++++++++++++---------------------------------------
 2 files changed, 22 insertions(+), 58 deletions(-)

-- 
2.7.4

Comments

H.J. Lu Nov. 6, 2017, 2:12 p.m. UTC | #1
On Mon, Oct 16, 2017 at 1:44 PM, Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
> Instead of rely on runtime check to assure correct pthread types

> size a better strategy would use _Static_assert to trigger an error

> on build time (and thus allowing to check to potentially ABI breakage

> on cross-compiling make check).

>

> Checked on x86_64-linux-gnu and with a build check for all affected

> ABIs (aarch64-linux-gnu, alpha-linux-gnu, arm-linux-gnueabihf,

> hppa-linux-gnu, i686-linux-gnu, ia64-linux-gnu, m68k-linux-gnu,

> microblaze-linux-gnu, mips64-linux-gnu, mips64-n32-linux-gnu,

> mips-linux-gnu, powerpc64le-linux-gnu, powerpc-linux-gnu,

> s390-linux-gnu, s390x-linux-gnu, sh4-linux-gnu, sparc64-linux-gnu,

> sparcv9-linux-gnu, tilegx-linux-gnu, tilegx-linux-gnu-x32,

> tilepro-linux-gnu, x86_64-linux-gnu, and x86_64-linux-x32).

>

>         * nptl/tst-typesizes.c: Make sizes check _Static_asserts at

>         built time.

> ---

>  ChangeLog            |  3 ++

>  nptl/tst-typesizes.c | 77 +++++++++++++---------------------------------------

>  2 files changed, 22 insertions(+), 58 deletions(-)

>

> diff --git a/nptl/tst-typesizes.c b/nptl/tst-typesizes.c

> index 78ed773..3355614 100644

> --- a/nptl/tst-typesizes.c

> +++ b/nptl/tst-typesizes.c

> @@ -20,56 +20,26 @@

>  #include <pthreadP.h>

>  #include <semaphore.h>

>

> -static const struct

> -{

> -  const char *name;

> -  size_t expected;

> -  size_t is;

> -} types[] =

> -  {

> -#define T(t, c) \

> -    { #t, c, sizeof (t) }

> -    T (pthread_attr_t, __SIZEOF_PTHREAD_ATTR_T),

> -    T (pthread_mutex_t, __SIZEOF_PTHREAD_MUTEX_T),

> -    T (pthread_mutexattr_t, __SIZEOF_PTHREAD_MUTEXATTR_T),

> -    T (pthread_cond_t, __SIZEOF_PTHREAD_COND_T),

> -    T (pthread_condattr_t, __SIZEOF_PTHREAD_CONDATTR_T),

> -    T (pthread_rwlock_t, __SIZEOF_PTHREAD_RWLOCK_T),

> -    T (pthread_rwlockattr_t, __SIZEOF_PTHREAD_RWLOCKATTR_T),

> -    T (pthread_barrier_t, __SIZEOF_PTHREAD_BARRIER_T),

> -    T (pthread_barrierattr_t, __SIZEOF_PTHREAD_BARRIERATTR_T)

> -  };

> -

>  static int

>  do_test (void)

>  {

> -  int result = 0;

> -

> -#define TEST_TYPE(name) \

> -  printf ("%s: ", #name);                                                    \

> -  if (sizeof (name) != sizeof (((name *) 0)->__size))                        \

> -    {                                                                        \

> -      printf ("expected %zu, is %zu\n",                                              \

> -             sizeof (((name *) 0)->__size), sizeof (name));                  \

> -      result = 1;                                                            \

> -    }                                                                        \

> -  else                                                                       \

> -    puts ("OK")

> -

> -  TEST_TYPE (pthread_mutex_t);

> -  TEST_TYPE (pthread_cond_t);

> -  TEST_TYPE (pthread_rwlock_t);

> -

> -#define TEST_TYPE2(name, internal)                                           \

> -  printf ("%s: ", #name);                                                    \

> -  if (sizeof (((name *) 0)->__size) < sizeof (internal))                     \

> -    {                                                                        \

> -      printf ("expected %zu, is %zu\n",                                              \

> -             sizeof (((name *) 0)->__size), sizeof (internal));              \

> -      result = 1;                                                            \

> -    }                                                                        \

> -  else                                                                       \

> -    puts ("OK")

> +#define TEST_TYPE(__type, __size) \

> +  _Static_assert (sizeof (__type) == __size, \

> +                 "sizeof (" #__type ") != " #__size)

> +

> +  TEST_TYPE (pthread_attr_t, __SIZEOF_PTHREAD_ATTR_T);

> +  TEST_TYPE (pthread_mutex_t, __SIZEOF_PTHREAD_MUTEX_T);

> +  TEST_TYPE (pthread_mutexattr_t, __SIZEOF_PTHREAD_MUTEXATTR_T);

> +  TEST_TYPE (pthread_cond_t, __SIZEOF_PTHREAD_COND_T);

> +  TEST_TYPE (pthread_condattr_t, __SIZEOF_PTHREAD_CONDATTR_T);

> +  TEST_TYPE (pthread_rwlock_t, __SIZEOF_PTHREAD_RWLOCK_T);

> +  TEST_TYPE (pthread_rwlockattr_t, __SIZEOF_PTHREAD_RWLOCKATTR_T);

> +  TEST_TYPE (pthread_barrier_t, __SIZEOF_PTHREAD_BARRIER_T);

> +  TEST_TYPE (pthread_barrierattr_t, __SIZEOF_PTHREAD_BARRIERATTR_T);

> +

> +#define TEST_TYPE2(__type, __internal) \

> +  _Static_assert (sizeof ((__type *) 0)->__size >= sizeof (__internal), \

> +                 "sizeof (" #__type ".__size) > sizeof (" #__internal ")")

>

>    TEST_TYPE2 (pthread_attr_t, struct pthread_attr);

>    TEST_TYPE2 (pthread_mutexattr_t, struct pthread_mutexattr);

> @@ -80,16 +50,7 @@ do_test (void)

>    TEST_TYPE2 (sem_t, struct new_sem);

>    TEST_TYPE2 (sem_t, struct old_sem);

>

> -  for (size_t i = 0; i < sizeof (types) / sizeof (types[0]); ++i)

> -    if (types[i].expected != types[i].is)

> -      {

> -       printf ("%s: expected %zu, is %zu\n",

> -               types[i].name, types[i].expected, types[i].is);

> -       result = 1;

> -      }

> -

> -  return result;

> +  return 0;

>  }

>

> -#define TEST_FUNCTION do_test ()

> -#include "../test-skeleton.c"

> +#include <support/test-driver.c>

> --

> 2.7.4

>


LGTM.

Thanks.

-- 
H.J.
diff mbox series

Patch

diff --git a/nptl/tst-typesizes.c b/nptl/tst-typesizes.c
index 78ed773..3355614 100644
--- a/nptl/tst-typesizes.c
+++ b/nptl/tst-typesizes.c
@@ -20,56 +20,26 @@ 
 #include <pthreadP.h>
 #include <semaphore.h>
 
-static const struct
-{
-  const char *name;
-  size_t expected;
-  size_t is;
-} types[] =
-  {
-#define T(t, c) \
-    { #t, c, sizeof (t) }
-    T (pthread_attr_t, __SIZEOF_PTHREAD_ATTR_T),
-    T (pthread_mutex_t, __SIZEOF_PTHREAD_MUTEX_T),
-    T (pthread_mutexattr_t, __SIZEOF_PTHREAD_MUTEXATTR_T),
-    T (pthread_cond_t, __SIZEOF_PTHREAD_COND_T),
-    T (pthread_condattr_t, __SIZEOF_PTHREAD_CONDATTR_T),
-    T (pthread_rwlock_t, __SIZEOF_PTHREAD_RWLOCK_T),
-    T (pthread_rwlockattr_t, __SIZEOF_PTHREAD_RWLOCKATTR_T),
-    T (pthread_barrier_t, __SIZEOF_PTHREAD_BARRIER_T),
-    T (pthread_barrierattr_t, __SIZEOF_PTHREAD_BARRIERATTR_T)
-  };
-
 static int
 do_test (void)
 {
-  int result = 0;
-
-#define TEST_TYPE(name) \
-  printf ("%s: ", #name);						      \
-  if (sizeof (name) != sizeof (((name *) 0)->__size))			      \
-    {									      \
-      printf ("expected %zu, is %zu\n",					      \
-	      sizeof (((name *) 0)->__size), sizeof (name));		      \
-      result = 1;							      \
-    }									      \
-  else									      \
-    puts ("OK")
-
-  TEST_TYPE (pthread_mutex_t);
-  TEST_TYPE (pthread_cond_t);
-  TEST_TYPE (pthread_rwlock_t);
-
-#define TEST_TYPE2(name, internal)					      \
-  printf ("%s: ", #name);						      \
-  if (sizeof (((name *) 0)->__size) < sizeof (internal))		      \
-    {									      \
-      printf ("expected %zu, is %zu\n",					      \
-	      sizeof (((name *) 0)->__size), sizeof (internal));	      \
-      result = 1;							      \
-    }									      \
-  else									      \
-    puts ("OK")
+#define TEST_TYPE(__type, __size) \
+  _Static_assert (sizeof (__type) == __size, \
+		  "sizeof (" #__type ") != " #__size)
+
+  TEST_TYPE (pthread_attr_t, __SIZEOF_PTHREAD_ATTR_T);
+  TEST_TYPE (pthread_mutex_t, __SIZEOF_PTHREAD_MUTEX_T);
+  TEST_TYPE (pthread_mutexattr_t, __SIZEOF_PTHREAD_MUTEXATTR_T);
+  TEST_TYPE (pthread_cond_t, __SIZEOF_PTHREAD_COND_T);
+  TEST_TYPE (pthread_condattr_t, __SIZEOF_PTHREAD_CONDATTR_T);
+  TEST_TYPE (pthread_rwlock_t, __SIZEOF_PTHREAD_RWLOCK_T);
+  TEST_TYPE (pthread_rwlockattr_t, __SIZEOF_PTHREAD_RWLOCKATTR_T);
+  TEST_TYPE (pthread_barrier_t, __SIZEOF_PTHREAD_BARRIER_T);
+  TEST_TYPE (pthread_barrierattr_t, __SIZEOF_PTHREAD_BARRIERATTR_T);
+
+#define TEST_TYPE2(__type, __internal) \
+  _Static_assert (sizeof ((__type *) 0)->__size >= sizeof (__internal), \
+		  "sizeof (" #__type ".__size) > sizeof (" #__internal ")")
 
   TEST_TYPE2 (pthread_attr_t, struct pthread_attr);
   TEST_TYPE2 (pthread_mutexattr_t, struct pthread_mutexattr);
@@ -80,16 +50,7 @@  do_test (void)
   TEST_TYPE2 (sem_t, struct new_sem);
   TEST_TYPE2 (sem_t, struct old_sem);
 
-  for (size_t i = 0; i < sizeof (types) / sizeof (types[0]); ++i)
-    if (types[i].expected != types[i].is)
-      {
-	printf ("%s: expected %zu, is %zu\n",
-		types[i].name, types[i].expected, types[i].is);
-	result = 1;
-      }
-
-  return result;
+  return 0;
 }
 
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/test-driver.c>