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