[PATCH] i386: Check -mmanual-endbr in pass_insert_endbranch::gate

H.J. Lu hjl.tools@gmail.com
Thu Feb 14 14:35:00 GMT 2019


When -mmanual-endbr is used with -fcf-protection, only functions marked
with cf_check attribute should be instrumented with ENDBR.  We should
skip rest_of_insert_endbranch on functions without cf_check attribute.

OK for trunk?

Thanks.

H.J.
---
gcc/

	PR target/89353
	* config/i386/i386.c (rest_of_insert_endbranch): Move the
	-mmanual-endbr and cf_check attribute check to ..
	(pass_insert_endbranch::gate): Here.

gcc/testsuite/

	PR target/89353
	* gcc.target/i386/cf_check-6.c: New test.
---
 gcc/config/i386/i386.c                     | 10 +++++-----
 gcc/testsuite/gcc.target/i386/cf_check-6.c | 22 ++++++++++++++++++++++
 2 files changed, 27 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/cf_check-6.c

diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
index fd05873ba39..a99ca23fffa 100644
--- a/gcc/config/i386/i386.c
+++ b/gcc/config/i386/i386.c
@@ -2640,9 +2640,6 @@ rest_of_insert_endbranch (void)
 
   if (!lookup_attribute ("nocf_check",
 			 TYPE_ATTRIBUTES (TREE_TYPE (cfun->decl)))
-      && (!flag_manual_endbr
-	  || lookup_attribute ("cf_check",
-			       DECL_ATTRIBUTES (cfun->decl)))
       && !cgraph_node::get (cfun->decl)->only_called_directly_p ())
     {
       /* Queue ENDBR insertion to x86_function_profiler.  */
@@ -2773,9 +2770,12 @@ public:
   {}
 
   /* opt_pass methods: */
-  virtual bool gate (function *)
+  virtual bool gate (function *fun)
     {
-      return ((flag_cf_protection & CF_BRANCH));
+      return ((flag_cf_protection & CF_BRANCH)
+	      && (!flag_manual_endbr
+		  || lookup_attribute ("cf_check",
+				       DECL_ATTRIBUTES (fun->decl))));
     }
 
   virtual unsigned int execute (function *)
diff --git a/gcc/testsuite/gcc.target/i386/cf_check-6.c b/gcc/testsuite/gcc.target/i386/cf_check-6.c
new file mode 100644
index 00000000000..292b964238d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/cf_check-6.c
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fcf-protection -mmanual-endbr" } */
+/* { dg-final { scan-assembler-not {\mendbr} } } */
+
+int
+bar (int* val)
+{
+  int status = 99;
+
+  if((val == 0))
+    {
+      status = 22;
+      goto end;
+    }
+
+  extern int x;
+  *val = x;
+
+  status = 0;
+end:
+  return status;
+}
-- 
2.20.1



More information about the Gcc-patches mailing list