From patchwork Tue Jun 2 08:45:25 2026 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: 136277 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 871534BA2E19 for ; Tue, 2 Jun 2026 09:23:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 871534BA2E19 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=OepAgQmP X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id 92D974BA23DB for ; Tue, 2 Jun 2026 08:46:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 92D974BA23DB Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 92D974BA23DB Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::333 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1780389988; cv=none; b=Kty2o1g8C1Nv/nUgNFLVQCsLl2MGdlv/l0HgamNRXI8OGfDbf51YLfubnAoIfWi7gwyGxs5hIHS9IKMPkR116ix1CVmqwUs5QYurPrJC1y0zI2G+zD3QKuj9fOwZRwG4n38PIRGnAnNkZXLcRYkYdtFka7BT37tEee08i7Cjbag= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1780389988; c=relaxed/simple; bh=VP/HC1E13wC87OJ6H4t52Qsr8b/kU+KfMDA+JPWRfR4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=SMXDskfs9FCh8QATSnqNC9hCT69ldR69g6PQPAo+BZlH2cWQMWZy+uxOWlnRRaUrhUaOse7CUFJu+EZeOVRXtDP77Yx9rJkm1eAcKpkE100EgIQEuJnni2SCF8MMC9Pba5eTH/RZDm+ISzyi8iVw8vhKcduvwtnQgFv31y2vmVU= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=OepAgQmP DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 92D974BA23DB Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-490aebf33e9so8027605e9.3 for ; Tue, 02 Jun 2026 01:46:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1780389987; x=1780994787; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xpC1EYLt/gLMZLwuCqIcyyVPi7c8oUIdWy+qltndQsE=; b=OepAgQmPygG33sdZGVWPKuYd/1Afy9SACx7g1+h1a6gK96wMnyBLdWnwUv9pTKH7Qv ye0LuShRL917p31woLdjnPonW/IzyFbG4qSAqpOxr52ehPRq8dK7t+ts4l0X4ULKxAxv J1YBVQfMv/mXHOB1Lip1dLqU72MMBIZZQT2Ds34jB1tA9/q8L1yRyBbH49a6Gh61YF2I hs9iWhNE1aWmlv69snhgDzt45pE+P7iOI+6PiQg79xIN1kTPcM6PMmMeuz2DRpfFpYqW E6fm/x/+d/pRCr2MHGpwVxxmrZsny9CSErpfO2ZMyvAT9kmPVmdJfNNob51nkFRh4iNn C4xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780389987; x=1780994787; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=xpC1EYLt/gLMZLwuCqIcyyVPi7c8oUIdWy+qltndQsE=; b=aZPy4P+GKamOVcXhAthHaA82hPAaSotujxynzVelfXSXz1XWxDcgADG+M0cl1uenAE SC+sYWTPKddaXEoLq5rKASmlOFM1FwvVscbeRB839FoqvWLPUzFsuOI3Ix4NJTIVlpIi 2JAPs/cS/uQlIvSEJECkmYgDnSZP31W9u/+6gitV/KtmiGrgmuUDAUHY3Ij3+NONkm73 nOAkftErhSj0yCsXoxPJjuHSieofYc9N8FDT4l9xe2z+wTUlR4Tk0g0Nn2nJGzx9dlLI k/1BIfdTbnCsy+Ixx7RIwnsXLOrjCVzAuVEeC5xkmdPm/3bnJg9/qjzHkx63LCKaUad9 nISA== X-Gm-Message-State: AOJu0Yw+sjvccHdvVwM6uHdXjqbwYN5zftlN8TPNbs/LEDgQRWsX6bBD U4VC8MnpwFPs4Tufpeza9Gg7DdqNaJJLIYrYdH25f15GZnFbxvc9YlP1DXfmfiPr9XIbuLNgnZj dztc= X-Gm-Gg: Acq92OGc6ezUh5a2xQ30jJExYkfaXY/rq2rqFbaKsmyfCkdmZRjB66lo4pLWlB4Lg33 o9NJsBiGUHw9Is6xm0JMuGXnONFTagXwzkqjtO5fZNhFfoqnuuJTMkapK8YgIxt+fSluT4xXn3Y qbdTNXjyYJ4+yymscUa4jpmGqkaUkmMhRLdOyNlaM1gVtyol3OTE9kOmyn1NwTREy70GfLckKJp jICGc1i4jbFW8VzB6+SYxKeowEOSlzBcvU0Oku3z5qldNhaoN4BEG4i5quGfIQl4gWLHnFQDwRv wAET9A/Y5RjJ87htVOg633DrwdXs++ko1Cy4NYd8QDUafrWPtq5Le4TEL8K+RfPNOTaSJfVkCmo +9yMNgQIYnG8jJJuwH4oTeAEQFKL68Ew+hJ0yJ3H4RGAg7jDW8NfYxHvfhGnr2lw1kCbSp7ZbeG KbUd4AQre/+YV2aRi7N6NEcVOaOqED/UR4TrEIXx9IHHhPGWVgTRuF/46VBrL6V5B2AUEcu2fwH Jt4yB0cGxeWwaoky5RC836gzs53GGs= X-Received: by 2002:a05:600c:c3cf:20b0:490:47e0:e13f with SMTP id 5b1f17b1804b1-490a292a3bamr198159625e9.3.1780389987442; Tue, 02 Jun 2026 01:46:27 -0700 (PDT) Received: from mecano.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-490ab55d39csm33907625e9.35.2026.06.02.01.46.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jun 2026 01:46:27 -0700 (PDT) From: =?utf-8?q?Marc_Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED 37/51] ada: Fix crash on allocator of class-wide interface as actual in subprogram call Date: Tue, 2 Jun 2026 10:45:25 +0200 Message-ID: <20260602084541.3829876-37-poulhies@adacore.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260602084541.3829876-1-poulhies@adacore.com> References: <20260602084541.3829876-1-poulhies@adacore.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_PBL, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org From: Eric Botcazou The crash occurs because the type of the allocator lacks the Master_Id to be passed to the function initializing the allocator, which is required because the interface is limited and therefore may be the progenitor of a type that contains tasks. It comes from a couple of problems: 1) the special conversion trick used in Resolve_Actuals to force the displacement of the pointer, which blocks the proper resolution of the allocator (its E_Allocator_Type is not replaced), and 2) the lack of Master_Id on the E_Access_Subtype created for the subtype of the parameter in the subprogram call, which is the not null variant of a named access type. The former problem is solved by resolving the allocator explicitly, while the latter is solved by changing Master_Id to live on root types only (in accordance with RM 9.3(2), which says that it should designate the master construct that elaborates the ultimate ancestor of a given access type). The change contains a couple of additional fixes: 3) the proper resolution of the allocator causes missing accessibility checks to be generated, which in turn generates a fair amount of useless access checks and 4) an incorrect transient scope would be created around the allocator when the type of the parameter is named, while it's only needed for an anonymous access type. gcc/ada/ChangeLog: * gen_il-gen-gen_entities.adb (Access_Kind): Add Root_Type_Only for the Master_Id field. * einfo.ads (Master_Id): Document that it lives on root types only. * accessibility.adb (Apply_Accessibility_Check_For_Allocator): Avoid generating secondary useless checks. * exp_ch3.adb (Build_Initialization_Call): Use the Master_Id of the type of target reference directly. (Build_Master): Minor tweak. (Expand_N_Full_Type_Declaration): Do not call Build_Master for a derived access type declaration. * exp_ch4.adb (Expand_N_Allocator): Test Master_Id directly on the access type. * sem_res.adb (Resolve_Actuals): In the case of an allocator, if the designated type is a CW interface, call Convert_To_And_Rewrite to add the special conversion but nevertheless resolve the allocator to the type of the formal afterward; do not establish a transient scope if the type is a named access type. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/accessibility.adb | 3 +- gcc/ada/einfo.ads | 6 ++-- gcc/ada/exp_ch3.adb | 11 +++--- gcc/ada/exp_ch4.adb | 2 +- gcc/ada/gen_il-gen-gen_entities.adb | 2 +- gcc/ada/sem_res.adb | 54 +++++++++++++---------------- 6 files changed, 38 insertions(+), 40 deletions(-) diff --git a/gcc/ada/accessibility.adb b/gcc/ada/accessibility.adb index 396c8914f9a..9b5d0fba73a 100644 --- a/gcc/ada/accessibility.adb +++ b/gcc/ada/accessibility.adb @@ -1169,7 +1169,8 @@ package body Accessibility is Insert_Action (N, Make_Implicit_If_Statement (N, Condition => Cond, - Then_Statements => Stmts)); + Then_Statements => Stmts), + Suppress => All_Checks); end if; end Apply_Accessibility_Check_For_Allocator; diff --git a/gcc/ada/einfo.ads b/gcc/ada/einfo.ads index 9c293f47814..b156de6ee05 100644 --- a/gcc/ada/einfo.ads +++ b/gcc/ada/einfo.ads @@ -3688,7 +3688,7 @@ package Einfo is -- having Has_Machine_Radix_Clause True. This happens when a type is -- derived from a type with a clause present. --- Master_Id +-- Master_Id [root type only] -- Defined in access types and subtypes. Empty unless Has_Task is set for -- the designated type, in which case it points to the entity for the -- Master_Id for the access type master. Also set for access-to-limited- @@ -5252,7 +5252,7 @@ package Einfo is -- E_Access_Type -- E_Access_Subtype -- Direct_Primitive_Operations $$$ type - -- Master_Id + -- Master_Id (root type only) -- Directly_Designated_Type -- Associated_Storage_Pool (root type only) -- Finalization_Collection (root type only) @@ -5704,7 +5704,7 @@ package Einfo is -- E_General_Access_Type -- First_Entity $$$ -- Renamed_Entity $$$ - -- Master_Id + -- Master_Id (root type only) -- Directly_Designated_Type -- Associated_Storage_Pool (root type only) -- Finalization_Collection (root type only) diff --git a/gcc/ada/exp_ch3.adb b/gcc/ada/exp_ch3.adb index 843dc173c25..12cbd58a42b 100644 --- a/gcc/ada/exp_ch3.adb +++ b/gcc/ada/exp_ch3.adb @@ -2564,8 +2564,7 @@ package body Exp_Ch3 is Append_To (Args, Make_Integer_Literal (Loc, Library_Task_Level)); elsif Present (Target_Ref) then Append_To (Args, - New_Occurrence_Of - (Master_Id (Base_Type (Root_Type (Etype (Target_Ref)))), Loc)); + New_Occurrence_Of (Master_Id (Etype (Target_Ref)), Loc)); else Append_To (Args, Make_Identifier (Loc, Name_uMaster)); end if; @@ -6882,8 +6881,8 @@ package body Exp_Ch3 is -- but testing Comes_From_Source may be too general in this case -- (affects some test output)??? - elsif not Is_Param_Block_Component_Type (Ptr_Typ) - and then Is_Limited_Class_Wide_Type (Desig_Typ) + elsif Is_Limited_Class_Wide_Type (Desig_Typ) + and then not Is_Param_Block_Component_Type (Ptr_Typ) then Build_Master_Entity (N); Build_Master_Renaming (Ptr_Typ); @@ -6901,7 +6900,9 @@ package body Exp_Ch3 is begin if Is_Access_Type (Def_Id) then - Build_Master (Def_Id); + if Nkind (Type_Definition (N)) /= N_Derived_Type_Definition then + Build_Master (Def_Id); + end if; if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then Expand_Access_Protected_Subprogram_Type (N); diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb index 2763aed4f9a..1bad0f9a3c8 100644 --- a/gcc/ada/exp_ch4.adb +++ b/gcc/ada/exp_ch4.adb @@ -4976,7 +4976,7 @@ package body Exp_Ch4 is -- created when expanding the function declaration. if Has_Task (Etyp) then - if No (Master_Id (Base_Type (PtrT))) then + if No (Master_Id (PtrT)) then -- The designated type was an incomplete type, and the -- access type did not get expanded. Salvage it now. diff --git a/gcc/ada/gen_il-gen-gen_entities.adb b/gcc/ada/gen_il-gen-gen_entities.adb index 716c1d33f7a..60de3aa27d8 100644 --- a/gcc/ada/gen_il-gen-gen_entities.adb +++ b/gcc/ada/gen_il-gen-gen_entities.adb @@ -652,7 +652,7 @@ begin -- Gen_IL.Gen.Gen_Entities Sm (Is_Local_Anonymous_Access, Flag), Sm (Is_Param_Block_Component_Type, Flag, Base_Type_Only), Sm (Is_Pure_Unit_Access_Type, Flag), - Sm (Master_Id, Node_Id), + Sm (Master_Id, Node_Id, Root_Type_Only), Sm (No_Pool_Assigned, Flag, Root_Type_Only), Sm (No_Strict_Aliasing, Flag, Base_Type_Only), Sm (Storage_Size_Variable, Node_Id, Impl_Base_Type_Only))); diff --git a/gcc/ada/sem_res.adb b/gcc/ada/sem_res.adb index 08d45ebca33..028e170021e 100644 --- a/gcc/ada/sem_res.adb +++ b/gcc/ada/sem_res.adb @@ -4513,51 +4513,47 @@ package body Sem_Res is end if; end if; - -- (Ada 2005: AI-251): If the actual is an allocator whose - -- directly designated type is a class-wide interface, we build - -- an anonymous access type to use it as the type of the - -- allocator. Later, when the subprogram call is expanded, if - -- the interface has a secondary dispatch table the expander - -- will add a type conversion to force the correct displacement - -- of the pointer. - if Nkind (A) = N_Allocator then declare DDT : constant Entity_Id := Directly_Designated_Type (Base_Type (Etype (F))); begin - -- Displace the pointer to the object to reference its - -- secondary dispatch table. + -- Ada 2005, AI-251: If the actual is an allocator whose + -- directly designated type is a class-wide interface, we + -- build a type conversion to force the displacement of + -- the pointer to reference the secondary dispatch table. + -- Note that we need to resolve the allocator explicitly, + -- otherwise its E_Allocator_Type will never be replaced, + -- since it's now the operand of a type conversion. if Is_Class_Wide_Type (DDT) and then Is_Interface (DDT) then - Rewrite (A, Convert_To (Etype (F), Relocate_Node (A))); + Convert_To_And_Rewrite (Etype (F), A); Flag_Interface_Pointer_Displacement (A); - - Analyze_And_Resolve (A, Etype (F), - Suppress => Access_Check); + Resolve (Expression (A), Etype (F)); + Analyze_And_Resolve + (A, Etype (F), Suppress => Access_Check); end if; - -- Ada 2005, AI-162:If the actual is an allocator, the - -- innermost enclosing statement is the master of the - -- created object. This needs to be done with expansion - -- enabled only, otherwise the transient scope will not - -- be removed in the expansion of the wrapped construct. + -- Ada 2005, AI-162: If the actual of an access parameter + -- is an allocator, the innermost enclosing statement is + -- the master of the created object. When the expander is + -- active, establish a transient scope to embody it. - if Expander_Active - and then (Needs_Finalization (DDT) - or else Has_Task (DDT)) - then - Establish_Transient_Scope - (A, Manage_Sec_Stack => False); + if Ekind (Etype (F)) = E_Anonymous_Access_Type then + Check_Restriction (No_Access_Parameter_Allocators, A); + + if Expander_Active + and then (Needs_Finalization (DDT) + or else Might_Have_Tasks (DDT)) + then + Establish_Transient_Scope + (A, Manage_Sec_Stack => False); + end if; end if; end; - - if Ekind (Etype (F)) = E_Anonymous_Access_Type then - Check_Restriction (No_Access_Parameter_Allocators, A); - end if; end if; -- (Ada 2005): The call may be to a primitive operation of a