public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "mpolacek at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/108060] [10/11/12/13 Regression] UBsan missed an out-of-bound bug at -O0 since r7-1900-g8a1b7b7fd75a3847
Date: Tue, 07 Mar 2023 23:19:25 +0000	[thread overview]
Message-ID: <bug-108060-4-fy7NzGwWmq@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-108060-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108060

--- Comment #7 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Candidate fix:

--- a/gcc/c-family/c-gimplify.cc
+++ b/gcc/c-family/c-gimplify.cc
@@ -106,6 +106,18 @@ ubsan_walk_array_refs_r (tree *tp, int *walk_subtrees,
void *data)
     }
   else if (TREE_CODE (*tp) == ARRAY_REF)
     ubsan_maybe_instrument_array_ref (tp, false);
+  else if (TREE_CODE (*tp) == MODIFY_EXPR)
+    {
+      /* Since r7-1900, we gimplify RHS before LHS.  Consider
+      a[b] |= c;
+    wherein we can have a single shared tree a[b] in both LHS and RHS.
+    If we only instrument the LHS and the access is invalid, the program
+    could crash before emitting a UBSan error.  So instrument the RHS
+    first.  */
+      *walk_subtrees = 0;
+      walk_tree (&TREE_OPERAND (*tp, 1), ubsan_walk_array_refs_r, pset, pset);
+      walk_tree (&TREE_OPERAND (*tp, 0), ubsan_walk_array_refs_r, pset, pset);
+    }
   return NULL_TREE;
 }

It handles
  b = 0;
  a[b] = (a[b], b = -32768, a[b] | c);
correctly (the first a[b] is OK but not the 2nd or 3rd).

  parent reply	other threads:[~2023-03-07 23:19 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-11 14:32 [Bug sanitizer/108060] New: UBsan missed an out-of-bound bug at -O0 shaohua.li at inf dot ethz.ch
2022-12-12 12:45 ` [Bug sanitizer/108060] UBsan missed an out-of-bound bug at -O0 since r7-1900-g8a1b7b7fd75a3847 marxin at gcc dot gnu.org
2022-12-12 16:29 ` [Bug sanitizer/108060] [10/11/12/13 Regression] " pinskia at gcc dot gnu.org
2022-12-21 11:52 ` [Bug c/108060] " rguenth at gcc dot gnu.org
2022-12-22 12:06 ` marxin at gcc dot gnu.org
2023-03-07 14:29 ` marxin at gcc dot gnu.org
2023-03-07 14:30 ` marxin at gcc dot gnu.org
2023-03-07 14:44 ` mpolacek at gcc dot gnu.org
2023-03-07 23:19 ` mpolacek at gcc dot gnu.org [this message]
2023-03-10 18:25 ` [Bug sanitizer/108060] " cvs-commit at gcc dot gnu.org
2023-03-10 18:32 ` [Bug sanitizer/108060] [10/11/12 " mpolacek at gcc dot gnu.org
2023-03-15 18:35 ` cvs-commit at gcc dot gnu.org
2023-03-15 18:35 ` [Bug sanitizer/108060] [10/11 " mpolacek at gcc dot gnu.org
2024-02-01  9:53 ` pinskia at gcc dot gnu.org

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=bug-108060-4-fy7NzGwWmq@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).