Blame SOURCES/glibc-rh1531168-3.patch

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