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