JuliaMath / JuliaMath/FFTW.jl

Memory unsafety after deepcopy of plan

Open
#261 13 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
300
Forks
61
PR merge metrics
No merged PRs in 30d

Description

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
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.