From patchwork Wed Nov 16 13:46:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Vehreschild X-Patchwork-Id: 82514 Delivered-To: patch@linaro.org Received: by 10.140.97.165 with SMTP id m34csp161456qge; Wed, 16 Nov 2016 05:47:01 -0800 (PST) X-Received: by 10.99.117.71 with SMTP id f7mr8446404pgn.126.1479304021212; Wed, 16 Nov 2016 05:47:01 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id q125si31948024pfb.256.2016.11.16.05.47.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Nov 2016 05:47:01 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-441638-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-441638-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-441638-patch=linaro.org@gcc.gnu.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:in-reply-to:references :mime-version:content-type; q=dns; s=default; b=EeMgG6gjC3kYYszK Wx0JIPu7mdbamjO81Cm4Hk5nck/pJXoIJOCWiln2JKwZ7mUceRH7FGxmLx1uxsY2 dEBQ6cH5DOZ7jS9vWKQfoCXYnTiiWGfngquEFc/Afbp6vi1upcDliEYHN0MvUe+6 H+jwkO1iZeKBEz2zxt42NKcoayg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:in-reply-to:references :mime-version:content-type; s=default; bh=A9Mo+C/mRj10GyqIv3p4Ek wIn+M=; b=saQ4nWcU2n55b8LsCBqHGAlKI7kEmPlB0w5mrEA17z0Ik3lbAuRSWq qTXwA+gAh+xvEQEe1ZWL0Ii55w8mktzh1sPYW5XQB9pY1GK5NjOqgCJA0IoMOU8z yX3cqDO4lMZtevX1SQGWB39cbHQPY+yJm98PR8vSkj+8HVieiJ4/k= Received: (qmail 62848 invoked by alias); 16 Nov 2016 13:46:43 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 62668 invoked by uid 89); 16 Nov 2016 13:46:34 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.4 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_LOW, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=no version=3.3.2 spammy=nicht, fix!, !!! X-Spam-User: qpsmtpd, 3 recipients X-HELO: mout.gmx.net Received: from mout.gmx.net (HELO mout.gmx.net) (212.227.15.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 16 Nov 2016 13:46:24 +0000 Received: from vepi2 ([92.76.205.227]) by mail.gmx.com (mrgmx002 [212.227.17.190]) with ESMTPSA (Nemesis) id 0Lh7sF-1cbeWZ2pSp-00oUzd; Wed, 16 Nov 2016 14:46:20 +0100 Date: Wed, 16 Nov 2016 14:46:19 +0100 From: Andre Vehreschild To: Janus Weil Cc: GCC-Patches-ML , GCC-Fortran-ML Subject: Re: [PATCH, Fortran, pr78356, v1] [7 Regression] [OOP] segfault allocating polymorphic variable with polymorphic component with allocatable component Message-ID: <20161116144619.3f27bd85@vepi2> In-Reply-To: References: <20161115172658.5608e616@vepi2> MIME-Version: 1.0 X-UI-Out-Filterresults: notjunk:1; V01:K0:kgSF3b4mY9k=:98u984fE30F4GCHpS2715y k+Rplf1tXpxCSj5Mc6vDt4J9g3+Z3MUV30YO3/gdPRG1EbSZoy8FpEuH+C3ta0u7OPbq1aKsa EsMFLeYrN2CrnxQYXW77D6N6mBHV63wkoxg/tE0YrvbkSg+SFn3+lDjPG2iabZYQePlIOmYUC VpCjHTHZgoDVREcUWPLZeCpwQ+k8zAUt9pd9/wNTB4747xkJ/EI6kOTlOSBF98ifcfAV7b+lN YW5GcZhUxSP7g6yP9/zs9C7MgoD/804DRPXhgtJ7EyhkoMLXmI6jqIqgDWR1Mo+x3DpyzzzMg X/5lSODcMF95VfZxKOuMFG749HbElGRK0VebRH1xqPoZ1ICPoDrqdxpteQ63bk7ucGEtbZiEb JKwRWTAorYKZndu/zG929N0KTpNYMskLIZ+pfZ+sJ/zurgEkyeo252/8DR2rp08v7EKCwYBxC 0v6NMCRWKilruIUTlhNo+a5NOOb7MjCWsd8wINRyPmG+fef9iQhdzO8SGPLyvSg0BNmZRSNR/ TxXnTGaawMYPl3hOCb5gOBlcz2wxlbZrZRf8FPsgq3G0aDLjKVGPzk96c0j7pvEO8fuy4114j TGrvc0kjIMP9Ypeau4Drd86l8tjg9O2N28jend9YwSdcXAx5VGrstpj2g5k0QCSFw+D02E0Ha AzF2kYwKq8+w3KRyTTiKj2TPi5NJF5TUET8VUA44Ukw+ibxOGc77pfdrUQJNHdO+xacqE5cTP LlbEHWscedRnF/9Xl4aPIkss/Ue3yLuekUCnb/hSIE/ZsTk87IWCLJ+0Pdc= Hi Janus, thanks for the quick review. Committed as r242490. Regards, Andre On Tue, 15 Nov 2016 22:24:43 +0100 Janus Weil wrote: > Hi Andre, > > > attached patch fixes the issue raised. The issue here was, that a copy of > > the base class was generated and its address passed to the > > _vptr->copy()-method, which then accessed memory, that was not present in > > the copy being an object of the base class. The patch fixes this by making > > sure the temporary handle is a pointer to the data to copy. > > > > Sorry, when that is not clear. I am not feeling so well today. So here in > > terms of pseudo code. This code was formerly generated: > > > > struct ac {}; > > struct a : struct ac { integer *i; }; > > > > a src, dst; > > ac temp; > > > > temp = src; // temp is now only a copy of ac > > > > _vptr.copy(&temp, &dst); // temp does not denote memory having a pointer to > > i > > > > After the patch, this code is generated: > > > > // types as above > > a src, dst; > > ac *temp; // !!! Now a pointer > > > > temp = &src; > > _vptr.copy(temp, &dst); // temp now points to memory that has a pointer to i > > // and is valid for copying. > > > > Bootstraps and regtests ok on x86_64-linux/F23. Ok for trunk? > > ok with me. Thanks for the quick fix! > > Cheers, > Janus -- Andre Vehreschild * Email: vehre ad gmx dot de Index: gcc/fortran/ChangeLog =================================================================== --- gcc/fortran/ChangeLog (Revision 242489) +++ gcc/fortran/ChangeLog (Arbeitskopie) @@ -1,3 +1,11 @@ +2016-11-16 Andre Vehreschild + + PR fortran/78356 + * class.c (gfc_is_class_scalar_expr): Prevent taking an array ref for + a component ref. + * trans-expr.c (gfc_trans_assignment_1): Ensure a reference to the + object to copy is generated, when assigning class objects. + 2016-11-14 Thomas Koenig * dump-parse-tree.c (show_code): Add prototype. Index: gcc/fortran/class.c =================================================================== --- gcc/fortran/class.c (Revision 242489) +++ gcc/fortran/class.c (Arbeitskopie) @@ -378,7 +378,8 @@ && CLASS_DATA (e->symtree->n.sym) && !CLASS_DATA (e->symtree->n.sym)->attr.dimension && (e->ref == NULL - || (strcmp (e->ref->u.c.component->name, "_data") == 0 + || (e->ref->type == REF_COMPONENT + && strcmp (e->ref->u.c.component->name, "_data") == 0 && e->ref->next == NULL))) return true; @@ -390,7 +391,8 @@ && CLASS_DATA (ref->u.c.component) && !CLASS_DATA (ref->u.c.component)->attr.dimension && (ref->next == NULL - || (strcmp (ref->next->u.c.component->name, "_data") == 0 + || (ref->next->type == REF_COMPONENT + && strcmp (ref->next->u.c.component->name, "_data") == 0 && ref->next->next == NULL))) return true; } Index: gcc/fortran/trans-expr.c =================================================================== --- gcc/fortran/trans-expr.c (Revision 242489) +++ gcc/fortran/trans-expr.c (Arbeitskopie) @@ -9628,6 +9628,7 @@ int n; bool maybe_workshare = false; symbol_attribute lhs_caf_attr, rhs_caf_attr, lhs_attr; + bool is_poly_assign; /* Assignment of the form lhs = rhs. */ gfc_start_block (&block); @@ -9648,6 +9649,19 @@ || gfc_is_alloc_class_scalar_function (expr2))) expr2->must_finalize = 1; + /* Checking whether a class assignment is desired is quite complicated and + needed at two locations, so do it once only before the information is + needed. */ + lhs_attr = gfc_expr_attr (expr1); + is_poly_assign = (use_vptr_copy || lhs_attr.pointer + || (lhs_attr.allocatable && !lhs_attr.dimension)) + && (expr1->ts.type == BT_CLASS + || gfc_is_class_array_ref (expr1, NULL) + || gfc_is_class_scalar_expr (expr1) + || gfc_is_class_array_ref (expr2, NULL) + || gfc_is_class_scalar_expr (expr2)); + + /* Only analyze the expressions for coarray properties, when in coarray-lib mode. */ if (flag_coarray == GFC_FCOARRAY_LIB) @@ -9676,6 +9690,10 @@ if (rss == gfc_ss_terminator) /* The rhs is scalar. Add a ss for the expression. */ rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2); + /* When doing a class assign, then the handle to the rhs needs to be a + pointer to allow for polymorphism. */ + if (is_poly_assign && expr2->rank == 0 && !UNLIMITED_POLY (expr2)) + rss->info->type = GFC_SS_REFERENCE; /* Associate the SS with the loop. */ gfc_add_ss_to_loop (&loop, lss); @@ -9835,14 +9853,7 @@ gfc_add_block_to_block (&loop.post, &rse.post); } - lhs_attr = gfc_expr_attr (expr1); - if ((use_vptr_copy || lhs_attr.pointer - || (lhs_attr.allocatable && !lhs_attr.dimension)) - && (expr1->ts.type == BT_CLASS - || (gfc_is_class_array_ref (expr1, NULL) - || gfc_is_class_scalar_expr (expr1)) - || (gfc_is_class_array_ref (expr2, NULL) - || gfc_is_class_scalar_expr (expr2)))) + if (is_poly_assign) { tmp = trans_class_assignment (&body, expr1, expr2, &lse, &rse, use_vptr_copy || (lhs_attr.allocatable Index: gcc/testsuite/ChangeLog =================================================================== --- gcc/testsuite/ChangeLog (Revision 242489) +++ gcc/testsuite/ChangeLog (Arbeitskopie) @@ -1,3 +1,8 @@ +2016-11-16 Andre Vehreschild + + PR fortran/78356 + * gfortran.dg/class_allocate_23.f08: New test. + 2016-11-16 Richard Biener PR middle-end/78333 Index: gcc/testsuite/gfortran.dg/class_allocate_23.f08 =================================================================== --- gcc/testsuite/gfortran.dg/class_allocate_23.f08 (nicht existent) +++ gcc/testsuite/gfortran.dg/class_allocate_23.f08 (Arbeitskopie) @@ -0,0 +1,31 @@ +! { dg-do run } +! +! Test that pr78356 is fixed. +! Contributed by Janus Weil and Andrew Benson + +program p + implicit none + type ac + end type + type, extends(ac) :: a + integer, allocatable :: b + end type + type n + class(ac), allocatable :: acr(:) + end type + type(n) :: s,t + allocate(a :: s%acr(1)) + call nncp(s,t) + select type (cl => t%acr(1)) + class is (a) + if (allocated(cl%b)) error stop + class default + error stop + end select +contains + subroutine nncp(self,tg) + type(n) :: self, tg + allocate(tg%acr(1),source=self%acr(1)) + end +end +