From patchwork Mon Mar 10 10:10:05 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uros Bizjak X-Patchwork-Id: 328527 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id E398D2C0089 for ; Mon, 10 Mar 2014 21:10:19 +1100 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; q=dns; s=default; b=eRqmC9qRUBJtvqWHSW lE58BDppbh0PgUlDC3rf68PtrfTMR3CGooMM+IftH7Riv6murx7VYKgixijxI3ZB 84zsWefYZySjqfqQ4BJzwvSdQcEcABa1yXpfTa/HRrtsXzmwFNS7dY2Oeep16smq JCF9sWAoRFQkDrkqj0eeUsYDc= 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 :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; s=default; bh=G3JXjTS+ilANxCejhVObCZul psM=; b=HO3hcJ8cAWZHlf56T0lsaDTK4dZTBXy8j/L2aBu2RLIBUayOJtitAX/4 bP09+AZGKTL1hnTNNuRYXv29mhhcftmnUxZqgAQtzPuwetE9rQgC3VFCXzHc2N34 BRLJI1Z2/zq8CTmbUNXoue37gWQuEDk2Rx3P/wNfb2zdmntRRP4= Received: (qmail 873 invoked by alias); 10 Mar 2014 10:10:10 -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 860 invoked by uid 89); 10 Mar 2014 10:10:09 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-oa0-f46.google.com Received: from mail-oa0-f46.google.com (HELO mail-oa0-f46.google.com) (209.85.219.46) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 10 Mar 2014 10:10:07 +0000 Received: by mail-oa0-f46.google.com with SMTP id i7so6809573oag.19 for ; Mon, 10 Mar 2014 03:10:06 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.182.241.8 with SMTP id we8mr220701obc.62.1394446205989; Mon, 10 Mar 2014 03:10:05 -0700 (PDT) Received: by 10.182.137.136 with HTTP; Mon, 10 Mar 2014 03:10:05 -0700 (PDT) In-Reply-To: <20140310074932.GQ22862@tucnak.redhat.com> References: <20140309173142.GP22862@tucnak.redhat.com> <20140310074932.GQ22862@tucnak.redhat.com> Date: Mon, 10 Mar 2014 11:10:05 +0100 Message-ID: Subject: Re: [PATCH, libgcc]: Avoid warning: array subscript is above array bounds when compiling crtstuff.c From: Uros Bizjak To: Jakub Jelinek Cc: Ian Lance Taylor , "gcc-patches@gcc.gnu.org" On Mon, Mar 10, 2014 at 8:49 AM, Jakub Jelinek wrote: > On Sun, Mar 09, 2014 at 10:38:25PM +0100, Uros Bizjak wrote: >> On Sun, Mar 9, 2014 at 6:31 PM, Jakub Jelinek wrote: >> > On Sun, Mar 09, 2014 at 09:41:59AM -0700, Ian Lance Taylor wrote: >> >> >>> Attached patch avoids a bunch of: >> >> >>> >> >> >>> ../../../gcc-svn/trunk/libgcc/crtstuff.c: In function 'frame_dummy': >> >> >>> ../../../gcc-svn/trunk/libgcc/crtstuff.c:463:19: warning: array >> >> >>> subscript is above array bounds [-Warray-bounds] >> >> >>> if (__JCR_LIST__[0]) >> >> >>> ^ >> >> >>> >> >> >>> when compiling libgcc. >> >> >>> >> >> >>> 2014-03-08 Uros Bizjak >> >> >>> >> >> >>> * crtstuff.c (__JCR_LIST__): Declare as zero-length array. >> > >> > I guess the only thing to avoid the warning (and potential miscompilation) >> > is to hide the access from the optimizers through something like: >> > void *jcr_list; >> > __asm ("" : "=g" (jcr_list) : "0" (__JCR_LIST__)); >> > and then use jcr_list instead of __JCR_LIST__. >> >> Attached patch builds on your idea, but jcr_list temporary has to be >> declared as void ** to allow proper dereference of pointer to void >> array. >> >> The resulting code is also a bit better, as shown by following test: > > Well, better is non-obvious, while it is smaller (which is good for > initialization and thus rarely executed code), the common case is that > *jcr_list is 0 (gcj is used rarely these days) and for the common case it is > one instruction longer. > Perhaps at least use if (__builtin_expect (*jcr_list != NULL, 0))? > Otherwise looks good to me. Following source: void frame_dummy (void) { void **jcr_list = __JCR_LIST__; if (__builtin_expect (*jcr_list != 0, 0)) register_classes (jcr_list); } generates exactly the same code while avoiding the warning. So, following your concern, I am testing following patch: --cut here-- --cut here-- Uros. Index: crtstuff.c =================================================================== --- crtstuff.c (revision 208448) +++ crtstuff.c (working copy) @@ -460,12 +460,13 @@ #endif /* USE_EH_FRAME_REGISTRY */ #ifdef JCR_SECTION_NAME - if (__JCR_LIST__[0]) + void **jcr_list = __JCR_LIST__; + if (__builtin_expect (*jcr_list != NULL, 0)) { void (*register_classes) (void *) = _Jv_RegisterClasses; __asm ("" : "+r" (register_classes)); if (register_classes) - register_classes (__JCR_LIST__); + register_classes (jcr_list); } #endif /* JCR_SECTION_NAME */ @@ -565,12 +566,13 @@ #endif #ifdef JCR_SECTION_NAME - if (__JCR_LIST__[0]) + void **jcr_list = __JCR_LIST__; + if (__builtin_expect (*jcr_list != NULL, 0)) { void (*register_classes) (void *) = _Jv_RegisterClasses; __asm ("" : "+r" (register_classes)); if (register_classes) - register_classes (__JCR_LIST__); + register_classes (jcr_list); } #endif