Kompiler GNU Fortran pada Subsistem Windows untuk Linux - Kesalahan segmentasi dengan melewatkan fungsi internal ke prosedur lain [duplikat]

Nov 29 2020

Saya yakin berikut ini adalah program Fortran 2008 yang valid dan berfungsi dengan baik pada sistem operasi macOS, Linux, dan Windows asli dengan kompiler Intel dan GNU Fortran.

module InternalFuncCaller_mod
    implicit none
    abstract interface 
        function getInternalFunc_proc(input) result(output)
            implicit none
            real, intent(in)    :: input
            real                :: output
        end function getInternalFunc_proc
    end interface
contains
    subroutine callInternalFunc(getInternalFunc, x)
        implicit none
        procedure(getInternalFunc_proc) :: getInternalFunc
        real, intent(in)                :: x
        write(*,*) getInternalFunc(x)
    end subroutine callInternalFunc
end module InternalFuncCaller_mod

module InternalFunc_mod
    implicit none
contains
    subroutine passInternalFunc()
        use InternalFuncCaller_mod, only: callInternalFunc
        implicit none
        call callInternalFunc(getThisInternalFunc, x = 4.)
    contains
        function getThisInternalFunc(x) result(sqrtx)
            implicit none
            real, intent(in)    :: x
            real                :: sqrtx
            sqrtx = sqrt(x)
        end function getThisInternalFunc
    end subroutine passInternalFunc
end module InternalFunc_mod

program testInternalFuncCall
    use InternalFunc_mod
    implicit none
    call passInternalFunc()
    write(*,*) "Done."
end program testInternalFuncCall

Namun, ketika dikompilasi dengan GFortran pada Subsistem Windows untuk Linux (WSL) (Ubuntu) dan dijalankan, ini memberikan pesan kesalahan SegFault berikut:

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0  0x7ffb84580d3a
#1  0x7ffb8457fed5
#2  0x7ffb843a620f
#3  0x7fffde946cb0
Segmentation fault (core dumped)

Saya telah menelusuri masalah ini ke pemanggilan fungsi internal dengan prosedur eksternal. Tetapi kode yang sama berfungsi dengan baik pada semua Sistem Operasi lain dengan kompiler Fortran yang berbeda. Jadi, ini tampaknya bukan bug dengan GNU GFortran, tetapi lebih mungkin menjadi masalah dengan kompilasi statis dan eksekusi kode yang berisi panggilan eksternal ke prosedur internal dari prosedur lain, khususnya, pada OS WSL.

To give more information, I have noticed that the library works fine (even with internal function calls) when it is built as a shared library. However, it fails with the same error message when compiled a static library.

So, it seems like a combination of some GFortran flags can somehow resolve the error (-fPIC -shared). Any help on how to resolve this problem is greatly appreciated.

Jawaban

3 Rob Nov 30 2020 at 09:25

As the person with the problem in Segmentation fault when passing internal function as argument I've found moving to WSL version 2 fixes the issue for me.