public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Andi Kleen <andi@firstfloor.org>
Cc: gcc-patches@gcc.gnu.org, richard.sandiford@linaro.org
Subject: Re: Make tree-ssa-strlen.c handle partial unterminated strings
Date: Fri, 05 May 2017 15:57:00 -0000	[thread overview]
Message-ID: <20170505155526.GE1809@tucnak> (raw)
In-Reply-To: <87efw3gv6b.fsf@firstfloor.org>

On Fri, May 05, 2017 at 08:50:04AM -0700, Andi Kleen wrote:
> Richard Sandiford <richard.sandiford@linaro.org> writes:
> 
> > tree-ssa-strlen.c looks for cases in which a string is built up using
> > operations like:
> >
> >     memcpy (a, "foo", 4);
> >     memcpy (a + 3, "bar", 4);
> >     int x = strlen (a);
> >
> > As a side-effect, it optimises the non-final memcpys so that they don't
> > include the nul terminator.
> >
> > However, after removing some "& ~0x1"s from tree-ssa-dse.c, the DSE pass
> > does this optimisation itself (because it can tell that later memcpys
> > overwrite the terminators).  The strlen pass wasn't able to handle these
> > pre-optimised calls in the same way as the unoptimised ones.
> >
> > This patch adds support for tracking unterminated strings.
> 
> Would that be useful as a warning too? If the pass can figure out
> the final string can be not null terminated when passed somewhere else,
> warn, because it's likely a bug in the program.

Why would it be a bug?  Not all sequences of chars are zero terminated
strings, it can be arbitrary memory and have size somewhere on the side.
Also, the fact that strlen pass sees a memcpy (a, "foo", 3); and a passed
somewhere else doesn't mean a isn't zero terminated, the pass records only
what it can prove, so even when you have:
memcpy (a, "abcdefgh", 9);
*p = 0; // unrelated pointer, but compiler can't prove that
memcpy (a, "foo", 3);
call (a);
there is really nothing wrong with it, the string is still zero terminated.
The pass had to flush the knowledge that it knew length of a on the wild
pointer store.

	Jakub

  reply	other threads:[~2017-05-05 15:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-05 12:02 Richard Sandiford
2017-05-05 15:55 ` Andi Kleen
2017-05-05 15:57   ` Jakub Jelinek [this message]
2017-05-05 16:29     ` Martin Sebor
2017-05-05 16:38       ` Jakub Jelinek
2017-05-05 16:49         ` Martin Sebor
2017-05-05 16:43       ` Jeff Law
2017-05-05 16:02 ` Jakub Jelinek
2017-05-07 10:18   ` Richard Sandiford
2017-05-10 14:18     ` Jakub Jelinek
2017-05-10 19:57       ` Richard Sandiford
2017-05-17  7:21         ` Richard Sandiford
2017-05-23 10:17           ` Jakub Jelinek
2017-05-05 16:10 ` Martin Sebor

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=20170505155526.GE1809@tucnak \
    --to=jakub@redhat.com \
    --cc=andi@firstfloor.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=richard.sandiford@linaro.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).