Bug 93925 - Invalid memory reference upon call of a routine taking a procedure pointer as argument
Summary: Invalid memory reference upon call of a routine taking a procedure pointer as...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 9.1.1
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords: wrong-code
Depends on:
Blocks: F2003
  Show dependency treegraph
 
Reported: 2020-02-25 09:16 UTC by martin
Modified: 2022-01-07 17:47 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-01-26 00:00:00


Attachments
classStar_map2 with pointer attribute for variable x (347 bytes, text/plain)
2021-01-26 05:57 UTC, martin
Details
classStar_map3 with class(*), pointer for variable x (353 bytes, text/plain)
2021-01-26 05:58 UTC, martin
Details
classStar_map4 without target nor pointer attributes for variable x (293 bytes, text/plain)
2021-01-26 05:59 UTC, martin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description martin 2020-02-25 09:16:56 UTC
The following code aborts with a segfault (invalid memory reference) upon call of the apply function. This happens with gfortran 9.1.1 as well as a recent gfortran-10.

module cs

implicit none
private

public classStar_map_ifc
public fun, apply

abstract interface
   function classStar_map_ifc(x) result(y)
      class(*), pointer            :: y
      class(*), target, intent(in) :: x
   end function classStar_map_ifc
end interface

contains

   function fun(x) result(y)
      class(*), pointer            :: y
      class(*), target, intent(in) :: x
      select type (x)
      type is (integer)
         y => x
      class default
         y => null()
      end select
   end function fun

   function apply(f, x) result(y)
      procedure(classStar_map_ifc) :: f
      integer, intent(in) :: x
      integer :: y
      class(*), pointer :: p
      p => f(x)
      select type (p)
      type is (integer)
         y = p
      end select
   end function apply

end module cs


program classStar_map2

use cs
implicit none

integer :: x, y
procedure(classStar_map_ifc), pointer :: f

x = 123654
y = apply(fun, x)
print *, x, y

end program classStar_map2
Comment 1 Thomas Koenig 2021-01-25 20:04:55 UTC
The test case violates, 15.5.2.4  Ordinary dummy variables, subsection 11
of the F2018 standard:

If the dummy argument has the TARGET attribute and the effective argument
does not have the TARGET attribute or is an array section with a vector
subscript, any pointers associated with the dummy argument
become undefined when execution of the procedure completes.

fun returns an undefined pointer, so anything can happen (including
a segfault).

nagfor catches this, by the way:

Runtime Error: 93925.f90, line 36: Reference to dangling pointer P
Target was RETURNed from procedure F
Comment 2 martin 2021-01-26 05:57:41 UTC
Created attachment 50048 [details]
classStar_map2 with pointer attribute for variable x
Comment 3 martin 2021-01-26 05:58:37 UTC
Created attachment 50049 [details]
classStar_map3 with class(*), pointer for variable x
Comment 4 martin 2021-01-26 05:59:23 UTC
Created attachment 50050 [details]
classStar_map4 without target nor pointer attributes for variable x
Comment 5 martin 2021-01-26 05:59:51 UTC
Sorry for this invalid test case, obviously I did reduce too much. The three attached variations should hopefully all be conforming to the standard and still produce the same error. Please reopen if the testcases are ok now.

The attached classStar_map2 is a slight variation on the original code, and just uses an "integer, pointer" variable for x (as is roughly the context where I originally encountered the bug).

classStar_map3 has no target attribute but uses "class(*), pointer" for variable as well as argument x.

classStar_map4 is a bit further reduced. It has no target nor pointer attribute for x, but does not use the value of x, just returns y=>null() and y=987456.
Comment 6 Thomas Koenig 2021-01-26 06:48:18 UTC
OK, these test cases look valid.

Reopening.
Comment 7 Paul Thomas 2021-01-27 11:16:48 UTC
Ah, go on! It's on my tree - I'll commit as obvious.

Paul
Comment 8 GCC Commits 2021-01-27 11:34:42 UTC
The master branch has been updated by Paul Thomas <pault@gcc.gnu.org>:

https://gcc.gnu.org/g:4225af228b5d52e8d7593dea49584c91b4bc2e1f

commit r11-6928-g4225af228b5d52e8d7593dea49584c91b4bc2e1f
Author: Paul Thomas <pault@gcc.gnu.org>
Date:   Wed Jan 27 11:34:02 2021 +0000

    Fortran: Fix ICE due to elemental procedure pointers [PR93924/5].
    
    2021-01-27  Paul Thomas  <pault@gcc.gnu.org>
    
    gcc/fortran
            PR fortran/93924
            PR fortran/93925
            * trans-expr.c (gfc_conv_procedure_call): Suppress the call to
            gfc_conv_intrinsic_to_class for unlimited polymorphic procedure
            pointers.
            (gfc_trans_assignment_1): Similarly suppress class assignment
            for class valued procedure pointers.
    
    gcc/testsuite/
            PR fortran/93924
            PR fortran/93925
            * gfortran.dg/proc_ptr_52.f90 : New test.
Comment 9 GCC Commits 2021-08-28 19:57:21 UTC
The releases/gcc-10 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:bf22e268b5c2a03216503254cfcb828016a7d998

commit r10-10077-gbf22e268b5c2a03216503254cfcb828016a7d998
Author: Paul Thomas <pault@gcc.gnu.org>
Date:   Wed Jan 27 11:34:02 2021 +0000

    Fortran: Fix ICE due to elemental procedure pointers [PR93924/5].
    
    2021-01-27  Paul Thomas  <pault@gcc.gnu.org>
    
    gcc/fortran
            PR fortran/93924
            PR fortran/93925
            * trans-expr.c (gfc_conv_procedure_call): Suppress the call to
            gfc_conv_intrinsic_to_class for unlimited polymorphic procedure
            pointers.
            (gfc_trans_assignment_1): Similarly suppress class assignment
            for class valued procedure pointers.
    
    gcc/testsuite/
            PR fortran/93924
            PR fortran/93925
            * gfortran.dg/proc_ptr_52.f90 : New test.
    
    (cherry picked from commit 4225af228b5d52e8d7593dea49584c91b4bc2e1f)
Comment 10 anlauf 2021-08-28 19:58:52 UTC
Cherry-picked to 10-branch for Paul after verifying that this fixes the issue.
Comment 11 GCC Commits 2021-09-06 18:43:50 UTC
The releases/gcc-9 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:49cefbec30499da06f90912090bcc5eabdfefa32

commit r9-9710-g49cefbec30499da06f90912090bcc5eabdfefa32
Author: Paul Thomas <pault@gcc.gnu.org>
Date:   Wed Jan 27 11:34:02 2021 +0000

    Fortran: Fix ICE due to elemental procedure pointers [PR93924/5].
    
    2021-01-27  Paul Thomas  <pault@gcc.gnu.org>
    
    gcc/fortran
            PR fortran/93924
            PR fortran/93925
            * trans-expr.c (gfc_conv_procedure_call): Suppress the call to
            gfc_conv_intrinsic_to_class for unlimited polymorphic procedure
            pointers.
            (gfc_trans_assignment_1): Similarly suppress class assignment
            for class valued procedure pointers.
    
    gcc/testsuite/
            PR fortran/93924
            PR fortran/93925
            * gfortran.dg/proc_ptr_52.f90 : New test.
    
    (cherry picked from commit 4225af228b5d52e8d7593dea49584c91b4bc2e1f)
Comment 12 anlauf 2021-09-06 18:48:15 UTC
Should be fixed on all open branches.  Closing.

Thanks for the report!