Bug 37253 - Segmentation fault with procedure pointer
Summary: Segmentation fault with procedure pointer
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: ---
Assignee: janus
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-08-27 10:50 UTC by Dominique d'Humieres
Modified: 2008-08-28 15:13 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2008-08-27 21:10:58


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dominique d'Humieres 2008-08-27 10:50:02 UTC
The following code

module myMod

  CONTAINS

  real function proc3( arg1 )
     integer :: arg1
     proc3 = arg1+7
  end function proc3

  subroutine proc4( arg1 )
     procedure(real), pointer :: arg1
     print*, 'the func: ', arg1(0)
  end subroutine proc4

end module myMod

program myProg
  use myMod
  PROCEDURE (real), POINTER :: p => NULL()
  p => proc3
  print*, 'the func: ', p(0)
  call proc4( p )
end program myProg

gives a segmentation fault at run time:

[ibook-dhum] f90/bug% a.out 
 the func:    7.0000000    
Segmentation fault
Comment 1 Tobias Burnus 2008-08-27 20:30:12 UTC
  call proc4( p )
should transfer to the tree code as
  proc4(&p)
(procpointer passed by reference) but it is actually
  proc(p)
The latter is OK for a PROCEDURE dummy argument but nor for a PROCEDURE POINTER dummy argument.

I know we came across the pass-by-refence issue before when implementing procpointers (cf. proc_ptr_7.f90). Why does not not work in this case? In proc_ptr_7.f90 we have:

  procedure(Integer(c_int)), pointer :: ptr
  call foo(ptr)
with
  subroutine foo(a)
  procedure(integer(c_int)), pointer :: a

which produces:
  foo (&ptr);

What is different here? 

 * * *

I think there is some issue with saving the information in the module. If one moves proc4 to the main program (via contains) then it work, i.e. the procpointer information is lost.
Comment 2 Tobias Burnus 2008-08-27 20:33:20 UTC
Actual, I have the feeling the attr.proc_pointer is not saved at all in module.c, cf. ab_attribute in that file. One should check whether other attributes are also missing.

Dominique: Thanks for finding it; may this helps also with PR 37254.
Comment 3 janus 2008-08-27 21:10:57 UTC
> Actual, I have the feeling the attr.proc_pointer is not saved at all in
> module.c, cf. ab_attribute in that file.

Exactly. Thanks for pointing this out. The following patch fixes it:

Index: gcc/fortran/module.c
===================================================================
--- gcc/fortran/module.c	(revision 139622)
+++ gcc/fortran/module.c	(working copy)
@@ -1643,7 +1643,7 @@ mio_internal_string (char *string)
 
 typedef enum
 { AB_ALLOCATABLE, AB_DIMENSION, AB_EXTERNAL, AB_INTRINSIC, AB_OPTIONAL,
-  AB_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
+  AB_POINTER, AB_PROC_POINTER, AB_TARGET, AB_DUMMY, AB_RESULT, AB_DATA,
   AB_IN_NAMELIST, AB_IN_COMMON, AB_FUNCTION, AB_SUBROUTINE, AB_SEQUENCE,
   AB_ELEMENTAL, AB_PURE, AB_RECURSIVE, AB_GENERIC, AB_ALWAYS_EXPLICIT,
   AB_CRAY_POINTER, AB_CRAY_POINTEE, AB_THREADPRIVATE, AB_ALLOC_COMP,
@@ -1661,6 +1661,7 @@ static const mstring attr_bits[] =
     minit ("INTRINSIC", AB_INTRINSIC),
     minit ("OPTIONAL", AB_OPTIONAL),
     minit ("POINTER", AB_POINTER),
+    minit ("PROC_POINTER", AB_PROC_POINTER),
     minit ("VOLATILE", AB_VOLATILE),
     minit ("TARGET", AB_TARGET),
     minit ("THREADPRIVATE", AB_THREADPRIVATE),
@@ -1743,6 +1744,8 @@ mio_symbol_attribute (symbol_attribute *
 	MIO_NAME (ab_attribute) (AB_OPTIONAL, attr_bits);
       if (attr->pointer)
 	MIO_NAME (ab_attribute) (AB_POINTER, attr_bits);
+      if (attr->proc_pointer)
+	MIO_NAME (ab_attribute) (AB_PROC_POINTER, attr_bits);
       if (attr->is_protected)
 	MIO_NAME (ab_attribute) (AB_PROTECTED, attr_bits);
       if (attr->value)
@@ -1839,6 +1842,9 @@ mio_symbol_attribute (symbol_attribute *
 	    case AB_POINTER:
 	      attr->pointer = 1;
 	      break;
+	    case AB_PROC_POINTER:
+	      attr->proc_pointer = 1;
+	      break;
 	    case AB_PROTECTED:
 	      attr->is_protected = 1;
 	      break;
Comment 4 janus 2008-08-28 11:24:02 UTC
> One should check whether other attributes are also missing.

attr.procedure is missing as well. I'll provide an extended patch.
Comment 5 janus 2008-08-28 15:12:17 UTC
Subject: Bug 37253

Author: janus
Date: Thu Aug 28 15:10:50 2008
New Revision: 139713

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=139713
Log:
2008-08-28  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/37253
	* module.c (ab_attribute,attr_bits,mio_symbol_attribute): Take care of
	saving attr.procedure and attr.proc_ptr to the module file.


2008-08-28  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/37253
	* gfortran.dg/proc_ptr_10.f90: New.

Added:
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_10.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/module.c
    trunk/gcc/testsuite/ChangeLog

Comment 6 janus 2008-08-28 15:13:13 UTC
Fixed in r139713. Closing.