public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/108988] New: gimple_fold_builtin_fputs doesn't preserve gimple_builtin_call_types_compatible_p
@ 2023-03-01 21:56 dmalcolm at gcc dot gnu.org
  2023-03-01 21:57 ` [Bug middle-end/108988] " dmalcolm at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: dmalcolm at gcc dot gnu.org @ 2023-03-01 21:56 UTC (permalink / raw)
  To: gcc-bugs

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?

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-03-03 23:16 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-01 21:56 [Bug middle-end/108988] New: gimple_fold_builtin_fputs doesn't preserve gimple_builtin_call_types_compatible_p dmalcolm at gcc dot gnu.org
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

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).