From patchwork Thu Nov 18 11:06:55 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 72080 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 84F4CB718F for ; Thu, 18 Nov 2010 22:07:05 +1100 (EST) Received: (qmail 27785 invoked by alias); 18 Nov 2010 11:07:03 -0000 Received: (qmail 27775 invoked by uid 22791); 18 Nov 2010 11:07:02 -0000 X-SWARE-Spam-Status: No, hits=-6.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, TW_CP, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 18 Nov 2010 11:06:58 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id oAIB6vbp009906 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 18 Nov 2010 06:06:57 -0500 Received: from tyan-ft48-01.lab.bos.redhat.com (tyan-ft48-01.lab.bos.redhat.com [10.16.42.4]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id oAIB6ugY023771 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Thu, 18 Nov 2010 06:06:56 -0500 Received: from tyan-ft48-01.lab.bos.redhat.com (localhost.localdomain [127.0.0.1]) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4) with ESMTP id oAIB6ueH026876 for ; Thu, 18 Nov 2010 12:06:56 +0100 Received: (from jakub@localhost) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4/Submit) id oAIB6tAN026875 for gcc-patches@gcc.gnu.org; Thu, 18 Nov 2010 12:06:55 +0100 Date: Thu, 18 Nov 2010 12:06:55 +0100 From: Jakub Jelinek To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix ICE on printf optimization with very large format string (PR middle-end/46534) Message-ID: <20101118110655.GV29412@tyan-ft48-01.lab.bos.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes 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 Hi! For the printf ("...\n") -> puts ("...") optimization we use alloca to copy the string and change it before passing it to build_string_literal. This doesn't work very well if the string is so long that we hit RLIMIT_STACK. Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2010-11-18 Jakub Jelinek PR middle-end/46534 * builtins.c (fold_builtin_printf): Don't use XALLOCAVEC for large strings. * gcc.c-torture/compile/pr46534.c: New test. Jakub --- gcc/builtins.c.jj 2010-11-16 21:47:24.000000000 +0100 +++ gcc/builtins.c 2010-11-18 09:44:19.118405504 +0100 @@ -12891,12 +12891,19 @@ fold_builtin_printf (location_t loc, tre if ((unsigned char)str[len - 1] == target_newline) { /* Create a NUL-terminated string that's one char shorter - than the original, stripping off the trailing '\n'. */ - char *newstr = XALLOCAVEC (char, len); + than the original, stripping off the trailing '\n'. + Don't use XALLOCAVEC if the string is too long. */ + char *newstr; + if (len > 16384) + newstr = XNEWVEC (char, len); + else + newstr = XALLOCAVEC (char, len); memcpy (newstr, str, len - 1); newstr[len - 1] = 0; newarg = build_string_literal (len, newstr); + if (len > 16384) + XDELETEVEC (newstr); if (fn_puts) call = build_call_expr_loc (loc, fn_puts, 1, newarg); } --- gcc/testsuite/gcc.c-torture/compile/pr46534.c.jj 2010-11-18 09:45:01.896404633 +0100 +++ gcc/testsuite/gcc.c-torture/compile/pr46534.c 2010-11-18 09:45:11.943542267 +0100 @@ -0,0 +1,17 @@ +/* PR middle-end/46534 */ + +extern int printf (const char *, ...); + +#define S1 " " +#define S2 S1 S1 S1 S1 S1 S1 S1 S1 S1 S1 +#define S3 S2 S2 S2 S2 S2 S2 S2 S2 S2 S2 +#define S4 S3 S3 S3 S3 S3 S3 S3 S3 S3 S3 +#define S5 S4 S4 S4 S4 S4 S4 S4 S4 S4 S4 +#define S6 S5 S5 S5 S5 S5 S5 S5 S5 S5 S5 +#define S7 S6 S6 S6 S6 S6 S6 S6 S6 S6 S6 + +void +foo (void) +{ + printf (S7 "\n"); +}