Skip to content

Memory unsafety after deepcopy of plan #261

Open
@kbarros

Description

@kbarros

The following will segfault on my machine:

using FFTW

function make_plan()
    plan = FFTW.plan_fft(zeros(10))
    return deepcopy(plan)
end

plan = make_plan()
GC.gc() # presumably C datastructures are freed in this step, even though they remain live
plan * ones(10) # segfault

Since FFTW plans are immutable, a quick fix might be to have deepcopy return the original plan,

Base.deepcopy(plan::FFTW.cFFTWPlan) = plan
# ... repeat for all FFTW plan types

Alternatively, attempting to deepcopy an FFTW plan could throw an error explaining that deepcopy cannot be used in conjunction with bindings to C libraries.

Tested on FFTW v1.5.0 with Julia 1.9 beta 4:

julia> versioninfo()
Julia Version 1.9.0-beta4
Commit b75ddb787ff (2023-02-07 21:53 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.5.0)
  CPU: 8 × Apple M1 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, apple-m1)
  Threads: 1 on 6 virtual cores

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions