From patchwork Thu Jun 15 08:04:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 108334 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp470930vqr; Thu, 15 Jun 2023 01:15:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5fKwWLxXFXWlz9A3sXquWWXVrvieF7/UuRo8pjc7SIFNDfNE9eVs69EyXNS5j99ieMfoVz X-Received: by 2002:a17:907:2d07:b0:967:13a3:d82c with SMTP id gs7-20020a1709072d0700b0096713a3d82cmr4079758ejc.26.1686816955570; Thu, 15 Jun 2023 01:15:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686816955; cv=none; d=google.com; s=arc-20160816; b=s6QycSBqMoorS1CRoFhhY1kKCKTo/O/hdI9TGuhaLSb1nNAQQ1yirC1qnT0n7p0PaM D4/rJqO80/AFTmWWaa9ulvoxMY68eK4Bke/AfTCofsYWUoc2rTSuN7J5QvkAm3RsQeDi DOMKrKNm+U4NwZMhjCM4s9el/nmEHZpQB0R7vcgbmEfZWWr2erIVGFVPEsef8UmiEVyN 1BiLGNBDb1j+Wx/CFEyD/OUGOVhpQKgHediEGT4S/ZszSiItj9u6K5qlfRIV2g8HKBRa IVvz6cebnQ+XXouSUqBVGvjF++M1y4Eyx5QWksIRkx2plM6zEZVmQXOGuTKNx4u8kqz3 Mrxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=d9trltaIPAMCRW2+T1/pKqc+w/pX+5B39d2Xrg3VY2g=; b=lswx2KRP1rfvFVjrsjxqKbfuXiSh0TbDauk7u5A0dXBBwk4Neg+XgFUSAZifnhm0s/ MptwACo/UJhkQr2eUEXb8dHJSs+UJU9iKAi6sTV0kS40kJY7x8I24Sjes96X4uNoAqLM V5IHvmJLSwCV6RJRo4y1oFH++zl801s1WTLYmgSmbJaapFQ1wugkTEKxurCvIsrMqBQX akt1o4+PI1YFdr8oULc2X1SwxS/aHg8sX3EHTyEIRGGLDwVBPbxurXQryMcZqCh/vrzn 2QaoSQlmEqk3qqygynSJ5h4vFsYJ7bgz8RBXkABiCatZ5Mv6Gy/eAjMrEC+xWhMBn9OW DzXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=bt9XjtAM; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id v4-20020a1709060b4400b0098282239139si1496489ejg.174.2023.06.15.01.15.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jun 2023 01:15:55 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=bt9XjtAM; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2C629394D8F8 for ; Thu, 15 Jun 2023 08:09:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C629394D8F8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686816565; bh=d9trltaIPAMCRW2+T1/pKqc+w/pX+5B39d2Xrg3VY2g=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=bt9XjtAMxp+hcn7Vc/KByN7FklOAr8+Vic+A/nQPufgHLLLFb7TeSSB+v2tDFVEmj E87xS8QbwBJLMyHcc2mit49QEr7TnbfY73I1EnlNkl6cE+9yWI/jRJZFk/fRrHAsdo aZYMEO7fqXxF68MqXaa3IGkemkDbwlAvoJYj1ocQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id 1CB73385770C for ; Thu, 15 Jun 2023 08:04:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1CB73385770C Received: by mail-wr1-x430.google.com with SMTP id ffacd0b85a97d-30fbf253dc7so358448f8f.0 for ; Thu, 15 Jun 2023 01:04:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686816270; x=1689408270; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=d9trltaIPAMCRW2+T1/pKqc+w/pX+5B39d2Xrg3VY2g=; b=AMfHIB+p0WaZPOJUSHQgLMIDeZ4at4TDvsqdXpFxfzEzh1DPK6FlK2SOg4yDBlhEY/ pizwIYlot7zzy5qtTNGhGBgAzsj/f0lhGBvfQcHmj2wMzLE2ZlpM3T1GJ8+vzBBYCcxn aTGhkKVwkKZxLK/G9lUj7gH367ykisdakBHjTZCSxkg9FEE1N87wzpyaIfa7SmZo82tp FIkpBgsyP6mPbBaauw+3XfWAQ9/IYl9ziwFHrBRkIOmI/5Q3vmGd7j/1Z8swOrApKPZA nxaCLzbpPwkKy5UdHGEcrbSKhcTzJmt+AYCaDnpGE52p2Dxjgzk3aGR+A0y6wO+2SZGm 5yUg== X-Gm-Message-State: AC+VfDzpCxFRPWL6KnOw6v1bAtvABVMbqetwCJzMj08TikEYGRdOVtmf JpbB6kxKikorDkMGs0G5//LYKoXUPXh2VIButYPung== X-Received: by 2002:a5d:43cb:0:b0:311:f11:4c55 with SMTP id v11-20020a5d43cb000000b003110f114c55mr2864936wrr.13.1686816269777; Thu, 15 Jun 2023 01:04:29 -0700 (PDT) Received: from poulhies-Precision-5550.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id e2-20020a5d5942000000b003063db8f45bsm20164696wri.23.2023.06.15.01.04.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jun 2023 01:04:29 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED] ada: Fix too small secondary stack allocation for returned conversion Date: Thu, 15 Jun 2023 10:04:28 +0200 Message-Id: <20230615080428.940106-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: =?utf-8?q?Marc_Poulhi=C3=A8s?= Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768755775910198355?= X-GMAIL-MSGID: =?utf-8?q?1768755775910198355?= From: Eric Botcazou The previous fix did not address a latent issue whereby the allocation would be made using the (static) subtype of the conversion instead of the (dynamic) subtype of the return object, so this change rewrites the code responsible for determining the type used for the allocation, and also contains a small improvement to the Has_Tag_Of_Type predicate. gcc/ada/ * exp_ch3.adb (Make_Allocator_For_Return): Rewrite the logic that determines the type used for the allocation and add assertions. * exp_util.adb (Has_Tag_Of_Type): Also return true for extension aggregates. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch3.adb | 92 +++++++++++++++++++++++++++++--------------- gcc/ada/exp_util.adb | 1 + 2 files changed, 63 insertions(+), 30 deletions(-) diff --git a/gcc/ada/exp_ch3.adb b/gcc/ada/exp_ch3.adb index 778eed7f16e..7ac4680b395 100644 --- a/gcc/ada/exp_ch3.adb +++ b/gcc/ada/exp_ch3.adb @@ -7114,8 +7114,64 @@ package body Exp_Ch3 is function Make_Allocator_For_Return (Expr : Node_Id) return Node_Id is Alloc : Node_Id; Alloc_Expr : Entity_Id; + Alloc_Typ : Entity_Id; begin + -- If the return object's declaration does not include an expression, + -- then we use its subtype for the allocation. Likewise in the case + -- of a degenerate expression like a raise expression. + + if No (Expr) + or else Nkind (Original_Node (Expr)) = N_Raise_Expression + then + Alloc_Typ := Typ; + + -- If the return object's declaration includes an expression, then + -- there are two cases: either the nominal subtype of the object is + -- definite and we can use it for the allocation directly, or it is + -- not and Analyze_Object_Declaration should have built an actual + -- subtype from the expression. + + -- However, there are exceptions in the latter case for interfaces + -- (see Analyze_Object_Declaration), as well as class-wide types and + -- types with unknown discriminants if they are additionally limited + -- (see Expand_Subtype_From_Expr), so we must cope with them. + + elsif Is_Interface (Typ) then + pragma Assert (Is_Class_Wide_Type (Typ)); + + -- For interfaces, we use the type of the expression, except if + -- we need to put back a conversion that we have removed earlier + -- in the processing. + + if Is_Class_Wide_Type (Etype (Expr)) then + Alloc_Typ := Typ; + else + Alloc_Typ := Etype (Expr); + end if; + + elsif Is_Class_Wide_Type (Typ) then + + -- For class-wide types, we have to make sure that we use the + -- dynamic type of the expression for the allocation, either by + -- means of its (static) subtype or through the actual subtype. + + if Has_Tag_Of_Type (Expr) then + Alloc_Typ := Etype (Expr); + + else pragma Assert (Ekind (Typ) = E_Class_Wide_Subtype + and then Present (Equivalent_Type (Typ))); + + Alloc_Typ := Typ; + end if; + + else pragma Assert (Is_Definite_Subtype (Typ) + or else (Has_Unknown_Discriminants (Typ) + and then Is_Limited_View (Typ))); + + Alloc_Typ := Typ; + end if; + -- If the return object's declaration includes an expression and the -- declaration isn't marked as No_Initialization, then we generate an -- allocator with a qualified expression. Although this is necessary @@ -7141,46 +7197,22 @@ package body Exp_Ch3 is Alloc_Expr := New_Copy_Tree (Expr); - -- In the constrained array case, deal with a potential sliding. - -- In the interface case, put back a conversion that we may have - -- removed earlier in the processing. - - if (Ekind (Typ) = E_Array_Subtype - or else (Is_Interface (Typ) - and then Is_Class_Wide_Type (Etype (Alloc_Expr)))) - and then Typ /= Etype (Alloc_Expr) - then - Alloc_Expr := Convert_To (Typ, Alloc_Expr); + if Etype (Alloc_Expr) /= Alloc_Typ then + Alloc_Expr := Convert_To (Alloc_Typ, Alloc_Expr); end if; - -- We always use the type of the expression for the qualified - -- expression, rather than the return object's type. We cannot - -- always use the return object's type because the expression - -- might be of a specific type and the return object might not. - Alloc := Make_Allocator (Loc, Expression => Make_Qualified_Expression (Loc, Subtype_Mark => - New_Occurrence_Of (Etype (Alloc_Expr), Loc), + New_Occurrence_Of (Alloc_Typ, Loc), Expression => Alloc_Expr)); else - -- If the return object is of a class-wide type, we cannot use - -- its type for the allocator. Instead we use the type of the - -- expression, which must be an aggregate of a definite type. - - if Is_Class_Wide_Type (Typ) then - Alloc := - Make_Allocator (Loc, - Expression => New_Occurrence_Of (Etype (Expr), Loc)); - - else - Alloc := - Make_Allocator (Loc, - Expression => New_Occurrence_Of (Typ, Loc)); - end if; + Alloc := + Make_Allocator (Loc, + Expression => New_Occurrence_Of (Alloc_Typ, Loc)); -- If the return object requires default initialization, then it -- will happen later following the elaboration of the renaming. diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb index 91959793638..0d0ad8a5d6c 100644 --- a/gcc/ada/exp_util.adb +++ b/gcc/ada/exp_util.adb @@ -7283,6 +7283,7 @@ package body Exp_Util is when N_Indexed_Component | N_Selected_Component | N_Aggregate + | N_Extension_Aggregate => return True;