From patchwork Tue Jun 11 12:44:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Vehreschild X-Patchwork-Id: 1946335 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=gmx.de header.i=vehre@gmx.de header.a=rsa-sha256 header.s=s31663417 header.b=WOmQSQWP; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vz7zH6WQJz20Pb for ; Tue, 11 Jun 2024 22:59:07 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 27543385DC1E for ; Tue, 11 Jun 2024 12:59:06 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) by sourceware.org (Postfix) with ESMTPS id 04AE1385842C; Tue, 11 Jun 2024 12:57:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 04AE1385842C Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 04AE1385842C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.227.15.19 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718110628; cv=none; b=Gqs3TmjuGMETBbyeaNBS9UGjZP/w1XEJGxIl6rL/bi1FoYgKaghlcrbBtdsyQYPcxkWbCy40218ZUlFV7muXI5tADSKQehoHhoEHizQ6Ml9x5P7mYXgLGJRd/ymL2DCDJs1xK+OMGycO1a+ev6ss2/vbWf0WKH8sXbiy9Zjvu5k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718110628; c=relaxed/simple; bh=1CMfstB+rjqjflBms3LqRcZ72MSmMtt0d7mnsTI0KPc=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=op44EikmIP6FbDAg+EPLOVMNZRsXzHSSa5GeP7TO/DVs2nKAkJI2cc0NVGlEKK2/9IHzyTn109QUXVc1K/V2tg0bzu1mB21YPT3q+RfbojNKwsig0H8WmTQ85jhmh0gu5NKWmDrkF2a8Vd/7msAk695vAD2enSiuJ3Zy1xdagKk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1718110623; x=1718715423; i=vehre@gmx.de; bh=7zK5JxAARtEJscaq/QXcWSfCFNDBH+zL0b86Hvqvpxk=; h=X-UI-Sender-Class:Date:From:To:Subject:Message-ID:MIME-Version: Content-Type:cc:content-transfer-encoding:content-type:date:from: message-id:mime-version:reply-to:subject:to; b=WOmQSQWPvBEmWHwM9ShsL1EyiCKhf4vzRLzFmKQ+PQ27PHseq7mng8zkZ81t5/k1 iEgXdRLwLC3nEzN8Am2EtC5yBjtu+xA90lE/j8MjTnh3TJj01qGfvxy3VYBRm5wPQ MI1bYUb4K2aYLzgydojn7APlXEpWBAZ29ZgYbPJR2YRBDc6rXhuWxu/NcpC3zvnyL iA6mCaw+fRMJ8F32A/tRAjgNpeIpcAC1/BrupliMZtOpec7CWDZ/hspS+AS7m2zhg xARSOTC6rMXXXWz4YU7HeF1j7et67EXeWgeK6AeH35Nsd2wQYmx+s0//ZYAK6X1Ih b/tVS//lyByRcx6wZg== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from vepi2 ([62.155.205.192]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MysVs-1sdeVZ2g0w-0123Qz; Tue, 11 Jun 2024 14:57:03 +0200 Date: Tue, 11 Jun 2024 14:44:09 +0200 From: Andre Vehreschild To: GCC-Patches-ML , GCC-Fortran-ML Subject: [Patch, Fortran, 90076] 1/3 Fix Polymorphic Allocate on Assignment Memory Leak Message-ID: <20240611144409.440dfbac@vepi2> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.41; x86_64-redhat-linux-gnu) MIME-Version: 1.0 X-Provags-ID: V03:K1:8FfWF32p6f1URMOZU5J9iO/5T34fzSSkWMViavfAQoqt4G/KFJf QFkXsWqasWSA51fdBzRydkmzeZ5wLoI0YSeR1j3Z4tkIgcDQDCydIhpRnLbvK150uh8cV62 DNPP9H1ThO30x0p2WqReaFXkf4ACOaEwQ8iRQKcNqZ/OK2MAuGuQ7gmSmSgZ0Ym0c1JUzmh DoAlr3ZcaST8yjO4HEDzQ== UI-OutboundReport: notjunk:1;M01:P0:qaWgPxwd1rg=;nR31uaJuo/G2gd4TDqM7GLFyXDS yN4x8786FJh91uYvr7rBWFBcwyYfNCj/djPSu9e4uvCBueClGc+6BBoCZgl7Xcb9qW+GUQbYn ji5mdNAiK50mqOS0AbTRnpZzdhZP4wbCPBW/KziGTsnFYEZDeZ4MoI9HE8C5yobWLJiIGvUy1 UkvjzN9JUSIerlu34ta4nH0HIZsfCD6F7DgGps+/ymoZNrPOmocg6r45lxWl11ajXkRPyJRHr NbkH5ZUdGj+6xU37eaK/XXXr6A/JrtW+IjI4OVSSdAoy3fP6IYz5vqKFvfR/W8iX6UJXq8kUV k2n1TPxn1NPN9e0OXwSk5G0N1WXws1MtiS3MzCbKGn6vPB7RPVGONUPBERdrX2/oRcxEe1laG XmwmarTn+bgzaX8VjwOvGEv3yK/4jLZ455zl+2TCL/hwHosiJpCMmeKlWB4kwQMaFzWgnXK0O 4CGSDT/aDRiHo8ktre/Jnkx2SMOcNTEvw/g+2u6NsKb1WrMSWQLF0mxMWPkZYsEXKOxixO3+t Hf5JBdimFo+fJKzcc1C/5pDPDsdAX9zBk/mf/R1pX8r8uKB4CR59xH9oFeEXoV3LhR12z8NEa DblsZIw2MckMD4LJ0y5Ec9rTraRLbk7ZU041X/iGum44rbOsyeBA45yOc72gq7MVo3Crv58DG UfY+ov9zo75aYJFj/gUN3QgmZOqu9Eat/HClcxSoaW6Utjbtrw8ahfa6LwaeV0lD/z0yuuRVV 1Q6y4Og2mBeelTXcNBES3dY5zLpeUK2dCos66Ey7NryWk15CtviwZnFa/AXxWothPADQ3kJFl N2rdmKF07vX5iIy4qDfGWwEnGmNf9L0ia5tapw9ZGXAAE= X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi all, the attached patch fix the last case in the bug report. The inital example code is already fixed by the combination of PR90068 and PR90072. The issue was the _vptr was not (re)set correctly, like in the __vtab_...-structure was not created. This made the compiler ICE. Regtests fine on x86_64 Fedora 39. Ok for mainline? Regards, Andre --- Andre Vehreschild * Email: vehre ad gmx dot de From e3a7f07e7dfad7ab347f148d2d46b633c0bbdecc Mon Sep 17 00:00:00 2001 From: Andre Vehreschild Date: Thu, 6 Jun 2024 14:01:13 +0200 Subject: [PATCH 1/3] Fortran: Set the vptr of a class typed result. PR fortran/90076 gcc/fortran/ChangeLog: * trans-decl.cc (gfc_generate_function_code): Set vptr for results to declared class type. * trans-expr.cc (gfc_reset_vptr): Allow to provide the typespec instead of the expression. * trans.h (gfc_reset_vptr): Same. gcc/testsuite/ChangeLog: * gfortran.dg/class_76.f90: Add declared vtab occurrence. * gfortran.dg/class_78.f90: New test. --- gcc/fortran/trans-decl.cc | 11 +++++----- gcc/fortran/trans-expr.cc | 10 +++++---- gcc/fortran/trans.h | 4 +++- gcc/testsuite/gfortran.dg/class_76.f90 | 2 +- gcc/testsuite/gfortran.dg/class_78.f90 | 29 ++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/class_78.f90 diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc index dca7779528b..88538713a02 100644 --- a/gcc/fortran/trans-decl.cc +++ b/gcc/fortran/trans-decl.cc @@ -7926,11 +7926,12 @@ gfc_generate_function_code (gfc_namespace * ns) && CLASS_DATA (sym)->attr.dimension == 0 && sym->result == sym) { - tmp = CLASS_DATA (sym)->backend_decl; - tmp = fold_build3_loc (input_location, COMPONENT_REF, - TREE_TYPE (tmp), result, tmp, NULL_TREE); - gfc_add_modify (&init, tmp, fold_convert (TREE_TYPE (tmp), - null_pointer_node)); + tmp = gfc_class_data_get (result); + gfc_add_modify (&init, tmp, + fold_convert (TREE_TYPE (tmp), + null_pointer_node)); + gfc_reset_vptr (&init, nullptr, result, + CLASS_DATA (sym->result)->ts.u.derived); } else if (sym->ts.type == BT_DERIVED && !sym->attr.allocatable) diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index d6f4d6bfe45..558a7380516 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -530,13 +530,14 @@ gfc_find_and_cut_at_last_class_ref (gfc_expr *e, bool is_mold, return base_expr; } - /* Reset the vptr to the declared type, e.g. after deallocation. Use the variable in CLASS_CONTAINER if available. Otherwise, recreate - one with E. The generated assignment code is added at the end of BLOCK. */ + one with e or derived. At least one of the two has to be set. The generated + assignment code is added at the end of BLOCK. */ void -gfc_reset_vptr (stmtblock_t *block, gfc_expr *e, tree class_container) +gfc_reset_vptr (stmtblock_t *block, gfc_expr *e, tree class_container, + gfc_symbol *derived) { tree vptr = NULL_TREE; @@ -546,6 +547,7 @@ gfc_reset_vptr (stmtblock_t *block, gfc_expr *e, tree class_container) if (vptr == NULL_TREE) { gfc_se se; + gcc_assert (e); /* Evaluate the expression and obtain the vptr from it. */ gfc_init_se (&se, NULL); @@ -570,7 +572,7 @@ gfc_reset_vptr (stmtblock_t *block, gfc_expr *e, tree class_container) tree vtable; /* Return the vptr to the address of the declared type. */ - vtab = gfc_find_derived_vtab (e->ts.u.derived); + vtab = gfc_find_derived_vtab (derived ? derived : e->ts.u.derived); vtable = vtab->backend_decl; if (vtable == NULL_TREE) vtable = gfc_get_symbol_decl (vtab); diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h index f94fa601400..5e064af5ccb 100644 --- a/gcc/fortran/trans.h +++ b/gcc/fortran/trans.h @@ -451,7 +451,9 @@ tree gfc_vptr_def_init_get (tree); tree gfc_vptr_copy_get (tree); tree gfc_vptr_final_get (tree); tree gfc_vptr_deallocate_get (tree); -void gfc_reset_vptr (stmtblock_t *, gfc_expr *, tree = NULL_TREE); +void +gfc_reset_vptr (stmtblock_t *, gfc_expr *, tree = NULL_TREE, + gfc_symbol * = nullptr); void gfc_reset_len (stmtblock_t *, gfc_expr *); tree gfc_get_class_from_gfc_expr (gfc_expr *); tree gfc_get_class_from_expr (tree); diff --git a/gcc/testsuite/gfortran.dg/class_76.f90 b/gcc/testsuite/gfortran.dg/class_76.f90 index 1ee1e1fc25f..c9842a15fea 100644 --- a/gcc/testsuite/gfortran.dg/class_76.f90 +++ b/gcc/testsuite/gfortran.dg/class_76.f90 @@ -61,6 +61,6 @@ contains end function newContainer end program returned_memory_leak -! { dg-final { scan-tree-dump-times "newabstract" 14 "original" } } +! { dg-final { scan-tree-dump-times "newabstract" 15 "original" } } ! { dg-final { scan-tree-dump-times "__builtin_free" 8 "original" } } diff --git a/gcc/testsuite/gfortran.dg/class_78.f90 b/gcc/testsuite/gfortran.dg/class_78.f90 new file mode 100644 index 00000000000..3e2a0245afb --- /dev/null +++ b/gcc/testsuite/gfortran.dg/class_78.f90 @@ -0,0 +1,29 @@ +! { dg-do run } +! +! PR fortran/90076 +! +! Contributed by Brad Richardson +! + +program assignment_memory_leak + implicit none + + type, abstract :: base + end type base + + type, extends(base) :: extended + end type extended + + call run() +contains + subroutine run() + class(base), allocatable :: var + + var = newVar() ! Crash fixed + end subroutine run + + function newVar() + class(extended), allocatable :: newVar + end function newVar +end program assignment_memory_leak + -- 2.45.1