public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/109066] New: Segfault when using defined assignment
@ 2023-03-08 18:17 abensonca at gcc dot gnu.org
  2023-03-09  8:04 ` [Bug fortran/109066] " pault at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: abensonca at gcc dot gnu.org @ 2023-03-08 18:17 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109066
           Summary: Segfault when using defined assignment
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: abensonca at gcc dot gnu.org
  Target Milestone: ---

The following code (compiled using current trunk), when run, causes a segfault,
and valgrind complains about an invalid read. The code appears correct to me,
and runs correctly (no segfault, no warnings from valgrind) when compiled with
ifort.

module Input_Parameters_Bug
  public

  type :: resourceManager
     integer   :: counter=0 ! Remove this to resolve segfault
   contains
     procedure :: resourceManagerAssign
     generic   :: assignment(=) => resourceManagerAssign
  end type resourceManager

  type hdf5Object
     private
     type   (resourceManager) :: objectManager
   contains
     procedure :: openGroup =>IO_HDF5_Open_Group
    ! procedure :: h5Assign  ! Add this defined assignment to avoid segfault.
    ! generic   :: assignment(=) => h5Assign
  end type hdf5Object

  interface hdf5Object
     module procedure hdf5Constructor
  end interface hdf5Object

  type :: inputParameters
     private
     type(hdf5Object), pointer :: outputParameters => null() ! Make this
allocatable instead of pointer to resolve segfault
  end type inputParameters

  interface inputParameters
     module procedure inputParametersConstructorNode
  end interface inputParameters

contains

  subroutine resourceManagerAssign(to,from)
    implicit none
    class(resourceManager), intent(  out) :: to
    class(resourceManager), intent(in   ) :: from
    to%counter=from%counter+1
    write (0,*) "ASSIGN",to%counter    
    return
  end subroutine resourceManagerAssign

  function hdf5Constructor() result(self)
    implicit none
    type(hdf5Object) :: self
    return
  end function hdf5Constructor

  function IO_HDF5_Open_Group(inObject) result (self)
    implicit none
    type(hdf5Object) :: self
    class(hdf5Object), intent(in   ) :: inObject
    write (0,*) "OPEN"
    return
  end function IO_HDF5_Open_Group

  subroutine h5Assign(to,from)
    implicit none
    class(hdf5Object), intent(  out) :: to
    class(hdf5Object), intent(in   ) :: from
    write (0,*) "ASSIGN H5"
    to%objectManager=from%objectManager
    return
  end subroutine h5Assign

  function inputParametersConstructorNode(outputParametersGroup) result(self)
    implicit none
    type(inputParameters) :: self
    type(hdf5Object     ), intent(in   ) :: outputParametersGroup
    allocate(self%outputParameters)
    write (0,*) "START"
    self%outputParameters=outputParametersGroup%openGroup()
    write (0,*) "STOP"
    return
  end function inputParametersConstructorNode

end module Input_Parameters_Bug

program Test_Parameters_Bug
  use :: Input_Parameters_Bug
  implicit none
  type(hdf5Object)          :: outputFile
  type(inputParameters) :: testParameters
  outputFile=hdf5Object()
  write (0,*) "CALL"
  testParameters=inputParameters(outputFile)
end program Test_Parameters_Bug


$ gfortran bug.F90 -g
$ valgrind --track-origins=yes ./a.out 
==19625== Memcheck, a memory error detector
==19625== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==19625== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==19625== Command: ./a.out
==19625== 
 ASSIGN           1
 CALL
 START
 OPEN
==19625== Use of uninitialised value of size 8
==19625==    at 0x4012DF:
__input_parameters_bug_MOD_inputparametersconstructornode (bug.F90:73)
==19625==    by 0x4017D4: MAIN__ (bug.F90:87)
==19625==    by 0x401812: main (bug.F90:81)
==19625==  Uninitialised value was created by a stack allocation
==19625==    at 0x401206:
__input_parameters_bug_MOD_inputparametersconstructornode (bug.F90:67)
==19625== 

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

Backtrace for this error:
#0  0x4e161ef in ???
#1  0x4012df in __input_parameters_bug_MOD_inputparametersconstructornode
        at
/data001/abenson/Galacticus/galacticus_gfortranFinalization/bug.F90:73
#2  0x4017d4 in test_parameters_bug
        at
/data001/abenson/Galacticus/galacticus_gfortranFinalization/bug.F90:87
#3  0x401812 in main
        at
/data001/abenson/Galacticus/galacticus_gfortranFinalization/bug.F90:81
==19625== 
==19625== Process terminating with default action of signal 11 (SIGSEGV)
==19625==    at 0x4E1613E: raise (in
/home/abenson/Galacticus/Tools/lib/libc-2.12.1.so)
==19625==    by 0x4E161EF: ??? (in
/home/abenson/Galacticus/Tools/lib/libc-2.12.1.so)
==19625==    by 0x4012DE:
__input_parameters_bug_MOD_inputparametersconstructornode (bug.F90:73)
==19625==    by 0x4017D4: MAIN__ (bug.F90:87)
==19625==    by 0x401812: main (bug.F90:81)
==19625== 
==19625== HEAP SUMMARY:
==19625==     in use at exit: 5,448 bytes in 18 blocks
==19625==   total heap usage: 22 allocs, 4 frees, 13,588 bytes allocated
==19625== 
==19625== LEAK SUMMARY:
==19625==    definitely lost: 0 bytes in 0 blocks
==19625==    indirectly lost: 0 bytes in 0 blocks
==19625==      possibly lost: 0 bytes in 0 blocks
==19625==    still reachable: 5,448 bytes in 18 blocks
==19625==         suppressed: 0 bytes in 0 blocks
==19625== Rerun with --leak-check=full to see details of leaked memory
==19625== 
==19625== For lists of detected and suppressed errors, rerun with: -s
==19625== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)
Segmentation fault

This segfault goes away if I:
1. Remove the "counter" variable from type "resourceManager"
2. Add a defined assignment for type "hdf5Object"
3. Change the "outputParameters" variable from a pointer to an allocatable.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-03-10  8:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-08 18:17 [Bug fortran/109066] New: Segfault when using defined assignment abensonca at gcc dot gnu.org
2023-03-09  8:04 ` [Bug fortran/109066] " pault at gcc dot gnu.org
2023-03-09 13:56 ` pault at gcc dot gnu.org
2023-03-09 15:33 ` kargl at gcc dot gnu.org
2023-03-10  8:12 ` paul.richard.thomas at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).