Bug 51972 - [OOP] Wrong code as _copy does not honor CLASS components
Summary: [OOP] Wrong code as _copy does not honor CLASS components
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2012-01-23 17:36 UTC by Tobias Burnus
Modified: 2012-02-05 09:46 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Test case (644 bytes, text/plain)
2012-01-23 17:36 UTC, Tobias Burnus
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2012-01-23 17:36:34 UTC
Created attachment 26435 [details]
Test case

The test case is a reduced version of chapter07/strategy_surrogate_f2003, available from http://www.cambridge.org/rouson under "Resources available".

When compiling and running the program, one might get:
  Program received signal SIGABRT: Process abort signal.

Backtrace for this error:
  #6  0x400AC5 in __lorenz_module_MOD___copy_lorenz_module_Lorenz at test.F90:42
  #7  0x401392 in __runge_kutta_2nd_module_MOD_integrate at test.F90:73
  #8  0x40153F in MAIN__ at test.F90:84


And with valgrind:
  Conditional jump or move depends on uninitialised value(s)
  at 0x400AD8: __lorenz_module_MOD___copy_lorenz_module_Lorenz (test.F90:42)
  by 0x4013B2: __runge_kutta_2nd_module_MOD_integrate (test.F90:73)


The issue is that there is MEMSET or CALLOC missing in "integrate". There is a call to memset in "constructor" and "assign_lorenz".
Comment 1 Tobias Burnus 2012-01-28 15:58:37 UTC
The following patch fixes the test case.


However, if one comments the "STOP 'Success'" line, one sees that there is another bug (double free). The problem is that _copy does not properly do a deep copy.

Expected: A deep copy of both attractor%state and attractor%integrand%quadrature.

Currently:

__copy_lorenz_module_Lorenz
{
    *dst = *src;
        dst->integrand = src->integrand;
            __builtin_memcpy (dst->state.data, src->state.data, D.1953 * 4);

Thus, there is no deep copy of "attractor%integrand%quadrature"; thus, both src->integrand->quadrature->_data and dst->integrand->quadrature->_data point to the same memory, causing - for the example program - a segfault at the end of the main program.



--- trans-stmt.c        (Revision 183664)
+++ trans-stmt.c        (Arbeitskopie)
@@ -4953 +4953,2 @@ gfc_trans_allocate (gfc_code * code)
-         if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->attr.alloc_comp)
+         if (al->expr->ts.type == BT_DERIVED
+             && expr->ts.u.derived->attr.alloc_comp)
Comment 2 Tobias Burnus 2012-01-28 16:57:33 UTC
Author: burnus
Date: Sat Jan 28 16:57:28 2012
New Revision: 183668

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=183668
Log:
2012-01-28  Tobias Burnus  <burnus@net-b.de>

        PR fortran/51972
        * trans-stmt.c (gfc_trans_allocate): Properly check whether
        we have a BT_CLASS which needs to be memset.

2012-01-28  Tobias Burnus  <burnus@net-b.de>

        PR fortran/51972
        * gfortran.dg/class_allocate_12.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/class_allocate_12.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/testsuite/ChangeLog
Comment 3 Tobias Burnus 2012-01-28 17:40:23 UTC
Simplified test case: The problem is that there is a "CLASS" contained in the derived type. This is currently not handled at all, but one needs: (a) an allocation and (b) another _vtab->_copy call.


type t
  integer :: x
end type t
type t2

  class(t), allocatable :: a
end type t2

type(t2) :: one, two

allocate (two%a)
two%a%x = 7890
one = two
if (one%a%x /= 7890) call abort ()
end
Comment 4 Tobias Burnus 2012-01-29 20:02:25 UTC
Author: burnus
Date: Sun Jan 29 20:02:19 2012
New Revision: 183680

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=183680
Log:
2012-01-29  Tobias Burnus  <burnus@net-b.de>

        PR fortran/51972
        * trans-array.c (structure_alloc_comps): Fix assignment of
        polymorphic components (polymorphic deep copying).

2012-01-29  Tobias Burnus  <burnus@net-b.de>

        PR fortran/51972
        * gfortran.dg/class_allocate_12.f90: Enable disabled test.
        * gfortran.dg/class_48.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/class_48.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/class_allocate_12.f90
Comment 5 Tobias Burnus 2012-01-29 20:23:42 UTC
FIXED on the 4.7 trunk.
Comment 6 Tobias Burnus 2012-02-05 09:46:24 UTC
Author: burnus
Date: Sun Feb  5 09:46:20 2012
New Revision: 183904

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=183904
Log:
2012-02-05  Tobias Burnus  <burnus@net-b.de>

        PR fortran/51972
        * gfortran.dg/class_48.f90: Add some further checks.


Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/class_48.f90