From patchwork Thu Sep 28 12:45:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Vehreschild X-Patchwork-Id: 146088 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp3283557vqu; Thu, 28 Sep 2023 05:45:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGYyYgZTsGXuIyO1KfWzwyWkGBa80i+7EeB/mYdZZWoEkdfIIDEQuzDbZF4+0Nsl2f2WtpN X-Received: by 2002:aa7:db46:0:b0:522:2111:1063 with SMTP id n6-20020aa7db46000000b0052221111063mr1082809edt.18.1695905151692; Thu, 28 Sep 2023 05:45:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695905151; cv=none; d=google.com; s=arc-20160816; b=0fTzqtUaEVGg6Rj9e7S3mitcfR7YYfLASHwzVrQaWTvcpG1bfpjiydBhOQCpgvWTpo NwZ1j34ZpECCssO0GWLajmSv5uhfwbdfjP4vtS2BYFwsw+LfuSawWV3qORV1RKNLg/HI 2v70briE+4a29DRXI0l8cBaCgSESL5eTUHcf3Y6P5iGk2Wf//a3KlH7IIr1MjRpXSgo7 ObWHwgGncE/tP5TO0ku1RmXeB38Ov2sIRXAE7MOHkgnCHgdlIXNMlgRSXDMcja4z0OcW UIp0iM04h5kj1QhHp05EoHK2fcShZi7mEYuuLkHNpO8XKsvdsqwkIZGj+IB92sJgDAoi pFFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:ui-outboundreport:mime-version :message-id:subject:cc:to:from:date:dkim-signature:dmarc-filter :delivered-to; bh=GKVdpdcpVCj6hjLWdnbl4KK5axVI47/7PktqXJzqp/Y=; fh=edVrmYVYyt0jqVX5ZCpb4QU7yEh2xt7xrf9xt/hbyGE=; b=ZbVSxx1yvGiU/wnUySxne/y2VWvtLluQjKwZOk6hynf9YlNdL3aWwNDXDUHhM0VNVu Y3CSn95NjVE/IcLjlHgzkZAfAXbNhLZfa0KwBraCpaAnUn6dW3LYiXq5TJhT1wlVpmfu cgsFQvEUTqCTaZPSHEs3FdlOSrmbNjB19i83dUV7eVz3L6kp5NfzxPScIbOi4o+dYzzR o+wvvH6Bq3jtQiAq9GXYmFOT4hPUD+v4kkV4ZEr/g0jnjn+ddXL7JvlaSLe2pN3rT06z uJExEbjyO8sIl6e9On+dlhjU+SyLGuBC5rEbexNvW7OCYWCoTg3GkEbsKifvQzBjGqEf Sguw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.de header.s=s31663417 header.b=hcgHn6qy; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmx.de Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id r12-20020aa7d58c000000b00530bad54f0esi14474285edq.561.2023.09.28.05.45.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 05:45:51 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gmx.de header.s=s31663417 header.b=hcgHn6qy; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmx.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5D889386C5B6 for ; Thu, 28 Sep 2023 12:45:35 +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.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 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 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778285440461579934 X-GMAIL-MSGID: 1778285440461579934 Hi all, attached patch fixes a crash in coarray programs when an allocatable derived 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 derived 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 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. diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index e0fc8ebc46b..8e94a9a469f 100644 --- 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 == 0 here, indicates an + allocatable coarrays. Dereference it correctly. */ + if (GFC_DESCRIPTOR_TYPE_P (decl_type)) + { + decl = 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 = der_type->components; c; c = c->next) @@ -11507,7 +11513,11 @@ gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block) { int rank; rank = sym->as ? sym->as->rank : 0; - tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, rank); + tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, rank, + (sym->attr.codimension + && flag_coarray == 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_wrapped_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/testsuite/gfortran.dg/coarray/alloc_comp_6.f90 new file mode 100644 index 00000000000..e8a74db2c18 --- /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 = 3.4 + example%y = 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