fa3bfd
The required TEST_COMPARE and C++ changes are included in the
fa3bfd
support/ rebase in glibc-rh1418978-1.patch
fa3bfd
fa3bfd
commit 579396ee082565ab5f42ff166a264891223b7b82
fa3bfd
Author: Florian Weimer <fweimer@redhat.com>
fa3bfd
Date:   Mon Jan 8 14:57:25 2018 +0100
fa3bfd
fa3bfd
    nptl: Add test for callee-saved register restore in pthread_exit
fa3bfd
    
fa3bfd
    GCC PR 83641 results in a miscompilation of libpthread, which
fa3bfd
    causes pthread_exit not to restore callee-saved registers before
fa3bfd
    running destructors for objects on the stack.  This test detects
fa3bfd
    this situation:
fa3bfd
    
fa3bfd
    info: unsigned int, direct pthread_exit call
fa3bfd
    tst-thread-exit-clobber.cc:80: numeric comparison failure
fa3bfd
       left: 4148288912 (0xf741dd90); from: value
fa3bfd
      right: 1600833940 (0x5f6ac994); from: magic_values.v2
fa3bfd
    info: double, direct pthread_exit call
fa3bfd
    info: unsigned int, indirect pthread_exit call
fa3bfd
    info: double, indirect pthread_exit call
fa3bfd
    error: 1 test failures
fa3bfd
fa3bfd
Index: glibc-2.17-c758a686/nptl/Makefile
fa3bfd
===================================================================
fa3bfd
--- glibc-2.17-c758a686.orig/nptl/Makefile
fa3bfd
+++ glibc-2.17-c758a686/nptl/Makefile
fa3bfd
@@ -199,6 +199,7 @@ CFLAGS-send.c = -fexceptions -fasynchron
fa3bfd
 CFLAGS-pt-system.c = -fexceptions
fa3bfd
 
fa3bfd
 CFLAGS-tst-minstack-throw.cc = -std=gnu++11
fa3bfd
+CFLAGS-tst-thread-exit-clobber.o = -std=gnu++11
fa3bfd
 
fa3bfd
 tests = tst-typesizes \
fa3bfd
 	tst-attr1 tst-attr2 tst-attr3 tst-default-attr \
fa3bfd
@@ -269,7 +270,8 @@ tests = tst-typesizes \
fa3bfd
 	tst-getpid1 tst-getpid2 tst-getpid3 \
fa3bfd
 	tst-initializers1 $(patsubst %,tst-initializers1-%,c89 gnu89 c99 gnu99) \
fa3bfd
 	tst-mutex-errorcheck \
fa3bfd
-	tst-minstack-cancel tst-minstack-exit tst-minstack-throw
fa3bfd
+	tst-minstack-cancel tst-minstack-exit tst-minstack-throw \
fa3bfd
+	tst-thread-exit-clobber
fa3bfd
 xtests = tst-setuid1 tst-setuid1-static tst-mutexpp1 tst-mutexpp6 tst-mutexpp10
fa3bfd
 test-srcs = tst-oddstacklimit
fa3bfd
 
fa3bfd
@@ -529,6 +531,7 @@ $(objpfx)tst-_res1: $(objpfx)tst-_res1mo
fa3bfd
 LDLIBS-tst-cancel24 = $(no-as-needed) -lstdc++
fa3bfd
 LDLIBS-tst-cancel24-static = $(LDLIBS-tst-cancel24)
fa3bfd
 LDLIBS-tst-minstack-throw = -lstdc++
fa3bfd
+LDLIBS-tst-thread-exit-clobber = -lstdc++
fa3bfd
 
fa3bfd
 extra-B-pthread.so = -B$(common-objpfx)nptl/
fa3bfd
 $(objpfx)libpthread.so: $(addprefix $(objpfx),$(crti-objs) $(crtn-objs))
fa3bfd
Index: glibc-2.17-c758a686/nptl/tst-thread-exit-clobber.cc
fa3bfd
===================================================================
fa3bfd
--- /dev/null
fa3bfd
+++ glibc-2.17-c758a686/nptl/tst-thread-exit-clobber.cc
fa3bfd
@@ -0,0 +1,243 @@
fa3bfd
+/* Test that pthread_exit does not clobber callee-saved registers.
fa3bfd
+   Copyright (C) 2018 Free Software Foundation, Inc.
fa3bfd
+   This file is part of the GNU C Library.
fa3bfd
+
fa3bfd
+   The GNU C Library is free software; you can redistribute it and/or
fa3bfd
+   modify it under the terms of the GNU Lesser General Public
fa3bfd
+   License as published by the Free Software Foundation; either
fa3bfd
+   version 2.1 of the License, or (at your option) any later version.
fa3bfd
+
fa3bfd
+   The GNU C Library is distributed in the hope that it will be useful,
fa3bfd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa3bfd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa3bfd
+   Lesser General Public License for more details.
fa3bfd
+
fa3bfd
+   You should have received a copy of the GNU Lesser General Public
fa3bfd
+   License along with the GNU C Library; if not, see
fa3bfd
+   <http://www.gnu.org/licenses/>.  */
fa3bfd
+
fa3bfd
+#include <stdio.h>
fa3bfd
+#include <support/check.h>
fa3bfd
+#include <support/xthread.h>
fa3bfd
+
fa3bfd
+/* This test attempts to check that callee-saved registers are
fa3bfd
+   restored to their original values when destructors are run after
fa3bfd
+   pthread_exit is called.  GCC PR 83641 causes this test to fail.
fa3bfd
+
fa3bfd
+   The constants have been chosen randomly and are magic values which
fa3bfd
+   are used to detect whether registers have been clobbered.  The idea
fa3bfd
+   is that these values are hidden behind a compiler barrier and only
fa3bfd
+   present in .rodata initially, so that it is less likely that they
fa3bfd
+   are in a register by accident.
fa3bfd
+
fa3bfd
+   The checker class can be stored in registers, and the magic values
fa3bfd
+   are directly loaded into these registers.  The checker destructor
fa3bfd
+   is eventually invoked by pthread_exit and calls one of the
fa3bfd
+   check_magic functions to verify that the class contents (that is,
fa3bfd
+   register value) is correct.
fa3bfd
+
fa3bfd
+   These tests are performed both for unsigned int and double values,
fa3bfd
+   to cover different calling conventions.  */
fa3bfd
+
fa3bfd
+template <class T>
fa3bfd
+struct values
fa3bfd
+{
fa3bfd
+  T v0;
fa3bfd
+  T v1;
fa3bfd
+  T v2;
fa3bfd
+  T v3;
fa3bfd
+  T v4;
fa3bfd
+};
fa3bfd
+
fa3bfd
+static const values<unsigned int> magic_values =
fa3bfd
+  {
fa3bfd
+    0x57f7fc72,
fa3bfd
+    0xe582daba,
fa3bfd
+    0x5f6ac994,
fa3bfd
+    0x35efddb7,
fa3bfd
+    0x1fbf5a74,
fa3bfd
+  };
fa3bfd
+
fa3bfd
+static const values<double> magic_values_double =
fa3bfd
+  {
fa3bfd
+    0.6764041905675465,
fa3bfd
+    0.9533336788140494,
fa3bfd
+    0.6091161359041452,
fa3bfd
+    0.7668653957125336,
fa3bfd
+    0.010374520235509666,
fa3bfd
+  };
fa3bfd
+
fa3bfd
+/* Special index value which tells check_magic that no check should be
fa3bfd
+   performed.  */
fa3bfd
+enum { no_check = -1 };
fa3bfd
+
fa3bfd
+/* Check that VALUE is the magic value for INDEX, behind a compiler
fa3bfd
+   barrier.  */
fa3bfd
+__attribute__ ((noinline, noclone, weak))
fa3bfd
+void
fa3bfd
+check_magic (int index, unsigned int value)
fa3bfd
+{
fa3bfd
+  switch (index)
fa3bfd
+    {
fa3bfd
+    case 0:
fa3bfd
+      TEST_COMPARE (value, magic_values.v0);
fa3bfd
+      break;
fa3bfd
+    case 1:
fa3bfd
+      TEST_COMPARE (value, magic_values.v1);
fa3bfd
+      break;
fa3bfd
+    case 2:
fa3bfd
+      TEST_COMPARE (value, magic_values.v2);
fa3bfd
+      break;
fa3bfd
+    case 3:
fa3bfd
+      TEST_COMPARE (value, magic_values.v3);
fa3bfd
+      break;
fa3bfd
+    case 4:
fa3bfd
+      TEST_COMPARE (value, magic_values.v4);
fa3bfd
+      break;
fa3bfd
+    case no_check:
fa3bfd
+      break;
fa3bfd
+    default:
fa3bfd
+      FAIL_EXIT1 ("invalid magic value index %d", index);
fa3bfd
+    }
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Check that VALUE is the magic value for INDEX, behind a compiler
fa3bfd
+   barrier.  Double variant.  */
fa3bfd
+__attribute__ ((noinline, noclone, weak))
fa3bfd
+void
fa3bfd
+check_magic (int index, double value)
fa3bfd
+{
fa3bfd
+  switch (index)
fa3bfd
+    {
fa3bfd
+    case 0:
fa3bfd
+      TEST_VERIFY (value == magic_values_double.v0);
fa3bfd
+      break;
fa3bfd
+    case 1:
fa3bfd
+      TEST_VERIFY (value == magic_values_double.v1);
fa3bfd
+      break;
fa3bfd
+    case 2:
fa3bfd
+      TEST_VERIFY (value == magic_values_double.v2);
fa3bfd
+      break;
fa3bfd
+    case 3:
fa3bfd
+      TEST_VERIFY (value == magic_values_double.v3);
fa3bfd
+      break;
fa3bfd
+    case 4:
fa3bfd
+      TEST_VERIFY (value == magic_values_double.v4);
fa3bfd
+      break;
fa3bfd
+    case no_check:
fa3bfd
+      break;
fa3bfd
+    default:
fa3bfd
+      FAIL_EXIT1 ("invalid magic value index %d", index);
fa3bfd
+    }
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Store a magic value and check, via the destructor, that it has the
fa3bfd
+   expected value.  */
fa3bfd
+template <class T, int I>
fa3bfd
+struct checker
fa3bfd
+{
fa3bfd
+  T value;
fa3bfd
+
fa3bfd
+  checker (T v)
fa3bfd
+    : value (v)
fa3bfd
+  {
fa3bfd
+  }
fa3bfd
+
fa3bfd
+  ~checker ()
fa3bfd
+  {
fa3bfd
+    check_magic (I, value);
fa3bfd
+  }
fa3bfd
+};
fa3bfd
+
fa3bfd
+/* The functions call_pthread_exit_0, call_pthread_exit_1,
fa3bfd
+   call_pthread_exit are used to call pthread_exit indirectly, with
fa3bfd
+   the intent of clobbering the register values.  */
fa3bfd
+
fa3bfd
+__attribute__ ((noinline, noclone, weak))
fa3bfd
+void
fa3bfd
+call_pthread_exit_0 (const values<unsigned int> *pvalues)
fa3bfd
+{
fa3bfd
+  checker<unsigned int, no_check> c0 (pvalues->v0);
fa3bfd
+  checker<unsigned int, no_check> c1 (pvalues->v1);
fa3bfd
+  checker<unsigned int, no_check> c2 (pvalues->v2);
fa3bfd
+  checker<unsigned int, no_check> c3 (pvalues->v3);
fa3bfd
+  checker<unsigned int, no_check> c4 (pvalues->v4);
fa3bfd
+
fa3bfd
+  pthread_exit (NULL);
fa3bfd
+}
fa3bfd
+
fa3bfd
+__attribute__ ((noinline, noclone, weak))
fa3bfd
+void
fa3bfd
+call_pthread_exit_1 (const values<double> *pvalues)
fa3bfd
+{
fa3bfd
+  checker<double, no_check> c0 (pvalues->v0);
fa3bfd
+  checker<double, no_check> c1 (pvalues->v1);
fa3bfd
+  checker<double, no_check> c2 (pvalues->v2);
fa3bfd
+  checker<double, no_check> c3 (pvalues->v3);
fa3bfd
+  checker<double, no_check> c4 (pvalues->v4);
fa3bfd
+
fa3bfd
+  values<unsigned int> other_values = { 0, };
fa3bfd
+  call_pthread_exit_0 (&other_values);
fa3bfd
+}
fa3bfd
+
fa3bfd
+__attribute__ ((noinline, noclone, weak))
fa3bfd
+void
fa3bfd
+call_pthread_exit ()
fa3bfd
+{
fa3bfd
+  values<double> other_values = { 0, };
fa3bfd
+  call_pthread_exit_1 (&other_values);
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Create on-stack objects and check that their values are restored by
fa3bfd
+   pthread_exit.  If Nested is true, call pthread_exit indirectly via
fa3bfd
+   call_pthread_exit.  */
fa3bfd
+template <class T, bool Nested>
fa3bfd
+__attribute__ ((noinline, noclone, weak))
fa3bfd
+void *
fa3bfd
+threadfunc (void *closure)
fa3bfd
+{
fa3bfd
+  const values<T> *pvalues = static_cast<const values<T> *> (closure);
fa3bfd
+
fa3bfd
+  checker<T, 0> c0 (pvalues->v0);
fa3bfd
+  checker<T, 1> c1 (pvalues->v1);
fa3bfd
+  checker<T, 2> c2 (pvalues->v2);
fa3bfd
+  checker<T, 3> c3 (pvalues->v3);
fa3bfd
+  checker<T, 4> c4 (pvalues->v4);
fa3bfd
+
fa3bfd
+  if (Nested)
fa3bfd
+    call_pthread_exit ();
fa3bfd
+  else
fa3bfd
+    pthread_exit (NULL);
fa3bfd
+
fa3bfd
+  /* This should not be reached.  */
fa3bfd
+  return const_cast<char *> ("");
fa3bfd
+}
fa3bfd
+
fa3bfd
+static int
fa3bfd
+do_test ()
fa3bfd
+{
fa3bfd
+  puts ("info: unsigned int, direct pthread_exit call");
fa3bfd
+  pthread_t thr
fa3bfd
+    = xpthread_create (NULL, &threadfunc<unsigned int, false>,
fa3bfd
+                       const_cast<values<unsigned int> *> (&magic_values));
fa3bfd
+  TEST_VERIFY (xpthread_join (thr) == NULL);
fa3bfd
+
fa3bfd
+  puts ("info: double, direct pthread_exit call");
fa3bfd
+  thr = xpthread_create (NULL, &threadfunc<double, false>,
fa3bfd
+                         const_cast<values<double> *> (&magic_values_double));
fa3bfd
+  TEST_VERIFY (xpthread_join (thr) == NULL);
fa3bfd
+
fa3bfd
+  puts ("info: unsigned int, indirect pthread_exit call");
fa3bfd
+  thr = xpthread_create (NULL, &threadfunc<unsigned int, true>,
fa3bfd
+                       const_cast<values<unsigned int> *> (&magic_values));
fa3bfd
+  TEST_VERIFY (xpthread_join (thr) == NULL);
fa3bfd
+
fa3bfd
+  puts ("info: double, indirect pthread_exit call");
fa3bfd
+  thr = xpthread_create (NULL, &threadfunc<double, true>,
fa3bfd
+                         const_cast<values<double> *> (&magic_values_double));
fa3bfd
+  TEST_VERIFY (xpthread_join (thr) == NULL);
fa3bfd
+
fa3bfd
+  return 0;
fa3bfd
+}
fa3bfd
+
fa3bfd
+#include <support/test-driver.c>