From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id BF4A43858D3C; Wed, 1 Mar 2023 21:56:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BF4A43858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677707812; bh=7DaURwpEkoEx1br9NZq+q0bebEuscpe63Q3h0KACckY=; h=From:To:Subject:Date:From; b=xqejqzBOrW+navgb3g9Wv3iXD33tsU2pVRMkBK9izYCSXdzNWPSghgg3NUA7PAV8s dqqZQaJYoOAsLc1ve4H6emMVA9LsYWZ0ZCNw/f0JOlyHy8RK028lQ8WuzRX2dJ3nqv tRbFY7TKyju5H5ksewjGEOY9uvRFFCGzeX06lm/I= From: "dmalcolm at gcc dot 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: dmalcolm at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D108988 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 =3D 0; i < 2; ++i) { FILE *fp =3D 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 =3D 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 =3D 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 =3D 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 =3D 2 (gdb) p arg $14 =3D (gdb) call debug_tree(arg) 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-eff= ects (e.g. fclose-ing the file, hence it stops reporting the leak). Is this a bug in gimple_fold_builtin_fprintf?=