arrfab / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh1401665-4.patch

147e83
commit faf0e9c84119742dd9ebb79060faa22c52ae80a1
147e83
Author: Florian Weimer <fweimer@redhat.com>
147e83
Date:   Fri Jan 27 06:53:19 2017 +0100
147e83
147e83
    nptl: Add tst-robust-fork
147e83
147e83
Index: glibc-2.17-c758a686/nptl/Makefile
147e83
===================================================================
147e83
--- glibc-2.17-c758a686.orig/nptl/Makefile
147e83
+++ glibc-2.17-c758a686/nptl/Makefile
147e83
@@ -271,7 +271,7 @@ tests = tst-typesizes \
147e83
 	tst-initializers1 $(patsubst %,tst-initializers1-%,c89 gnu89 c99 gnu99) \
147e83
 	tst-mutex-errorcheck \
147e83
 	tst-minstack-cancel tst-minstack-exit tst-minstack-throw \
147e83
-	tst-thread-exit-clobber
147e83
+	tst-thread-exit-clobber tst-robust-fork
147e83
 xtests = tst-setuid1 tst-setuid1-static tst-mutexpp1 tst-mutexpp6 tst-mutexpp10
147e83
 test-srcs = tst-oddstacklimit
147e83
 
147e83
Index: glibc-2.17-c758a686/nptl/tst-robust-fork.c
147e83
===================================================================
147e83
--- /dev/null
147e83
+++ glibc-2.17-c758a686/nptl/tst-robust-fork.c
147e83
@@ -0,0 +1,184 @@
147e83
+/* Test the interaction of fork and robust mutexes.
147e83
+   Copyright (C) 2017 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+#include <errno.h>
147e83
+#include <stdbool.h>
147e83
+#include <stdio.h>
147e83
+#include <support/check.h>
147e83
+#include <support/test-driver.h>
147e83
+#include <support/xthread.h>
147e83
+#include <support/xunistd.h>
147e83
+#include <sys/mman.h>
147e83
+
147e83
+/* Data shared between processes. */
147e83
+struct shared
147e83
+{
147e83
+  pthread_mutex_t parent_mutex;
147e83
+  pthread_mutex_t child_mutex;
147e83
+};
147e83
+
147e83
+/* These flags control which mutex settings are enabled in the parent
147e83
+   and child (separately).  */
147e83
+enum mutex_bits
147e83
+  {
147e83
+    mutex_pshared = 1,
147e83
+    mutex_robust = 2,
147e83
+    mutex_pi = 4,
147e83
+    mutex_check = 8,
147e83
+
147e83
+    /* All bits combined.  */
147e83
+    mutex_all_bits = 15,
147e83
+  };
147e83
+
147e83
+static void
147e83
+mutex_init (pthread_mutex_t *mutex, int bits)
147e83
+{
147e83
+  pthread_mutexattr_t attr;
147e83
+  xpthread_mutexattr_init (&attr);
147e83
+  if (bits & mutex_pshared)
147e83
+    xpthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED);
147e83
+  if (bits & mutex_robust)
147e83
+    xpthread_mutexattr_setrobust (&attr, PTHREAD_MUTEX_ROBUST);
147e83
+  if (bits & mutex_pi)
147e83
+    xpthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_INHERIT);
147e83
+  if (bits & mutex_check)
147e83
+    xpthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK);
147e83
+  xpthread_mutex_init (mutex, &attr);
147e83
+  xpthread_mutexattr_destroy (&attr);
147e83
+}
147e83
+
147e83
+static void
147e83
+one_test (int parent_bits, int child_bits, int nonshared_bits,
147e83
+          bool lock_nonshared, bool lock_child)
147e83
+{
147e83
+
147e83
+  struct shared *shared = xmmap (NULL, sizeof (*shared),
147e83
+                                 PROT_READ | PROT_WRITE,
147e83
+                                 MAP_ANONYMOUS | MAP_SHARED, -1);
147e83
+  mutex_init (&shared->parent_mutex, parent_bits);
147e83
+  mutex_init (&shared->child_mutex, child_bits);
147e83
+
147e83
+  /* Acquire the parent mutex in the parent.  */
147e83
+  xpthread_mutex_lock (&shared->parent_mutex);
147e83
+
147e83
+  pthread_mutex_t nonshared_mutex;
147e83
+  mutex_init (&nonshared_mutex, nonshared_bits);
147e83
+  if (lock_nonshared)
147e83
+    xpthread_mutex_lock (&nonshared_mutex);
147e83
+
147e83
+  pid_t pid = xfork ();
147e83
+  if (pid == 0)
147e83
+    {
147e83
+      /* Child process.  */
147e83
+      if (lock_child)
147e83
+        xpthread_mutex_lock (&shared->child_mutex);
147e83
+      else
147e83
+        xmunmap (shared, sizeof (*shared));
147e83
+      if (lock_nonshared)
147e83
+        /* Reinitialize the non-shared mutex if it was locked in the
147e83
+           parent.  */
147e83
+        mutex_init (&nonshared_mutex, nonshared_bits);
147e83
+      xpthread_mutex_lock (&nonshared_mutex);
147e83
+      /* For robust mutexes, the _exit call will perform the unlock
147e83
+         instead.  */
147e83
+      if (lock_child && !(child_bits & mutex_robust))
147e83
+        xpthread_mutex_unlock (&shared->child_mutex);
147e83
+      _exit (0);
147e83
+    }
147e83
+  /* Parent process. */
147e83
+  {
147e83
+    int status;
147e83
+    xwaitpid (pid, &status, 0);
147e83
+    TEST_VERIFY (status == 0);
147e83
+  }
147e83
+
147e83
+  if (parent_bits & mutex_check)
147e83
+    /* Test for expected self-deadlock.  This is only possible to
147e83
+       detect if the mutex is error-checking.  */
147e83
+    TEST_VERIFY_EXIT (pthread_mutex_lock (&shared->parent_mutex) == EDEADLK);
147e83
+
147e83
+  pid = xfork ();
147e83
+  if (pid == 0)
147e83
+    {
147e83
+      /* Child process.  We can perform some checks only if we are
147e83
+         dealing with process-shared mutexes.  */
147e83
+      if (parent_bits & mutex_pshared)
147e83
+        /* It must not be possible to acquire the parent mutex.
147e83
+
147e83
+           NB: This check touches a mutex which has been acquired in
147e83
+           the parent at fork time, so it might be deemed undefined
147e83
+           behavior, pending the resolution of Austin Groups issue
147e83
+           1112.  */
147e83
+        TEST_VERIFY_EXIT (pthread_mutex_trylock (&shared->parent_mutex)
147e83
+                          == EBUSY);
147e83
+      if (lock_child && (child_bits & mutex_robust))
147e83
+        {
147e83
+          if (!(child_bits & mutex_pshared))
147e83
+            /* No further tests possible.  */
147e83
+            _exit (0);
147e83
+          TEST_VERIFY_EXIT (pthread_mutex_lock (&shared->child_mutex)
147e83
+                            == EOWNERDEAD);
147e83
+          xpthread_mutex_consistent (&shared->child_mutex);
147e83
+        }
147e83
+      else
147e83
+        /* We did not acquire the lock in the first child process, or
147e83
+           we unlocked the mutex again because the mutex is not a
147e83
+           robust mutex.  */
147e83
+        xpthread_mutex_lock (&shared->child_mutex);
147e83
+      xpthread_mutex_unlock (&shared->child_mutex);
147e83
+      _exit (0);
147e83
+    }
147e83
+  /* Parent process. */
147e83
+  {
147e83
+    int status;
147e83
+    xwaitpid (pid, &status, 0);
147e83
+    TEST_VERIFY (status == 0);
147e83
+  }
147e83
+
147e83
+  if (lock_nonshared)
147e83
+    xpthread_mutex_unlock (&nonshared_mutex);
147e83
+  xpthread_mutex_unlock (&shared->parent_mutex);
147e83
+  xpthread_mutex_destroy (&shared->parent_mutex);
147e83
+  xpthread_mutex_destroy (&shared->child_mutex);
147e83
+  xpthread_mutex_destroy (&nonshared_mutex);
147e83
+  xmunmap (shared, sizeof (*shared));
147e83
+}
147e83
+
147e83
+static int
147e83
+do_test (void)
147e83
+{
147e83
+  for (int parent_bits = 0; parent_bits <= mutex_all_bits; ++parent_bits)
147e83
+    for (int child_bits = 0; child_bits <= mutex_all_bits; ++child_bits)
147e83
+      for (int nonshared_bits = 0; nonshared_bits <= mutex_all_bits;
147e83
+           ++nonshared_bits)
147e83
+        for (int lock_nonshared = 0; lock_nonshared < 2; ++lock_nonshared)
147e83
+          for (int lock_child = 0; lock_child < 2; ++lock_child)
147e83
+            {
147e83
+              if (test_verbose)
147e83
+                printf ("info: parent_bits=0x%x child_bits=0x%x"
147e83
+                        " nonshared_bits=0x%x%s%s\n",
147e83
+                        parent_bits, child_bits, nonshared_bits,
147e83
+                        lock_nonshared ? " lock_nonshared" : "",
147e83
+                        lock_child ? " lock_child" : "");
147e83
+              one_test (parent_bits, child_bits, nonshared_bits,
147e83
+                        lock_nonshared, lock_child);
147e83
+            }
147e83
+  return 0;
147e83
+}
147e83
+
147e83
+#include <support/test-driver.c>