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

Message ID 1508446807-21704-2-git-send-email-adhemerval.zanella@linaro.org
State New
Headers show
Series
  • [1/3] nptl: Add tests for internal pthread_mutex_t offsets
Related show

Commit Message

Adhemerval Zanella Oct. 19, 2017, 9 p.m.
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

Adhemerval Zanella Oct. 25, 2017, 2:15 p.m. | #1
Ping.


On 19/10/2017 19:00, Adhemerval Zanella 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>
Andreas Schwab Oct. 25, 2017, 2:36 p.m. | #2
On Okt 19 2017, 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).


How about putting the static asserts in some library source?

Andreas.

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

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>