public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Alexandre Oliva <aoliva@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc(refs/users/aoliva/heads/testme)] hardcfr: fix builtin_return noreturn mishandling
Date: Sat,  1 Oct 2022 04:50:07 +0000 (GMT)	[thread overview]
Message-ID: <20221001045007.4E3083858282@sourceware.org> (raw)

https://gcc.gnu.org/g:a162f8e5378c2dfeada2c38b5847c953e978c48e

commit a162f8e5378c2dfeada2c38b5847c953e978c48e
Author: Alexandre Oliva <oliva@adacore.com>
Date:   Fri Sep 30 23:20:33 2022 -0300

    hardcfr: fix builtin_return noreturn mishandling

Diff:
---
 gcc/gimple-harden-control-flow.cc                      | 18 ++++++++++--------
 .../c-c++-common/torture/harden-cfr-bret-always.c      | 13 +++++++++++++
 .../c-c++-common/torture/harden-cfr-bret-never.c       | 13 +++++++++++++
 .../c-c++-common/torture/harden-cfr-bret-noopt.c       | 12 ++++++++++++
 .../c-c++-common/torture/harden-cfr-bret-noret.c       | 12 ++++++++++++
 .../c-c++-common/torture/harden-cfr-bret-nothrow.c     | 13 +++++++++++++
 .../c-c++-common/torture/harden-cfr-bret-retcl.c       | 12 ++++++++++++
 gcc/testsuite/c-c++-common/torture/harden-cfr-bret.c   |  8 +++++++-
 8 files changed, 92 insertions(+), 9 deletions(-)

diff --git a/gcc/gimple-harden-control-flow.cc b/gcc/gimple-harden-control-flow.cc
index 1c93bf622e8..cd5b3cf69b2 100644
--- a/gcc/gimple-harden-control-flow.cc
+++ b/gcc/gimple-harden-control-flow.cc
@@ -1294,12 +1294,15 @@ pass_harden_control_flow_redundancy::execute (function *fun)
 	    continue;
 	  }
 
-	/* If there are no exceptions, then any noreturn call must have
-	   zero successor edges.  Otherwise, check for blocks without
-	   non-EH successors, but skip those with resx stmts and edges
-	   (i.e., those other than that in bb_eh_cleanup), since those
-	   will go through bb_eh_cleanup, that will have been counted as
-	   noreturn above because it has no successors.  */
+	/* If there are no exceptions, it would seem like any noreturn
+	   call must have zero successor edges, but __builtin_return
+	   gets successor edges.  We don't want to handle it here, it
+	   will be dealt with in sibcall_search_preds.  Otherwise,
+	   check for blocks without non-EH successors, but skip those
+	   with resx stmts and edges (i.e., those other than that in
+	   bb_eh_cleanup), since those will go through bb_eh_cleanup,
+	   that will have been counted as noreturn above because it
+	   has no successors.  */
 	gcc_checking_assert (bb != bb_eh_cleanup
 			     || !check_at_escaping_exceptions);
 	if (flag_exceptions && is_a <gresx *> (stmt)
@@ -1309,8 +1312,7 @@ pass_harden_control_flow_redundancy::execute (function *fun)
 	    ? false
 	    : (!flag_exceptions
 	       || gimple_call_nothrow_p (as_a <gcall *> (stmt)))
-	    ? (/* Catch cases that should not have successors.  */
-	       gcc_unreachable (), check_before_nothrow_noreturn_calls)
+	    ? false /* rather than check_before_nothrow_noreturn_calls */
 	    : always_throwing_noreturn_call_p (stmt)
 	    ? check_before_always_throwing_noreturn_calls
 	    : check_before_throwing_noreturn_calls)
diff --git a/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-always.c b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-always.c
new file mode 100644
index 00000000000..779896c60e8
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-always.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-fharden-control-flow-redundancy -fhardcfr-check-noreturn-calls=always -fdump-tree-hardcfr -ffat-lto-objects" } */
+
+/* Check that, even enabling all checks before noreturn calls (leaving
+   returning calls enabled), we get checks before __builtin_return without
+   duplication (__builtin_return is both noreturn and a returning call).  */
+
+#include "harden-cfr-bret.c"
+
+/* Out-of-line checking, before both builtin_return and return in f.  */
+/* { dg-final { scan-tree-dump-times "__hardcfr_check" 2 "hardcfr" } } */
+/* Inline checking before builtin_return in g.  */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 1 "hardcfr" } } */
diff --git a/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-never.c b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-never.c
new file mode 100644
index 00000000000..49ce17f5b93
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-never.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-fharden-control-flow-redundancy -fhardcfr-check-noreturn-calls=never -fdump-tree-hardcfr -ffat-lto-objects" } */
+
+/* Check that, even enabling checks before never noreturn calls (leaving
+   returning calls enabled), we get checks before __builtin_return without
+   duplication (__builtin_return is both noreturn and a returning call).  */
+
+#include "harden-cfr-bret.c"
+
+/* Out-of-line checking, before both builtin_return and return in f.  */
+/* { dg-final { scan-tree-dump-times "__hardcfr_check" 2 "hardcfr" } } */
+/* Inline checking before builtin_return in g.  */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 1 "hardcfr" } } */
diff --git a/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-noopt.c b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-noopt.c
new file mode 100644
index 00000000000..1512614791f
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-noopt.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-fharden-control-flow-redundancy -fhardcfr-check-noreturn-calls=never -fno-hardcfr-check-returning-calls -fdump-tree-hardcfr -ffat-lto-objects" } */
+
+/* Check that, even disabling checks before both noreturn and returning
+   calls, we still get checks before __builtin_return.  */
+
+#include "harden-cfr-bret.c"
+
+/* Out-of-line checking, before both builtin_return and return in f.  */
+/* { dg-final { scan-tree-dump-times "__hardcfr_check" 2 "hardcfr" } } */
+/* Inline checking before builtin_return in g.  */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 1 "hardcfr" } } */
diff --git a/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-noret.c b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-noret.c
new file mode 100644
index 00000000000..fd95bb7e3e3
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-noret.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-fharden-control-flow-redundancy -fno-hardcfr-check-returning-calls -fdump-tree-hardcfr -ffat-lto-objects" } */
+
+/* Check that, even disabling checks before returning calls (leaving noreturn
+   calls enabled), we still get checks before __builtin_return.  */
+
+#include "harden-cfr-bret.c"
+
+/* Out-of-line checking, before both builtin_return and return in f.  */
+/* { dg-final { scan-tree-dump-times "__hardcfr_check" 2 "hardcfr" } } */
+/* Inline checking before builtin_return in g.  */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 1 "hardcfr" } } */
diff --git a/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-nothrow.c b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-nothrow.c
new file mode 100644
index 00000000000..c5c361234c4
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-nothrow.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-fharden-control-flow-redundancy -fhardcfr-check-noreturn-calls=nothrow -fdump-tree-hardcfr -ffat-lto-objects" } */
+
+/* Check that, even enabling checks before nothrow noreturn calls (leaving
+   returning calls enabled), we get checks before __builtin_return without
+   duplication (__builtin_return is both noreturn and a returning call).  */
+
+#include "harden-cfr-bret.c"
+
+/* Out-of-line checking, before both builtin_return and return in f.  */
+/* { dg-final { scan-tree-dump-times "__hardcfr_check" 2 "hardcfr" } } */
+/* Inline checking before builtin_return in g.  */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 1 "hardcfr" } } */
diff --git a/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-retcl.c b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-retcl.c
new file mode 100644
index 00000000000..137dfbb95d6
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret-retcl.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-fharden-control-flow-redundancy -fhardcfr-check-noreturn-calls=never -fdump-tree-hardcfr -ffat-lto-objects" } */
+
+/* Check that, even disabling checks before noreturn calls (leaving returning
+   calls enabled), we still get checks before __builtin_return.  */
+
+#include "harden-cfr-bret.c"
+
+/* Out-of-line checking, before both builtin_return and return in f.  */
+/* { dg-final { scan-tree-dump-times "__hardcfr_check" 2 "hardcfr" } } */
+/* Inline checking before builtin_return in g.  */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 1 "hardcfr" } } */
diff --git a/gcc/testsuite/c-c++-common/torture/harden-cfr-bret.c b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret.c
index 70acdc95f25..b459ff6b864 100644
--- a/gcc/testsuite/c-c++-common/torture/harden-cfr-bret.c
+++ b/gcc/testsuite/c-c++-common/torture/harden-cfr-bret.c
@@ -7,5 +7,11 @@ int f(int i) {
   return i;
 }
 
-/* Out-of-line checking, before both builtin_return and return.  */
+int g(int i) {
+  __builtin_return (&i);
+}
+
+/* Out-of-line checking, before both builtin_return and return in f.  */
 /* { dg-final { scan-tree-dump-times "__hardcfr_check" 2 "hardcfr" } } */
+/* Inline checking before builtin_return in g.  */
+/* { dg-final { scan-tree-dump-times "__builtin_trap" 1 "hardcfr" } } */

             reply	other threads:[~2022-10-01  4:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-01  4:50 Alexandre Oliva [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-09-10  3:03 Alexandre Oliva

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221001045007.4E3083858282@sourceware.org \
    --to=aoliva@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).