[Bug fortran/85537] Invalid memory reference at runtime when calling subroutine through procedure pointer

2019-03-27 Thread janus at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85537

--- Comment #15 from janus at gcc dot gnu.org ---
(In reply to Richard Biener from comment #13)
> And the FE indeed accepts
> 
>   procedure(ibar), pointer, save :: bar_ptr => bar_impl
> 
> but it should probably reject that.

Indeed this is only valid since Fortran 2008:

$ gfortran c0.f90 -std=f2003
c0.f90:12:53:

   12 |   procedure(ibar), pointer :: bar_ptr => bar_impl
  | 1
Error: Fortran 2008: non-NULL pointer initialization at (1)

[Bug fortran/85537] Invalid memory reference at runtime when calling subroutine through procedure pointer

2019-03-27 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85537

--- Comment #14 from Richard Biener  ---
Probably missed handling in add_init_expr_to_sym which needs to special case
procedure-pointers to nested functions?

[Bug fortran/85537] Invalid memory reference at runtime when calling subroutine through procedure pointer

2019-03-27 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85537

--- Comment #13 from Richard Biener  ---
The issue seems to be that the indirect call doesn't set up the static
chain and this is because appearantly bar_ptr has static storage duration:

foo ()
{
  static voidD.27 (*) (void) bar_ptrD.3873 = bar_implD.3867;
  integer(kind=4)D.8 D.3885;
  voidD.27 (*) (void) bar_ptr.2_1;
  integer(kind=4)D.8 _2;

   :
  _2 = 0;
  CHAIN.5_4(D)->aD.3880 = _2;
  bar_impl (); [static-chain: CHAIN.5_4(D)]
  bar_ptr.2_1 = bar_ptrD.3873;
  bar_ptr.2_1 ();
  return;

I think this isn't supported:

void foo()
{
  int a;
  int bar()
{
  return a;
}

  static int (*bp)() = bar;
  bp();
}

> ./cc1 -quiet t.c
t.c: In function ‘foo’:
t.c:9:24: error: initializer element is not constant
9 |   static int (*bp)() = bar;
  |^~~


I'm not sure how bar_ptr ends up statically allocated, that must be an
error on the FE side - I'd have expected a SAVE to be needed?  And
the FE indeed accepts

  procedure(ibar), pointer, save :: bar_ptr => bar_impl

but it should probably reject that.  The IL it emits is the same, with
or without the save.

TREE_STATIC is set here:

  if (!sym->attr.use_assoc
&& (sym->attr.save != SAVE_NONE || sym->attr.data
  || (sym->value && sym->ns->proc_name->attr.is_main_program)))
TREE_STATIC (decl) = 1;

beacuse sym->attr.save == SAVE_IMPLICIT

As said, the testcase is invalid if it needs SAVE since that prolongs
lifetime of the static chain over the duration of the contained function.

[Bug fortran/85537] Invalid memory reference at runtime when calling subroutine through procedure pointer

2019-03-25 Thread tkoenig at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85537

Thomas Koenig  changed:

   What|Removed |Added

  Component|rtl-optimization|fortran
Summary|[7/8/9 Regression] Invalid  |Invalid memory reference at
   |memory reference at runtime |runtime when calling
   |when calling subroutine |subroutine through
   |through procedure pointer   |procedure pointer

--- Comment #12 from Thomas Koenig  ---
(In reply to Thomas Koenig from comment #11)
> r237104 fails for me, testing r237008.

r237007 also fails, as do earlier versions of the compiler.

Not a regression then, but rather a design fault - seems like
we need to do some magic to tell the middle end that we could
access global variables (i.e. make sure that %r10 contains the
right address).

[Bug fortran/85537] Invalid memory reference at runtime when calling subroutine through procedure pointer

2018-04-26 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85537

Richard Biener  changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
   Last reconfirmed||2018-04-26
 Ever confirmed|0   |1

--- Comment #1 from Richard Biener  ---
Confirmed with GCC 7.