传递内部函数作为参数时出现分段错误

时间:2018-04-22 12:39:41

标签: fortran gfortran windows-subsystem-for-linux

我有一些代码将主程序的内部函数作为函数的参数传递:当最终调用传递的函数时,它会导致分段错误。这只发生在我使用Windows子系统Linux时(我在WSL上使用Ubuntu 16);在本机Linux或Mac机器上运行时不会发生这种情况。

崩溃的最小例子:

module test1

implicit none

contains

subroutine x(ff,y)

    interface 
        real function ff(y)
            real, intent(in) :: y
        end function ff
    end interface
    real, intent(in) :: y
    integer z

    z=ff(y)

end subroutine x

end module test1

program tester
use test1
implicit none

call x(f,1.0)

contains

real function f(y)
    real, intent(in) :: y

    write(*,*) y
    f=y*y
end function f

end program tester

编译:

 gfortran-7 -ggdb test_fun_passing.f90 -o test

回溯,gdb输出:

(gdb) bt                                                                                             
#0  0x00007ffffffde320 in ?? ()                                                                         
#1  0x0000000000400734 in test1::x (ff=0x7ffffffde320, y=1) at test_fun_passing.f90:17                  
#2  0x0000000000400829 in tester () at test_fun_passing.f90:31                                          
#3  0x0000000000400860 in main (argc=1, argv=0x7ffffffde64f) at test_fun_passing.f90:27                 
#4  0x00007ffffec70830 in __libc_start_main (main=0x40082c <main>, argc=1, argv=0x7ffffffde448,             init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffffffde438)        at ../csu/libc-start.c:291                                                                         

#5  0x0000000000400669 in _start ()                                                                     

这个确实有效(将f移动到它自己的模块中但仍然作为参数传递)所以它是f包含在程序中的东西。

module test1

implicit none

contains

subroutine x(ff,y)

    interface 
        real function ff(y)
            real, intent(in) :: y
        end function ff
    end interface
    real, intent(in) :: y
    integer z

    z=ff(y)

end subroutine x

end module test1

module test2

implicit none

contains

real function f(y)
    real, intent(in) :: y

    write(*,*) y
    f=y*y
end function f

end module test2

program tester
use test1
use test2
implicit none

call x(f,1.0)

end program tester


gfortran-7 -ggdb test_fun_passing.f90 -o test && ./test                                                                                              
1.00000000        

以这种方式传递f对Fortran有效,还是我在本机Linux上传播了一些非标准功能?

1 个答案:

答案 0 :(得分:1)

看起来我遇到了这个问题:

https://github.com/Microsoft/WSL/issues/3083https://github.com/Microsoft/WSL/issues/286

WSL具有不可执行的堆栈。运行:

excestack -c test
在本机linux上,要从二进制文件中删除execstack,会触发与我在WSL上相同的错误消息。在WSL上清除/设置execstack(使用-c / -s)不会做任何事情。从github bug报告来看,它似乎不太可能被修复。