public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r9-10085] match.pd: Avoid (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST opt in GENERIC when sanitizing [PR10
Date: Wed, 11 May 2022 06:21:03 +0000 (GMT)	[thread overview]
Message-ID: <20220511062103.6D5F73857418@sourceware.org> (raw)

https://gcc.gnu.org/g:57aeb7f535b9c04ed34ab9479498143ee73fb568

commit r9-10085-g57aeb7f535b9c04ed34ab9479498143ee73fb568
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Jun 29 11:24:38 2021 +0200

    match.pd: Avoid (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST opt in GENERIC when sanitizing [PR101210]
    
    When we have (intptr_t) x == cst where x has REFERENCE_TYPE, this
    optimization creates x == cst out of it where cst has REFERENCE_TYPE.
    If it is done in GENERIC folding, it can results in ubsan failures
    where the INTEGER_CST with REFERENCE_TYPE is instrumented.
    
    Fixed by deferring it to GIMPLE folding in this case.
    
    2021-06-29  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/101210
            * match.pd ((intptr_t)x eq/ne CST to x eq/ne (typeof x) CST): Don't
            perform the optimization in GENERIC when sanitizing and x has a
            reference type.
    
            * g++.dg/ubsan/pr101210.C: New test.
    
    (cherry picked from commit 53fd7544aff6d0a18869017cb9bb921a7f5dcd04)

Diff:
---
 gcc/match.pd                          |  7 ++++++-
 gcc/testsuite/g++.dg/ubsan/pr101210.C | 13 +++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 633eb5dc170..9d4d4748192 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4044,7 +4044,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (cmp (convert @0) INTEGER_CST@1)
   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
 	 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
-	 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
+	 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
+	 /* Don't perform this optimization in GENERIC if @0 has reference
+	    type when sanitizing.  See PR101210.  */
+	 && !(GENERIC
+	      && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
+	      && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
 	|| (INTEGRAL_TYPE_P (TREE_TYPE (@0))
 	    && POINTER_TYPE_P (TREE_TYPE (@1))
 	    && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
diff --git a/gcc/testsuite/g++.dg/ubsan/pr101210.C b/gcc/testsuite/g++.dg/ubsan/pr101210.C
new file mode 100644
index 00000000000..955b8203f44
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ubsan/pr101210.C
@@ -0,0 +1,13 @@
+// PR c++/101210
+// { dg-do run }
+// { dg-options "-fsanitize=null,alignment -fno-sanitize-recover=null,alignment" }
+
+int v[2];
+int
+main ()
+{
+  int x;
+  int &y = x;
+  v[0] = reinterpret_cast<__INTPTR_TYPE__>(&y) == 0;
+  v[1] = reinterpret_cast<__INTPTR_TYPE__>(&y) == 1;
+}


                 reply	other threads:[~2022-05-11  6:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220511062103.6D5F73857418@sourceware.org \
    --to=jakub@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).