public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dmalcolm at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/108988] New: gimple_fold_builtin_fputs doesn't preserve gimple_builtin_call_types_compatible_p
Date: Wed, 01 Mar 2023 21:56:52 +0000	[thread overview]
Message-ID: <bug-108988-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 108988
           Summary: gimple_fold_builtin_fputs doesn't preserve
                    gimple_builtin_call_types_compatible_p
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dmalcolm at gcc dot gnu.org
  Target Milestone: ---

Whilst working on PR analyzer/107565, I noticed that in this function:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
typedef struct FILE   FILE;

FILE* fopen (const char*, const char*);
int fprintf (FILE *, const char *, ...);

#define NULL ((void *)0)

void
test_2 (void)
{
  int i;

  for (i = 0; i < 2; ++i) {
    FILE *fp = fopen ("/tmp/test", "w");
    fprintf (fp, "hello");
  }
} // should report a leak here

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

the fprintf (fp, "hello"); is optimized to:

  __builtin_fwrite ("hello", 1, 5, fp);

but this call has:

(gdb) p gimple_builtin_call_types_compatible_p (repl, gimple_call_fndecl
(repl))
$23 = false


Specifically, the fprintf is optimized to:
  __builtin_fputs ("hello", fp);

Within gimple_fold_builtin_fprintf this has:

(gdb) call debug(stmt)
__builtin_fputs ("hello", fp);

(gdb) p gimple_builtin_call_types_compatible_p (stmt, gimple_call_fndecl
(stmt))
$19 = true

which is optimized to:

(gdb) call debug(repl)
__builtin_fwrite ("hello", 1, 5, fp);

(gdb) p gimple_builtin_call_types_compatible_p (repl, gimple_call_fndecl
(repl))
$23 = false

Note how the resulting call has "false" for
gimple_builtin_call_types_compatible_p; this is due to argument idx 2 (the 5):

(gdb) p i
$13 = 2
(gdb) p arg
$14 = <integer_cst 0x7fffea7f9ba0>
(gdb) call debug_tree(arg)
 <integer_cst 0x7fffea7f9ba0 type <integer_type 0x7fffea663150 ssizetype>
constant 5>



In the analyzer I'm checking that gimple_builtin_call_types_compatible_p is
true when handling a builtin that it "knows" how to handle, otherwise the
analyzer falls back to assuming that the call could have arbitrary side-effects
(e.g. fclose-ing the file, hence it stops reporting the leak).

Is this a bug in gimple_fold_builtin_fprintf?

             reply	other threads:[~2023-03-01 21:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-01 21:56 dmalcolm at gcc dot gnu.org [this message]
2023-03-01 21:57 ` [Bug middle-end/108988] " dmalcolm at gcc dot gnu.org
2023-03-01 22:20 ` [Bug tree-optimization/108988] " pinskia at gcc dot gnu.org
2023-03-01 22:26 ` cvs-commit at gcc dot gnu.org
2023-03-02  9:38 ` jakub at gcc dot gnu.org
2023-03-03 10:20 ` cvs-commit at gcc dot gnu.org
2023-03-03 10:23 ` jakub at gcc dot gnu.org
2023-03-03 22:48 ` cvs-commit at gcc dot gnu.org
2023-03-03 23:16 ` dmalcolm 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-108988-4@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).