x86: Allow -fcf-protection with external thunk

Message ID 20200429015953.1446167-1-hjl.tools@gmail.com
State New
Headers show
Series
  • x86: Allow -fcf-protection with external thunk
Related show

Commit Message

Jakub Jelinek via Gcc-patches April 29, 2020, 1:59 a.m.
Allow -fcf-protection with external thunk since the external thunk can be
made compatible with -fcf-protection.

OK for master?

Thanks.

H.J.
---
gcc/

	PR target/93654
	* config/i386/i386-options.c (ix86_set_indirect_branch_type):
	Allow -fcf-protection with -mindirect-branch=thunk-extern and
	-mfunction-return=thunk-extern.
	* doc/invoke.texi: Update notes for -fcf-protection=branch with
	-mindirect-branch=thunk-extern and -mindirect-return=thunk-extern.

gcc/testsuite/

	PR target/93654
	* gcc.target/i386/pr93654.c: New test.
---
 gcc/config/i386/i386-options.c          |  4 ++++
 gcc/doc/invoke.texi                     | 10 +++++++---
 gcc/testsuite/gcc.target/i386/pr93654.c |  9 +++++++++
 3 files changed, 20 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/pr93654.c

-- 
2.25.4

Comments

Jakub Jelinek via Gcc-patches April 29, 2020, 6:37 a.m. | #1
On Wed, Apr 29, 2020 at 4:22 AM H.J. Lu via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>

> Allow -fcf-protection with external thunk since the external thunk can be

> made compatible with -fcf-protection.

>

> OK for master?


OK.  I guess also OK for backporting to branches where CET support is
in a reasonable
state (not sure to what extent you backported the various fixes in this area).

Thanks,
Richard.

> Thanks.

>

> H.J.

> ---

> gcc/

>

>         PR target/93654

>         * config/i386/i386-options.c (ix86_set_indirect_branch_type):

>         Allow -fcf-protection with -mindirect-branch=thunk-extern and

>         -mfunction-return=thunk-extern.

>         * doc/invoke.texi: Update notes for -fcf-protection=branch with

>         -mindirect-branch=thunk-extern and -mindirect-return=thunk-extern.

>

> gcc/testsuite/

>

>         PR target/93654

>         * gcc.target/i386/pr93654.c: New test.

> ---

>  gcc/config/i386/i386-options.c          |  4 ++++

>  gcc/doc/invoke.texi                     | 10 +++++++---

>  gcc/testsuite/gcc.target/i386/pr93654.c |  9 +++++++++

>  3 files changed, 20 insertions(+), 3 deletions(-)

>  create mode 100644 gcc/testsuite/gcc.target/i386/pr93654.c

>

> diff --git a/gcc/config/i386/i386-options.c b/gcc/config/i386/i386-options.c

> index e0be4932534..5c21fce06a4 100644

> --- a/gcc/config/i386/i386-options.c

> +++ b/gcc/config/i386/i386-options.c

> @@ -3083,6 +3083,8 @@ ix86_set_indirect_branch_type (tree fndecl)

>                 ? "thunk-extern" : "thunk"));

>

>        if (cfun->machine->indirect_branch_type != indirect_branch_keep

> +         && (cfun->machine->indirect_branch_type

> +             != indirect_branch_thunk_extern)

>           && (flag_cf_protection & CF_RETURN))

>         error ("%<-mindirect-branch%> and %<-fcf-protection%> are not "

>                "compatible");

> @@ -3126,6 +3128,8 @@ ix86_set_indirect_branch_type (tree fndecl)

>                 ? "thunk-extern" : "thunk"));

>

>        if (cfun->machine->function_return_type != indirect_branch_keep

> +         && (cfun->machine->function_return_type

> +             != indirect_branch_thunk_extern)

>           && (flag_cf_protection & CF_RETURN))

>         error ("%<-mfunction-return%> and %<-fcf-protection%> are not "

>                "compatible");

> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi

> index fed38e88ae5..5bb7d94833e 100644

> --- a/gcc/doc/invoke.texi

> +++ b/gcc/doc/invoke.texi

> @@ -30097,9 +30097,9 @@ Note that @option{-mcmodel=large} is incompatible with

>  @option{-mindirect-branch=thunk-extern} since the thunk function may

>  not be reachable in the large code model.

>

> -Note that @option{-mindirect-branch=thunk-extern} is incompatible with

> -@option{-fcf-protection=branch} since the external thunk cannot be modified

> -to disable control-flow check.

> +Note that @option{-mindirect-branch=thunk-extern} is compatible with

> +@option{-fcf-protection=branch} since the external thunk can be made

> +to enable control-flow check.

>

>  @item -mfunction-return=@var{choice}

>  @opindex mfunction-return

> @@ -30112,6 +30112,10 @@ object file.  You can control this behavior for a specific function by

>  using the function attribute @code{function_return}.

>  @xref{Function Attributes}.

>

> +Note that @option{-mindirect-return=thunk-extern} is compatible with

> +@option{-fcf-protection=branch} since the external thunk can be made

> +to enable control-flow check.

> +

>  Note that @option{-mcmodel=large} is incompatible with

>  @option{-mfunction-return=thunk} and

>  @option{-mfunction-return=thunk-extern} since the thunk function may

> diff --git a/gcc/testsuite/gcc.target/i386/pr93654.c b/gcc/testsuite/gcc.target/i386/pr93654.c

> new file mode 100644

> index 00000000000..ec5bdce86a6

> --- /dev/null

> +++ b/gcc/testsuite/gcc.target/i386/pr93654.c

> @@ -0,0 +1,9 @@

> +/* { dg-do compile } */

> +/* { dg-options "-O2 -fcf-protection -mfunction-return=thunk-extern -mindirect-branch=thunk-extern" } */

> +

> +int

> +bar (void (*foo) (void))

> +{

> +  foo ();

> +  return 0;

> +}

> --

> 2.25.4

>
Jakub Jelinek via Gcc-patches April 29, 2020, 12:02 p.m. | #2
On Tue, Apr 28, 2020 at 11:38 PM Richard Biener
<richard.guenther@gmail.com> wrote:
>

> On Wed, Apr 29, 2020 at 4:22 AM H.J. Lu via Gcc-patches

> <gcc-patches@gcc.gnu.org> wrote:

> >

> > Allow -fcf-protection with external thunk since the external thunk can be

> > made compatible with -fcf-protection.

> >

> > OK for master?

>

> OK.  I guess also OK for backporting to branches where CET support is

> in a reasonable

> state (not sure to what extent you backported the various fixes in this area).

>

> Thanks,

> Richard.


Fixed on master.  I am backporting it to GCC 8 and 9 branches.

Thanks.

-- 
H.J.
Jakub Jelinek via Gcc-patches April 29, 2020, 12:18 p.m. | #3
On Wed, Apr 29, 2020 at 5:02 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>

> On Tue, Apr 28, 2020 at 11:38 PM Richard Biener

> <richard.guenther@gmail.com> wrote:

> >

> > On Wed, Apr 29, 2020 at 4:22 AM H.J. Lu via Gcc-patches

> > <gcc-patches@gcc.gnu.org> wrote:

> > >

> > > Allow -fcf-protection with external thunk since the external thunk can be

> > > made compatible with -fcf-protection.

> > >

> > > OK for master?

> >

> > OK.  I guess also OK for backporting to branches where CET support is

> > in a reasonable

> > state (not sure to what extent you backported the various fixes in this area).

> >

> > Thanks,

> > Richard.

>

> Fixed on master.  I am backporting it to GCC 8 and 9 branches.

>


No need for GCC 8.  It is a regression for GCC 9/10.

-- 
H.J.

Patch

diff --git a/gcc/config/i386/i386-options.c b/gcc/config/i386/i386-options.c
index e0be4932534..5c21fce06a4 100644
--- a/gcc/config/i386/i386-options.c
+++ b/gcc/config/i386/i386-options.c
@@ -3083,6 +3083,8 @@  ix86_set_indirect_branch_type (tree fndecl)
 		? "thunk-extern" : "thunk"));
 
       if (cfun->machine->indirect_branch_type != indirect_branch_keep
+	  && (cfun->machine->indirect_branch_type
+	      != indirect_branch_thunk_extern)
 	  && (flag_cf_protection & CF_RETURN))
 	error ("%<-mindirect-branch%> and %<-fcf-protection%> are not "
 	       "compatible");
@@ -3126,6 +3128,8 @@  ix86_set_indirect_branch_type (tree fndecl)
 		? "thunk-extern" : "thunk"));
 
       if (cfun->machine->function_return_type != indirect_branch_keep
+	  && (cfun->machine->function_return_type
+	      != indirect_branch_thunk_extern)
 	  && (flag_cf_protection & CF_RETURN))
 	error ("%<-mfunction-return%> and %<-fcf-protection%> are not "
 	       "compatible");
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index fed38e88ae5..5bb7d94833e 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -30097,9 +30097,9 @@  Note that @option{-mcmodel=large} is incompatible with
 @option{-mindirect-branch=thunk-extern} since the thunk function may
 not be reachable in the large code model.
 
-Note that @option{-mindirect-branch=thunk-extern} is incompatible with
-@option{-fcf-protection=branch} since the external thunk cannot be modified
-to disable control-flow check.
+Note that @option{-mindirect-branch=thunk-extern} is compatible with
+@option{-fcf-protection=branch} since the external thunk can be made
+to enable control-flow check.
 
 @item -mfunction-return=@var{choice}
 @opindex mfunction-return
@@ -30112,6 +30112,10 @@  object file.  You can control this behavior for a specific function by
 using the function attribute @code{function_return}.
 @xref{Function Attributes}.
 
+Note that @option{-mindirect-return=thunk-extern} is compatible with
+@option{-fcf-protection=branch} since the external thunk can be made
+to enable control-flow check.
+
 Note that @option{-mcmodel=large} is incompatible with
 @option{-mfunction-return=thunk} and
 @option{-mfunction-return=thunk-extern} since the thunk function may
diff --git a/gcc/testsuite/gcc.target/i386/pr93654.c b/gcc/testsuite/gcc.target/i386/pr93654.c
new file mode 100644
index 00000000000..ec5bdce86a6
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr93654.c
@@ -0,0 +1,9 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fcf-protection -mfunction-return=thunk-extern -mindirect-branch=thunk-extern" } */
+
+int
+bar (void (*foo) (void))
+{
+  foo (); 
+  return 0;
+}