From patchwork Sun May 23 18:40:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 43547 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8AA8F388E801; Sun, 23 May 2021 18:41:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8AA8F388E801 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1621795289; bh=FzJkao1caBpswALlhtxjnYVIhCvFDloZjmPB8Zsk5yw=; h=References:In-Reply-To:Date:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=QzMPpjGkJmYsfpp5O+9WTH/XoxMr8dCQghwaOiDcl2x++6zWTArv4aaD2XH0wOq5+ 2YcAPJacC3Y8WdLH9x8NQXpmXDvK6aCDwaQrMyxXLENj5V8naRPRYKEQsNTVNys7F/ h4O4+XE9t4naRJXs0Ec27atAd4sxquiu7s0uQgkw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by sourceware.org (Postfix) with ESMTPS id D2AC8385380B for ; Sun, 23 May 2021 18:41:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D2AC8385380B Received: by mail-ot1-x32c.google.com with SMTP id n32-20020a9d1ea30000b02902a53d6ad4bdso23079766otn.3 for ; Sun, 23 May 2021 11:41:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=FzJkao1caBpswALlhtxjnYVIhCvFDloZjmPB8Zsk5yw=; b=tRr7hCC/yn5L3bBUItLA9Woq9zO+1qciTDxFc0j4oQdqrfzsNMPc/oM+qMxUlH6rbc cCjdztz/PJuzkQjzjXKZye1uy7GktTtSpUKeC8xd5x3Jd+hnZy/YAN1ORjXzCd2VpOCz yMgzFxD50Px4DCqB1WOb08GxsLJI2yXQHF20oIedlC4Ay3IWBscL1cMaWwS2wqerFnDS w4+M+1YQNZsKs9cH1koaGkVsZGCY5/76B8sbxg5JsyawFsKTAWqrWxqDxcqsSkvEzdLg POoiHmg7T5Hmbmk3kC2KGwutN7TteTyfI5FdedHNZ9FxjN/TDJq40di3s4dk29YQyYua d1CA== X-Gm-Message-State: AOAM531MemkR/qckYtEp/YNCll2IKfM9hvQTNaW0ddBlZ2NughyXz0Cv 2ODXWpHCAvziKA0Cv2eUwk7cWgH0QL9SMOpb0Q6YcqlE X-Google-Smtp-Source: ABdhPJx3D3R5MdnltLeI//VPlrOOhnKF6dbaiMzIQ8KecycW8UJIAzMhjFrLxZhNkGPVv7thUY8V6ea3EJjcw7GxOH8= X-Received: by 2002:a9d:66d4:: with SMTP id t20mr16414147otm.125.1621795286275; Sun, 23 May 2021 11:41:26 -0700 (PDT) MIME-Version: 1.0 References: <20210523172942.3047571-1-hjl.tools@gmail.com> <87tumtmixn.fsf@igel.home> In-Reply-To: <87tumtmixn.fsf@igel.home> Date: Sun, 23 May 2021 11:40:50 -0700 Message-ID: Subject: [PATCH v2] x86-64: Align child stack to 16 bytes [BZ #27902] To: Andreas Schwab X-Spam-Status: No, score=-3033.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: "H.J. Lu via Libc-alpha" Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" On Sun, May 23, 2021 at 10:57 AM Andreas Schwab wrote: > > On Mai 23 2021, H.J. Lu via Libc-alpha wrote: > > > + /* NB: Try to force misaligned child stack. */ > > + char st[STACK_SIZE + 4] __attribute__ ((aligned (1))); > > If you want a misaligned pointer, you need to start with a known > alignment. > Fixed. Here is the v2 patch. OK for master? Thanks. From ae2dc0fe317b93d3ac1d62b2d191d45e9bee0cea Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Sun, 23 May 2021 10:25:10 -0700 Subject: [PATCH v2] x86-64: Align child stack to 16 bytes [BZ #27902] In the x86-64 clone wrapper, align child stack to 16 bytes per the x86-64 psABI. --- sysdeps/unix/sysv/linux/Makefile | 2 +- sysdeps/unix/sysv/linux/tst-misalign-clone.c | 99 ++++++++++++++++++++ sysdeps/unix/sysv/linux/x86_64/clone.S | 3 + 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 sysdeps/unix/sysv/linux/tst-misalign-clone.c diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 70c3b3f8a3..d355b49033 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -109,7 +109,7 @@ tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \ tst-tgkill tst-sysvsem-linux tst-sysvmsg-linux tst-sysvshm-linux \ tst-timerfd tst-ppoll \ tst-clock_adjtime tst-adjtimex tst-ntp_adjtime tst-ntp_gettime \ - tst-ntp_gettimex tst-sigtimedwait + tst-ntp_gettimex tst-sigtimedwait tst-misalign-clone # Test for the symbol version of fcntl that was replaced in glibc 2.28. ifeq ($(have-GLIBC_2.27)$(build-shared),yesyes) diff --git a/sysdeps/unix/sysv/linux/tst-misalign-clone.c b/sysdeps/unix/sysv/linux/tst-misalign-clone.c new file mode 100644 index 0000000000..070a457d64 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-misalign-clone.c @@ -0,0 +1,99 @@ +/* Copyright (C) 2021 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 +#include +#include +#include + +static int +f (void *arg) +{ + bool ok = true; + + puts ("in f"); + + if (TEST_STACK_ALIGN ()) + ok = false; + + return ok ? 0 : 1; +} + +static int +do_test (void) +{ + bool ok = true; + + puts ("in main"); + + if (TEST_STACK_ALIGN ()) + ok = false; + +#ifdef __ia64__ +# define STACK_SIZE (256 * 1024) +#else +# define STACK_SIZE (128 * 1024) +#endif + + char st[STACK_SIZE + 1]; + /* NB: Align child stack to 1 byte. */ + char *stack = PTR_ALIGN_UP (&st[0], 2) + 1; + +#ifdef __ia64__ + extern int __clone2 (int (*__fn) (void *__arg), void *__child_stack_base, + size_t __child_stack_size, int __flags, + void *__arg, ...); + pid_t p = __clone2 (f, stack, STACK_SIZE, 0, 0); +#else +# if _STACK_GROWS_DOWN + pid_t p = clone (f, stack + STACK_SIZE, 0, 0); +# elif _STACK_GROWS_UP + pid_t p = clone (f, stack, 0, 0); +# else +# error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP" +# endif +#endif + if (p == -1) + { + printf("clone failed: %m\n"); + return 1; + } + + int e; + xwaitpid (p, &e, __WCLONE); + if (!WIFEXITED (e)) + { + if (WIFSIGNALED (e)) + printf ("died from signal %s\n", strsignal (WTERMSIG (e))); + else + puts ("did not terminate correctly"); + return 1; + } + if (WEXITSTATUS (e) != 0) + ok = false; + + return ok ? 0 : 1; +} + +#include diff --git a/sysdeps/unix/sysv/linux/x86_64/clone.S b/sysdeps/unix/sysv/linux/x86_64/clone.S index 31ac12da0c..5f52ce7813 100644 --- a/sysdeps/unix/sysv/linux/x86_64/clone.S +++ b/sysdeps/unix/sysv/linux/x86_64/clone.S @@ -57,6 +57,9 @@ ENTRY (__clone) testq %rsi,%rsi /* no NULL stack pointers */ jz SYSCALL_ERROR_LABEL + /* Align stack to 16 bytes per the x86-64 psABI. */ + andq $-16, %rsi + /* Insert the argument onto the new stack. */ subq $16,%rsi movq %rcx,8(%rsi) -- 2.31.1