From patchwork Thu Apr 11 13:38:33 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 235748 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 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 3E7E62C00CA for ; Thu, 11 Apr 2013 23:39:30 +1000 (EST) 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=KduMdsPwoA0Pv8jWKbON73SmKN5mVfgMlv5LOLjnfOzEPekmPn xFzB4TKjuP4vP1XjQ1KZcpURcxlZsoOccfBU4l4o95iCwqFF0PWUjVPah1aQvHie B/vRSL0e3Xd2w8X34KfWL/r+ADRD0GKrLvw12zVDXcDOJSsd2mfi3H/eg= 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=lm5uILUIMhx5RemaDBOVowbEPkA=; b=l9EpCe2VS0e/CYJFVFFi 7pyFLr7OV4gZhOYGWwecsZiW1fLuvUm+0LlHXfOGz5EGcATtOHxV4EM+gik2L7y/ IUydRtX1lONeASMGHSwf8kvhk+zXczVxjDG5u7/Ej+XI5tekNaRCx3kkSaYGInkE F3yC7CvLMMOgWk4dVwCx4FM= Received: (qmail 29418 invoked by alias); 11 Apr 2013 13:38:36 -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 29400 invoked by uid 89); 11 Apr 2013 13:38:35 -0000 X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_NO autolearn=ham version=3.3.1 Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Thu, 11 Apr 2013 13:38:35 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 82E4C2EA95; Thu, 11 Apr 2013 09:38:33 -0400 (EDT) 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 Ir+s90TSrHXm; Thu, 11 Apr 2013 09:38:33 -0400 (EDT) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id 6583C2EA94; Thu, 11 Apr 2013 09:38:33 -0400 (EDT) Received: by kwai.gnat.com (Postfix, from userid 4192) id 5C1623FF09; Thu, 11 Apr 2013 09:38:33 -0400 (EDT) Date: Thu, 11 Apr 2013 09:38:33 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Ed Schonberg Subject: [Ada] Missing range checks on the expression for Priority in protected types Message-ID: <20130411133833.GA31294@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Found: No This patch fixes a missing check on the priority of a protected type, when it is set by a pragma or an aspect. A static value for priority that is out of the bounds of the corresponding System type is an error. A dynamic value that is out of bounds must raise a runtime exception. Compiling t.adb must yield: t.adb:6:26: warning: value not in range of type "System.Priority" t.adb:6:26: warning: "Constraint_Error" will be raised at run time t.adb:14:04: value not in range of type "System.Any_Priority" t.adb:14:04: static expression fails Constraint_Check --- with System; use System; with Text_IO; use Text_IO; procedure t is task T is pragma Priority (2 * Any_Priority'Last); entry E; end T; task body T is begin accept E; end; protected P is pragma Priority (2 * Any_Priority'Last); procedure Set (To : Boolean); function Get return Boolean; private Value : Boolean; end p; protected body P is procedure Set (To : Boolean) is begin Value := To; end Set; function Get return Boolean is begin return Value; end Get; end p; begin P.Set (True); pragma Assert (P.Get); end t; --- Tested on x86_64-pc-linux-gnu, committed on trunk 2013-04-11 Ed Schonberg * sem_prag.adb (Analyze_Pragma, case Priority): pre-analyze expression with type Any_Priority. * exp_ch9.adb (Initialize_Protection): Check that the value of the priority expression is within the bounds of the proper priority type. Index: exp_ch9.adb =================================================================== --- exp_ch9.adb (revision 197743) +++ exp_ch9.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 1992-2012, Free Software Foundation, Inc. -- +-- Copyright (C) 1992-2013, 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- -- @@ -13388,6 +13388,7 @@ Args : List_Id; L : constant List_Id := New_List; Has_Entry : constant Boolean := Has_Entries (Ptyp); + Prio_Type : Entity_Id; Restricted : constant Boolean := Restricted_Profile; begin @@ -13456,18 +13457,37 @@ Expression (First (Pragma_Argument_Associations (Prio_Clause))); + -- Get_Rep_Item returns either priority pragma. + + if Pragma_Name (Prio_Clause) = Name_Priority then + Prio_Type := RTE (RE_Any_Priority); + else + Prio_Type := RTE (RE_Interrupt_Priority); + end if; + -- Attribute definition clause Priority else + if Chars (Prio_Clause) = Name_Priority then + Prio_Type := RTE (RE_Any_Priority); + else + Prio_Type := RTE (RE_Interrupt_Priority); + end if; + Prio := Expression (Prio_Clause); end if; -- If priority is a static expression, then we can duplicate it -- with no problem and simply append it to the argument list. + -- However, it has only be pre-analyzed, so we need to check + -- now that it is in the bounds of the priority type. if Is_Static_Expression (Prio) then + Set_Analyzed (Prio, False); Append_To (Args, - Duplicate_Subexpr_No_Checks (Prio)); + Make_Type_Conversion (Loc, + Subtype_Mark => New_Occurrence_Of (Prio_Type, Loc), + Expression => Duplicate_Subexpr (Prio))); -- Otherwise, the priority may be a per-object expression, if -- it depends on a discriminant of the type. In this case, @@ -13477,18 +13497,13 @@ -- appropriate approach, but that could generate declarations -- improperly placed in the enclosing scope. - -- Note: Use System.Any_Priority as the expected type for the - -- non-static priority expression, in case the expression has - -- not been analyzed yet (as occurs for example with pragma - -- Interrupt_Priority). - else Temp := Make_Temporary (Loc, 'R', Prio); Append_To (L, Make_Object_Declaration (Loc, Defining_Identifier => Temp, Object_Definition => - New_Occurrence_Of (RTE (RE_Any_Priority), Loc), + New_Occurrence_Of (Prio_Type, Loc), Expression => Relocate_Node (Prio))); Append_To (Args, New_Occurrence_Of (Temp, Loc)); Index: sem_prag.adb =================================================================== --- sem_prag.adb (revision 197798) +++ sem_prag.adb (working copy) @@ -14521,7 +14521,7 @@ -- described in "Handling of Default and Per-Object -- Expressions" in sem.ads. - Preanalyze_Spec_Expression (Arg, Standard_Integer); + Preanalyze_Spec_Expression (Arg, RTE (RE_Any_Priority)); if not Is_Static_Expression (Arg) then Check_Restriction (Static_Priorities, Arg);