[Bug fortran/88399] New: program segmentation faults when out-of-memory

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Bug fortran/88399] New: program segmentation faults when out-of-memory

law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88399

            Bug ID: 88399
           Summary: program segmentation faults when out-of-memory
           Product: gcc
           Version: 5.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: schorkl72 at gmail dot com
  Target Milestone: ---

Created attachment 45176
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45176&action=edit
wrapper for malloc() to simulate out-of-memory

The following program segmentation faults if allocate is unsuccessful:

program main
  type initialized
     integer :: i = 42
  end type

  type(initialized), pointer :: ptr
  integer :: st

  allocate(ptr, stat=st)
  if (st /= 0) stop "allocation failed"
end

To test it, compile the code snippet (say main.f90) and the attached malloc
wrapper as:

$ gfortran -c main.f90
$ gcc -c wrap_malloc.c
$ gfortran -Wl,--wrap=malloc main.o wrap_malloc.o
$ ./a.out
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7EFEF00CBE08
#1  0x7EFEF00CAF90
#2  0x7EFEEFD1B4AF
#3  0x40073A in MAIN__ at main.f90:?
Segmentation fault

The problem is the default initialization (= 42) of the derived type component.
Without it, the program runs correctly and stops with "allocation failed".
Reply | Threaded
Open this post in threaded view
|

[Bug fortran/88399] program segmentation faults when out-of-memory

law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88399

Dominique d'Humieres <dominiq at lps dot ens.fr> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING
   Last reconfirmed|                            |2018-12-13
     Ever confirmed|0                           |1

--- Comment #1 from Dominique d'Humieres <dominiq at lps dot ens.fr> ---
The darwin linker does not accept -Wl,--wrap=malloc:

ld: unknown option: --wrap=malloc
Reply | Threaded
Open this post in threaded view
|

[Bug fortran/88399] program segmentation faults when out-of-memory

law at redhat dot com
In reply to this post by law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88399

Harald Anlauf <anlauf at gmx dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |anlauf at gmx dot de

--- Comment #2 from Harald Anlauf <anlauf at gmx dot de> ---
AFAICT the problem seems fixed since 7.1, it is still present at 6.1.

Looking at the tree dump, one can see that gfortran <= 6 initialized
the derived type even when the malloc failed.  It is now protected by
an appropriate if/goto.

I recommend to update to 7.1+