From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 375783858D33 for ; Tue, 27 Jun 2023 06:27:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 375783858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 3623821850; Tue, 27 Jun 2023 06:27:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1687847256; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=w6qnsYCwVMHbCFh5lgFDSUYsi0JRTEsBb8i1ZKraeUQ=; b=BvBZ8kdwWxBNxfS96rKk7tLTVgxliJ8pfn3Zomg4Re9O3b+ZuTlUjH54JiWJwjbheMqjvk MadlK4C47TPke/6AoVn82WW2RkY4YD8/p/Geh0EP2ZqwtztnZ5CSG+lVbsLS6S2wiw9BnU 32A4pPJYIHYj2axu9IlRMPiaFLSbs6s= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1687847256; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=w6qnsYCwVMHbCFh5lgFDSUYsi0JRTEsBb8i1ZKraeUQ=; b=s1FJN6R3uQmC3md7ZNKn9whN59KgZszQXErnH33m1BNHpExxor9091U20PkQe0Cuq/3G82 SzztMTnn7cNKW6Ag== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 16F7A2C141; Tue, 27 Jun 2023 06:27:36 +0000 (UTC) Date: Tue, 27 Jun 2023 06:27:36 +0000 (UTC) From: Richard Biener To: Qing Zhao cc: Alexandre Oliva , Qing Zhao via Gcc-patches Subject: Re: [PATCH v3] Add leafy mode for zero-call-used-regs In-Reply-To: <5E5837EE-01C5-4971-96FE-9CB72F58B22D@oracle.com> Message-ID: References: <66ECAC37-E763-4469-B31A-7A2B031026F4@oracle.com> <0DA25420-AE6D-418B-B2F6-7A99BCC34345@oracle.com> <3CF608E7-C293-4627-8FE9-8B580D69D764@oracle.com> <80999785-4FFD-4C6E-B501-57E92F45053A@oracle.com> <5E5837EE-01C5-4971-96FE-9CB72F58B22D@oracle.com> User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Mon, 26 Jun 2023, Qing Zhao wrote: > > > > On Jun 23, 2023, at 7:27 PM, Alexandre Oliva wrote: > > > > On Jun 23, 2023, Qing Zhao via Gcc-patches wrote: > > > >> It?s better to add this definition earlier in the list of the ?three > >> basic values?, to make it ?four basic values?, like the following: > > > > Oh, my, sorry for being so dense, I had managed to miss that bit all > > this time somehow :-( > > > >> The sentence "This value is mainly to provide users a more efficient mode to zero > >> call-used registers in leaf functions.? just for your reference, > >> the wording can certainly be improved. -:) > > > > :-) got it, thanks. How about this? > > > > > > Add leafy mode for zero-call-used-regs > > > > Introduce 'leafy' to auto-select between 'used' and 'all' for leaf and > > nonleaf functions, respectively. > > > > Regstrapped on x86_64-linux-gnu. Ok to install? > > LGTM now. > > However, I am not a reviewer, you might still need approval from a middle-end reviewer. OK. > thanks. > > Qing > > > > > > > > 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 | 30 ++++++++++++++++++-- > > 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, 103 insertions(+), 3 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 852f6b629bea8..739c40368f556 100644 > > --- a/gcc/doc/extend.texi > > +++ b/gcc/doc/extend.texi > > @@ -4349,7 +4349,7 @@ through registers. > > In order to satisfy users with different security needs and control the > > run-time overhead at the same time, the @var{choice} parameter provides a > > flexible way to choose the subset of the call-used registers to be zeroed. > > -The three basic values of @var{choice} are: > > +The four basic values of @var{choice} are: > > > > @itemize @bullet > > @item > > @@ -4362,10 +4362,16 @@ the function. > > > > @item > > @samp{all} zeros all call-used registers. > > + > > +@item > > +@samp{leafy} behaves like @samp{used} in a leaf function, and like > > +@samp{all} in a nonleaf function. This makes for leaner zeroing in leaf > > +functions, where the set of used registers is known, and that may be > > +enough for some purposes of register zeroing. > > @end itemize > > > > In addition to these three basic choices, it is possible to modify > > -@samp{used} or @samp{all} as follows: > > +@samp{used}, @samp{all}, and @samp{leafy} as follows: > > > > @itemize @bullet > > @item > > @@ -4412,10 +4418,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. > > +@samp{all-gpr-arg}, @samp{leafy-arg}, and @samp{leafy-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 2e650bf1c487c..0d2dab1b99dd4 100644 > > --- a/gcc/flag-types.h > > +++ b/gcc/flag-types.h > > @@ -348,6 +348,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; > > @@ -356,6 +357,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 6a79a8290f67e..dd2c1136e0725 100644 > > --- a/gcc/function.cc > > +++ b/gcc/function.cc > > @@ -5866,6 +5866,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 3087bdac2c6c9..ac81d4e42944d 100644 > > --- a/gcc/opts.cc > > +++ b/gcc/opts.cc > > @@ -2115,6 +2115,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" } } */ > > > > > > -- > > Alexandre Oliva, happy hacker https://FSFLA.org/blogs/lxo/ > > Free Software Activist GNU Toolchain Engineer > > Disinformation flourishes because many people care deeply about injustice > > but very few check the facts. Ask me about > > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; HRB 36809 (AG Nuernberg)