FFTW.jl icon indicating copy to clipboard operation
FFTW.jl copied to clipboard

Julia crash when using fft_plan

Open JuhaHeiskala opened this issue 2 years ago • 4 comments

(Filed this first in Julia issues 51249, but got directed here. My gut feeling was that Julia gives invalid memory to FFTW)

Julia 1.9.3 installed from Generic linux package on Ubuntu 22.04 system.

julia> versioninfo()
Julia Version 1.9.3
Commit bed2cd540a1 (2023-08-24 14:43 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 32 × AMD Ryzen 9 7950X 16-Core Processor
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, znver3)
  Threads: 1 on 32 virtual cores
Environment:
  LD_LIBRARY_PATH = :/usr/local/lib

I get a crash when using the below code as a module with using Test, but not if I load the source directly in REPL include("Test.jl").

Test.jl code below view in fft_out = fft(view(A, :, 1)) is not actually needed, also fft_plan*A[:,1] crashes. I just used the view when hit this the first time)

module Test
using FFTW

A = randn(ComplexF64, 128, 10)

fft_out = fft(view(A, :, 1))
fft_plan = plan_fft(zeros(ComplexF64, 128))

# This doesn't crash when loading the module
fft_out = fft_plan*view(A, :, 1)

function test()

  A = randn(ComplexF64, 128, 10)
  # this crashes
  f = fft_plan*view(A, :, 1)

  # this doesn't crash either loaded from REPL or as module
  # f = fft(view(A, :, 1))
end

end # module Test

I.e. below does not crash

julia> include("Test.jl")
Main.Test

julia> Test.test()
128-element Vector{ComplexF64}:
   -5.399150700691746 - 13.263908986406738im
   4.4856379107268936 + 14.76279541263508im
...

but this crashes

julia> using Test

julia> Test.test()

[532823] signal (11.1): Segmentation fault
in expression starting at REPL[2]:1
fftw_execute_dft at /home/juha/.julia/artifacts/e95ca94c82899616429924e9fdc7eccda275aa38/lib/libfftw3.so (unknown line)
unknown function (ip: 0x7fe691102216)

JuhaHeiskala avatar Sep 08 '23 18:09 JuhaHeiskala

The plan object contains a pointer: https://github.com/JuliaMath/FFTW.jl/blob/ef8fc5b781da762a5332d62341fecd10fbccb658/src/fft.jl#L252-L253 You can't serialise a pointer in a session and then deserialise it in another session, the old pointer is garbage in the new session, hence the segmentation fault. Please read the documentation about module loading.

giordano avatar Sep 08 '23 18:09 giordano

Ah, ok, thanks. Did't realize that the module variables are stored in the precompiled cache.

JuhaHeiskala avatar Sep 08 '23 18:09 JuhaHeiskala

Global variables are, yes. You may find this blog post about what happens in top-level of Julia packages useful.

giordano avatar Sep 08 '23 18:09 giordano

Should FFTW check for C_NULL and provide a better error?

vchuravy avatar Sep 08 '23 18:09 vchuravy