From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x32.google.com (mail-oa1-x32.google.com [IPv6:2001:4860:4864:20::32]) by sourceware.org (Postfix) with ESMTPS id 3B8063858414 for ; Fri, 23 Jun 2023 23:27:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3B8063858414 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-oa1-x32.google.com with SMTP id 586e51a60fabf-1a9acdddb20so1631485fac.0 for ; Fri, 23 Jun 2023 16:27:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1687562863; x=1690154863; h=content-transfer-encoding:mime-version:user-agent:message-id :in-reply-to:date:errors-to:references:organization:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=vBaT1xptoUMvAsLclZ1y7R+nb0LXHoh2BGTH0/7NvG8=; b=L6D3D/C17Vn2xptQLzOeXxV8pemE5TV4UIpHuZRHjLBHR0SzQY0FvQtx4qTMnyz8in WXsyJKueUd7w8sajnHBwf7caZ3iZBj+V22Rvp0scMQT9C8cMQOcXTe9Db1zMmbdsn+Q0 GAEHyMvg/omq2A7eWRbo8XvFuaaEVi0Lh2xEx5Z7gFEOrw1ldgLq8FIRLz88TcoHwwwZ 5cmeTzcfe6iUrCjJqZW+Vp1tLMiWQGF6SPmSDc+ok1ktRv+T0NQs9IgbrPqqK/GTnu9Z fIhggrotClHLTFIOyf8bBz26gPrydE8FD2uPD5SdlV+NBS9U9+p0jRbQbX3o03ExNpzl IJ5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687562863; x=1690154863; h=content-transfer-encoding:mime-version:user-agent:message-id :in-reply-to:date:errors-to:references:organization:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vBaT1xptoUMvAsLclZ1y7R+nb0LXHoh2BGTH0/7NvG8=; b=jq1lQtp3Ue6tv4IyGcZHVotu7EiSwlQIe+dinHUTuU8Y3JZqJZJFp8+v2a/CnwBeW4 j0rfxdgps3r1qqTx3UhL2d2yUtryaG/XqlzFltKsS2ZOj2Zykj2YIYfvY49F8M4USH3i giHumsVK3aFQF43lmCkHzLbun1cEzxY/Pz9zDc155r69z1yzl3JIVcZadchfVVdq7tX6 omqIKMHOuCwewp5eiRFbG8eB0pCD/EBz+rc/n1iVtYgMjWQF1R5ITWkgEF3PqPis60BG iULaeTg4Sbq121d/0z8cJfAWH3PcI9xCjaun8vLsqzYZvKFnWJBeGMpbyGwSp/lXuWBJ wT0A== X-Gm-Message-State: AC+VfDxl6115T9F78G6iOFUZ0wnLe01Y3HSkhVUnr1SuIXO/Lv3ILGrd WlxoYgc9M0hI9ZDhG62qFvSbnmVI8lTMsITZdLo= X-Google-Smtp-Source: ACHHUZ6j/YNMaUD4whFv+Y8bRpdNcGT7Zcp/pjaEy1Y+WGAFmsYCo7/Y/+UTUilO820ZYfE5KaefbA== X-Received: by 2002:a05:6870:9885:b0:1a6:8637:47ce with SMTP id eg5-20020a056870988500b001a6863747cemr8908922oab.6.1687562863423; Fri, 23 Jun 2023 16:27:43 -0700 (PDT) Received: from free.home ([2804:7f1:2080:5f5c:a5d4:5604:3034:12b5]) by smtp.gmail.com with ESMTPSA id k3-20020a056870570300b0019e4fe93d72sm414334oap.42.2023.06.23.16.27.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 16:27:42 -0700 (PDT) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 35NNRRO2881488 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 23 Jun 2023 20:27:28 -0300 From: Alexandre Oliva To: Qing Zhao via Gcc-patches Cc: Qing Zhao Subject: [PATCH v3] Add leafy mode for zero-call-used-regs Organization: Free thinker, does not speak for AdaCore 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> Errors-To: aoliva@lxoliva.fsfla.org Date: Fri, 23 Jun 2023 20:27:27 -0300 In-Reply-To: (Qing Zhao via Gcc-patches's message of "Fri, 23 Jun 2023 14:47:11 +0000") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,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 Jun 23, 2023, Qing Zhao via Gcc-patches wrote: > It=E2=80=99s better to add this definition earlier in the list of the =E2= =80=9Cthree > basic values=E2=80=9D, to make it =E2=80=9Cfour basic values=E2=80=9D, li= ke 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=20 > call-used registers in leaf functions.=E2=80=9D just for your referen= ce, > 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? 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: =20 @itemize @bullet @item @@ -4362,10 +4362,16 @@ the function. =20 @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 =20 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: =20 @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 =20 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. =20 The default for the attribute is controlled by @option{-fzero-call-used-re= gs}. @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 =3D 1UL << 2; const unsigned int ONLY_ARG =3D 1UL << 3; const unsigned int ENABLED =3D 1UL << 4; + const unsigned int LEAFY_MODE =3D 1UL << 5; const unsigned int USED_GPR_ARG =3D ENABLED | ONLY_USED | ONLY_GPR | ONL= Y_ARG; const unsigned int USED_GPR =3D ENABLED | ONLY_USED | ONLY_GPR; const unsigned int USED_ARG =3D ENABLED | ONLY_USED | ONLY_ARG; @@ -356,6 +357,10 @@ namespace zero_regs_flags { const unsigned int ALL_GPR =3D ENABLED | ONLY_GPR; const unsigned int ALL_ARG =3D ENABLED | ONLY_ARG; const unsigned int ALL =3D ENABLED; + const unsigned int LEAFY_GPR_ARG =3D ENABLED | LEAFY_MODE | ONLY_GPR | O= NLY_ARG; + const unsigned int LEAFY_GPR =3D ENABLED | LEAFY_MODE | ONLY_GPR; + const unsigned int LEAFY_ARG =3D ENABLED | LEAFY_MODE | ONLY_ARG; + const unsigned int LEAFY =3D ENABLED | LEAFY_MODE; } =20 /* 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 z= ero_regs_type) only_used =3D zero_regs_type & ONLY_USED; only_arg =3D zero_regs_type & ONLY_ARG; =20 + if ((zero_regs_type & LEAFY_MODE) && leaf_function_p ()) + only_used =3D 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_us= ed_regs_opts[] =3D 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/t= estsuite/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=3Dleafy" } */ + +volatile int result =3D 0; +int=20 +__attribute__((noipa)) +foo (int x) +{ + return x; +} +int main() +{ + result =3D foo (2); + return 0; +} diff --git a/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-2.c b/gcc/t= estsuite/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 =3D 0; + +int=20 +__attribute__((noipa)) +__attribute__ ((zero_call_used_regs("leafy"))) +foo1 (int x) +{ + return (x + 1); +} + +int=20 +__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/gc= c/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=3Dleafy -fno-stack-protector -f= no-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/gc= c/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=3Dleafy-gpr -fno-stack-protecto= r -fno-PIC" } */ + +extern int bar (int); + +void +foo (void) +{ + int x =3D 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" } } */ --=20 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