public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "qinzhao at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/109071] -Warray-bounds false positive warnings due to code duplication from jump threading
Date: Mon, 22 Apr 2024 21:19:03 +0000	[thread overview]
Message-ID: <bug-109071-4-cL6HROFk28@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-109071-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #9 from qinzhao at gcc dot gnu.org ---
(In reply to Kees Cook from comment #8)

> Normally -Warray-bounds doesn't warn when a value is totally unknown (i.e.
> "index" here can be [-INT_MAX,INT_MAX]). Why does the warning change when
> the MAX_ENTRIES test is moved inside assign()?

it's due to both inline transformation + thread jump optimization (and some
other compiler transformation inbetween). 

***After GCC inlines both calls to "assign" into the caller "sparx5_set" and
applies some other optimizations on the caller body before thread jump phase,
the body of the routine "sparx5_set" is (logically):

void sparx5_set (int * ptr, struct nums * sg, int index)
{
  if (index >= 4)
    warn ();
  *ptr = 0;

  *val = sg->vals[index];
  if (index >= 4)
    warn ();
  *ptr = *val;

  return;
}

***Thread jump optimization tried to reduce the # of branches inside the
routine "sparx5_set", in order to do this, sometime it needs to duplicate the
code. for the above routine, after thread jump optimization, the body of the
routine "sparx5_set" becomes (logically):

void sparx5_set (int * ptr, struct nums * sg, int index)
{
  if (index >= 4)
    { 
      warn ();
      *ptr = 0;               // code duplications since "warn" does return;
      *val = sg->vals[index]; // same this line. in this path, since it's under
                              // the condition "index >= 4", the compiler knows
                              // the value of "index" is larger then 4,
therefore
                              // the out-of-bound warning.
      warn ();
    }
  else
    { 
      *ptr = 0;
      *val = sg->vals[index];
    }
  *ptr = *val;
  return;

}

with the thread jump optimization, the # of branches inside the routine
"sparx5_set" is reduced from 2 to 1, however, due to the code duplication
(which is needed for the correctness of the code), we got a out-of-bound
warning. 

actually, I don't think that the compiler's behavior is wrong. and it's not
very reasonable for the users of -Warray-bounds to assume there is zero false
positive warnings. 
However, it might be reasonable to put such warnings to -Warray-bounds=2 but
not in -Warray-bounds=1?

  parent reply	other threads:[~2024-04-22 21:19 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-08 21:04 [Bug c/109071] New: -Warray-bounds warning when array index checked via inline kees at outflux dot net
2023-03-08 21:13 ` [Bug tree-optimization/109071] " pinskia at gcc dot gnu.org
2023-03-09 10:18 ` rguenth at gcc dot gnu.org
2023-03-09 16:15 ` kees at outflux dot net
2023-03-10 15:51 ` qinzhao at gcc dot gnu.org
2024-04-22 19:12 ` [Bug tree-optimization/109071] -Warray-bounds false positive warnings due to code duplication from jump threading qinzhao at gcc dot gnu.org
2024-04-22 19:21 ` kees at outflux dot net
2024-04-22 20:04 ` qinzhao at gcc dot gnu.org
2024-04-22 20:15 ` kees at outflux dot net
2024-04-22 21:19 ` qinzhao at gcc dot gnu.org [this message]
2024-05-13 14:21 ` qinzhao at gcc dot gnu.org
2024-05-14 16:19 ` qinzhao 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-109071-4-cL6HROFk28@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).