olga / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh1531168-3.patch

00db10
commit 8a5df95ffa83f525a4f638ead743f4fa2b7fe45a
00db10
Author: Florian Weimer <fweimer@redhat.com>
00db10
Date:   Thu Jan 4 18:00:05 2018 +0100
00db10
00db10
    i386: In makecontext, align the stack before calling exit [BZ #22667]
00db10
    
00db10
    Before this change, if glibc was compiled with SSE instructions and a
00db10
    sufficiently recent GCC, an unaligned stack access in
00db10
    __run_exit_handlers would cause stdlib/tst-makecontext to crash.
00db10
00db10
diff --git a/stdlib/Makefile b/stdlib/Makefile
00db10
index 764aad69d8c50b9b..b5553eafc2a4bbd5 100644
00db10
--- a/stdlib/Makefile
00db10
+++ b/stdlib/Makefile
00db10
@@ -71,7 +71,8 @@ tests		:= tst-strtol tst-strtod testmb testrand testsort testdiv   \
00db10
 		   tst-qsort2 tst-makecontext2 tst-strtod6 tst-unsetenv1    \
00db10
 		   tst-makecontext3 bug-getcontext bug-fmtmsg1		    \
00db10
 		   tst-secure-getenv tst-strtod-overflow tst-strtod-round   \
00db10
-		   tst-tininess tst-strtod-underflow tst-strfmon_l
00db10
+		   tst-tininess tst-strtod-underflow tst-strfmon_l	    \
00db10
+		   tst-makecontext-align
00db10
 tests-static	:= tst-secure-getenv
00db10
 
00db10
 include ../Makeconfig
00db10
diff --git a/stdlib/tst-makecontext-align.c b/stdlib/tst-makecontext-align.c
00db10
new file mode 100644
00db10
index 0000000000000000..82394b4f6b024c9b
00db10
--- /dev/null
00db10
+++ b/stdlib/tst-makecontext-align.c
00db10
@@ -0,0 +1,241 @@
00db10
+/* Check stack alignment provided by makecontext.
00db10
+   Copyright (C) 2018 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 <stdint.h>
00db10
+#include <stdio.h>
00db10
+#include <stdlib.h>
00db10
+#include <support/check.h>
00db10
+#include <support/namespace.h>
00db10
+#include <support/xunistd.h>
00db10
+#include <sys/mman.h>
00db10
+#include <ucontext.h>
00db10
+
00db10
+/* Used for error reporting.  */
00db10
+static const char *context;
00db10
+
00db10
+/* Check that ADDRESS is aligned to ALIGNMENT bytes, behind a compiler
00db10
+   barrier.  */
00db10
+__attribute__ ((noinline, noclone, weak))
00db10
+void
00db10
+check_align (void *address, size_t alignment)
00db10
+{
00db10
+  uintptr_t uaddress = (uintptr_t) address;
00db10
+  if ((uaddress % alignment) != 0)
00db10
+    {
00db10
+      support_record_failure ();
00db10
+      printf ("error: %s: object at address %p is not aligned to %zu bytes\n",
00db10
+              context, address, alignment);
00db10
+    }
00db10
+}
00db10
+
00db10
+/* Various alignment checking functions.  */
00db10
+
00db10
+__attribute__ ((noinline, noclone, weak))
00db10
+void
00db10
+check_align_int (void)
00db10
+{
00db10
+  int a;
00db10
+  check_align (&a, __alignof__ (a));
00db10
+}
00db10
+
00db10
+__attribute__ ((noinline, noclone, weak))
00db10
+void
00db10
+check_align_long (void)
00db10
+{
00db10
+  long a;
00db10
+  check_align (&a, __alignof__ (a));
00db10
+}
00db10
+
00db10
+__attribute__ ((noinline, noclone, weak))
00db10
+void
00db10
+check_align_long_long (void)
00db10
+{
00db10
+  long long a;
00db10
+  check_align (&a, __alignof__ (a));
00db10
+}
00db10
+
00db10
+__attribute__ ((noinline, noclone, weak))
00db10
+void
00db10
+check_align_double (void)
00db10
+{
00db10
+  double a;
00db10
+  check_align (&a, __alignof__ (a));
00db10
+}
00db10
+
00db10
+__attribute__ ((noinline, noclone, weak))
00db10
+void
00db10
+check_align_4 (void)
00db10
+{
00db10
+  int a __attribute__ ((aligned (4)));
00db10
+  check_align (&a, 4);
00db10
+}
00db10
+
00db10
+__attribute__ ((noinline, noclone, weak))
00db10
+void
00db10
+check_align_8 (void)
00db10
+{
00db10
+  double a __attribute__ ((aligned (8)));
00db10
+  check_align (&a, 8);
00db10
+}
00db10
+
00db10
+__attribute__ ((noinline, noclone, weak))
00db10
+void
00db10
+check_align_16 (void)
00db10
+{
00db10
+  struct aligned
00db10
+  {
00db10
+    double x0  __attribute__ ((aligned (16)));
00db10
+    double x1;
00db10
+  } a;
00db10
+  check_align (&a, 16);
00db10
+}
00db10
+
00db10
+__attribute__ ((noinline, noclone, weak))
00db10
+void
00db10
+check_align_32 (void)
00db10
+{
00db10
+  struct aligned
00db10
+  {
00db10
+    double x0  __attribute__ ((aligned (32)));
00db10
+    double x1;
00db10
+    double x2;
00db10
+    double x3;
00db10
+  } a;
00db10
+  check_align (&a, 32);
00db10
+}
00db10
+
00db10
+/* Call all the alignment checking functions.  */
00db10
+__attribute__ ((noinline, noclone, weak))
00db10
+void
00db10
+check_alignments (void)
00db10
+{
00db10
+  check_align_int ();
00db10
+  check_align_long ();
00db10
+  check_align_long_long ();
00db10
+  check_align_double ();
00db10
+  check_align_4 ();
00db10
+  check_align_8 ();
00db10
+  check_align_16 ();
00db10
+  check_align_32 ();
00db10
+}
00db10
+
00db10
+/* Callback functions for makecontext and their invokers (to be used
00db10
+   with support_isolate_in_subprocess).  */
00db10
+
00db10
+static ucontext_t ucp;
00db10
+
00db10
+static void
00db10
+callback_0 (void)
00db10
+{
00db10
+  context = "callback_0";
00db10
+  check_alignments ();
00db10
+  context = "after return from callback_0";
00db10
+}
00db10
+
00db10
+static void
00db10
+invoke_callback_0 (void *closure)
00db10
+{
00db10
+  makecontext (&ucp, (void *) callback_0, 0);
00db10
+  if (setcontext (&ucp) != 0)
00db10
+    FAIL_EXIT1 ("setcontext");
00db10
+  FAIL_EXIT1 ("setcontext returned");
00db10
+}
00db10
+
00db10
+static void
00db10
+callback_1 (int arg1)
00db10
+{
00db10
+  context = "callback_1";
00db10
+  check_alignments ();
00db10
+  TEST_COMPARE (arg1, 101);
00db10
+  context = "after return from callback_1";
00db10
+}
00db10
+
00db10
+static void
00db10
+invoke_callback_1 (void *closure)
00db10
+{
00db10
+  makecontext (&ucp, (void *) callback_1, 1, 101);
00db10
+  if (setcontext (&ucp) != 0)
00db10
+    FAIL_EXIT1 ("setcontext");
00db10
+  FAIL_EXIT1 ("setcontext returned");
00db10
+}
00db10
+
00db10
+static void
00db10
+callback_2 (int arg1, int arg2)
00db10
+{
00db10
+  context = "callback_2";
00db10
+  check_alignments ();
00db10
+  TEST_COMPARE (arg1, 201);
00db10
+  TEST_COMPARE (arg2, 202);
00db10
+  context = "after return from callback_2";
00db10
+}
00db10
+
00db10
+static void
00db10
+invoke_callback_2 (void *closure)
00db10
+{
00db10
+  makecontext (&ucp, (void *) callback_2, 2, 201, 202);
00db10
+  if (setcontext (&ucp) != 0)
00db10
+    FAIL_EXIT1 ("setcontext");
00db10
+  FAIL_EXIT1 ("setcontext returned");
00db10
+}
00db10
+
00db10
+static void
00db10
+callback_3 (int arg1, int arg2, int arg3)
00db10
+{
00db10
+  context = "callback_3";
00db10
+  check_alignments ();
00db10
+  TEST_COMPARE (arg1, 301);
00db10
+  TEST_COMPARE (arg2, 302);
00db10
+  TEST_COMPARE (arg3, 303);
00db10
+  context = "after return from callback_3";
00db10
+}
00db10
+
00db10
+static void
00db10
+invoke_callback_3 (void *closure)
00db10
+{
00db10
+  makecontext (&ucp, (void *) callback_3, 3, 301, 302, 303);
00db10
+  if (setcontext (&ucp) != 0)
00db10
+    FAIL_EXIT1 ("setcontext");
00db10
+  FAIL_EXIT1 ("setcontext returned");
00db10
+}
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  context = "direct call";
00db10
+  check_alignments ();
00db10
+
00db10
+  atexit (check_alignments);
00db10
+
00db10
+  if (getcontext (&ucp) != 0)
00db10
+    FAIL_UNSUPPORTED ("getcontext");
00db10
+
00db10
+  ucp.uc_link = NULL;
00db10
+  ucp.uc_stack.ss_size = 512 * 1024;
00db10
+  ucp.uc_stack.ss_sp = xmmap (NULL, ucp.uc_stack.ss_size,
00db10
+                              PROT_READ | PROT_WRITE,
00db10
+                              MAP_PRIVATE | MAP_ANONYMOUS, -1);
00db10
+
00db10
+  support_isolate_in_subprocess (invoke_callback_0, NULL);
00db10
+  support_isolate_in_subprocess (invoke_callback_1, NULL);
00db10
+  support_isolate_in_subprocess (invoke_callback_2, NULL);
00db10
+  support_isolate_in_subprocess (invoke_callback_3, NULL);
00db10
+
00db10
+  return 0;
00db10
+}
00db10
+
00db10
+#include <support/test-driver.c>
00db10
diff --git a/sysdeps/unix/sysv/linux/i386/makecontext.S b/sysdeps/unix/sysv/linux/i386/makecontext.S
00db10
index 48643864b05568b0..5e02aa78925c4bdc 100644
00db10
--- a/sysdeps/unix/sysv/linux/i386/makecontext.S
00db10
+++ b/sysdeps/unix/sysv/linux/i386/makecontext.S
00db10
@@ -113,9 +113,19 @@ L(exitcode):
00db10
 	call	JUMPTARGET(__setcontext)
00db10
 	/* If this returns (which can happen if the syscall fails) we'll
00db10
 	   exit the program with the return error value (-1).  */
00db10
+	jmp L(call_exit)
00db10
 
00db10
-	movl	%eax, (%esp)
00db10
-2:	call	HIDDEN_JUMPTARGET(exit)
00db10
+2:
00db10
+	/* Exit with status 0.  */
00db10
+	xorl	%eax, %eax
00db10
+
00db10
+L(call_exit):
00db10
+	/* Align the stack and pass the exit code (from %eax).  */
00db10
+	andl	$0xfffffff0, %esp
00db10
+	subl	$12, %esp
00db10
+	pushl	%eax
00db10
+
00db10
+	call	HIDDEN_JUMPTARGET(exit)
00db10
 	/* The 'exit' call should never return.  In case it does cause
00db10
 	   the process to terminate.  */
00db10
 	hlt