From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) by sourceware.org (Postfix) with ESMTPS id B64EB3858C78; Fri, 29 Sep 2023 10:01:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B64EB3858C78 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=1695981680; x=1696586480; i=vehre@gmx.de; bh=YUNJRFjoxp9Wh167EW/ewy+pJ5Xn1lpMsTGe6UdrxiI=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:In-Reply-To:References; b=mr2eYPLX36EMeZxLzMbkl15UOSKk6XbYLrwZeP+zp6UT5wUdUXXsQFtp6+92eRWSNPXt64lA9w3 5N2dllbH1RlTUvC9cMYBD7uL2MMqWGJoclnkiuvXfg7CjP4TNlvKBmBtCrOqrj0jgMcR895SkDyrl 1Q4kk/JfRKgpBsKqk38Ho7yJP3UgKivthg52VCLZrNofLWfFPxEvJvZ3OcR1F3T2MAciOr60k3HGJ CTYWD6OXkzOIE89ZusK77e3mObmw0qU8aaAgXhVbKjy6WM+JvClv+JFa61ZCGnNf3wM7zAXHnxGAs m+LCQTr5qfCogd80uojSeiIx8c8khPB/6rlw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from vepi2 ([79.194.173.146]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1Ml6m4-1rV6Ul45mO-00lVOA; Fri, 29 Sep 2023 12:01:20 +0200 Date: Fri, 29 Sep 2023 12:01:18 +0200 From: Andre Vehreschild To: Paul Richard Thomas Cc: GCC-Fortran-ML , GCC-Patches-ML , Katherine Rasmussen , Brad Richardson Subject: Re: [Fortran, Patch, Coarray, PR 37336] Fix crash in finalizer when derived type coarray is already freed. Message-ID: <20230929120118.078188ff@vepi2> In-Reply-To: References: <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_/Q2hoY/H9/lzpI.SZKUq72sl" X-Provags-ID: V03:K1:XIsmfgl3jAKRkcsRExoKSohEu2JuapMG4VTxmoMYZCF2UmpRp5B Il2C3xSJBE/mLgz8dNviXtYd66qjqnXzF3t6CGw4DJKXaLRbP32W5ddJf1CxawXp/PE6V1I ebAnr76FaFVHPlYcgsC4KhrAFO+c6vRHMG0nB2X/0Pnu4Cq6r1x5as22zEBX77X9sfeo2KX 0+VRHrZGej2bIPD6GcdVA== UI-OutboundReport: notjunk:1;M01:P0:LnUwh33udHs=;ficVNuX6FgQ2tY8XZxB+SZbLVQ2 cFBZ1r2SFlmKo5lUZP/U/8YKnQMTfpdMSWFdISfd2C8jpUWzAkIXsNCdyYAO6P0Zcqmxu9efb fhoP20+DGlnz9DC2AQ21Oa9MpZ4j9p9211CCbpQw86txO9rG/WDO0FpTiheP8QMhWI9ixonY0 lsF4LScvyGE3lD8L74c1xwnkuC72XvpauOoEoj602zx6WKDXa0FC+FQnNcgsfPGnizxnshFsc m1mqpSTHHAR34lP+/ETo+ZCm3anKI+g6WI/cTep+CULk0yiup5GU1zgpcQJ268LLrpCPRvEMs U6KwxVJOmk51VsDG4T6dtfW6ggAN+EVD5T7xliOYhTXkVKTf7D+9NntA9mQe2Tm6ZooFJkFY5 brzZjuJO93WxcigIdU7Gg2RHi3yRFnjiSQ8WLPI12doUSQQaJx1OP7xoC9AmXRWCZggUFJO0z bBLT2ySFPg6w9e9cBGjTa6jggUWxheA3qI5Vy2t/qutaUHtP95wDwEmhS6muQYGH2UTFiFh7i 4AEC8Cnup28W4ES0hYHyAyOljolMNpHo7xHr6qfbfod6BIpuFCGWgLRMWd47DukMm3sWXyLir bHC03HwXMff8A/OKT2WByjryn3IRN0juojiy7LMT6nQvxaN5dy1wmxylDnKpmj2HLwOFNOD7x x3UI8FRqnxGfhJlnQxM1BiTV3SiKHHroFtQFjGH3TtsXclNWF3SMOTvI61VjhJRlTBZ54HK2c iY5KdSrZvezsPLXy6qkkVPor/gVBY6YyHnRycb5R9098YiSeSYeJ8oY2TITrzcxqsb0hE4XFx 8AKgnY6kseDcbq6oAlB57ZCLE+pFW84jlUco57tDD1iA3asGq/2gGL+0nnqf2qAmnO3U7G0M5 u5IwziexUR0GTLsXNrSVrn085hXqbRSzndgtvM/DwUsmY8AyYQKpyXI+IisLoMNVhXVU0PqfN x1ppiQ== X-Spam-Status: No, score=-10.2 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 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_/Q2hoY/H9/lzpI.SZKUq72sl Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi Paul, thanks for the quick review. I've added a testcase with a module and a finalizer in the derived type. This also is no problem. Regtests ok on x86_64_linux_gnu/f37. Ok for trunk? Regards, Andre On Thu, 28 Sep 2023 19:21:12 +0100 Paul Richard Thomas wrote: > Hi Andre, > > The patch looks fine to me. Since you mention it in the comment, is it > worth declaring the derived type 'foo' in a module and giving it a > final routine? > > Thanks for the patch. > > Paul > > On Thu, 28 Sept 2023 at 13:45, Andre Vehreschild via Fortran > wrote: > > > > Hi all, > > > > attached patch fixes a crash in coarray programs when an allocatable d= erived > > typed coarray was freed explicitly. The generated cleanup code did not= take > > into account, that the coarray may have been deallocated already. The = patch > > fixes this by moving the statements accessing components inside the de= rived > > type into the block guard by its allocated check. > > > > Regtested ok on f37/x86_64. Ok for master? > > > > Regards, > > Andre > > -- > > Andre Vehreschild * Email: vehre ad gmx dot de =2D- Andre Vehreschild * Email: vehre ad gmx dot de --MP_/Q2hoY/H9/lzpI.SZKUq72sl Content-Type: text/x-patch Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=caf_finalizer_fix_v2.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 diff --git a/gcc/testsuite/gfortran.dg/coarray/alloc_comp_7.f90 b/gcc/test= suite/gfortran.dg/coarray/alloc_comp_7.f90 new file mode 100644 index 00000000000..5ebd31f3df7 =2D-- /dev/null +++ b/gcc/testsuite/gfortran.dg/coarray/alloc_comp_7.f90 @@ -0,0 +1,49 @@ +! { dg-do run } + +module alloc_comp_module_7 + + public :: check + + type :: foo + real :: x + integer, allocatable :: y(:) + contains + final :: foo_final + end type + +contains + + subroutine foo_final(f) + type(foo), intent(inout) :: f + + if (allocated(f%y)) then + f%y =3D -1 + end if + end subroutine foo_final + + 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%y) + deallocate(example) + end block ! example%y shall not be accessed here by the finalizer, + ! because example is already deallocated + end subroutine check +end module alloc_comp_module_7 + +program alloc_comp_7 + + use alloc_comp_module_7, only: check + + implicit none + + call check() + +end program alloc_comp_7 + --MP_/Q2hoY/H9/lzpI.SZKUq72sl Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=caf_finalizer_fix_v2.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. * gfortran.dg/coarray/alloc_comp_7.f90: New test. --MP_/Q2hoY/H9/lzpI.SZKUq72sl--