public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rguenth at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/109609] [12/13/14 Regression] tail call for function even when passing a ptr which references a local array still
Date: Tue, 25 Apr 2023 07:15:05 +0000	[thread overview]
Message-ID: <bug-109609-4-dA0dWf03rc@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-109609-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #11 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #10)
> On the first testcase reverting the offending rev. shows that it causes
> 
>    <bb 2> [local count: 137085152]:
> -  MEM[(char *)&buf + 12B] = 0;
> -  _19 = *id_8(D);
> -  if (_19 != 0)
> +  _18 = *id_7(D);
> +  if (_18 != 0)
> 
> thus we DSE the store to the end.
> 
> The issue is that the fnspec we have for strncpy says the access size
> is specified by argument 3 but what it specified there is the _maximum_
> size read, not the actual size.  So instead of "1cO313" it should be
> "1cO31 " ('1' is somewhat odd then, it says we copy 'src' to 'dst'
> but we only say the 'dst' write covers arg 3 size - I guess that's OK
> for points-to analysis, the additional zeros written do not have pointers,
> but if we use it differently it might be a wrong spec?)
> 
> I'm scanning other builtins for similar issues.

Note a different fix would be to re-interpret the case of reads and
say the size is _up to_ the specified length, thus use
ao_ref_init_from_ptr_and_range instead of _and_size.

strncat, stpncpy, strncmp, strnlen, strndup are affected similarly.

for functions like memchr I'm not sure if we can assume all 'n' bytes are
read (thus if that would cause known overread -> undefined behavior).

Honza?  Any opinion?


Fix for the testcase, but incomplete as noted above:

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 0e06fa5b2e0..133707c1617 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -11562,11 +11562,12 @@ builtin_fnspec (tree callee)
       case BUILT_IN_STPCPY_CHK:
        return ".cO 1 ";
       case BUILT_IN_STRNCPY:
+      case BUILT_IN_STRNCPY_CHK:
+       return "1cO31 ";
       case BUILT_IN_MEMCPY:
       case BUILT_IN_MEMMOVE:
       case BUILT_IN_TM_MEMCPY:
       case BUILT_IN_TM_MEMMOVE:
-      case BUILT_IN_STRNCPY_CHK:
       case BUILT_IN_MEMCPY_CHK:
       case BUILT_IN_MEMMOVE_CHK:
        return "1cO313";

  parent reply	other threads:[~2023-04-25  7:15 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-24 19:34 [Bug c++/109609] New: Invalid strncpy/strncat optimization in GCC 12 gburca-gnu at ebixio dot com
2023-04-24 19:38 ` [Bug middle-end/109609] " pinskia at gcc dot gnu.org
2023-04-24 19:40 ` pinskia at gcc dot gnu.org
2023-04-24 19:47 ` [Bug middle-end/109609] [12/13/14 Regression] " jakub at gcc dot gnu.org
2023-04-24 19:50 ` pinskia at gcc dot gnu.org
2023-04-24 19:52 ` [Bug middle-end/109609] [12/13/14 Regression] tail call for function even when passing a ptr which references a local array still jakub at gcc dot gnu.org
2023-04-24 19:56 ` pinskia at gcc dot gnu.org
2023-04-24 20:30 ` gburca-gnu at ebixio dot com
2023-04-24 20:52 ` jakub at gcc dot gnu.org
2023-04-25  6:30 ` rguenth at gcc dot gnu.org
2023-04-25  7:06 ` rguenth at gcc dot gnu.org
2023-04-25  7:15 ` rguenth at gcc dot gnu.org [this message]
2023-04-25  7:37 ` jakub at gcc dot gnu.org
2023-04-25  7:42 ` jakub at gcc dot gnu.org
2023-04-25 12:54 ` rguenth at gcc dot gnu.org
2023-04-25 14:53 ` cvs-commit at gcc dot gnu.org
2023-04-25 14:53 ` [Bug middle-end/109609] [12/13 " rguenth at gcc dot gnu.org
2023-04-25 16:14 ` gburca-gnu at ebixio dot com
2023-04-26  9:34 ` cvs-commit at gcc dot gnu.org
2023-04-26 10:41 ` cvs-commit at gcc dot gnu.org
2023-04-26 10:43 ` rguenth 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-109609-4-dA0dWf03rc@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).