From patchwork Fri Nov 23 19:50:33 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 201394 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 E89C52C008C for ; Sat, 24 Nov 2012 06:50:50 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1354305053; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Date:From:To:Cc:Subject:Message-ID:Reply-To: References:MIME-Version:Content-Type:Content-Disposition: In-Reply-To:User-Agent:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=ovxqc3TyxGIaGVyQI+QDul5kzGw=; b=J5jaLJyrQCqtbQY 0SigGTvDQ21E4syi2TDg0JjVUMLRcLVJpQk2BEC41jwRqhHpIANbOi/oXLHk7uzc cH7F09FXaN39QPOTuXHrFvdySQB9Tzq3BVAfDSDlb/npV+v4Fkr4alBHG5Fhs6xa yGrUvnoclboAOpAumRhoGwwfac70= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Received:Date:From:To:Cc:Subject:Message-ID:Reply-To:References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To:User-Agent:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=Lh0OVuUm8azOfiouEGoqQs3CjvZtope0XWfPkAPa4Xho1rAjZJclEnh5D6hX1D Tj2bcL8Y0r1Gr9WI1aG6izJ0y8PyfYtMu1RL6Nc9czC5G7mqQAC0V4RSpw19qDDp 90XcHOWE84PHOCoVEc7enWKRT7YZUqjajzt5mnmDO77mA=; Received: (qmail 21470 invoked by alias); 23 Nov 2012 19:50:47 -0000 Received: (qmail 21449 invoked by uid 22791); 23 Nov 2012 19:50:46 -0000 X-SWARE-Spam-Status: No, hits=-6.6 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, RP_MATCHES_RCVD, SPF_HELO_PASS 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; Fri, 23 Nov 2012 19:50:38 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id qANJoape011568 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 23 Nov 2012 14:50:37 -0500 Received: from zalov.redhat.com (vpn1-7-208.ams2.redhat.com [10.36.7.208]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id qANJoYfv009233 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Fri, 23 Nov 2012 14:50:35 -0500 Received: from zalov.cz (localhost [127.0.0.1]) by zalov.redhat.com (8.14.5/8.14.5) with ESMTP id qANJoXX4030866; Fri, 23 Nov 2012 20:50:33 +0100 Received: (from jakub@localhost) by zalov.cz (8.14.5/8.14.5/Submit) id qANJoXgT030865; Fri, 23 Nov 2012 20:50:33 +0100 Date: Fri, 23 Nov 2012 20:50:33 +0100 From: Jakub Jelinek To: "H.J. Lu" Cc: Uros Bizjak , gcc-patches@gcc.gnu.org, Tom Tromey Subject: Re: [PATCH] Allocate extra 16 bytes for -fsanitize=address Message-ID: <20121123195033.GG2315@tucnak.redhat.com> Reply-To: Jakub Jelinek References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: 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 On Fri, Nov 23, 2012 at 11:33:37AM -0800, H.J. Lu wrote: > 2012-11-21 H.J. Lu > > PR bootstrap/55380 > * charset.c (_cpp_convert_input): Clear CPP_PAD_BUFFER_SIZE > bytes if CLEAR_CPP_PAD_BUFFER isn't 0. Allocate extra > CPP_PAD_BUFFER_SIZE bytes and clear it if CLEAR_CPP_PAD_BUFFER > isn't 0. > > * files.c (read_file_guts): Allocate extra CPP_PAD_BUFFER_SIZE > bytes. > > * internal.h (CPP_PAD_BUFFER_SIZE): New. Defined to 16 for > -fsanitize=address if __SANITIZE_ADDRESS__ is defined. > (CLEAR_CPP_PAD_BUFFER): New. I'd say you are making this way too much complicated. The following patch just changes those + 1 to + 16, adds memset of the 16 bytes unconditionally, but as it also fixes a thinko which IMHO affects the most common cases (regular file, with no conversion needed), I'd say it ought to be faster than the old version (the old version would allocate st.st_size + 1 bytes long buffer, read st.st_size bytes into it, call _cpp_convert_input with len st.st_size and size st.st_size (the latter should have been st.st_size + 1, i.e. the allocated size) and thus to.len >= to.asize was true and we called realloc with the same length as malloc has been called originally. 2012-11-23 Jakub Jelinek PR bootstrap/55380 * files.c (read_file_guts): Allocate extra 16 bytes instead of 1 byte at the end of buf. Pass size + 16 instead of size to _cpp_convert_input. * charset.c (_cpp_convert_input): Reallocate if there aren't at least 16 bytes beyond to.len in the buffer. Clear 16 bytes at to.text + to.len. Jakub --- libcpp/files.c.jj 2012-11-22 11:04:24.000000000 +0100 +++ libcpp/files.c 2012-11-23 20:37:03.146379853 +0100 @@ -671,7 +671,7 @@ read_file_guts (cpp_reader *pfile, _cpp_ the majority of C source files. */ size = 8 * 1024; - buf = XNEWVEC (uchar, size + 1); + buf = XNEWVEC (uchar, size + 16); total = 0; while ((count = read (file->fd, buf + total, size - total)) > 0) { @@ -682,7 +682,7 @@ read_file_guts (cpp_reader *pfile, _cpp_ if (regular) break; size *= 2; - buf = XRESIZEVEC (uchar, buf, size + 1); + buf = XRESIZEVEC (uchar, buf, size + 16); } } @@ -699,7 +699,7 @@ read_file_guts (cpp_reader *pfile, _cpp_ file->buffer = _cpp_convert_input (pfile, CPP_OPTION (pfile, input_charset), - buf, size, total, + buf, size + 16, total, &file->buffer_start, &file->st.st_size); file->buffer_valid = true; --- libcpp/charset.c.jj 2011-01-06 10:22:00.000000000 +0100 +++ libcpp/charset.c 2012-11-23 20:40:39.736116642 +0100 @@ -1,6 +1,6 @@ /* CPP Library - charsets Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2008, 2009, - 2010 Free Software Foundation, Inc. + 2010, 2012 Free Software Foundation, Inc. Broken out of c-lex.c Apr 2003, adding valid C99 UCN ranges. @@ -1729,9 +1729,12 @@ _cpp_convert_input (cpp_reader *pfile, c iconv_close (input_cset.cd); /* Resize buffer if we allocated substantially too much, or if we - haven't enough space for the \n-terminator. */ - if (to.len + 4096 < to.asize || to.len >= to.asize) - to.text = XRESIZEVEC (uchar, to.text, to.len + 1); + haven't enough space for the \n-terminator or following + 15 bytes of padding. */ + if (to.len + 4096 < to.asize || to.len + 16 > to.asize) + to.text = XRESIZEVEC (uchar, to.text, to.len + 16); + + memset (to.text + to.len, '\0', 16); /* If the file is using old-school Mac line endings (\r only), terminate with another \r, not an \n, so that we do not mistake