From patchwork Sun May 23 17:29:42 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: 43545 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 6A384388C02E; Sun, 23 May 2021 17:29:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A384388C02E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1621790988; bh=IFZXUCuK8CpfWywTFuLNvkVrrBrf9/jbdS814TN0dFE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Vnu88GiP0/isng42xIJlFkzcV0M+CmzzQXK0+RAPCykVNoOSeLX/LiCdQ3sNCqiPD REkODNUukxNURbGMjmO5x9D1fRTiUYOQfVBstCRZQpo3uqFcv6HrLbuO3fBgUZ50GG TUAwvCdW2qp2kYt/oK9KGH00YPPkc/qQLL86sBjs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by sourceware.org (Postfix) with ESMTPS id 9FB87385380B for ; Sun, 23 May 2021 17:29:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9FB87385380B Received: by mail-pg1-x529.google.com with SMTP id t193so18365009pgb.4 for ; Sun, 23 May 2021 10:29:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=IFZXUCuK8CpfWywTFuLNvkVrrBrf9/jbdS814TN0dFE=; b=gQyTIv0oVsYtWa7wFEhdw18CE277OaVG5OSM9wyrVkpmMY+5OP3IxEPmzNfm9IhMyl /+fe3cmMjfp+AIggj0SuodKG0DcZmwxmD/cmtcAMHLeSXuaAuztl8267EKMsA3FN8747 jijevAFtBodFNFZRwx5wxxLIHoulcYqyU4oN0m6TW4J7in01EjG52YYVC9WgU5dop1zr Ntqqepgaz6mIyj7Jf/6pkCYjIWZqD1ZN6dOAOHCeBNUwl0KT2JhJ/yk5PwvaqjYn5+AN 0LufOoIalmVFs+xEXBjMmAXQiBy/SgFyUHl7PlkxsVN8pjWv+yyY4zgvtuTvoCPRUgKN y5Dw== X-Gm-Message-State: AOAM530roWmtP+Ch8WwM+z5Nx2OfmadTaAE2R3fqKlb9lcFwHxecgrp6 mN5o2oMqjSRiesHt4L3rg+hDP4vV5ho= X-Google-Smtp-Source: ABdhPJz2lKO4LTA7zvcWp64WTgQMmleruNYna1XD0kt35ShGMGUAde+WBBUO1i3V5zhJlAv8WjUsUg== X-Received: by 2002:a63:5110:: with SMTP id f16mr9506435pgb.51.1621790984436; Sun, 23 May 2021 10:29:44 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.56.38.102]) by smtp.gmail.com with ESMTPSA id 136sm8989288pfu.195.2021.05.23.10.29.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 May 2021 10:29:43 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id 8912BC0330 for ; Sun, 23 May 2021 10:29:42 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH] x86-64: Align child stack to 16 bytes Date: Sun, 23 May 2021 10:29:42 -0700 Message-Id: <20210523172942.3047571-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3035.1 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" Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" 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 | 97 ++++++++++++++++++++ sysdeps/unix/sysv/linux/x86_64/clone.S | 3 + 3 files changed, 101 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 9bcd38a732..283b2d0bd8 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -327,5 +327,5 @@ CFLAGS-gai.c += -DNEED_NETLINK endif ifeq ($(subdir),nptl) -tests += tst-align-clone tst-getpid1 +tests += tst-align-clone tst-misalign-clone tst-getpid1 endif 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..3eb91b9b1f --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-misalign-clone.c @@ -0,0 +1,97 @@ +/* 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 + +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 + + /* NB: Try to force misaligned child stack. */ + char st[STACK_SIZE + 4] __attribute__ ((aligned (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, st, sizeof (st), 0, 0); +#else +# if _STACK_GROWS_DOWN + pid_t p = clone (f, st + sizeof (st), 0, 0); +# elif _STACK_GROWS_UP + pid_t p = clone (f, st, 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)