Segmentation fault when calling fortran function

I am a bit lost with a script a fortran script that I am trying to use. I run into a seg fault error when calling dcapecalc2d from [rip_cape.f90]((https://github.com/NCAR/wrf-python/blob/develop/fortran/rip_cape.f90).

signal (11): Segmentation fault
in expression starting at /data/kwamahiu/codepen/test.jl:45
fstrlen at /src/gcc-7.3.0/libgfortran/runtime/string.c:37 [inlined]
fc_strdup at /src/gcc-7.3.0/libgfortran/runtime/string.c:129
find_file at /src/gcc-7.3.0/libgfortran/io/unix.c:1730
new_unit at /src/gcc-7.3.0/libgfortran/io/open.c:518
already_open at /src/gcc-7.3.0/libgfortran/io/open.c:704 [inlined]
st_open at /src/gcc-7.3.0/libgfortran/io/open.c:883
dlookup_table_ at /data/kwamahiu/codepen/rip_cape.so (unknown line)
dcapecalc2d_ at /data/kwamahiu/codepen/rip_cape.so (unknown line)
top-level scope at /data/kwamahiu/codepen/test.jl:45
jl_toplevel_eval_flex at /buildworker/worker/package_linux64/build/src/toplevel.c:834
jl_parse_eval_all at /buildworker/worker/package_linux64/build/src/ast.c:913
jl_load_rewrite at /buildworker/worker/package_linux64/build/src/toplevel.c:914
include at ./Base.jl:380
include at ./Base.jl:368
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2214 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2398
exec_options at ./client.jl:296
_start at ./client.jl:506
jfptr__start_53898.clone_1 at /data/kwamahiu/julialang/lib/julia/sys.so (unknown line)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2214 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2398
unknown function (ip: 0x401931) 
unknown function (ip: 0x401533)
__libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
unknown function (ip: 0x4015d4)
Allocations: 18403097 (Pool: 18399264; Big: 3833); GC: 20
Segmentation fault (core dumped)

In case they are needed, these are the required files: rip_cape90, wrf_constants.f90 and psadilookup.dat

I downloaded the files and compile them thusly

gfortran -c wrf_constants.f90
gfortran rip_cape.f90 -o rip_cape.so -shared -fPIC

and this is the script that I am using to call the relevant function

full_p = rand(19.8566:1.0:1026.856, 120,97,8)
t = rand(188.094:0.23:305.0942, 120,97,8)
full_ph = rand(10.50011:0.37:27877.50, 120, 97, 8)
qvapor = rand(0.0:0.01:0.021, 120,97,8)
psfc = rand(754.52:1031.38, 120,97,8)
ter = rand(-10:2512.17, 120,97,8)

mix, mjy, mkzh = size(full_p)
cape, cin = zeros(Float64, mix, mjy, mkzh)
prsf, prs_new = tmk_new = qvp_new = ght_new = zeros(Float64, mkzh, mix, mjy)

ter_follow = 1
i3dflag = 0
errmsg = Vector{Cstring}(undef, 512)
errstat = 0
cmsg= 9.969209968386869

ccall((:dcapecalc2d_,
        "rip_cape.so"),
        Cvoid,
        (Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64},
         Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64},
         Ref{Float64}, Ref{Integer}, Ref{Integer}, Ref{Integer}, Ref{Integer},
         Ptr{UInt8}, Ref{Integer}, Ptr{UInt8}),
        full_p, t, qvapor, full_ph, ter, psfc,
        cape, cin, prsf, prs_new, tmk_new, qvp_new, ght_new,
        cmsg, mix, mjy, mkzh, ter_follow,
        "psadilookup.dat", errstat, errmsg)

Can anybody tell me what I have done wrong and how to fix this.

Ref{Integer} seems unlikely. Does Ref{Int} work there?

1 Like

Thanks at @jameson. Chaning Ref{Integer} to Ref{Int} has gotten rid of the seg fault.

However, I now get the following

At line 149 of file rip_cape.f90
Fortran runtime error: End of record

I think this is because the fortran script is unable to read the file. I have had a look at the Bit Types section in the documentation using Ptr{UInt8} for strings. Is this correct?

1 Like

In gfortran, strings are passed as two arguments, a Ptr{UInt8}, and a trailing long (often 32-bits)

@jameson I made add the trailing long bits as per your suggestion

ccall((:dcapecalc3d_,
        "/data/kwamahiu/codepen/rip_cape.so"),
        Cvoid,
        (Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64},
         Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64},
         Ref{Float64}, Ref{Int}, Ref{Int}, Ref{Int}, Ref{Int},
         Ptr{UInt8}, Ref{Int}, Ptr{UInt8}, UInt32, UInt32),    
                                            ^^^^^^^^^^^^
        full_p, t, qvapor, full_ph, ter, psfc,
        cape, cin, prsf, prs_new, tmk_new, qvp_new, ght_new,
        cmsg, mix, mjy, mkzh, ter_follow,
        fname, errstat, errmsg, sizeof(fname), sizeof(errmsg))
                                       ^^^^^^^^^^^^

Everything seems to be working.

Thanks.