Bug 87838 - Segmentation fault with function pointer to contained function
Summary: Segmentation fault with function pointer to contained function
Status: RESOLVED WORKSFORME
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 8.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-11-01 01:44 UTC by menospaamthereaper
Modified: 2019-06-18 21:20 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-11-03 00:00:00


Attachments
Output from -save-temps (592 bytes, text/plain)
2018-11-01 01:44 UTC, menospaamthereaper
Details

Note You need to log in before you can comment on or make changes to this bug.
Description menospaamthereaper 2018-11-01 01:44:30 UTC
Created attachment 44938 [details]
Output from -save-temps

Function pointer to a function contained within the same program gives a segmentation fault. Confirmed in both 7.3.0 and 8.2.0-1 (Ubuntu 18.04 installed from apt). Compiled with gfortran test.f03 and gfortran-8 test.f03. Minimal failing example:

program test_func_ptrs

     implicit none

      abstract interface
          subroutine func()
          end subroutine
      end interface

    procedure (func), pointer :: f_ptr => null ()

    f_ptr => f1

    call f_ptr()

     contains

     subroutine f1()
         implicit none

        return
     end subroutine

 end program test_func_ptrs
Comment 1 Dominique d'Humieres 2018-11-03 11:40:02 UTC
WORKSFORME on x86_64-apple-darwin18.2. I don't get any segmentation fault with the various revisions I have tested (from 5.5 up to trunk).
Comment 2 menospaamthereaper 2018-11-04 04:01:29 UTC
Thank you Dominique for testing this on a Mac. So perhaps the problem is specific to Linux.

Additional information that might help with debugging the segmentation fault:

1) Compiling this code with

gfortran -ffree-form -std=f2003 test.f 

gives the error

test.f:12:13:

     f_ptr => f1
             1
Error: Fortran 2008: Internal procedure ‘f1’ is invalid in procedure pointer assignment at (1)

(this code is within gcc/fortran/expr.c: 

	  if (attr.proc == PROC_INTERNAL &&
	      !gfc_notify_std(GFC_STD_F2008, "Internal procedure %qs "
			      "is invalid in procedure pointer assignment "
			      "at %L", rvalue->symtree->name, &rvalue->where))
)

2) However,  

gfortran -ffree-form -std=f2008 test.f

compiles successfully but gives a segmentation fault.

./a.out 

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

Backtrace for this error:
#0  0x7fe7aae3b31a
#1  0x7fe7aae3a503
#2  0x7fe7aaa5ef1f
#3  0x7fffed8fe6c0
Segmentation fault (core dumped)
Comment 3 janus 2019-03-09 16:13:22 UTC
(In reply to menospaamthereaper from comment #2)
> Thank you Dominique for testing this on a Mac. So perhaps the problem is
> specific to Linux.

I just compiled the test case on Ubuntu 18.04 with gfortran versions 5, 6, 7, 8 and trunk. I don't see a segfault with any of these. Also gdb and valgrind don't show any kind of problem.


> Additional information that might help with debugging the segmentation fault:
> 
> 1) Compiling this code with
> 
> gfortran -ffree-form -std=f2003 test.f 
> 
> gives the error
> 
> test.f:12:13:
> 
>      f_ptr => f1
>              1
> Error: Fortran 2008: Internal procedure ‘f1’ is invalid in procedure pointer
> assignment at (1)

This error is correct. That's just not allowed in Fortran 2003. All fine.


> 2) However,  
> 
> gfortran -ffree-form -std=f2008 test.f
> 
> compiles successfully but gives a segmentation fault.
> 
> ./a.out 
> 
> Program received signal SIGSEGV: Segmentation fault - invalid memory
> reference.
> 
> Backtrace for this error:
> #0  0x7fe7aae3b31a
> #1  0x7fe7aae3a503
> #2  0x7fe7aaa5ef1f
> #3  0x7fffed8fe6c0
> Segmentation fault (core dumped)

Could you please compile with the -g flag, in order to get a more meaningful backtrace? Alternatively try running the executable via gdb or valgrind for further debugging.
Comment 4 Dominique d'Humieres 2019-03-23 18:55:30 UTC
> Could you please compile with the -g flag, in order to get a more meaningful
> backtrace? Alternatively try running the executable via gdb or valgrind
> for further debugging.

Any progress before closing the PR as WORKSFORME?
Comment 5 kargls 2019-06-18 21:20:42 UTC
No feedback from OP.  Works for me on FreeBSD.  Closing.