Delayed segmentation error and non_overridable keyword

I am developing Fortran object-oriented code for numerical optimization with polymorphism supported by abstract types. Since it is good practice a TDD, I try to write all the optimization tests in an abstract style class(generic_optimizer), which then has to run every instance of a class, for example type(newton_raphson).

All optimization tests contain a call call my_problem%solve(...)that is defined as deferredin an abstract type and, of course, has a different implementation in each derived type.

Problem: if in every non-abstract class I define a deferred function as non_overridable, I get a segmentation error, for example:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()

(gdb) where
#0  0x0000000000000000 in ?? ()
#1  0x0000000000913efe in __newton_raphson_MOD_nr_solve ()
#2  0x00000000008cfafa in MAIN__ ()
#3  0x00000000008cfb2b in main ()
#4  0x0000003a3c81ed5d in __libc_start_main () from /lib64/libc.so.6
#5  0x00000000004048f9 in _start ()

, , non_overridable. , , . ?

, . gfortran 5.3.0 6.1.0.

module generic_type_module
    implicit none
    private

    type, abstract, public :: generic_type
        real(8) :: some_data
        contains
        procedure (sqrt_interface), deferred :: square_root
        procedure, non_overridable           :: sqrt_test
    end type generic_type

    abstract interface
       real(8) function sqrt_interface(this,x) result(sqrtx)
          import generic_type
          class(generic_type), intent(in) :: this
          real(8), intent(in) :: x
       end function sqrt_interface
    end interface

    contains

    subroutine sqrt_test(this,x)
        class(generic_type), intent(in) :: this
        real(8), intent(in) :: x
        print *, 'sqrt(',x,') = ',this%square_root(x)
    end subroutine sqrt_test

end module generic_type_module

module actual_types_module
    use generic_type_module
    implicit none
    private

    type, public, extends(generic_type) :: crashing
       real(8) :: other_data
       contains
       procedure, non_overridable :: square_root => crashing_square_root
    end type crashing
    type, public, extends(generic_type) :: working
       real(8) :: other_data
       contains
       procedure :: square_root => working_square_root
    end type working

    contains

    real(8) function crashing_square_root(this,x) result(sqrtx)
       class(crashing), intent(in) :: this
       real(8), intent(in) :: x
       sqrtx = sqrt(x)
    end function crashing_square_root
    real(8) function working_square_root(this,x) result(sqrtx)
       class(working), intent(in) :: this
       real(8), intent(in) :: x
       sqrtx = sqrt(x)
    end function working_square_root

end module actual_types_module

program deferred_test
    use actual_types_module
    implicit none
    type(crashing) :: crashes
    type(working)  :: works

    call works%sqrt_test(2.0_8)
    call crashes%sqrt_test(2.0_8)

end program
+4
1

, OP,

module types
    implicit none

    type :: Type1
    contains
        procedure :: test
        procedure :: square => Type1_square
    endtype

    type, extends(Type1) :: Type2
    contains
       procedure, non_overridable :: square => Type2_square
    endtype

contains

    subroutine test( this, x )
        class(Type1) :: this
        real :: x
        print *, "square(", x, ") = ",this % square( x )
    end subroutine

    function Type1_square( this, x ) result( y )
       class(Type1) :: this
       real :: x, y
       y = -100      ! dummy
    end function

    function Type2_square( this, x ) result( y )
       class(Type2) :: this
       real :: x, y
       y = x**2
    end function

end module

program main
    use types
    implicit none
    type(Type1) :: t1
    type(Type2) :: t2

    call t1 % test( 2.0 )
    call t2 % test( 2.0 )
end program

gfortran-6

square(   2.00000000     ) =   -100.000000
square(   2.00000000     ) =   -100.000000

ifort- {14,16} Oracle fortran 12.5

square(   2.000000     ) =   -100.0000    
square(   2.000000     ) =    4.000000

( , ):

    subroutine test( this, x )
        class(Type1) :: this
        real :: x, y
        call this % square( x, y )
        print *, "square(", x, ") = ", y
    end subroutine

    subroutine Type1_square( this, x, y )
        class(Type1) :: this
        real :: x, y
        print *, "Type1_square:"
        y = -100      ! dummy
    end subroutine

    subroutine Type2_square( this, x, y )
        class(Type2) :: this
        real :: x, y
        print *, "Type2_square:"
        y = x**2
    end subroutine

. gfortran-6

Type1_square:
square(   2.00000000     ) =   -100.000000    
Type1_square:
square(   2.00000000     ) =   -100.000000

ifort- {14,16} Oracle fortran 12.5

Type1_square:
square(   2.000000     ) =   -100.0000    
Type2_square:
square(   2.000000     ) =    4.000000 

non_overridable , gfortran , . , gfortran + non_overridable ( )...

(, OP , , gfortran deferred (generic_type) , , .)


Edit

gfortran , Type1 abstract. , Type1

    type, abstract :: Type1    ! now an abstract type (cannot be instantiated)
    contains
        procedure :: test
        procedure :: square => Type1_square
    endtype

-

program main
    use types
    implicit none
    type(Type2) :: t2

    call t2 % test( 2.0 )
end program

ifort-16    : square(   2.000000     ) =    4.000000    
oracle-12.5 : square( 2.0 ) =  4.0
gfortran-6  : square(   2.00000000     ) =   -100.000000  

square() Type1 deferred (.. ) OP,

type, abstract :: Type1  ! now an abstract type (cannot be instantiated)
contains
    procedure :: test
    procedure(Type1_square), deferred :: square  ! has no implementation yet
endtype

abstract interface
    function Type1_square( this, x ) result( y )
        import
        class(Type1) :: this
        real :: x, y
    end function
end interface

ifort-16 Oracle-12.5 4.0 call t2 % test( 2.0 ), gfortran-6 . ,

$ gfortran -fsanitize=address test.f90   # on Linux x86_64

ASAN:SIGSEGV    (<-- or "ASAN:DEADLYSIGNAL" on OSX 10.9)
=================================================================
==22045==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 
                (pc 0x000000000000 bp 0x7fff1d23ecd0 sp 0x7fff1d23eac8 T0)
==22045==Hint: pc points to the zero page.

, , square() Type1 ( ) gfortran (, ). , non_overridable Type2, gfortran 4.0 ( ).

0

Source: https://habr.com/ru/post/1660308/


All Articles