From patchwork Thu Jan 4 17:03:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 855730 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-88830-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="nJzjGyGA"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zCDdm5WhTz9s7G for ; Fri, 5 Jan 2018 04:03:24 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:to:subject:mime-version:content-type :content-transfer-encoding:message-id:from; q=dns; s=default; b= rXAEpvEpqF3BaUA8XoaGKNHGiqrDEwe07yuWv66ac/Jr5IzXr7Loj7L3FtnzWRAT QIdnPK1eOBjagKY+k4bm8/oRnDOZPP+fI+brjeW4vqq9YMAoLQrQHJE179eOG9vg 1UQKVBxm3zZotG3+JA3kexZ+N+jFuOf0SKA7NHBI2zw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:to:subject:mime-version:content-type :content-transfer-encoding:message-id:from; s=default; bh=XjUAZX u4EEOQmAQvTFDtj+RMuPQ=; b=nJzjGyGAGgs2qS3IRUUxG0Q0QZbzBJ9cIPV74s eQm0Q9z7FpX2wlIQmJFzMJyDVAN1fj2UT1+v1KRY8XTH04zxupwIPTaO61ftfsJ3 DiEme7M/saoS8LTxxB0ZyiEe5SZGwwywAxTDxbsCrutl6eE3ALU7nCydaIKCgRRg bkWOw= Received: (qmail 21910 invoked by alias); 4 Jan 2018 17:03:17 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 21898 invoked by uid 89); 4 Jan 2018 17:03:17 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=1089 X-HELO: mx1.redhat.com Date: Thu, 04 Jan 2018 18:03:13 +0100 To: libc-alpha@sourceware.org Subject: [PATCH] i386: In makecontext, align the stack before calling exit [BZ #22667] User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Message-Id: <20180104170313.40A56401336F0@oldenburg.str.redhat.com> From: fweimer@redhat.com (Florian Weimer) Before this change, if glibc was compiled with SSE instructions and a sufficiently recent GCC, an unaligned stack access in __run_exit_handlers would cause stdlib/tst-makecontext to crash. 2018-01-04 Florian Weimer [BZ #22667] * sysdeps/unix/sysv/linux/i386/makecontext.S (__makecontext): Align the stack before calling exit. * stdlib/tst-makecontext-align.c: New file. * stdlib/Makefile (tests): Add tst-makecontext-align. Reviewed-by: Carlos O'Donell diff --git a/stdlib/Makefile b/stdlib/Makefile index 691325b91b..7c363a6e4d 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -83,7 +83,8 @@ tests := tst-strtol tst-strtod testmb testrand testsort testdiv \ tst-getrandom tst-atexit tst-at_quick_exit \ tst-cxa_atexit tst-on_exit test-atexit-race \ test-at_quick_exit-race test-cxa_atexit-race \ - test-on_exit-race test-dlclose-exit-race + test-on_exit-race test-dlclose-exit-race \ + tst-makecontext-align tests-internal := tst-strtod1i tst-strtod3 tst-strtod4 tst-strtod5i \ tst-tls-atexit tst-tls-atexit-nodelete diff --git a/stdlib/tst-makecontext-align.c b/stdlib/tst-makecontext-align.c new file mode 100644 index 0000000000..82394b4f6b --- /dev/null +++ b/stdlib/tst-makecontext-align.c @@ -0,0 +1,241 @@ +/* Check stack alignment provided by makecontext. + Copyright (C) 2018 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* Used for error reporting. */ +static const char *context; + +/* Check that ADDRESS is aligned to ALIGNMENT bytes, behind a compiler + barrier. */ +__attribute__ ((noinline, noclone, weak)) +void +check_align (void *address, size_t alignment) +{ + uintptr_t uaddress = (uintptr_t) address; + if ((uaddress % alignment) != 0) + { + support_record_failure (); + printf ("error: %s: object at address %p is not aligned to %zu bytes\n", + context, address, alignment); + } +} + +/* Various alignment checking functions. */ + +__attribute__ ((noinline, noclone, weak)) +void +check_align_int (void) +{ + int a; + check_align (&a, __alignof__ (a)); +} + +__attribute__ ((noinline, noclone, weak)) +void +check_align_long (void) +{ + long a; + check_align (&a, __alignof__ (a)); +} + +__attribute__ ((noinline, noclone, weak)) +void +check_align_long_long (void) +{ + long long a; + check_align (&a, __alignof__ (a)); +} + +__attribute__ ((noinline, noclone, weak)) +void +check_align_double (void) +{ + double a; + check_align (&a, __alignof__ (a)); +} + +__attribute__ ((noinline, noclone, weak)) +void +check_align_4 (void) +{ + int a __attribute__ ((aligned (4))); + check_align (&a, 4); +} + +__attribute__ ((noinline, noclone, weak)) +void +check_align_8 (void) +{ + double a __attribute__ ((aligned (8))); + check_align (&a, 8); +} + +__attribute__ ((noinline, noclone, weak)) +void +check_align_16 (void) +{ + struct aligned + { + double x0 __attribute__ ((aligned (16))); + double x1; + } a; + check_align (&a, 16); +} + +__attribute__ ((noinline, noclone, weak)) +void +check_align_32 (void) +{ + struct aligned + { + double x0 __attribute__ ((aligned (32))); + double x1; + double x2; + double x3; + } a; + check_align (&a, 32); +} + +/* Call all the alignment checking functions. */ +__attribute__ ((noinline, noclone, weak)) +void +check_alignments (void) +{ + check_align_int (); + check_align_long (); + check_align_long_long (); + check_align_double (); + check_align_4 (); + check_align_8 (); + check_align_16 (); + check_align_32 (); +} + +/* Callback functions for makecontext and their invokers (to be used + with support_isolate_in_subprocess). */ + +static ucontext_t ucp; + +static void +callback_0 (void) +{ + context = "callback_0"; + check_alignments (); + context = "after return from callback_0"; +} + +static void +invoke_callback_0 (void *closure) +{ + makecontext (&ucp, (void *) callback_0, 0); + if (setcontext (&ucp) != 0) + FAIL_EXIT1 ("setcontext"); + FAIL_EXIT1 ("setcontext returned"); +} + +static void +callback_1 (int arg1) +{ + context = "callback_1"; + check_alignments (); + TEST_COMPARE (arg1, 101); + context = "after return from callback_1"; +} + +static void +invoke_callback_1 (void *closure) +{ + makecontext (&ucp, (void *) callback_1, 1, 101); + if (setcontext (&ucp) != 0) + FAIL_EXIT1 ("setcontext"); + FAIL_EXIT1 ("setcontext returned"); +} + +static void +callback_2 (int arg1, int arg2) +{ + context = "callback_2"; + check_alignments (); + TEST_COMPARE (arg1, 201); + TEST_COMPARE (arg2, 202); + context = "after return from callback_2"; +} + +static void +invoke_callback_2 (void *closure) +{ + makecontext (&ucp, (void *) callback_2, 2, 201, 202); + if (setcontext (&ucp) != 0) + FAIL_EXIT1 ("setcontext"); + FAIL_EXIT1 ("setcontext returned"); +} + +static void +callback_3 (int arg1, int arg2, int arg3) +{ + context = "callback_3"; + check_alignments (); + TEST_COMPARE (arg1, 301); + TEST_COMPARE (arg2, 302); + TEST_COMPARE (arg3, 303); + context = "after return from callback_3"; +} + +static void +invoke_callback_3 (void *closure) +{ + makecontext (&ucp, (void *) callback_3, 3, 301, 302, 303); + if (setcontext (&ucp) != 0) + FAIL_EXIT1 ("setcontext"); + FAIL_EXIT1 ("setcontext returned"); +} + +static int +do_test (void) +{ + context = "direct call"; + check_alignments (); + + atexit (check_alignments); + + if (getcontext (&ucp) != 0) + FAIL_UNSUPPORTED ("getcontext"); + + ucp.uc_link = NULL; + ucp.uc_stack.ss_size = 512 * 1024; + ucp.uc_stack.ss_sp = xmmap (NULL, ucp.uc_stack.ss_size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1); + + support_isolate_in_subprocess (invoke_callback_0, NULL); + support_isolate_in_subprocess (invoke_callback_1, NULL); + support_isolate_in_subprocess (invoke_callback_2, NULL); + support_isolate_in_subprocess (invoke_callback_3, NULL); + + return 0; +} + +#include diff --git a/sysdeps/unix/sysv/linux/i386/makecontext.S b/sysdeps/unix/sysv/linux/i386/makecontext.S index efa4955033..e3ca3dc0d5 100644 --- a/sysdeps/unix/sysv/linux/i386/makecontext.S +++ b/sysdeps/unix/sysv/linux/i386/makecontext.S @@ -108,9 +108,19 @@ L(exitcode): call HIDDEN_JUMPTARGET(__setcontext) /* If this returns (which can happen if the syscall fails) we'll exit the program with the return error value (-1). */ + jmp L(call_exit) - movl %eax, (%esp) -2: call HIDDEN_JUMPTARGET(exit) +2: + /* Exit with status 0. */ + xorl %eax, %eax + +L(call_exit): + /* Align the stack and pass the exit code (from %eax). */ + andl $0xfffffff0, %esp + subl $12, %esp + pushl %eax + + call HIDDEN_JUMPTARGET(exit) /* The 'exit' call should never return. In case it does cause the process to terminate. */ hlt