From patchwork Fri Oct 21 07:31:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 1692899 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=XYUztacI; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Mtx525blcz1ygT for ; Fri, 21 Oct 2022 18:32:38 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3C98B385737D for ; Fri, 21 Oct 2022 07:32:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3C98B385737D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666337556; bh=v3ZEc7RLzaE6pWmnVx5GloCs2fe7Ef3i8tKQkeTsmVg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=XYUztacITNM26uu/QYBA2wqLjq721KhFtmZAd+b3NjE6jJSVym4hMijqXcVKHmO3e jPi0B0AgBPcbWkkW+SWg9stwWAx3AjeuQAJlDyc5n+CDHxF1Uq85h4lpKQcnvZzQbA rx+61eHqkYcUMPB+47fbk7al7pQ+yg6k0+QZ76dk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTPS id 9BA693857349 for ; Fri, 21 Oct 2022 07:31:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9BA693857349 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 7C54E116105; Fri, 21 Oct 2022 03:31:18 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 1Jx8UIn9WGYB; Fri, 21 Oct 2022 03:31:18 -0400 (EDT) Received: from free.home (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPS id F2493116100; Fri, 21 Oct 2022 03:31:17 -0400 (EDT) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 29L7V9og964525 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 21 Oct 2022 04:31:10 -0300 To: gcc-patches@gcc.gnu.org Subject: [PATCH zero-call-used-regs] Add leafy mode for zero-call-used-regs Organization: Free thinker, does not speak for AdaCore Date: Fri, 21 Oct 2022 04:31:09 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alexandre Oliva via Gcc-patches From: Alexandre Oliva Reply-To: Alexandre Oliva Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Introduce 'leafy' to auto-select between 'used' and 'all' for leaf and nonleaf functions, respectively. Regstrapped on x86_64-linux-gnu. Ok to install? for gcc/ChangeLog * doc/extend.texi (zero-call-used-regs): Document leafy and variants thereof. * flag-types.h (zero_regs_flags): Add LEAFY_MODE, as well as LEAFY and variants. * function.cc (gen_call_ued_regs_seq): Set only_used for leaf functions in leafy mode. * opts.cc (zero_call_used_regs_opts): Add leafy and variants. for gcc/testsuite/ChangeLog * c-c++-common/zero-scratch-regs-leafy-1.c: New. * c-c++-common/zero-scratch-regs-leafy-2.c: New. * gcc.target/i386/zero-scratch-regs-leafy-1.c: New. * gcc.target/i386/zero-scratch-regs-leafy-2.c: New. --- gcc/doc/extend.texi | 22 ++++++++++++++++++-- gcc/flag-types.h | 5 +++++ gcc/function.cc | 3 +++ gcc/opts.cc | 4 ++++ .../c-c++-common/zero-scratch-regs-leafy-1.c | 15 ++++++++++++++ .../c-c++-common/zero-scratch-regs-leafy-2.c | 21 +++++++++++++++++++ .../gcc.target/i386/zero-scratch-regs-leafy-1.c | 12 +++++++++++ .../gcc.target/i386/zero-scratch-regs-leafy-2.c | 16 +++++++++++++++ 8 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-1.c create mode 100644 gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-2.c create mode 100644 gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-1.c create mode 100644 gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-2.c diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 04af0584d82cc..bf11956c467fb 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -4391,10 +4391,28 @@ zeros all call-used registers that pass arguments. @item all-gpr-arg zeros all call-used general purpose registers that pass arguments. + +@item leafy +Same as @samp{used} in a leaf function, and same as @samp{all} in a +nonleaf function. + +@item leafy-gpr +Same as @samp{used-gpr} in a leaf function, and same as @samp{all-gpr} +in a nonleaf function. + +@item leafy-arg +Same as @samp{used-arg} in a leaf function, and same as @samp{all-arg} +in a nonleaf function. + +@item leafy-gpr-arg +Same as @samp{used-gpr-arg} in a leaf function, and same as +@samp{all-gpr-arg} in a nonleaf function. + @end table -Of this list, @samp{used-arg}, @samp{used-gpr-arg}, @samp{all-arg}, -and @samp{all-gpr-arg} are mainly used for ROP mitigation. +Of this list, @samp{used-arg}, @samp{used-gpr-arg}, @samp{leafy-arg}, +@samp{leafy-gpr-arg}, @samp{all-arg}, and @samp{all-gpr-arg} are mainly +used for ROP mitigation. The default for the attribute is controlled by @option{-fzero-call-used-regs}. @end table diff --git a/gcc/flag-types.h b/gcc/flag-types.h index d2e751060ffce..b90c85167dcd4 100644 --- a/gcc/flag-types.h +++ b/gcc/flag-types.h @@ -338,6 +338,7 @@ namespace zero_regs_flags { const unsigned int ONLY_GPR = 1UL << 2; const unsigned int ONLY_ARG = 1UL << 3; const unsigned int ENABLED = 1UL << 4; + const unsigned int LEAFY_MODE = 1UL << 5; const unsigned int USED_GPR_ARG = ENABLED | ONLY_USED | ONLY_GPR | ONLY_ARG; const unsigned int USED_GPR = ENABLED | ONLY_USED | ONLY_GPR; const unsigned int USED_ARG = ENABLED | ONLY_USED | ONLY_ARG; @@ -346,6 +347,10 @@ namespace zero_regs_flags { const unsigned int ALL_GPR = ENABLED | ONLY_GPR; const unsigned int ALL_ARG = ENABLED | ONLY_ARG; const unsigned int ALL = ENABLED; + const unsigned int LEAFY_GPR_ARG = ENABLED | LEAFY_MODE | ONLY_GPR | ONLY_ARG; + const unsigned int LEAFY_GPR = ENABLED | LEAFY_MODE | ONLY_GPR; + const unsigned int LEAFY_ARG = ENABLED | LEAFY_MODE | ONLY_ARG; + const unsigned int LEAFY = ENABLED | LEAFY_MODE; } /* Settings of flag_incremental_link. */ diff --git a/gcc/function.cc b/gcc/function.cc index 6474a663b30b8..16582e698041a 100644 --- a/gcc/function.cc +++ b/gcc/function.cc @@ -5879,6 +5879,9 @@ gen_call_used_regs_seq (rtx_insn *ret, unsigned int zero_regs_type) only_used = zero_regs_type & ONLY_USED; only_arg = zero_regs_type & ONLY_ARG; + if ((zero_regs_type & LEAFY_MODE) && leaf_function_p ()) + only_used = true; + /* For each of the hard registers, we should zero it if: 1. it is a call-used register; and 2. it is not a fixed register; diff --git a/gcc/opts.cc b/gcc/opts.cc index ae079fcd20eea..39f6a1b278dc6 100644 --- a/gcc/opts.cc +++ b/gcc/opts.cc @@ -2099,6 +2099,10 @@ const struct zero_call_used_regs_opts_s zero_call_used_regs_opts[] = ZERO_CALL_USED_REGS_OPT (all-gpr, zero_regs_flags::ALL_GPR), ZERO_CALL_USED_REGS_OPT (all-arg, zero_regs_flags::ALL_ARG), ZERO_CALL_USED_REGS_OPT (all, zero_regs_flags::ALL), + ZERO_CALL_USED_REGS_OPT (leafy-gpr-arg, zero_regs_flags::LEAFY_GPR_ARG), + ZERO_CALL_USED_REGS_OPT (leafy-gpr, zero_regs_flags::LEAFY_GPR), + ZERO_CALL_USED_REGS_OPT (leafy-arg, zero_regs_flags::LEAFY_ARG), + ZERO_CALL_USED_REGS_OPT (leafy, zero_regs_flags::LEAFY), #undef ZERO_CALL_USED_REGS_OPT {NULL, 0U} }; diff --git a/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-1.c b/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-1.c new file mode 100644 index 0000000000000..c1a0c31ba1c37 --- /dev/null +++ b/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-1.c @@ -0,0 +1,15 @@ +/* { dg-do run } */ +/* { dg-options "-O2 -fzero-call-used-regs=leafy" } */ + +volatile int result = 0; +int +__attribute__((noipa)) +foo (int x) +{ + return x; +} +int main() +{ + result = foo (2); + return 0; +} diff --git a/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-2.c b/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-2.c new file mode 100644 index 0000000000000..d450620c1fcfe --- /dev/null +++ b/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-2.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +#include +int result = 0; + +int +__attribute__((noipa)) +__attribute__ ((zero_call_used_regs("leafy"))) +foo1 (int x) +{ + return (x + 1); +} + +int +__attribute__((noipa)) +__attribute__ ((zero_call_used_regs("leafy"))) +foo2 (int x) +{ + return foo1 (x + 2); +} diff --git a/gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-1.c b/gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-1.c new file mode 100644 index 0000000000000..2277710c771b7 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fzero-call-used-regs=leafy -fno-stack-protector -fno-PIC" } */ + +void +foo (void) +{ +} + +/* { dg-final { scan-assembler-not "vzeroall" } } */ +/* { dg-final { scan-assembler-not "%xmm" } } */ +/* { dg-final { scan-assembler-not "xorl\[ \t\]+%" } } */ +/* { dg-final { scan-assembler-not "movl\[ \t\]+%" } } */ diff --git a/gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-2.c b/gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-2.c new file mode 100644 index 0000000000000..24b85c3dbb766 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-2.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fzero-call-used-regs=leafy-gpr -fno-stack-protector -fno-PIC" } */ + +extern int bar (int); + +void +foo (void) +{ + int x = bar (0); + if (x) + bar (1); +} + +/* { dg-final { scan-assembler "xorl\[ \t\]+%eax, %eax" } } */ +/* { dg-final { scan-assembler "xorl\[ \t\]+%edx, %edx" } } */ +/* { dg-final { scan-assembler "xorl\[ \t\]+%ecx, %ecx" } } */