From patchwork Tue Apr 9 06:43:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1081923 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-498996-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="nOUBRJrc"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44dd711HRNz9sP1 for ; Tue, 9 Apr 2019 16:44:12 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=l2NwpZ4GVeqVH1Q8I56lv41CNEcN/ igvia1po2tTJC+hqk5Z9OB7DOTNzCjh6J02EGBPP8AGsulmE7EyzXNk93f8Rwe20 jr96Wd4PDstKz3ueRYv7P72RE+nnm4n2ZWx0FmtjP54G2OK5SCW/Tnnl2iQbU0S0 Fs06UuzbbEFNis= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; s=default; bh=fcEA/8aCazCvhkeHSaIv+Fvo/po=; b=nOU BRJrctvBRmzr++vmtwHdc62vsCWNcHZgBJGMBJbwg1PJzfw7EddGoPgDW88kTOHr zKrAlSt//JUhV1t72YLTWjifw9nolHoc1vfv9pzggYtxOpSNNZiV3ST6+++j/OkO 8JUta8gP5XrQ3AbGa4X75EB8DBdpSbNDS6Dxcb4Y= Received: (qmail 80256 invoked by alias); 9 Apr 2019 06:44:05 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 80246 invoked by uid 89); 9 Apr 2019 06:44:05 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-7.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=i686-linux, i686linux, cst X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 09 Apr 2019 06:44:03 +0000 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8DD973082E60; Tue, 9 Apr 2019 06:44:02 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.40.205.45]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2DC0B5D9C9; Tue, 9 Apr 2019 06:44:02 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id x396i0vh016112; Tue, 9 Apr 2019 08:44:00 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id x396hvRS016111; Tue, 9 Apr 2019 08:43:57 +0200 Date: Tue, 9 Apr 2019 08:43:57 +0200 From: Jakub Jelinek To: Richard Biener , Jeff Law Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix s{, n}printf folding ICEs with slightly bogus prototypes (PR tree-optimization/89998) Message-ID: <20190409064357.GH21066@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.11.3 (2019-02-01) X-IsSubscribed: yes Hi! If there are major differences in argument or return types of builtin prototypes, we already don't mark them as builtins, but if there are smaller differences like signedness changes of integral types with the same precision, we still accept them (with warning). The following patch makes sure we don't ICE in those cases by using the lhs type where possible instead of hardcoding that s{,n}printf return always int. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2019-04-09 Jakub Jelinek PR tree-optimization/89998 * gimple-ssa-sprintf.c (try_substitute_return_value): Use lhs type instead of integer_type_node if possible, don't add ranges if return type is not compatible with int. * gimple-fold.c (gimple_fold_builtin_sprintf, gimple_fold_builtin_snprintf): Use lhs type instead of hardcoded integer_type_node. * gcc.c-torture/compile/pr89998-1.c: New test. * gcc.c-torture/compile/pr89998-2.c: New test. Jakub --- gcc/gimple-ssa-sprintf.c.jj 2019-03-05 09:42:23.867092470 +0100 +++ gcc/gimple-ssa-sprintf.c 2019-04-08 11:23:43.568301945 +0200 @@ -3692,10 +3692,10 @@ try_substitute_return_value (gimple_stmt are badly declared. */ && !stmt_ends_bb_p (info.callstmt)) { - tree cst = build_int_cst (integer_type_node, retval[0]); + tree cst = build_int_cst (lhs ? TREE_TYPE (lhs) : integer_type_node, + retval[0]); - if (lhs == NULL_TREE - && info.nowrite) + if (lhs == NULL_TREE && info.nowrite) { /* Remove the call to the bounded function with a zero size (e.g., snprintf(0, 0, "%i", 123)) if there is no lhs. */ @@ -3736,7 +3736,7 @@ try_substitute_return_value (gimple_stmt } } } - else if (lhs) + else if (lhs && types_compatible_p (TREE_TYPE (lhs), integer_type_node)) { bool setrange = false; --- gcc/gimple-fold.c.jj 2019-03-07 20:07:20.292011398 +0100 +++ gcc/gimple-fold.c 2019-04-08 11:10:45.380940700 +0200 @@ -3231,11 +3231,10 @@ gimple_fold_builtin_sprintf (gimple_stmt gimple_set_no_warning (repl, true); gimple_seq_add_stmt_without_update (&stmts, repl); - if (gimple_call_lhs (stmt)) + if (tree lhs = gimple_call_lhs (stmt)) { - repl = gimple_build_assign (gimple_call_lhs (stmt), - build_int_cst (integer_type_node, - strlen (fmt_str))); + repl = gimple_build_assign (lhs, build_int_cst (TREE_TYPE (lhs), + strlen (fmt_str))); gimple_seq_add_stmt_without_update (&stmts, repl); gsi_replace_with_seq_vops (gsi, stmts); /* gsi now points at the assignment to the lhs, get a @@ -3285,12 +3284,12 @@ gimple_fold_builtin_sprintf (gimple_stmt gimple_set_no_warning (repl, true); gimple_seq_add_stmt_without_update (&stmts, repl); - if (gimple_call_lhs (stmt)) + if (tree lhs = gimple_call_lhs (stmt)) { - if (!useless_type_conversion_p (integer_type_node, + if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (orig_len))) - orig_len = fold_convert (integer_type_node, orig_len); - repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len); + orig_len = fold_convert (TREE_TYPE (lhs), orig_len); + repl = gimple_build_assign (lhs, orig_len); gimple_seq_add_stmt_without_update (&stmts, repl); gsi_replace_with_seq_vops (gsi, stmts); /* gsi now points at the assignment to the lhs, get a @@ -3370,10 +3369,10 @@ gimple_fold_builtin_snprintf (gimple_stm gimple_seq stmts = NULL; gimple *repl = gimple_build_call (fn, 2, dest, fmt); gimple_seq_add_stmt_without_update (&stmts, repl); - if (gimple_call_lhs (stmt)) + if (tree lhs = gimple_call_lhs (stmt)) { - repl = gimple_build_assign (gimple_call_lhs (stmt), - build_int_cst (integer_type_node, len)); + repl = gimple_build_assign (lhs, + build_int_cst (TREE_TYPE (lhs), len)); gimple_seq_add_stmt_without_update (&stmts, repl); gsi_replace_with_seq_vops (gsi, stmts); /* gsi now points at the assignment to the lhs, get a @@ -3422,12 +3421,12 @@ gimple_fold_builtin_snprintf (gimple_stm gimple_seq stmts = NULL; gimple *repl = gimple_build_call (fn, 2, dest, orig); gimple_seq_add_stmt_without_update (&stmts, repl); - if (gimple_call_lhs (stmt)) + if (tree lhs = gimple_call_lhs (stmt)) { - if (!useless_type_conversion_p (integer_type_node, + if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (orig_len))) - orig_len = fold_convert (integer_type_node, orig_len); - repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len); + orig_len = fold_convert (TREE_TYPE (lhs), orig_len); + repl = gimple_build_assign (lhs, orig_len); gimple_seq_add_stmt_without_update (&stmts, repl); gsi_replace_with_seq_vops (gsi, stmts); /* gsi now points at the assignment to the lhs, get a --- gcc/testsuite/gcc.c-torture/compile/pr89998-1.c.jj 2019-04-08 12:52:04.946948129 +0200 +++ gcc/testsuite/gcc.c-torture/compile/pr89998-1.c 2019-04-08 12:33:33.315156426 +0200 @@ -0,0 +1,42 @@ +/* PR tree-optimization/89998 */ + +unsigned int sprintf (char *str, const char *fmt, ...); +unsigned int snprintf (char *str, __SIZE_TYPE__ len, const char *fmt, ...); + +int +f1 (char *s) +{ + return sprintf (s, "foo"); +} + +int +f2 (char *s) +{ + return sprintf (s, "%d", 123); +} + +int +f3 (int *p, char *s) +{ + const char *t = "bar"; + return sprintf (s, "%s", t); +} + +int +f4 (char *s) +{ + return snprintf (s, 8, "foo"); +} + +int +f5 (char *s) +{ + return snprintf (s, 8, "%d", 123); +} + +int +f6 (int *p, char *s) +{ + const char *t = "bar"; + return snprintf (s, 8, "%s", t); +} --- gcc/testsuite/gcc.c-torture/compile/pr89998-2.c.jj 2019-04-08 12:52:08.852883591 +0200 +++ gcc/testsuite/gcc.c-torture/compile/pr89998-2.c 2019-04-08 12:52:00.846015892 +0200 @@ -0,0 +1,4 @@ +/* PR tree-optimization/89998 */ +/* { dg-additional-options "-fno-printf-return-value" } */ + +#include "pr89998-1.c"