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

Remove unsafe @cfunction wrappers CVRhsFn_wrapper and similar #384

Merged
merged 1 commit into from
Jan 13, 2023
Merged

Remove unsafe @cfunction wrappers CVRhsFn_wrapper and similar #384

merged 1 commit into from
Jan 13, 2023

Conversation

sjdaines
Copy link
Contributor

Addresses issue #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 PR #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.

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.
Copy link
Member

@ChrisRackauckas ChrisRackauckas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a reasonable change, thanks for figuring out what was going on there.

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

Successfully merging this pull request may close these issues.

2 participants