From patchwork Thu Feb 5 11:22:45 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 436753 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 5336114017C for ; Thu, 5 Feb 2015 22:23:43 +1100 (AEDT) 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:mime-version:content-type; q=dns; s=default; b=uikUw0ec24a0bqRc1+T/Fjr90R+EImMOWWEapTLc8b967mG/Tq R2HTy9zJ3Z0nxG6Msp5oUIgwJ7QDxciW77FhMhS51Y67LksFagUZIGOo7eKXlMAe y23YeF3KS35Ir49ZxwV8KaW/XvSJGCHF6jc0kwSIavUOm6lksp/iYMkTg= 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:mime-version:content-type; s= default; bh=s39SEKCGeF4ZbpPIAhCpE+Vn69w=; b=A3cz4xVNL4/Ni6TL+zkv mOfLz6w7TrvCrjeyyoSH8/gefnyAQ5/H7QihlmyblNLBqS07PB3GTOkRdStCbign Ljty6aVhWMES4iTVw2hPBZ8+nLcrNxBzrAEZhKdX1Xau5BsYXac96sPClsyK47Iz LNAULkr5UMSqThxv3H79iBw= Received: (qmail 11506 invoked by alias); 5 Feb 2015 11:22:48 -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 11487 invoked by uid 89); 5 Feb 2015 11:22:48 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham version=3.3.2 X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Thu, 05 Feb 2015 11:22:47 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 9883D1166EC; Thu, 5 Feb 2015 06:22:45 -0500 (EST) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id WWeX2tEZEgj2; Thu, 5 Feb 2015 06:22:45 -0500 (EST) Received: from kwai.gnat.com (kwai.gnat.com [IPv6:2620:20:4000:0:7a2b:cbff:fe60:cb11]) by rock.gnat.com (Postfix) with ESMTP id 865BC1166E8; Thu, 5 Feb 2015 06:22:45 -0500 (EST) Received: by kwai.gnat.com (Postfix, from userid 4192) id 8295F3FE3B; Thu, 5 Feb 2015 06:22:45 -0500 (EST) Date: Thu, 5 Feb 2015 06:22:45 -0500 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Robert Dewar Subject: [Ada] Fix missing index check with optimization on Message-ID: <20150205112245.GA20558@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) In some unusual cases, the index check on a subscript of an unconstrained array was omitted. The following test program 1. package Out_Constraint_Violation is 2. type Arr is array (Natural range <>) of Integer; 3. procedure Violate 4. (Input : Arr; Modifier : Arr; Output: out Arr); 5. end Out_Constraint_Violation; 1. package body Out_Constraint_Violation is 2. procedure Violate 3. (Input : Arr; Modifier : Arr; Output: out Arr) is 4. begin 5. for J in Input'Range loop 6. -- For the error to occur, 7. -- we need to have a declare block ... 8. declare 9. -- ... with at least this level of complexity. 10. Product : constant Integer := Modifier (J); 11. begin 12. Output (J) := Product / 2; 13. end; 14. end loop; 15. end Violate; 16. end Out_Constraint_Violation; 1. with Ada.Text_IO; use Ada.Text_IO; 2. procedure Out_Constraint_Violation.Main is 3. Size : constant := 10; 4. Input : Arr (1 .. Size) := (others => 128); 5. Modifier : Arr (1 .. Size) := (others => 128); 6. Output : Arr (1 .. Size + 1) := (others => 42); 7. begin 8. Violate (Input, Modifier, Output (1 .. Size - 1)); 9. Put_Line ("Size:" & Integer'Image (Size)); 10. for L in Size - 1 .. Size + 1 loop 11. Put_Line ("Output(" & L'Img & "):" & 12. Output (L)'Img); 13. end loop; 14. end Out_Constraint_Violation.Main; If compiled with -O should raise CE at run time: raised CONSTRAINT_ERROR : out_constraint_violation.adb:12 index check failed Tested on x86_64-pc-linux-gnu, committed on trunk 2015-02-05 Robert Dewar * checks.adb (Enable_Range_Check): Disconnect attempted optimization for the case of range check for subscript of unconstrained array. Index: checks.adb =================================================================== --- checks.adb (revision 220439) +++ checks.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 1992-2014, Free Software Foundation, Inc. -- +-- Copyright (C) 1992-2015, Free Software Foundation, Inc. -- -- -- -- GNAT is free software; you can redistribute it and/or modify it under -- -- terms of the GNU General Public License as published by the Free Soft- -- @@ -5521,10 +5521,14 @@ return; end if; - -- Ditto if the prefix is an explicit dereference whose designated - -- type is unconstrained. + -- Ditto if prefix is simply an unconstrained array. We used + -- to think this case was OK, if the prefix was not an explicit + -- dereference, but we have now seen a case where this is not + -- true, so it is safer to just suppress the optimization in this + -- case. The back end is getting better at eliminating redundant + -- checks in any case, so the loss won't be important. - elsif Nkind (Prefix (P)) = N_Explicit_Dereference + elsif Is_Array_Type (Atyp) and then not Is_Constrained (Atyp) then Activate_Range_Check (N);