bca718
commit 88ed594f5d431d855256edbe7e886c8cf4b575dc
bca718
Author: Roland McGrath <roland@hack.frob.com>
bca718
Date:   Tue May 19 15:04:41 2015 -0700
bca718
bca718
    BZ#18434: Fix sem_post EOVERFLOW check for [!__HAVE_64B_ATOMICS].
bca718
bca718
Index: glibc-2.17-c758a686/nptl/Makefile
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/nptl/Makefile
bca718
+++ glibc-2.17-c758a686/nptl/Makefile
bca718
@@ -224,6 +224,7 @@ tests = tst-typesizes \
bca718
 	tst-key1 tst-key2 tst-key3 tst-key4 \
bca718
 	tst-sem1 tst-sem2 tst-sem3 tst-sem4 tst-sem5 tst-sem6 tst-sem7 \
bca718
 	tst-sem8 tst-sem9 tst-sem10 tst-sem11 tst-sem12 tst-sem13 tst-sem14 \
bca718
+	tst-sem15 \
bca718
 	tst-barrier1 tst-barrier2 tst-barrier3 tst-barrier4 \
bca718
 	tst-align tst-align2 tst-align3 \
bca718
 	tst-basic1 tst-basic2 tst-basic3 tst-basic4 tst-basic5 tst-basic6 \
bca718
Index: glibc-2.17-c758a686/nptl/tst-sem15.c
bca718
===================================================================
bca718
--- /dev/null
bca718
+++ glibc-2.17-c758a686/nptl/tst-sem15.c
bca718
@@ -0,0 +1,99 @@
bca718
+/* Test for SEM_VALUE_MAX overflow detection: BZ #18434.
bca718
+   Copyright (C) 2015 Free Software Foundation, Inc.
bca718
+   This file is part of the GNU C Library.
bca718
+
bca718
+   The GNU C Library is free software; you can redistribute it and/or
bca718
+   modify it under the terms of the GNU Lesser General Public
bca718
+   License as published by the Free Software Foundation; either
bca718
+   version 2.1 of the License, or (at your option) any later version.
bca718
+
bca718
+   The GNU C Library is distributed in the hope that it will be useful,
bca718
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bca718
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bca718
+   Lesser General Public License for more details.
bca718
+
bca718
+   You should have received a copy of the GNU Lesser General Public
bca718
+   License along with the GNU C Library; if not, see
bca718
+   <http://www.gnu.org/licenses/>.  */
bca718
+
bca718
+#include <errno.h>
bca718
+#include <limits.h>
bca718
+#include <semaphore.h>
bca718
+#include <stdio.h>
bca718
+#include <string.h>
bca718
+#include <unistd.h>
bca718
+
bca718
+
bca718
+static int
bca718
+do_test (void)
bca718
+{
bca718
+  sem_t s;
bca718
+
bca718
+  if (sem_init (&s, 0, SEM_VALUE_MAX))
bca718
+    {
bca718
+      printf ("sem_init: %m\n");
bca718
+      return 1;
bca718
+    }
bca718
+
bca718
+  int result = 0;
bca718
+
bca718
+  int value = 0xdeadbeef;
bca718
+  if (sem_getvalue (&s, &value))
bca718
+    {
bca718
+      printf ("sem_getvalue: %m\n");
bca718
+      result = 1;
bca718
+    }
bca718
+  else
bca718
+    {
bca718
+      printf ("sem_getvalue after init: %d\n", value);
bca718
+      if (value != SEM_VALUE_MAX)
bca718
+	{
bca718
+	  printf ("\tshould be %d\n", SEM_VALUE_MAX);
bca718
+	  result = 1;
bca718
+	}
bca718
+    }
bca718
+
bca718
+  errno = 0;
bca718
+  if (sem_post(&s) == 0)
bca718
+    {
bca718
+      puts ("sem_post at SEM_VALUE_MAX succeeded!");
bca718
+      result = 1;
bca718
+    }
bca718
+  else
bca718
+    {
bca718
+      printf ("sem_post at SEM_VALUE_MAX: %m (%d)\n", errno);
bca718
+      if (errno != EOVERFLOW)
bca718
+	{
bca718
+	  printf ("\tshould be %s (EOVERFLOW = %d)\n",
bca718
+		  strerror (EOVERFLOW), EOVERFLOW);
bca718
+	  result = 1;
bca718
+	}
bca718
+    }
bca718
+
bca718
+  value = 0xbad1d00d;
bca718
+  if (sem_getvalue (&s, &value))
bca718
+    {
bca718
+      printf ("sem_getvalue: %m\n");
bca718
+      result = 1;
bca718
+    }
bca718
+  else
bca718
+    {
bca718
+      printf ("sem_getvalue after post: %d\n", value);
bca718
+      if (value != SEM_VALUE_MAX)
bca718
+	{
bca718
+	  printf ("\tshould be %d\n", SEM_VALUE_MAX);
bca718
+	  result = 1;
bca718
+	}
bca718
+    }
bca718
+
bca718
+  if (sem_destroy (&s))
bca718
+    {
bca718
+      printf ("sem_destroy: %m\n");
bca718
+      result = 1;
bca718
+    }
bca718
+
bca718
+  return result;
bca718
+}
bca718
+
bca718
+#define TEST_FUNCTION do_test ()
bca718
+#include "../test-skeleton.c"
bca718
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/sem_post.c
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/nptl/sysdeps/unix/sysv/linux/sem_post.c
bca718
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/sem_post.c
bca718
@@ -83,14 +83,14 @@ __new_sem_post (sem_t *sem)
bca718
   unsigned int v = atomic_load_relaxed (&isem->value);
bca718
   do
bca718
     {
bca718
-      if ((v << SEM_VALUE_SHIFT) == SEM_VALUE_MAX)
bca718
+      if ((v >> SEM_VALUE_SHIFT) == SEM_VALUE_MAX)
bca718
 	{
bca718
 	  __set_errno (EOVERFLOW);
bca718
 	  return -1;
bca718
 	}
bca718
     }
bca718
-  while (!atomic_compare_exchange_weak_release (&isem->value,
bca718
-      &v, v + (1 << SEM_VALUE_SHIFT)));
bca718
+  while (!atomic_compare_exchange_weak_release
bca718
+	 (&isem->value, &v, v + (1 << SEM_VALUE_SHIFT)));
bca718
 
bca718
   /* If there is any potentially blocked waiter, wake one of them.  */
bca718
   if ((v & SEM_NWAITERS_MASK) != 0)