public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <rguenther@suse.de>
To: Kees Cook <keescook@chromium.org>
Cc: Qing Zhao <qing.zhao@oracle.com>,
	 "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
	 "pinskia@gmail.com" <pinskia@gmail.com>,
	 "dmalcolm@redhat.com" <dmalcolm@redhat.com>
Subject: Re: [RFC][PATCH] PR tree-optimization/109071 - -Warray-bounds false positive warnings due to code duplication from jump threading
Date: Wed, 15 May 2024 07:50:20 +0200 (CEST)	[thread overview]
Message-ID: <n1979q22-6115-7q27-4sn1-84p6q893rq2n@fhfr.qr> (raw)
In-Reply-To: <202405141238.E787A138@keescook>

[-- Attachment #1: Type: text/plain, Size: 3647 bytes --]

On Tue, 14 May 2024, Kees Cook wrote:

> On Tue, May 14, 2024 at 02:17:16PM +0000, Qing Zhao wrote:
> > The current major issue with the warning is:  the constant index value 4
> > is not in the source code, it’s a compiler generated intermediate value
> > (even though it’s a correct value -:)). Such warning messages confuse
> > the end-users with information that cannot be connected directly to the
> > source code.
> 
> Right -- this "4" comes from -fsanitize=array-bounds (in "warn but
> continue" mode).
> 
> Now, the minimized PoC shows a situation that triggers the situation, but
> I think it's worth looking at the original code that caused this false
> positive:
> 
> 	for (i = 0; i < sg->num_entries; i++) {
> 		gce = &sg->gce[i];
> 
> 
> The issue here is that sg->num_entries has already been bounds-checked
> in a separate function. As a result, the value range tracking for "i"
> here is unbounded.
> 
> Enabling -fsanitize=array-bounds means the sg->gce[i] access gets
> instrumented, and suddenly "i" gains an implicit range, induced by the
> sanitizer.
> 
> (I would point out that this is very similar to the problems we've had
> with -fsanitize=shift[1][2]: the sanitizer induces a belief about a
> given variable's range this isn't true.)
> 
> Now, there is an argument to be made that the original code should be
> doing:
> 
> 	for (i = 0; i < 4 && i < sg->num_entries; i++) {
> 
> But this is:
> 
> a) logically redundant (Linux maintainers don't tend to like duplicating
>    their range checking)
> 
> b) a very simple case
> 
> The point of the sanitizers is to catch "impossible" situations at
> run-time for the cases where some value may end up out of range. Having
> it _induce_ a range on the resulting code makes no sense.
> 
> Could we, perhaps, have sanitizer code not influence the value range
> tracking? That continues to look like the root cause for these things.

The sanitizer code adds checks that are not distinguishable from
user code exactly because we want value-range analysis to eventually
elide even (redundant) sanitizer checks.

I think the fix for the source when there's a-priori knowledge
of sg->num_entries is to assert that knowledge through language
features or when using C through GNU extensions like assert()
using __builtin_unreachable ().  That also serves documentation
purposes "this code expects sg->num_entries to be bounds-checked".

To me it doesn't make much sense to mix sanitizing of array
accesses and at the same time do -Warray-bound diagnostics.

Note I tried to teach jump threading to be less aggressive
threading paths to exceptional situations (I think the
sanitizer runtime calls are at least marked unlikely), but
the comment was that even those are very much desired but
I can't remember the details.  This was as part of PR111515
but I think I've run into this earlier when trying to
improve back_threader_profitability::possibly_profitable_path_p.
There we have

  /* Threading is profitable if the path duplicated is hot but also
     in a case we separate cold path from hot path and permit optimization
     of the hot path later.  Be on the agressive side here. In some testcases,
     as in PR 78407 this leads to noticeable improvements.  */

here we have

  if (A)
    unlikely();
  B;
  if (A)
    unlikely ();

and we choose to perform that path separation which optimizes
the not exceptional path which automatically separates the
exceptional path as well.

IMO that sanitizer mode that continues running is bad - it makes
the compiler aware of undefined behavior and make the code run
into it with open eyes.  You get what you asked for.

Richard.

  reply	other threads:[~2024-05-15  5:50 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-13 19:48 Qing Zhao
2024-05-13 20:46 ` Jeff Law
2024-05-13 21:41   ` Kees Cook
2024-05-13 23:38     ` Andrew Pinski
2024-05-14  0:14       ` Kees Cook
2024-05-14 14:57         ` Qing Zhao
2024-05-14 15:08           ` Jeff Law
2024-05-14 16:03             ` Qing Zhao
2024-05-14 13:08 ` Richard Biener
2024-05-14 14:17   ` Qing Zhao
2024-05-14 14:29     ` Richard Biener
2024-05-14 15:19       ` Qing Zhao
2024-05-14 17:14         ` Richard Biener
2024-05-14 17:21           ` Qing Zhao
2024-05-15  6:09             ` Richard Biener
2024-05-15 13:37               ` Qing Zhao
2024-05-14 19:50     ` Kees Cook
2024-05-15  5:50       ` Richard Biener [this message]
2024-05-15 14:00   ` David Malcolm
2024-05-21 15:13     ` Qing Zhao
2024-05-21 21:36       ` David Malcolm
2024-05-22  7:38         ` Richard Biener
2024-05-22 18:53           ` Qing Zhao
2024-05-23 11:46             ` Richard Biener
2024-05-23 14:03               ` Qing Zhao
2024-05-23 14:13                 ` David Malcolm
2024-05-23 14:23                   ` Qing Zhao
2024-05-31 21:22               ` Qing Zhao
2024-06-03  6:29                 ` Richard Biener
2024-06-03 14:33                   ` Qing Zhao
2024-06-03 14:48                   ` David Malcolm
2024-06-04  7:43                     ` Richard Biener
2024-06-04 20:30                       ` Qing Zhao
2024-06-05  7:26                         ` Richard Biener
2024-06-05 16:38                           ` Qing Zhao
2024-06-05 17:07                             ` Richard Biener
2024-06-05 17:58                               ` Qing Zhao
2024-06-07 19:13                                 ` Qing Zhao
2024-06-12 18:15                                   ` Qing Zhao
2024-05-14 16:43 ` Kees Cook

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=n1979q22-6115-7q27-4sn1-84p6q893rq2n@fhfr.qr \
    --to=rguenther@suse.de \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=keescook@chromium.org \
    --cc=pinskia@gmail.com \
    --cc=qing.zhao@oracle.com \
    /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).