Bug 41753 - [OOP] segfault with -O2 using methods as actual arguments
Summary: [OOP] segfault with -O2 using methods as actual arguments
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-10-19 10:19 UTC by Paul Thomas
Modified: 2010-06-06 12:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-10-19 15:46:06


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Thomas 2009-10-19 10:19:35 UTC
The following produces a segfault at runtime with -O2 and higher

module m
  type t
    real x
  contains
    procedure, pass :: a
  end type
contains
  real function a (arg)
    class (t) :: arg
    a = arg%x
  end function

  real function b (arg)
    real :: arg
    b = arg*2
  end function

  subroutine s
    class(t), allocatable :: z
    allocate(z)          ! see tree-optimized output
    z%x = 42             ! see tree-optimized output
    print *, b (z%a())   ! see tree-optimized output
  end subroutine
end module

  use m
  call s
end

With -O0, the marked lines give
<bb 2>:
  D.1348_1 = malloc (4);
  if (D.1348_1 == 0B)
    goto <bb 3>;
  else
    goto <bb 4>;

<bb 3>:
  _gfortran_os_error (&"Out of memory"[1]{lb: 1 sz: 1});

<bb 4>:
  D.1374_2 = (struct t *) D.1348_1;
  z.$data = D.1374_2;
  z.$vindex = 2800126;
  z.$size = 4;
  D.1375_3 = z.$data;
  D.1375_3->x = 4.2e+1;
  dt_parm.0.common.filename = &"test.f03"[1]{lb: 1 sz: 1};
  dt_parm.0.common.line = 22;
  dt_parm.0.common.flags = 128;
  dt_parm.0.common.unit = 6;
  _gfortran_st_write (&dt_parm.0);
  D.1376_4 = __m_MOD_a (&z);
  D.1350 = D.1376_4;
  D.1377_5 = __m_MOD_b (&D.1350);
  D.1351 = D.1377_5;
  _gfortran_transfer_real (&dt_parm.0, &D.1351, 4);
  _gfortran_st_write_done (&dt_parm.0);

whilst with -O2
<bb 2>:
  D.1348_1 = malloc (4);
  if (D.1348_1 == 0B)
    goto <bb 3>;
  else
    goto <bb 4>;

<bb 3>:
  _gfortran_os_error (&"Out of memory"[1]{lb: 1 sz: 1});

<bb 4>:
  D.1374_2 = (struct t *) D.1348_1;
  dt_parm.0.common.filename = &"test.f03"[1]{lb: 1 sz: 1};
  dt_parm.0.common.line = 22;
  dt_parm.0.common.flags = 128;
  dt_parm.0.common.unit = 6;
  _gfortran_st_write (&dt_parm.0);
  arg_21 = (struct .class.t & restrict) &z;
  D.1389_22 = arg_21->$data;
  D.1386_23 = D.1389_22->x;
  D.1392_26 = D.1386_23 * 2.0e+0;
  D.1351 = D.1392_26;
  _gfortran_transfer_real (&dt_parm.0, &D.1351, 4);
  _gfortran_st_write_done (&dt_parm.0);


ie. the important part of the allocation and the entire assignment disappear!
Since the PRINT then tries to access the data component of the CLASS object, the segfault results
Comment 1 Dominique d'Humieres 2009-10-19 10:23:49 UTC
Confirmed on (i686|powerpc)-apple-darwin9 revision 152966. On 
Comment 2 Paul Thomas 2009-10-19 15:46:06 UTC
Have cc'd Richi to see if he can shed any light on this.

Paul
Comment 3 janus 2010-06-06 03:26:38 UTC
At r160335, I don't see the failure on x86_64-unknown-linux-gnu. Maybe it has been fixed by some middle-end changes by now. Can anyone confirm that the error is gone?
Comment 4 Dominique d'Humieres 2010-06-06 08:05:31 UTC
> At r160335, I don't see the failure on x86_64-unknown-linux-gnu. Maybe it has
> been fixed by some middle-end changes by now. Can anyone confirm that the error
> is gone?

No error for the flags I have tried on x86_64-apple-darwin10.3.0 and powerpc-apple-darwin9.
Comment 5 Paul Thomas 2010-06-06 12:49:38 UTC
(In reply to comment #4)
It's fixed for me too. x86_64/FC9

I'll mark it as fixed - thanks for noting that it had gone.

Paul