public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeff Law <law@redhat.com>
To: Bernd Edlinger <bernd.edlinger@hotmail.de>,
	Martin Sebor <msebor@gmail.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>
Cc: Richard Biener <rguenther@suse.de>, Jakub Jelinek <jakub@redhat.com>
Subject: Re: [PATCH] Make strlen range computations more conservative
Date: Sun, 05 Aug 2018 15:58:00 -0000	[thread overview]
Message-ID: <8533f533-0e89-3e62-e503-e424658f0a5d@redhat.com> (raw)
In-Reply-To: <AM5PR0701MB265793BFD851F9FE0D9222A3E4210@AM5PR0701MB2657.eurprd07.prod.outlook.com>

On 08/05/2018 12:08 AM, Bernd Edlinger wrote:
> On 08/04/18 23:56, Martin Sebor wrote:
>> On 08/03/2018 01:00 AM, Jeff Law wrote:
>>> On 07/24/2018 05:18 PM, Bernd Edlinger wrote:
>>>> On 07/24/18 23:46, Jeff Law wrote:
>>>>> On 07/24/2018 01:59 AM, Bernd Edlinger wrote:
>>>>>> Hi!
>>>>>>
>>>>>> This patch makes strlen range computations more conservative.
>>>>>>
>>>>>> Firstly if there is a visible type cast from type A to B before passing
>>>>>> then value to strlen, don't expect the type layout of B to restrict the
>>>>>> possible return value range of strlen.
>>>>> Why do you think this is the right thing to do?  ie, is there language
>>>>> in the standards that makes you think the code as it stands today is
>>>>> incorrect from a conformance standpoint?  Is there a significant body of
>>>>> code that is affected in an adverse way by the current code?  If so,
>>>>> what code?
>>>>>
>>>>>
>>>>
>>>> I think if you have an object, of an effective type A say char[100], then
>>>> you can cast the address of A to B, say typedef char (*B)[2] for instance
>>>> and then to const char *, say for use in strlen.  I may be wrong, but I think
>>>> that we should at least try not to pick up char[2] from B, but instead
>>>> use A for strlen ranges, or leave this range open.  Currently the range
>>>> info for strlen is [0..1] in this case, even if we see the type cast
>>>> in the generic tree.
>>> ISTM that you're essentially saying that the cast to const char *
>>> destroys any type information we can exploit here.  But if that's the
>>> case, then I don't think we can even derive a range of [0,99].  What's
>>> to say that "A" didn't result from a similar cast of some object that
>>> was char[200] that happened out of the scope of what we could see during
>>> the strlen range computation?
>>>
>>> If that is what you're arguing, then I think there's a re-evaluation
>>> that needs to happen WRT strlen range computation/
>>>
>>> And just to be clear, I do see this as a significant correctness question.
>>>
>>> Martin, thoughts?
>>
>> The argument is that given:
>>
>>    struct S { char a[4], b; };
>>
>>    char a[8] = "1234567";
>>
>> this is valid and should pass:
>>
>>    __attribute__ ((noipa))
>>    void f (struct S *p)
>>    {
>>      assert (7 == strlen (p->a));
>>    }
>>
>>    int main (void)
>>    {
>>      f ((struct S*)a);
>>    }
>>
>> (This is the basic premise behind pr86259.)
>>
>> This argument is wrong and the code is invalid.  For the access
>> to p->a to be valid p must point to an object of struct S (it
>> doesn't) and the p->a array must hold a nul-terminated string
>> (it also doesn't).
>>
>> This should not be surprising because the following equivalent
>> code behaves the same way:
>>
>>    __attribute__ ((noipa))
>>    void f (struct S *p)
>>    {
>>      int n = 0;
>>      for (int i = 0; p->a[i]; ++i)
>>        ++n;
>>      if (3 != n)
>>        __builtin_abort ();
>>    }
>>
>> and also because for write accesses, GCC (helpfully) enforces
>> the restriction with _FORTIFY_SOURCE=2:
>>
>>    __attribute__ ((noipa))
>>    void f (struct S *p)
>>    {
>>      strcpy (p->a, "1234");   // aborts
>>    }
>>
>> I care less about the optimization than I do about the basic
>> premise that it's essential to respect subobject boundaries(*).
>> It would make little sense to undo the strlen optimization
>> without also undoing the optimization for the direct array
>> access case.  Undoing either would raise the question about
>> the validity of the _FORRTIFY_SOURCE=2 behavior.  That would
>> be a huge step backwards in terms of code security.  If we
>> did some of these but not others, it would make the behavior
>> inconsistent and surprising, all to accommodate one instance
>> of invalid code.
>>
>> If we had a valid test case where the strlen optimization
>> leads to invalid code, or even if there were a significant
>> number of bug reports showing that it breaks an invalid
>> but common idiom, I would certainly feel compelled to
>> make it right somehow.  But there has been just one bug
>> report with clearly invalid code that should be easily
>> corrected.
>>
> 
> 
> I see this from a software engineering POV.
> 
> If we have code like this:
> 
> void test (const char *x)
> {
>    assert (strlen (x) < 10);
> }
> 
> One would usually expect the program to abort (or at least abort with
> a near 100% likelihood) if x is not a valid string with length less
> than 10.
I would not expect the program to abort if this function were called
with an invalid (ie unterminated) string.  In that scenario I know
enough to not expect anything because my program is broken, plain and
simple.

> 
> But if lto and other optimizations show that this code is invoked with
> an invalid, non-zero terminated string the assertion is suddenly gone.
And the program is invalid as it exhibits undefined behavior.  One
undefined behavior is exhibited I have no expectations.  I *like* when
we do something like trap as soon as undefined behavior is discovered,
but I do not expect it.

> 
> Martin, why do you insist that GCC has to do this, and that it must
> be a good idea to do so, just based on the language definition?
We do this all the time in all kinds of situations.

The language definition provides the contract that the programmer and
compiler must adhere to.  When the contract is broken we can not be
responsible for the resulting code as the input source is simply broken.

> 
> Why do we need assertions at all, when all programs have to be completely
> correct before we may compile them?
That's what compilers do.  When they see code that is pointless it gets
removed.  As engineers we sometimes say "while this is undefined
behavior we're not going to exploit the undefined-ness".  That is based
on our experience as developers.  And sometimes we may not even agree
where the line between optimize this vs do not because it's going to
surprise a developer.

Jeff

  reply	other threads:[~2018-08-05 15:58 UTC|newest]

Thread overview: 121+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-24  7:59 Bernd Edlinger
2018-07-24 14:50 ` Richard Biener
2018-07-25 13:03   ` Bernd Edlinger
2018-07-24 16:14 ` Martin Sebor
2018-07-24 21:46 ` Jeff Law
2018-07-24 23:18   ` Bernd Edlinger
2018-07-25  4:52     ` Jeff Law
2018-07-25  7:23     ` Richard Biener
2018-07-25 19:37       ` Martin Sebor
2018-07-26  8:55         ` Richard Biener
2018-08-07  2:24           ` Martin Sebor
2018-08-07  8:51             ` Richard Biener
2018-08-07 14:37               ` Martin Sebor
2018-08-07 17:44                 ` Richard Biener
2018-08-08  2:33                   ` Martin Sebor
2018-08-17 10:31                     ` Richard Biener
2018-08-17 15:49                       ` Martin Sebor
2018-08-19 15:55                         ` Bernd Edlinger
2018-08-20 10:24                           ` Richard Biener
2018-08-20 17:23                             ` Bernd Edlinger
2018-08-21  8:46                               ` Richard Biener
2018-08-21 22:25                                 ` Jeff Law
2018-08-22  4:05                                   ` Bernd Edlinger
2018-08-22 16:05                                     ` Martin Sebor
2018-08-22 17:22                                       ` Bernd Edlinger
2018-08-22 22:34                                         ` Jeff Law
2018-08-22 22:57                                           ` Bernd Edlinger
2018-08-22 22:57                                           ` Martin Sebor
2018-08-22 23:08                                             ` Bernd Edlinger
2018-08-21 22:43                           ` Jeff Law
2018-08-22  4:16                             ` Bernd Edlinger
2018-08-22 23:41                               ` Jeff Law
2018-08-26  9:58                                 ` Bernd Edlinger
2018-09-15  9:22                                   ` Bernd Edlinger
2018-10-10 23:12                                     ` Jeff Law
2018-10-12 15:03                                     ` Jeff Law
2018-10-13  9:07                                       ` Bernd Edlinger
2018-10-17 23:59                                         ` Jeff Law
2018-10-20 11:16                                           ` Bernd Edlinger
2018-11-16 17:26                                             ` Bernd Edlinger
2018-08-22 13:10                             ` Bernd Edlinger
2018-10-24  9:14                             ` Maxim Kuvyrkov
2018-10-24 13:38                               ` Bernd Edlinger
2018-10-24 14:26                                 ` Maxim Kuvyrkov
2018-08-03  7:29         ` Jeff Law
2018-08-03  7:19       ` Jeff Law
2018-08-03  7:48         ` Jakub Jelinek
2018-08-06 14:58           ` Jeff Law
2018-08-20 10:06         ` Richard Biener
2018-07-25 17:31     ` Martin Sebor
2018-07-27  6:49       ` Bernd Edlinger
2018-07-31  3:45         ` Martin Sebor
2018-07-31  6:38           ` Jakub Jelinek
2018-07-31 15:17             ` Martin Sebor
2018-07-31 15:48               ` Jakub Jelinek
2018-07-31 23:20                 ` Martin Sebor
2018-08-01  6:55                   ` Bernd Edlinger
2018-08-03  4:19                     ` Martin Sebor
2018-08-06 15:39                       ` Jeff Law
2018-08-01  7:19                   ` Richard Biener
2018-08-01  8:40                     ` Jakub Jelinek
2018-08-03  3:59                       ` Martin Sebor
2018-08-03  7:43                         ` Jakub Jelinek
2018-08-04 20:52                           ` Martin Sebor
2018-08-05  6:51                             ` Bernd Edlinger
2018-08-05 15:49                               ` Jeff Law
2018-08-06 17:15                                 ` Martin Sebor
2018-08-06 17:40                                   ` Jeff Law
2018-08-07  3:39                                     ` Martin Sebor
2018-08-07  5:45                                       ` Richard Biener
2018-08-07 15:02                                         ` Martin Sebor
2018-08-07 15:33                                           ` Bernd Edlinger
2018-08-07 16:31                                             ` Martin Sebor
2018-08-07 17:46                                               ` Richard Biener
2018-08-08 15:51                                                 ` Martin Sebor
2018-08-08 16:12                                                   ` Bernd Edlinger
2018-08-08 17:19                                                   ` Richard Biener
2018-08-07 15:32                                       ` Jeff Law
2018-08-06 22:39                                   ` Jeff Law
2018-08-05 17:00                             ` Jeff Law
2018-08-05 17:27                             ` Richard Biener
2018-08-06 15:36                               ` Martin Sebor
2018-08-02  3:13                     ` Martin Sebor
2018-08-02 10:22                       ` Bernd Edlinger
2018-08-02 15:42                         ` Martin Sebor
2018-08-02 17:00                           ` Martin Sebor
2018-08-02 18:15                             ` Bernd Edlinger
2018-08-03  3:06                               ` Martin Sebor
2018-08-02 18:20                             ` Jakub Jelinek
2018-08-03  3:24                               ` Martin Sebor
2018-08-09  5:36                           ` Jeff Law
2018-08-10 16:56                             ` Martin Sebor
2018-08-15  4:39                               ` Jeff Law
2018-08-20 10:12                                 ` Richard Biener
2018-08-20 10:23                                   ` Bernd Edlinger
2018-08-20 14:26                                     ` Jeff Law
2018-08-20 15:16                                       ` Bernd Edlinger
2018-08-20 20:42                                         ` Martin Sebor
2018-08-20 21:31                                           ` Bernd Edlinger
2018-08-21  2:43                                             ` Martin Sebor
2018-08-21  5:38                                               ` Bernd Edlinger
2018-08-21 21:58                                         ` Jeff Law
2018-08-03  7:47                       ` Jeff Law
2018-08-03  7:38                     ` Jeff Law
2018-08-06 15:34         ` Jeff Law
2018-08-03  7:00     ` Jeff Law
2018-08-04 21:56       ` Martin Sebor
2018-08-05  6:08         ` Bernd Edlinger
2018-08-05 15:58           ` Jeff Law [this message]
2018-08-06 11:57             ` Bernd Edlinger
2018-08-06 15:12         ` Jeff Law
2018-08-06 16:32           ` Martin Sebor
2018-08-06 17:44             ` Richard Biener
2018-08-06 23:59               ` Martin Sebor
2018-08-07 15:54                 ` Jeff Law
2018-08-06 22:48             ` Jeff Law
2018-08-09  5:26     ` Jeff Law
2018-08-09  6:27       ` Richard Biener
2018-08-17  5:09         ` Jeff Law
2018-07-25  7:08   ` Richard Biener
2018-08-02 16:45     ` Jeff Law

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=8533f533-0e89-3e62-e503-e424658f0a5d@redhat.com \
    --to=law@redhat.com \
    --cc=bernd.edlinger@hotmail.de \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=msebor@gmail.com \
    --cc=rguenther@suse.de \
    /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).