https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78356

            Bug ID: 78356
           Summary: segfault allocating polymorphic variable with
                    polymorphic component with allocatable component
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: abensonca at gmail dot com
  Target Milestone: ---

The following code causes a segfault at runtime using gfortran 7.0.0 (r242406),
but runs successfully when compiled with gfortran 6.0.0. Output and output from
valgrind are appended below.

module m
  implicit none
  type ac
  end type ac
  type, extends(ac) :: a
     integer, allocatable, dimension(:) :: b
  end type a
  type n
     class(ac), allocatable, dimension(:) :: acr
   contains
     procedure :: ncp => nncp
  end type n
contains
  subroutine nncp(self,tg)
    implicit none
    class(n), intent(in   ) :: self
    class(n), intent(inout) :: tg
    allocate(tg%acr(1),source=self%acr(1))
    return
  end subroutine nncp
end module m
program p
  use m
  implicit none
  type(n) :: s,t
  allocate(a :: s%acr(1))
  call s%ncp(t)
end program

$ gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/home/abenson/Galacticus/Tools/libexec/gcc/x86_64-pc-linux-gnu/7.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-trunk/configure --prefix=/home/abenson/Galacticus/Tools
--enable-languages=c,c++,fortran --disable-multilib
Thread model: posix
gcc version 7.0.0 20161115 (experimental) (GCC) 

$ a.out 

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7ff186f0d1ef in ???
        at
/data001/abenson/Galacticus/Tools/glibc-2.12.1/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0
#1  0x7ff186f5a460 in ???
        at ../sysdeps/x86_64/memcpy.S:161
#2  0x40167b in __copy_m_A
        at /home/abenson/Galacticus/v0.9.4/c.F90:22
#3  0x400f59 in __m_MOD_nncp
        at /home/abenson/Galacticus/v0.9.4/c.F90:18
#4  0x401593 in p
        at /home/abenson/Galacticus/v0.9.4/c.F90:27
#5  0x4016c4 in main
        at /home/abenson/Galacticus/v0.9.4/c.F90:23
Segmentation fault

$ valgrind a.out 
==1544== Memcheck, a memory error detector
==1544== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==1544== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==1544== Command: a.out
==1544== 
==1544== Argument 'size' of function malloc has a fishy (possibly negative)
value: -274793986988
==1544==    at 0x4A05B60: malloc (vg_replace_malloc.c:299)
==1544==    by 0x401647: __copy_m_A.3590 (c.F90:22)
==1544==    by 0x400F59: __m_MOD_nncp (c.F90:18)
==1544==    by 0x401593: MAIN__ (c.F90:27)
==1544==    by 0x4016C4: main (c.F90:23)
==1544== 
==1544== Source and destination overlap in memcpy(0x0, 0x2,
18446743798915564628)
==1544==    at 0x4A0A4D3: memcpy (vg_replace_strmem.c:1019)
==1544==    by 0x40167B: __copy_m_A.3590 (c.F90:22)
==1544==    by 0x400F59: __m_MOD_nncp (c.F90:18)
==1544==    by 0x401593: MAIN__ (c.F90:27)
==1544==    by 0x4016C4: main (c.F90:23)
==1544== 
==1544== Invalid read of size 2
==1544==    at 0x4A0A6E8: memcpy (vg_replace_strmem.c:1019)
==1544==    by 0x40167B: __copy_m_A.3590 (c.F90:22)
==1544==    by 0x400F59: __m_MOD_nncp (c.F90:18)
==1544==    by 0x401593: MAIN__ (c.F90:27)
==1544==    by 0x4016C4: main (c.F90:23)
==1544==  Address 0x2 is not stack'd, malloc'd or (recently) free'd
==1544== 

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x56431ef in ???
        at
/data001/abenson/Galacticus/Tools/glibc-2.12.1/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0
#1  0x4a0a6e8 in _vgr20180ZZ_libcZdsoZa_memcpy
        at ../shared/vg_replace_strmem.c:1019
#2  0x40167b in ???
#3  0x400f59 in ???
#4  0x401593 in ???
#5  0x4016c4 in ???
#6  0x562fd0c in __libc_start_main
        at /data001/abenson/Galacticus/Tools/glibc-2.12.1/csu/libc-start.c:226
#7  0x400728 in ???
==1544== 
==1544== Process terminating with default action of signal 11 (SIGSEGV)
==1544==    at 0x564313E: raise (raise.c:67)
==1544==    by 0x56431EF: ??? (in
/home/abenson/Galacticus/Tools/lib/libc-2.12.1.so)
==1544==    by 0x4A0A6E7: memcpy (vg_replace_strmem.c:1019)
==1544==    by 0x40167B: __copy_m_A.3590 (c.F90:22)
==1544==    by 0x400F59: __m_MOD_nncp (c.F90:18)
==1544==    by 0x401593: MAIN__ (c.F90:27)
==1544==    by 0x4016C4: main (c.F90:23)
==1544== 
==1544== HEAP SUMMARY:
==1544==     in use at exit: 3,964 bytes in 17 blocks
==1544==   total heap usage: 21 allocs, 4 frees, 12,104 bytes allocated
==1544== 
==1544== LEAK SUMMARY:
==1544==    definitely lost: 0 bytes in 0 blocks
==1544==    indirectly lost: 0 bytes in 0 blocks
==1544==      possibly lost: 0 bytes in 0 blocks
==1544==    still reachable: 3,964 bytes in 17 blocks
==1544==         suppressed: 0 bytes in 0 blocks
==1544== Rerun with --leak-check=full to see details of leaked memory
==1544== 
==1544== For counts of detected and suppressed errors, rerun with: -v
==1544== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 3 from 3)
Segmentation fault


Allocating the "b" component of "s%acr(1)" doesn't resolve the problem.

Reply via email to