Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CVRhsFn_wrapper unsafe use of @cfunction($f, ...) #381

Closed
sjdaines opened this issue Jan 13, 2023 · 1 comment
Closed

CVRhsFn_wrapper unsafe use of @cfunction($f, ...) #381

sjdaines opened this issue Jan 13, 2023 · 1 comment

Comments

@sjdaines
Copy link
Contributor

sjdaines commented Jan 13, 2023

See eg CVRhsFn_wrapper:

CVRhsFn_wrapper(fp::CVRhsFn) = fp
CVRhsFn_wrapper(f) = @cfunction($f, Cint, (realtype, N_Vector, N_Vector, Ptr{Cvoid})).ptr

which is used in

function CVodeInit(cvode_mem, f, t0, y0)
__y0 = convert(NVector, y0)
CVodeInit(cvode_mem, CVRhsFn_wrapper(f), t0, convert(N_Vector, __y0))
end

The problem is that a temporary Base.CFunction closure is returned by @cfunction($f, ...), which may be garbage collected so it is not safe to return the enclosed pointer (somewhat similar to the problem with NVector addressed by #380, but more fundamental as the RHS function needs to persist for the whole lifetime of the solver use).

Suggested fix is to remove CVRhsFn_wrapper (which is only used by a few tests of the low-level interface), and require that the caller either uses the @cfunction(f, ...) form to generate a C pointer (ie f known at compile time and the runtime Julia RHS function passed in to Sundials C code as part of the opaque user data, this is what the high-level interface does), or manages the lifetime of the @cfunction explicitly.

Addressed by PR #384

ChrisRackauckas pushed a commit that referenced this issue Jan 13, 2023
Addresses #381

This is theoretically a breaking change, however any application code
that was using the low-level API in this way (passing a Julia function into
eg CVodeInit and relying on CVRhsFn_wrapper) would likely segfault randomly
due to garbage collection of the wrapper.

The problem with CVRhsFn_wrapper and similar is that a temporary
Base.CFunction closure is returned by  @cfunction($f, ...),
which may be garbage collected hence it is not safe
to return the enclosed pointer.
This is somewhat similar to the problem with
NVector addressed by #380,
but more fundamental as the RHS function
needs to persist for the whole lifetime of the solver use.

Fix here is to remove these wrappers (which were only used by a few tests
of the low-level interface), and require that the caller either uses
the @cfunction(f, ...) form to generate a C pointer (ie f known at
compile time and the runtime Julia RHS function passed in to
Sundials C code as part of the opaque user data, this is what the
high-level interface does), or manages the lifetime of the @cfunction explicitly.
@ChrisRackauckas
Copy link
Member

Fixed thanks. I wonder if this will end up fixing the ability to use Sundials' sensitivity analysis stuff too. It would be interesting to try and get https://github.com/SciML/Sundials.jl/blob/master/test/cvodes_dns.jl up and running

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants