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