From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by sourceware.org (Postfix) with ESMTPS id A10A53858C01; Thu, 28 Sep 2023 12:45:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A10A53858C01 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1695905107; x=1696509907; i=vehre@gmx.de; bh=kNkVEYnBw+P9yJ+b/oydy6vc62O5vZyzF+DrMRAGeIA=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject; b=hcgHn6qyE/H8+jOPSFylKhVHhgEzSe/Zs7sqBnKNDLvKcDdP3DRT6NHU2/rNxzammK4iMZtte53 PDMpkBgkk6yghijX0YhhdTIIxuuUFNM8zMIZ1i8BYPQ6SdJxZGsGqWxe7o6x63w3BQeN/7WcQ6Zok Bglit1/a/Xd76xI6lw9T16ln+jTGzZ7nrcEMv04n53cySi8tBXnmJ86qAOGJ0K+BwnR6RAGB1o2ah xHuvQLYHrWoT/D1YnEsHSyRBmjDusBIET/yba2wJ+KCD6C5aOpwqDA7C3UjM5EA+3UFqqVPo/xeLc pfCZue2w8f/IpMaGA4PKJHTmCXJjYSgfsa9g== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from vepi2 ([79.194.173.146]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1M59GG-1qkjvb0Tu0-0018B7; Thu, 28 Sep 2023 14:45:07 +0200 Date: Thu, 28 Sep 2023 14:45:05 +0200 From: Andre Vehreschild To: GCC-Fortran-ML , GCC-Patches-ML Cc: Damian Rouson , Katherine Rasmussen , Brad Richardson Subject: [Fortran, Patch, Coarray, PR 37336] Fix crash in finalizer when derived type coarray is already freed. Message-ID: <20230928144505.77e03a8e@vepi2> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/8LYq3fNTyaaim.+sFziiyWC" X-Provags-ID: V03:K1:HQrhMjVn/1RSrGTQXXUNsH2EHDlGu4+GY5y1160UKHh6h+4V8Bx wHMCJmUvrIbKEX6GMLGS9G5QN/fE096OD4GZWVvgEBiFvvUQqF1hzlfdF1WmoQ19TjseWlv whjnpIRj6cj6EuzVhD1fJgijF2Fm1bowE6TOi6nU6xMqi/hFeh3FM2zI1Sa/dJjRPDtZfSo dOCOgiq4BRYOOc475r12A== UI-OutboundReport: notjunk:1;M01:P0:0TwPtUxaPhc=;domUeEaMA0uj62sZ0yOjEzhbHLv 2t+HNhkz0C4bE3jrsDkXAMTUVhOqSB1k4GFtCZ3Qu3HBQz4U/lgmeUjGq/0feQTYKyTl42zs+ xrN3RNuFUVWxi+9/TCfr6TWV6Z0Qoh4D1Ka9902RpManEbuU/dYtbxxdPQgqi+sWmWDA+OnRh /FxUrZvJCurYO3jmVtOxRI4HPMQTPwjqYB+6uh8gfbwkCQIwkMbPfVohkR+dn3MyjCBzgdbnI S0l+TendfY8RXIBkQZs+DUtBSwl5IePzINoxqrz564WeJhv/hrm5OGp3g6wmFmBJYsS9AWSNo uetmUamlCp17rzU6AziQhjkjbt25oiPlZzJpf3+JRbNgrfM8VbcBZq8sqv9PuxXlF7ZIj6K3K lbgLiwr+fmlEHT/JTehxJQaNGOLLVmg/on1oMzCEMq2hMyesjjsXrB8I6Tpvs3kcFL/O4/TUc o7wklDLBNn/MFmeS7PD5qyt9FMvXC2Ee0hcdpfITvQTz7SLVhENnjGNbh8KnerXs5ber3t3Tw /uPxTkX7Ph88UUcR7sAEMoWNGG/1ZQHIG1GgikcZfCjRdM7d4MsMiYzgTvf2TrezlHmxU3bLv DOlouAXByvYjLvm/xwBrTCVQIeAGj2bD7xQMQSeAHIr2Jd0rmJCJxIhlH3Uog5cWTxZW/dOQJ K8DfcspNwiwdzwWAGAYRJQ+aMONQ6bxBEUMrY8fLeBDqmPCpJcF6x4hWm/QJm4IoDZ2SIjWjZ xAiz5RtrzYRXBd8WSbbeXdPGZCn8jm32X7rHfI4G9162+fL7fKQpiygXtgdPiPi8PissxCnQ2 6DwYv5TK3STCVCaOwMkylFVCYvHPIsEbmQc8089oPWCWX7ryQPtrJD2fCgbJ/sLl4k6ve9nv4 wlU0O7XFB6LAcVVvZRHidfzkpdkFOmheD0E8dLgcEhtAnQp29oN1XXP0QcJEWlf9jBCRmKvS5 iBFQIhPTlRY0nlPbAuWK3ziJKII= X-Spam-Status: No, score=-10.0 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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --MP_/8LYq3fNTyaaim.+sFziiyWC Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi all, attached patch fixes a crash in coarray programs when an allocatable deriv= ed typed coarray was freed explicitly. The generated cleanup code did not tak= e into account, that the coarray may have been deallocated already. The patc= h fixes this by moving the statements accessing components inside the derive= d type into the block guard by its allocated check. Regtested ok on f37/x86_64. Ok for master? Regards, Andre =2D- Andre Vehreschild * Email: vehre ad gmx dot de --MP_/8LYq3fNTyaaim.+sFziiyWC Content-Type: text/x-patch Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=caf_finalizer_fix.patch diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index e0fc8ebc46b..8e94a9a469f 100644 =2D-- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -9320,6 +9320,12 @@ structure_alloc_comps (gfc_symbol * der_type, tree = decl, tree dest, gfc_add_expr_to_block (&fnblock, tmp); } + /* Still having a descriptor array of rank =3D=3D 0 here, indicates an + allocatable coarrays. Dereference it correctly. */ + if (GFC_DESCRIPTOR_TYPE_P (decl_type)) + { + decl =3D build_fold_indirect_ref (gfc_conv_array_data (decl)); + } /* Otherwise, act on the components or recursively call self to act on a chain of components. */ for (c =3D der_type->components; c; c =3D c->next) @@ -11507,7 +11513,11 @@ gfc_trans_deferred_array (gfc_symbol * sym, gfc_w= rapped_block * block) { int rank; rank =3D sym->as ? sym->as->rank : 0; - tmp =3D gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, r= ank); + tmp =3D gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, r= ank, + (sym->attr.codimension + && flag_coarray =3D=3D GFC_FCOARRAY_LIB) + ? GFC_STRUCTURE_CAF_MODE_IN_COARRAY + : 0); gfc_add_expr_to_block (&cleanup, tmp); } @@ -11521,9 +11531,11 @@ gfc_trans_deferred_array (gfc_symbol * sym, gfc_w= rapped_block * block) NULL_TREE, NULL_TREE, true, e, sym->attr.codimension ? GFC_CAF_COARRAY_DEREGISTER - : GFC_CAF_COARRAY_NOCOARRAY); + : GFC_CAF_COARRAY_NOCOARRAY, + NULL_TREE, gfc_finish_block (&cleanup)); if (e) gfc_free_expr (e); + gfc_init_block (&cleanup); gfc_add_expr_to_block (&cleanup, tmp); } diff --git a/gcc/testsuite/gfortran.dg/coarray/alloc_comp_6.f90 b/gcc/test= suite/gfortran.dg/coarray/alloc_comp_6.f90 new file mode 100644 index 00000000000..e8a74db2c18 =2D-- /dev/null +++ b/gcc/testsuite/gfortran.dg/coarray/alloc_comp_6.f90 @@ -0,0 +1,29 @@ +! { dg-do run } + +program alloc_comp_6 + + implicit none + + type :: foo + real :: x + integer, allocatable :: y(:) + end type + + call check() + +contains + + subroutine check() + block + type(foo), allocatable :: example[:] ! needs to be a coarray + + allocate(example[*]) + allocate(example%y(10)) + example%x =3D 3.4 + example%y =3D 4 + + deallocate(example) + end block ! example%y shall not be accessed here by the finalizer, + ! because example is already deallocated + end subroutine check +end program alloc_comp_6 --MP_/8LYq3fNTyaaim.+sFziiyWC Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=caf_finalizer_fix.clog Fortran: Free alloc. comp. in allocated coarrays only. When freeing allocatable components of an allocatable coarray, add a check that the coarray is still allocated, before accessing the components. This patch adds to PR fortran/37336, but does not fix it completely. gcc/fortran/ChangeLog: PR fortran/37336 * trans-array.cc (structure_alloc_comps): Deref coarray. (gfc_trans_deferred_array): Add freeing of components after check for allocated coarray. gcc/testsuite/ChangeLog: PR fortran/37336 * gfortran.dg/coarray/alloc_comp_6.f90: New test. --MP_/8LYq3fNTyaaim.+sFziiyWC--