You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用-fno-automatic编译仍遇栈溢出,Fortran子程序调用段错误

问题:Julia调用Fortran共享库时触发段错误(初始误判为栈溢出)

问题概述

从Julia调用Fortran编译的共享库时,初始报错为StackOverflowError,尝试-fno-automatic编译选项、调整ulimit -s均无效,且Mac环境下编译运行无异常。经gdb调试确认,实际是调用传递给subroutine1的回调子程序F时直接触发段错误,程序未进入F的执行逻辑。

环境信息

Mac gfortran版本

$ gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/12.2.0/bin/../libexec/gcc/aarch64-apple-darwin22/12/lto-wrapper
Target: aarch64-apple-darwin22
Configured with: ../configure --prefix=/opt/homebrew/opt/gcc --libdir=/opt/homebrew/opt/gcc/lib/gcc/current --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-12 --with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr --with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl --with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 12.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=aarch64-apple-darwin22 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (Homebrew GCC 12.2.0)

Linux gfortran版本

$ gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.3.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04) 

编译命令

gfortran -J./src -fsyntax-only -c -ffree-line-length-300 src/module_1.f90
gfortran -J./src  -fsyntax-only -c -ffree-line-length-300 src/module_2.f90 
gfortran -J./src  -fsyntax-only -c -ffree-line-length-300 src/module_3.f90 
gfortran -J./src  -fsyntax-only -c -ffree-line-length-300 src/module_4.f90 
gfortran -shared -fPIC -ffree-line-length-300 -g -fbounds-check -Wall -Wno-tabs ./src/file1.f90 ./src/file2.f90 ./src/file3.f90 ./src/file4.f90 ./src/file5.f90 ./src/file6.f90 -o shared_object_file.so

调试信息

Julia报错

┌ Error: ERROR: 
│   exception =
│    StackOverflowError:

gdb调试日志

Thread 1 "julia" hit Breakpoint 1, module1::subroutine1 (m1=0, m2=11, n1=1, n2=66, n=66, id=..., 
f=0x1554b2c4de9c, j=0x1554b2c4de80, max_iter=100, x=..., y=..., tol=1e-08, iflag=.TRUE.) at ./src/solve_model.f90:261
261         do k=1, max_iter
(gdb) step
262             call F()
(gdb) step
Thread 1 "julia" received signal SIGSEGV, Segmentation fault.
0x00001554b2c4de9c in ?? ()

代码片段

subroutine1的接口定义:

subroutine subroutine1(arg1, arg2, arg3, arg4, arg5, arg6, F, J, arg9, arg10)
    implicit none
    interface           
        subroutine F(arg1, arg2, arg3, arg4, arg5, arg6)
            use ISO_FORTRAN_ENV, only: int64
            integer(int64), intent(in) :: arg1, arg2, arg3, arg4, arg5, arg6(arg5)
            double precision, intent(inout)::arg9(arg1:arg2, arg3:arg4)
            double precision, intent(out):: arg10(arg1:arg2, arg5), y(arg1:arg2, arg3:arg4)
        end subroutine

调试思路建议

  • 校验回调接口完全匹配
    • 确认Julia中传递的回调函数,其参数类型、个数、数组维度、intent属性与Fortran中F的接口定义完全一致,重点关注int64类型匹配、带偏移的数组上下界(如arg9(arg1:arg2, arg3:arg4))在Julia中的处理逻辑
    • 注意Fortran(列优先)与Julia(行优先)的数组存储顺序差异,若回调涉及数组操作,存储顺序不匹配会直接导致内存访问越界
  • 验证函数指针传递有效性
    • 在Fortran的subroutine1中打印F的函数指针地址,对比Julia中传递的回调地址,排查指针传递过程是否被篡改
    • 在Fortran中定义本地测试用的F子程序,替换Julia传递的回调,确认subroutine1本身逻辑正常,排除Fortran代码自身问题
  • 增强编译与调试选项
    • 添加-fcheck=all编译选项(覆盖-fbounds-check,开启全量运行时检查),捕获参数不匹配、数组越界等问题
    • 关闭优化(添加-O0),避免编译器优化导致调试信息失真
    • 添加-fbacktrace编译选项,获取更详细的调用栈回溯信息
  • 内存边界检测
    • 使用valgrind运行Julia程序,检测无效指针访问、内存越界等问题,命令示例:valgrind --leak-check=full julia your_script.jl
  • 排查平台差异
    • 对比Mac与Linux的编译选项,确认函数调用约定、共享库编译参数完全一致
    • 检查Linux下Julia的版本、目标架构(如x86_64)与Fortran编译的共享库是否匹配

内容的提问来源于stack exchange,提问作者cjp1032

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 04:45:41