arrfab / rpms / glibc

Forked from rpms/glibc 4 years ago
Clone

Blame SOURCES/glibc-rh1529982.patch

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