Patchwork [Fortran] PR53389 realloc-on-assignment: Memory leak and wrong double evaluation (4.6-4.8 regression)

login
register
mail settings
Submitter Tobias Burnus
Date May 21, 2012, 5:51 p.m.
Message ID <4FBA809B.40609@net-b.de>
Download mbox | patch
Permalink /patch/160399/
State New
Headers show

Comments

Tobias Burnus - May 21, 2012, 5:51 p.m.
First: I have another rather simple patch, which still needs to be 
reviewed: http://gcc.gnu.org/ml/fortran/2012-05/msg00086.html

  * * *

When a realloc on assignment is done, gfortran first obtains the 
descriptor by calling realloc_lhs_loop_for_fcn_call and then does the 
actual function call using gfc_conv_function_expr.

However, realloc_lhs_loop_for_fcn_call already causes that the RHS is 
evaluated (via gfc_add_loop_ss_code). The double evaluation of an inner 
function call, causes memory leaks - and double evaluation 
(correctness/performance issue).

The following code solves the issue. I am not 100% sure that it 
correctly does so, but it solved the problem and the testsuite passed. 
(It might obsolete the check in "se->direct_byref" of 
gfc_conv_procedure_call.)


The test case is a bit pointless as it will only fail if one uses 
valgrind on it; thus, one could consider committing the patch even 
without. However, it might not harm to have it in the test suite.


Build and regtested on x86-64-linux.
OK for the trunk and 4.6/4.7?

Tobias
Paul Richard Thomas - May 22, 2012, 9:58 a.m.
Dear Tobias,

OK for trunk.  As you say, the testcase does no harm and so should be retained.

Thanks for the patch.

Cheers

Paul

On 21 May 2012 19:51, Tobias Burnus <burnus@net-b.de> wrote:
> First: I have another rather simple patch, which still needs to be reviewed:
> http://gcc.gnu.org/ml/fortran/2012-05/msg00086.html
>
>  * * *
>
> When a realloc on assignment is done, gfortran first obtains the descriptor
> by calling realloc_lhs_loop_for_fcn_call and then does the actual function
> call using gfc_conv_function_expr.
>
> However, realloc_lhs_loop_for_fcn_call already causes that the RHS is
> evaluated (via gfc_add_loop_ss_code). The double evaluation of an inner
> function call, causes memory leaks - and double evaluation
> (correctness/performance issue).
>
> The following code solves the issue. I am not 100% sure that it correctly
> does so, but it solved the problem and the testsuite passed. (It might
> obsolete the check in "se->direct_byref" of gfc_conv_procedure_call.)
>
>
> The test case is a bit pointless as it will only fail if one uses valgrind
> on it; thus, one could consider committing the patch even without. However,
> it might not harm to have it in the test suite.
>
>
> Build and regtested on x86-64-linux.
> OK for the trunk and 4.6/4.7?
>
> Tobias

Patch

2012-05-21  Tobias Burnus  <burnus@net-b.de>

	PR fortran/53389
	* trans-array.c (gfc_add_loop_ss_code): Don't evaluate expression, if
	ss->is_alloc_lhs is set.

2012-05-21  Tobias Burnus  <burnus@net-b.de>

	PR fortran/53389
	* gfortran.dg/realloc_on_assign_15.f90: New.

diff --git a/gcc/fortran/trans-array.c b/gcc/fortran/trans-array.c
index b24d1c3..02bb38d 100644
--- a/gcc/fortran/trans-array.c
+++ b/gcc/fortran/trans-array.c
@@ -2401,6 +2401,11 @@  gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
   bool skip_nested = false;
   int n;
 
+  /* Don't evaluate the arguments for realloc_lhs_loop_for_fcn_call; otherwise,
+     arguments could get evaluated multiple times.  */
+  if (ss->is_alloc_lhs)
+    return;
+
   outer_loop = outermost_loop (loop);
 
   /* TODO: This can generate bad code if there are ordering dependencies,
--- /dev/null	2012-05-21 07:36:22.011729607 +0200
+++ gcc/gcc/testsuite/gfortran.dg/realloc_on_assign_15.f90	2012-05-21 18:18:59.000000000 +0200
@@ -0,0 +1,40 @@ 
+! { dg-do run }
+!
+! PR fortran/53389
+!
+! The program was leaking memory before due to
+! realloc on assignment and nested functions.
+!
+module foo
+  implicit none
+  contains
+
+  function filler(array, val)
+    real, dimension(:), intent(in):: array
+    real, dimension(size(array)):: filler
+    real, intent(in):: val
+
+    filler=val
+
+  end function filler
+end module
+
+program test
+  use foo
+  implicit none
+
+  real, dimension(:), allocatable:: x, y
+  integer, parameter:: N=1000 !*1000
+  integer:: i
+
+!  allocate( x(N) )
+  allocate( y(N) )
+  y=0.0
+
+  do i=1, N
+!    print *,i
+    x=filler(filler(y, real(2*i)), real(i))
+    y=y+x
+  end do
+
+end program test